about summary refs log tree commit diff
path: root/src/libexpr/parser.y
diff options
context:
space:
mode:
authorEelco Dolstra <e.dolstra@tudelft.nl>2004-10-25T16·54+0000
committerEelco Dolstra <e.dolstra@tudelft.nl>2004-10-25T16·54+0000
commit37d7abd69402f0e7a78d4d2f2d78996409a8563a (patch)
treebf844e4486b0e055921583c4b7c2d3d843b34485 /src/libexpr/parser.y
parentf4d44a002688262d33093494a7fea1bb11b97ac9 (diff)
* New language feature: with expressions.
  The expression `with E1; E2' evaluates to E2 with all bindings in
  the attribute set E1 substituted.  E.g.,

    with {x = 123;}; x

  evaluates to 123.  That is, the attribute set E1 is in scope in E2.

  This is particularly useful when importing files containing lots
  definitions.  E.g., instead of

    let {
      inherit (import ./foo.nix) a b c d e f;

      body = ... a ... f ...;
    }

  we can now say

    with import ./foo.nix;

    ... a ... f ...

  I.e., we don't have to say what variables should be brought into scope.

Diffstat (limited to 'src/libexpr/parser.y')
-rw-r--r--src/libexpr/parser.y12
1 files changed, 5 insertions, 7 deletions
diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y
index 347516f69239..88ee8326b8d3 100644
--- a/src/libexpr/parser.y
+++ b/src/libexpr/parser.y
@@ -41,11 +41,11 @@ ATerm makePos(YYLTYPE * loc, void * data)
   ATermList ts;
 }
 
-%type <t> start expr expr_function expr_assert expr_if expr_op
+%type <t> start expr expr_function expr_if expr_op
 %type <t> expr_app expr_select expr_simple bind inheritsrc formal
 %type <ts> binds ids expr_list formals
 %token <t> ID INT STR PATH URI
-%token IF THEN ELSE ASSERT LET REC INHERIT EQ NEQ AND OR IMPL
+%token IF THEN ELSE ASSERT WITH LET REC INHERIT EQ NEQ AND OR IMPL
 
 %nonassoc IMPL
 %left OR
@@ -67,12 +67,10 @@ expr_function
     { $$ = ATmake("Function(<term>, <term>, <term>)", $2, $5, CUR_POS); }
   | ID ':' expr_function
     { $$ = ATmake("Function1(<term>, <term>, <term>)", $1, $3, CUR_POS); }
-  | expr_assert
-  ;
-
-expr_assert
-  : ASSERT expr ';' expr_assert
+  | ASSERT expr ';' expr_function
     { $$ = ATmake("Assert(<term>, <term>, <term>)", $2, $4, CUR_POS); }
+  | WITH expr ';' expr_function
+    { $$ = ATmake("With(<term>, <term>, <term>)", $2, $4, CUR_POS); }
   | expr_if
   ;