From 9279174dde3e1a450e63e866d2683352dd8238d3 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Thu, 14 Aug 2008 14:00:44 +0000 Subject: [PATCH] * Added an experimental feature suggested by Andres: ellipses ("...") in attribute set pattern matches. This allows defining a function that takes *at least* the listed attributes, while ignoring additional attributes. For instance, {stdenv, fetchurl, fuse, ...}: stdenv.mkDerivation { ... }; defines a function that requires an attribute set that contains the specified attributes but ignores others. The main advantage is that we can then write in all-packages.nix aefs = import ../bla/aefs pkgs; instead of aefs = import ../bla/aefs { inherit stdenv fetchurl fuse; }; This saves a lot of typing (not to mention not having to update all-packages.nix with purely mechanical changes). It saves as much typing as the "args: with args;" style, but has the advantage that the function arguments are properly declared (not implicit in what the body of the "with" uses). --- src/libexpr/eval.cc | 12 ++++++---- src/libexpr/expr-to-xml.cc | 4 +++- src/libexpr/lexer.l | 1 + src/libexpr/nixexpr-ast.def | 8 +++---- src/libexpr/nixexpr.cc | 5 ++-- src/libexpr/nixexpr.hh | 1 + src/libexpr/parser.y | 24 ++++++++++++++----- tests/lang/eval-okay-patterns.exp | 2 +- tests/lang/eval-okay-patterns.nix | 5 +++- tests/lang/eval-okay-xml.exp.xml | 10 ++++++++ tests/lang/eval-okay-xml.nix | 2 ++ tests/lang/parse-okay-1.exp | 2 +- tests/lang/parse-okay-regression-20041027.exp | 2 +- tests/lang/parse-okay-subversion.exp | 2 +- 14 files changed, 57 insertions(+), 23 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index ebcbac539..e79d42504 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -84,11 +84,12 @@ 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)) { + else if (matchAttrsPat(pat, formals, ellipsis)) { arg = evalExpr(state, arg); @@ -122,8 +123,8 @@ static void patternMatch(EvalState & state, } /* Check that each actual argument is listed as a formal - argument. */ - if (attrsUsed != nrAttrs) + argument (unless the attribute match specifies a `...'). */ + if (ellipsis == eFalse && attrsUsed != nrAttrs) throw TypeError(format("the function does not expect an argument named `%1%'") % aterm2String(attrs.begin()->key)); } @@ -402,9 +403,10 @@ Expr autoCallFunction(Expr e, const ATermMap & args) Pattern pat; ATerm body, pos; ATermList formals; - + ATermBool ellipsis; + /* !!! this should be more general */ - if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals)) { + if (matchFunction(e, pat, body, pos) && matchAttrsPat(pat, formals, ellipsis)) { 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 6ec906356..e401001ea 100644 --- a/src/libexpr/expr-to-xml.cc +++ b/src/libexpr/expr-to-xml.cc @@ -45,15 +45,17 @@ 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)) { + else if (matchAttrsPat(pat, formals, ellipsis)) { XMLOpenElement _(doc, "attrspat"); 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"); diff --git a/src/libexpr/lexer.l b/src/libexpr/lexer.l index b7c1d19f8..81aec99e1 100644 --- a/src/libexpr/lexer.l +++ b/src/libexpr/lexer.l @@ -95,6 +95,7 @@ let { return LET; } in { return IN; } rec { return REC; } inherit { return INHERIT; } +\.\.\. { return ELLIPSIS; } \=\= { return EQ; } \!\= { return NEQ; } diff --git a/src/libexpr/nixexpr-ast.def b/src/libexpr/nixexpr-ast.def index 670db1976..ab749b3db 100644 --- a/src/libexpr/nixexpr-ast.def +++ b/src/libexpr/nixexpr-ast.def @@ -66,7 +66,7 @@ PrimOp | int ATermBlob ATermList | Expr | Attrs | ATermList | Expr | Closed | Expr | Expr | Rec | ATermList ATermList | Expr | -Bool | ATerm | Expr | +Bool | ATermBool | Expr | Null | | Expr | Bind | string Expr Pos | ATerm | @@ -76,7 +76,7 @@ Inherit | Expr ATermList Pos | ATerm | Scope | | Expr | VarPat | string | Pattern | -AttrsPat | ATermList | Pattern | +AttrsPat | ATermList ATermBool | Pattern | # bool = `...' AtPat | Pattern Pattern | Pattern | Formal | string DefaultValue | ATerm | @@ -84,8 +84,8 @@ Formal | string DefaultValue | ATerm | DefaultValue | Expr | DefaultValue | NoDefaultValue | | DefaultValue | -True | | ATerm | -False | | ATerm | +True | | ATermBool | +False | | ATermBool | PrimOpDef | int ATermBlob | ATerm | diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index b2d775abb..d96b5be97 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -114,12 +114,13 @@ static void varsBoundByPattern(ATermMap & map, Pattern pat) { ATerm name; ATermList formals; - Pattern pat1, pat2; + 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)) { + else if (matchAttrsPat(pat, formals, ellipsis)) { for (ATermIterator i(formals); i; ++i) { ATerm d1; if (!matchFormal(*i, name, d1)) abort(); diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh index 320d1dc97..43ac19f92 100644 --- a/src/libexpr/nixexpr.hh +++ b/src/libexpr/nixexpr.hh @@ -24,6 +24,7 @@ typedef ATerm Expr; typedef ATerm DefaultValue; typedef ATerm Pos; typedef ATerm Pattern; +typedef ATerm ATermBool; /* A STL vector of ATerms. Should be used with great care since it's diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index c48aa34aa..b7624df7e 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -208,16 +208,22 @@ static void freeAndUnprotect(void * p) %union { ATerm t; ATermList ts; + struct { + ATermList formals; + bool ellipsis; + } formals; } %type start expr expr_function expr_if expr_op %type expr_app expr_select expr_simple bind inheritsrc formal %type pattern pattern2 -%type binds ids expr_list formals string_parts ind_string_parts +%type binds ids expr_list string_parts ind_string_parts +%type formals %token ID INT STR IND_STR PATH URI %token IF THEN ELSE ASSERT WITH LET IN REC INHERIT EQ NEQ AND OR IMPL %token DOLLAR_CURLY /* == ${ */ %token IND_STRING_OPEN IND_STRING_CLOSE +%token ELLIPSIS %nonassoc IMPL %left OR @@ -326,7 +332,7 @@ pattern pattern2 : ID { $$ = makeVarPat($1); } - | '{' formals '}' { $$ = makeAttrsPat($2); } + | '{' formals '}' { $$ = makeAttrsPat($2.formals, $2.ellipsis ? eTrue : eFalse); } ; binds @@ -357,9 +363,14 @@ expr_list ; formals - : formal ',' formals { $$ = ATinsert($3, $1); } /* idem - right recursive */ - | formal { $$ = ATinsert(ATempty, $1); } - | { $$ = ATempty; } + : formal ',' formals /* idem - right recursive */ + { $$.formals = ATinsert($3.formals, $1); $$.ellipsis = $3.ellipsis; } + | formal + { $$.formals = ATinsert(ATempty, $1); $$.ellipsis = false; } + | + { $$.formals = ATempty; $$.ellipsis = false; } + | ELLIPSIS + { $$.formals = ATempty; $$.ellipsis = true; } ; formal @@ -401,13 +412,14 @@ static void checkPatternVars(ATerm pos, ATermMap & map, Pattern pat) ATerm name; ATermList formals; Pattern pat1, pat2; + ATermBool ellipsis; if (matchVarPat(pat, name)) { if (map.get(name)) throw EvalError(format("duplicate formal function argument `%1%' at %2%") % aterm2String(name) % showPos(pos)); map.set(name, name); } - else if (matchAttrsPat(pat, formals)) { + else if (matchAttrsPat(pat, formals, ellipsis)) { for (ATermIterator i(formals); i; ++i) { ATerm d1; if (!matchFormal(*i, name, d1)) abort(); diff --git a/tests/lang/eval-okay-patterns.exp b/tests/lang/eval-okay-patterns.exp index 382a0bd26..8422900dd 100644 --- a/tests/lang/eval-okay-patterns.exp +++ b/tests/lang/eval-okay-patterns.exp @@ -1 +1 @@ -Str("abcxyzDDDDEFgh",[]) +Str("abcxyzDDDDEFghijk",[]) diff --git a/tests/lang/eval-okay-patterns.nix b/tests/lang/eval-okay-patterns.nix index bcb9f3842..c233c406c 100644 --- a/tests/lang/eval-okay-patterns.nix +++ b/tests/lang/eval-okay-patterns.nix @@ -8,9 +8,12 @@ let i = args@args2: args.x + args2.y; + j = {x, y, z, ...}: x + y + z; + in f {x = "a"; y = "b"; z = "c";} + g {x = "x"; y = "y"; z = "z";} + h {x = "D";} + h {x = "D"; y = "E"; z = "F";} + - i {x = "g"; y = "h";} + i {x = "g"; y = "h";} + + j {x = "i"; y = "j"; z = "k"; bla = "bla"; foo = "bar";} diff --git a/tests/lang/eval-okay-xml.exp.xml b/tests/lang/eval-okay-xml.exp.xml index 4f55c69a7..72a96d54c 100644 --- a/tests/lang/eval-okay-xml.exp.xml +++ b/tests/lang/eval-okay-xml.exp.xml @@ -22,6 +22,16 @@ + + + + + + + + + + diff --git a/tests/lang/eval-okay-xml.nix b/tests/lang/eval-okay-xml.nix index 44afbff1e..b9389bfae 100644 --- a/tests/lang/eval-okay-xml.nix +++ b/tests/lang/eval-okay-xml.nix @@ -14,4 +14,6 @@ rec { at = args@{x, y, z}: x; + ellipsis = {x, y, z, ...}: x; + } diff --git a/tests/lang/parse-okay-1.exp b/tests/lang/parse-okay-1.exp index 112f5e1f6..bb8746ec4 100644 --- a/tests/lang/parse-okay-1.exp +++ b/tests/lang/parse-okay-1.exp @@ -1 +1 @@ -Function(AttrsPat([Formal("x",NoDefaultValue),Formal("y",NoDefaultValue),Formal("z",NoDefaultValue)]),OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),NoPos) +Function(AttrsPat([Formal("x",NoDefaultValue),Formal("y",NoDefaultValue),Formal("z",NoDefaultValue)],Bool(False)),OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),NoPos) diff --git a/tests/lang/parse-okay-regression-20041027.exp b/tests/lang/parse-okay-regression-20041027.exp index 214ff99e1..a302e2488 100644 --- a/tests/lang/parse-okay-regression-20041027.exp +++ b/tests/lang/parse-okay-regression-20041027.exp @@ -1 +1 @@ -Function(AttrsPat([Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue)]),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1",[]),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("7e935a42428d63a387b3c048be0f2756",[]),NoPos),Bind("url",Str("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2",[]),NoPos)])),NoPos)])),NoPos) +Function(AttrsPat([Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue)],Bool(False)),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1",[]),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("7e935a42428d63a387b3c048be0f2756",[]),NoPos),Bind("url",Str("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2",[]),NoPos)])),NoPos)])),NoPos) diff --git a/tests/lang/parse-okay-subversion.exp b/tests/lang/parse-okay-subversion.exp index 75b779431..102153c18 100644 --- a/tests/lang/parse-okay-subversion.exp +++ b/tests/lang/parse-okay-subversion.exp @@ -1 +1 @@ -Function(AttrsPat([Formal("localServer",DefaultValue(Var("false"))),Formal("httpServer",DefaultValue(Var("false"))),Formal("sslSupport",DefaultValue(Var("false"))),Formal("pythonBindings",DefaultValue(Var("false"))),Formal("javaSwigBindings",DefaultValue(Var("false"))),Formal("javahlBindings",DefaultValue(Var("false"))),Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue),Formal("openssl",DefaultValue(Var("null"))),Formal("httpd",DefaultValue(Var("null"))),Formal("db4",DefaultValue(Var("null"))),Formal("expat",NoDefaultValue),Formal("swig",DefaultValue(Var("null"))),Formal("j2sdk",DefaultValue(Var("null")))]),Assert(OpNEq(Var("expat"),Var("null")),Assert(OpImpl(Var("localServer"),OpNEq(Var("db4"),Var("null"))),Assert(OpImpl(Var("httpServer"),OpAnd(OpNEq(Var("httpd"),Var("null")),OpEq(Select(Var("httpd"),"expat"),Var("expat")))),Assert(OpImpl(Var("sslSupport"),OpAnd(OpNEq(Var("openssl"),Var("null")),OpImpl(Var("httpServer"),OpEq(Select(Var("httpd"),"openssl"),Var("openssl"))))),Assert(OpImpl(Var("pythonBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"pythonSupport"))),Assert(OpImpl(Var("javaSwigBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"javaSupport"))),Assert(OpImpl(Var("javahlBindings"),OpNEq(Var("j2sdk"),Var("null"))),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("builder",Path("/foo/bar"),NoPos),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),NoPos),Bind("expat",Var("expat"),NoPos),Bind("httpServer",Var("httpServer"),NoPos),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),NoPos),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),NoPos),Bind("javaSwigBindings",Var("javaSwigBindings"),NoPos),Bind("javahlBindings",Var("javahlBindings"),NoPos),Bind("localServer",Var("localServer"),NoPos),Bind("name",Str("subversion-1.1.1",[]),NoPos),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),NoPos),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),NoPos),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),NoPos),Bind("pythonBindings",Var("pythonBindings"),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("a180c3fe91680389c210c99def54d9e0",[]),NoPos),Bind("url",Str("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2",[]),NoPos)])),NoPos),Bind("sslSupport",Var("sslSupport"),NoPos),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),NoPos)])),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos) +Function(AttrsPat([Formal("localServer",DefaultValue(Var("false"))),Formal("httpServer",DefaultValue(Var("false"))),Formal("sslSupport",DefaultValue(Var("false"))),Formal("pythonBindings",DefaultValue(Var("false"))),Formal("javaSwigBindings",DefaultValue(Var("false"))),Formal("javahlBindings",DefaultValue(Var("false"))),Formal("stdenv",NoDefaultValue),Formal("fetchurl",NoDefaultValue),Formal("openssl",DefaultValue(Var("null"))),Formal("httpd",DefaultValue(Var("null"))),Formal("db4",DefaultValue(Var("null"))),Formal("expat",NoDefaultValue),Formal("swig",DefaultValue(Var("null"))),Formal("j2sdk",DefaultValue(Var("null")))],Bool(False)),Assert(OpNEq(Var("expat"),Var("null")),Assert(OpImpl(Var("localServer"),OpNEq(Var("db4"),Var("null"))),Assert(OpImpl(Var("httpServer"),OpAnd(OpNEq(Var("httpd"),Var("null")),OpEq(Select(Var("httpd"),"expat"),Var("expat")))),Assert(OpImpl(Var("sslSupport"),OpAnd(OpNEq(Var("openssl"),Var("null")),OpImpl(Var("httpServer"),OpEq(Select(Var("httpd"),"openssl"),Var("openssl"))))),Assert(OpImpl(Var("pythonBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"pythonSupport"))),Assert(OpImpl(Var("javaSwigBindings"),OpAnd(OpNEq(Var("swig"),Var("null")),Select(Var("swig"),"javaSupport"))),Assert(OpImpl(Var("javahlBindings"),OpNEq(Var("j2sdk"),Var("null"))),Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("builder",Path("/foo/bar"),NoPos),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),NoPos),Bind("expat",Var("expat"),NoPos),Bind("httpServer",Var("httpServer"),NoPos),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),NoPos),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),NoPos),Bind("javaSwigBindings",Var("javaSwigBindings"),NoPos),Bind("javahlBindings",Var("javahlBindings"),NoPos),Bind("localServer",Var("localServer"),NoPos),Bind("name",Str("subversion-1.1.1",[]),NoPos),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),NoPos),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),NoPos),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),NoPos),Bind("pythonBindings",Var("pythonBindings"),NoPos),Bind("src",Call(Var("fetchurl"),Attrs([Bind("md5",Str("a180c3fe91680389c210c99def54d9e0",[]),NoPos),Bind("url",Str("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2",[]),NoPos)])),NoPos),Bind("sslSupport",Var("sslSupport"),NoPos),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),NoPos)])),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos),NoPos)