about summary refs log tree commit diff
path: root/src/libexpr
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2010-03-25T12·19+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2010-03-25T12·19+0000
commit8a10360c912bc344ea9ce7f8871a47a6e036552f (patch)
tree09ed76a3e11754ee09c48cd491782848df169063 /src/libexpr
parent7482349fe8ba9f285f6c7e53d8573fc367ecff8b (diff)
* 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.

Diffstat (limited to 'src/libexpr')
-rw-r--r--src/libexpr/eval-test.cc136
-rw-r--r--src/libexpr/eval.cc15
-rw-r--r--src/libexpr/expr-to-xml.cc14
-rw-r--r--src/libexpr/nixexpr-ast.def4
-rw-r--r--src/libexpr/nixexpr.cc8
-rw-r--r--src/libexpr/parser.y45
-rw-r--r--src/libexpr/primops.cc7
7 files changed, 73 insertions, 156 deletions
diff --git a/src/libexpr/eval-test.cc b/src/libexpr/eval-test.cc
index 27cf685f9de4..71b16384bec6 100644
--- a/src/libexpr/eval-test.cc
+++ b/src/libexpr/eval-test.cc
@@ -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\"", "/");
diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc
index cd9c64594747..1501fc0480b2 100644
--- a/src/libexpr/eval.cc
+++ b/src/libexpr/eval.cc
@@ -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) {
diff --git a/src/libexpr/expr-to-xml.cc b/src/libexpr/expr-to-xml.cc
index e401001ead01..9b3062804e29 100644
--- a/src/libexpr/expr-to-xml.cc
+++ b/src/libexpr/expr-to-xml.cc
@@ -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);
     }
 }
 
diff --git a/src/libexpr/nixexpr-ast.def b/src/libexpr/nixexpr-ast.def
index 00b5f5a13716..2bf8492dd05e 100644
--- a/src/libexpr/nixexpr-ast.def
+++ b/src/libexpr/nixexpr-ast.def
@@ -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("")
diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc
index 3675dcceaf66..e581b1329c88 100644
--- a/src/libexpr/nixexpr.cc
+++ b/src/libexpr/nixexpr.cc
@@ -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();
 }
 
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 8706ce025477..e55dfd76f591 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -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 (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)) { 
+    
+    if (matchAttrsPat(pat, formals, ellipsis, name)) { 
         for (ATermIterator i(formals); i; ++i) {
-            ATerm d1;
-            if (!matchFormal(*i, name, d1)) abort();
-            if (map.get(name))
+            ATerm d1, name2;
+            if (!matchFormal(*i, name2, d1)) abort();
+            if (map.get(name2))
                 throw ParseError(format("duplicate formal function argument `%1%' at %2%")
-                    % aterm2String(name) % showPos(pos));
-            map.set(name, name);
+                    % aterm2String(name2) % showPos(pos));
+            map.set(name2, name2);
         }
     }
-    else if (matchAtPat(pat, pat1, pat2)) {
-        checkPatternVars(pos, map, pat1);
-        checkPatternVars(pos, map, pat2);
+
+    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 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
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index 7dddc91a86ab..52292f3f8461 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -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);
-    }
 }