mirror of
https://github.com/NixOS/nix
synced 2025-06-27 12:41:15 +02:00
* Simplify @-patterns: only {attrs}@name' or
name@{attrs}' are now
allowed. So `name1@name2', `{attrs1}@{attrs2}' and so on are now no longer legal. This is no big loss because they were not useful anyway. This also changes the output of builtins.toXML for @-patterns slightly.
This commit is contained in:
parent
7482349fe8
commit
8a10360c91
13 changed files with 83 additions and 173 deletions
|
@ -111,91 +111,6 @@ Env * allocEnv()
|
|||
}
|
||||
|
||||
|
||||
static bool patternIsStrict(Pattern pat)
|
||||
{
|
||||
ATerm name, ellipsis, pat1, pat2;
|
||||
ATermList formals;
|
||||
if (matchVarPat(pat, name)) return false;
|
||||
else if (matchAttrsPat(pat, formals, ellipsis)) return true;
|
||||
else if (matchAtPat(pat, pat1, pat2))
|
||||
return patternIsStrict(pat1) || patternIsStrict(pat2);
|
||||
else abort();
|
||||
}
|
||||
|
||||
|
||||
static void bindVarPats(Pattern pat, Env & newEnv,
|
||||
Env * argEnv, Expr argExpr, Value * & vArg)
|
||||
{
|
||||
Pattern pat1, pat2;
|
||||
if (matchAtPat(pat, pat1, pat2)) {
|
||||
bindVarPats(pat1, newEnv, argEnv, argExpr, vArg);
|
||||
bindVarPats(pat2, newEnv, argEnv, argExpr, vArg);
|
||||
return;
|
||||
}
|
||||
|
||||
ATerm name;
|
||||
if (!matchVarPat(pat, name)) abort();
|
||||
|
||||
if (vArg) {
|
||||
Value & v = newEnv.bindings[aterm2String(name)];
|
||||
v.type = tCopy;
|
||||
v.val = vArg;
|
||||
} else {
|
||||
vArg = &newEnv.bindings[aterm2String(name)];
|
||||
vArg->type = tThunk;
|
||||
vArg->thunk.env = argEnv;
|
||||
vArg->thunk.expr = argExpr;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static void bindAttrPats(Pattern pat, Env & newEnv,
|
||||
Value & vArg, Value * & vArgInEnv)
|
||||
{
|
||||
Pattern pat1, pat2;
|
||||
if (matchAtPat(pat, pat1, pat2)) {
|
||||
bindAttrPats(pat1, newEnv, vArg, vArgInEnv);
|
||||
bindAttrPats(pat2, newEnv, vArg, vArgInEnv);
|
||||
return;
|
||||
}
|
||||
|
||||
ATerm name;
|
||||
if (matchVarPat(pat, name)) {
|
||||
if (vArgInEnv) {
|
||||
Value & v = newEnv.bindings[aterm2String(name)];
|
||||
v.type = tCopy;
|
||||
v.val = vArgInEnv;
|
||||
} else {
|
||||
vArgInEnv = &newEnv.bindings[aterm2String(name)];
|
||||
*vArgInEnv = vArg;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
ATerm ellipsis;
|
||||
ATermList formals;
|
||||
if (matchAttrsPat(pat, formals, ellipsis)) {
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
Expr name, def;
|
||||
DefaultValue def2;
|
||||
if (!matchFormal(*i, name, def2)) abort(); /* can't happen */
|
||||
|
||||
Bindings::iterator j = vArg.attrs->find(aterm2String(name));
|
||||
if (j == vArg.attrs->end())
|
||||
throw TypeError(format("the argument named `%1%' required by the function is missing")
|
||||
% aterm2String(name));
|
||||
|
||||
Value & v = newEnv.bindings[aterm2String(name)];
|
||||
v.type = tCopy;
|
||||
v.val = &j->second;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
abort();
|
||||
}
|
||||
|
||||
|
||||
static void eval(Env * env, Expr e, Value & v)
|
||||
{
|
||||
printMsg(lvlError, format("eval: %1%") % e);
|
||||
|
@ -279,16 +194,44 @@ static void eval(Env * env, Expr e, Value & v)
|
|||
Env * env2 = allocEnv();
|
||||
env2->up = env;
|
||||
|
||||
if (patternIsStrict(v.lambda.pat)) {
|
||||
Value vArg;
|
||||
eval(env, arg, vArg);
|
||||
if (vArg.type != tAttrs) throw TypeError("expected attribute set");
|
||||
Value * vArg2 = 0;
|
||||
bindAttrPats(v.lambda.pat, *env2, vArg, vArg2);
|
||||
} else {
|
||||
Value * vArg = 0;
|
||||
bindVarPats(v.lambda.pat, *env2, env, arg, vArg);
|
||||
ATermList formals; ATerm ellipsis;
|
||||
|
||||
if (matchVarPat(v.lambda.pat, name)) {
|
||||
Value & vArg = env2->bindings[aterm2String(name)];
|
||||
vArg.type = tThunk;
|
||||
vArg.thunk.env = env;
|
||||
vArg.thunk.expr = arg;
|
||||
}
|
||||
|
||||
else if (matchAttrsPat(v.lambda.pat, formals, ellipsis, name)) {
|
||||
Value * vArg;
|
||||
Value vArg_;
|
||||
|
||||
if (name == sNoAlias)
|
||||
vArg = &vArg_;
|
||||
else
|
||||
vArg = &env2->bindings[aterm2String(name)];
|
||||
|
||||
eval(env, arg, *vArg);
|
||||
if (vArg->type != tAttrs) throw TypeError("expected attribute set");
|
||||
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
Expr name, def;
|
||||
DefaultValue def2;
|
||||
if (!matchFormal(*i, name, def2)) abort(); /* can't happen */
|
||||
|
||||
Bindings::iterator j = vArg->attrs->find(aterm2String(name));
|
||||
if (j == vArg->attrs->end())
|
||||
throw TypeError(format("the argument named `%1%' required by the function is missing")
|
||||
% aterm2String(name));
|
||||
|
||||
Value & v = env2->bindings[aterm2String(name)];
|
||||
v.type = tCopy;
|
||||
v.val = &j->second;
|
||||
}
|
||||
}
|
||||
|
||||
else abort();
|
||||
|
||||
eval(env2, v.lambda.body, v);
|
||||
return;
|
||||
|
@ -319,13 +262,10 @@ void run(Strings args)
|
|||
doTest("rec { x = 1; y = x; }.y");
|
||||
doTest("(x: x) 1");
|
||||
doTest("(x: y: y) 1 2");
|
||||
doTest("(x@y: x) 1");
|
||||
doTest("(x@y: y) 2");
|
||||
doTest("(x@y@z: y) 3");
|
||||
doTest("x: x");
|
||||
doTest("({x, y}: x) { x = 1; y = 2; }");
|
||||
doTest("({x, y}@args: args.x) { x = 1; y = 2; }");
|
||||
doTest("({x, y}@args@args2: args2.x) { x = 1; y = 2; }");
|
||||
doTest("(args@{x, y}: args.x) { x = 1; y = 2; }");
|
||||
|
||||
//Expr e = parseExprFromString(state, "let x = \"a\"; in x + \"b\"", "/");
|
||||
//Expr e = parseExprFromString(state, "(x: x + \"b\") \"a\"", "/");
|
||||
|
|
|
@ -85,16 +85,17 @@ static void patternMatch(EvalState & state,
|
|||
{
|
||||
ATerm name;
|
||||
ATermList formals;
|
||||
Pattern pat1, pat2;
|
||||
ATermBool ellipsis;
|
||||
|
||||
if (matchVarPat(pat, name))
|
||||
subs.set(name, arg);
|
||||
|
||||
else if (matchAttrsPat(pat, formals, ellipsis)) {
|
||||
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
||||
|
||||
arg = evalExpr(state, arg);
|
||||
|
||||
if (name != sNoAlias) subs.set(name, arg);
|
||||
|
||||
/* Get the actual arguments. */
|
||||
ATermMap attrs;
|
||||
queryAllAttrs(arg, attrs);
|
||||
|
@ -131,11 +132,6 @@ static void patternMatch(EvalState & state,
|
|||
% aterm2String(attrs.begin()->key));
|
||||
}
|
||||
|
||||
else if (matchAtPat(pat, pat1, pat2)) {
|
||||
patternMatch(state, pat1, arg, subs, subsRecursive);
|
||||
patternMatch(state, pat2, arg, subs, subsRecursive);
|
||||
}
|
||||
|
||||
else abort();
|
||||
}
|
||||
|
||||
|
@ -425,12 +421,11 @@ Path coerceToPath(EvalState & state, Expr e, PathSet & context)
|
|||
Expr autoCallFunction(Expr e, const ATermMap & args)
|
||||
{
|
||||
Pattern pat;
|
||||
ATerm body, pos;
|
||||
ATerm body, pos, name;
|
||||
ATermList formals;
|
||||
ATermBool ellipsis;
|
||||
|
||||
/* !!! this should be more general */
|
||||
if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis)) {
|
||||
if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis, name)) {
|
||||
ATermMap actualArgs(ATgetLength(formals));
|
||||
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
|
|
|
@ -44,23 +44,19 @@ static void printPatternAsXML(Pattern pat, XMLWriter & doc)
|
|||
{
|
||||
ATerm name;
|
||||
ATermList formals;
|
||||
Pattern pat1, pat2;
|
||||
ATermBool ellipsis;
|
||||
if (matchVarPat(pat, name))
|
||||
doc.writeEmptyElement("varpat", singletonAttrs("name", aterm2String(name)));
|
||||
else if (matchAttrsPat(pat, formals, ellipsis)) {
|
||||
XMLOpenElement _(doc, "attrspat");
|
||||
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
||||
XMLAttrs attrs;
|
||||
if (name != sNoAlias) attrs["name"] = aterm2String(name);
|
||||
if (ellipsis == eTrue) attrs["ellipsis"] = "1";
|
||||
XMLOpenElement _(doc, "attrspat", attrs);
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
Expr name; ATerm dummy;
|
||||
if (!matchFormal(*i, name, dummy)) abort();
|
||||
doc.writeEmptyElement("attr", singletonAttrs("name", aterm2String(name)));
|
||||
}
|
||||
if (ellipsis == eTrue) doc.writeEmptyElement("ellipsis");
|
||||
}
|
||||
else if (matchAtPat(pat, pat1, pat2)) {
|
||||
XMLOpenElement _(doc, "at");
|
||||
printPatternAsXML(pat1, doc);
|
||||
printPatternAsXML(pat2, doc);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -77,8 +77,7 @@ Inherit | Expr ATermList Pos | ATerm |
|
|||
Scope | | Expr |
|
||||
|
||||
VarPat | string | Pattern |
|
||||
AttrsPat | ATermList ATermBool | Pattern | # bool = `...'
|
||||
AtPat | Pattern Pattern | Pattern |
|
||||
AttrsPat | ATermList ATermBool string | Pattern | # bool = `...'
|
||||
|
||||
Formal | string DefaultValue | ATerm |
|
||||
|
||||
|
@ -95,3 +94,4 @@ AttrRHS | Expr Pos | ATerm |
|
|||
eTrue = makeBool(makeTrue())
|
||||
eFalse = makeBool(makeFalse())
|
||||
sOverrides = toATerm("__overrides")
|
||||
sNoAlias = toATerm("")
|
||||
|
|
|
@ -114,23 +114,19 @@ static void varsBoundByPattern(ATermMap & map, Pattern pat)
|
|||
{
|
||||
ATerm name;
|
||||
ATermList formals;
|
||||
Pattern pat1, pat2;
|
||||
ATermBool ellipsis;
|
||||
/* Use makeRemoved() so that it can be used directly in
|
||||
substitute(). */
|
||||
if (matchVarPat(pat, name))
|
||||
map.set(name, makeRemoved());
|
||||
else if (matchAttrsPat(pat, formals, ellipsis)) {
|
||||
else if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
||||
if (name != sNoAlias) map.set(name, makeRemoved());
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
ATerm d1;
|
||||
if (!matchFormal(*i, name, d1)) abort();
|
||||
map.set(name, makeRemoved());
|
||||
}
|
||||
}
|
||||
else if (matchAtPat(pat, pat1, pat2)) {
|
||||
varsBoundByPattern(map, pat1);
|
||||
varsBoundByPattern(map, pat2);
|
||||
}
|
||||
else abort();
|
||||
}
|
||||
|
||||
|
|
|
@ -140,31 +140,29 @@ static Expr fixAttrs(bool recursive, ATermList as)
|
|||
|
||||
static void checkPatternVars(ATerm pos, ATermMap & map, Pattern pat)
|
||||
{
|
||||
ATerm name;
|
||||
ATerm name = sNoAlias;
|
||||
ATermList formals;
|
||||
Pattern pat1, pat2;
|
||||
ATermBool ellipsis;
|
||||
if (matchVarPat(pat, name)) {
|
||||
|
||||
if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
ATerm d1, name2;
|
||||
if (!matchFormal(*i, name2, d1)) abort();
|
||||
if (map.get(name2))
|
||||
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
|
||||
% aterm2String(name2) % showPos(pos));
|
||||
map.set(name2, name2);
|
||||
}
|
||||
}
|
||||
|
||||
else matchVarPat(pat, name);
|
||||
|
||||
if (name != sNoAlias) {
|
||||
if (map.get(name))
|
||||
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
|
||||
% aterm2String(name) % showPos(pos));
|
||||
map.set(name, name);
|
||||
}
|
||||
else if (matchAttrsPat(pat, formals, ellipsis)) {
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
ATerm d1;
|
||||
if (!matchFormal(*i, name, d1)) abort();
|
||||
if (map.get(name))
|
||||
throw ParseError(format("duplicate formal function argument `%1%' at %2%")
|
||||
% aterm2String(name) % showPos(pos));
|
||||
map.set(name, name);
|
||||
}
|
||||
}
|
||||
else if (matchAtPat(pat, pat1, pat2)) {
|
||||
checkPatternVars(pos, map, pat1);
|
||||
checkPatternVars(pos, map, pat2);
|
||||
}
|
||||
else abort();
|
||||
}
|
||||
|
||||
|
||||
|
@ -323,7 +321,7 @@ static void freeAndUnprotect(void * p)
|
|||
|
||||
%type <t> start expr expr_function expr_if expr_op
|
||||
%type <t> expr_app expr_select expr_simple bind inheritsrc formal
|
||||
%type <t> pattern pattern2
|
||||
%type <t> pattern
|
||||
%type <ts> binds ids attrpath expr_list string_parts ind_string_parts
|
||||
%type <formals> formals
|
||||
%token <t> ID INT STR IND_STR PATH URI
|
||||
|
@ -433,13 +431,10 @@ ind_string_parts
|
|||
;
|
||||
|
||||
pattern
|
||||
: pattern2 '@' pattern { $$ = makeAtPat($1, $3); }
|
||||
| pattern2
|
||||
;
|
||||
|
||||
pattern2
|
||||
: ID { $$ = makeVarPat($1); }
|
||||
| '{' formals '}' { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse); }
|
||||
| '{' formals '}' { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse, sNoAlias); }
|
||||
| '{' formals '}' '@' ID { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse, $5); }
|
||||
| ID '@' '{' formals '}' { $$ = makeAttrsPat($4.formals, $4.ellipsis ? eTrue : eFalse, $1); }
|
||||
;
|
||||
|
||||
binds
|
||||
|
|
|
@ -846,19 +846,14 @@ static void attrsInPattern(ATermMap & map, Pattern pat)
|
|||
{
|
||||
ATerm name;
|
||||
ATermList formals;
|
||||
Pattern pat1, pat2;
|
||||
ATermBool ellipsis;
|
||||
if (matchAttrsPat(pat, formals, ellipsis)) {
|
||||
if (matchAttrsPat(pat, formals, ellipsis, name)) {
|
||||
for (ATermIterator i(formals); i; ++i) {
|
||||
ATerm def;
|
||||
if (!matchFormal(*i, name, def)) abort();
|
||||
map.set(name, makeAttrRHS(makeBool(def != constNoDefaultValue), makeNoPos()));
|
||||
}
|
||||
}
|
||||
else if (matchAtPat(pat, pat1, pat2)) {
|
||||
attrsInPattern(map, pat1);
|
||||
attrsInPattern(map, pat2);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue