From 57751fdb55ad04d82542165417511d26304cadc2 Mon Sep 17 00:00:00 2001 From: Eelco Dolstra Date: Mon, 24 Jul 2006 15:16:03 +0000 Subject: [PATCH] * Refactoring to support domain checks. --- src/libexpr/eval.cc | 12 +++++++----- src/libexpr/get-drvs.cc | 7 +++---- src/libexpr/nixexpr-ast.def | 9 +++++++-- src/libexpr/nixexpr.cc | 15 ++++++--------- src/libexpr/nixexpr.hh | 3 +++ src/libexpr/parser.cc | 6 ++---- src/libexpr/parser.y | 7 +++++-- tests/lang/parse-okay-1.exp | 2 +- tests/lang/parse-okay-regression-20041027.exp | 2 +- tests/lang/parse-okay-subversion.exp | 2 +- 10 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/libexpr/eval.cc b/src/libexpr/eval.cc index 5315619d4..e45de729b 100644 --- a/src/libexpr/eval.cc +++ b/src/libexpr/eval.cc @@ -37,15 +37,17 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg) ATermVector defsUsed; ATermList recAttrs = ATempty; for (ATermIterator i(formals); i; ++i) { - Expr name, def = 0; - if (!matchNoDefFormal(*i, name) && !matchDefFormal(*i, name, def)) - abort(); /* can't happen */ + Expr name, def; DefaultValue def2; ATerm dummy; + if (!matchFormal(*i, name, dummy, def2)) abort(); /* can't happen */ + if (!matchDefaultValue(def2, def)) def = 0; if (subs[name] == 0) { if (def == 0) throw TypeError(format("the argument named `%1%' required by the function is missing") % aterm2String(name)); defsUsed.push_back(name); recAttrs = ATinsert(recAttrs, makeBind(name, def, makeNoPos())); } + /* !!! check that the argument are in the valid values list, + if present */ } /* Make a recursive attribute set out of the (argument-name, @@ -64,8 +66,8 @@ static Expr substArgs(Expr body, ATermList formals, Expr arg) /* One or more actual arguments were not declared as formal arguments. Find out which. */ for (ATermIterator i(formals); i; ++i) { - Expr name, def; - matchNoDefFormal(*i, name) || matchDefFormal(*i, name, def); + Expr name; ATerm d1, d2; + matchFormal(*i, name, d1, d2); subs.remove(name); } throw TypeError(format("the function does not expect an argument named `%1%'") diff --git a/src/libexpr/get-drvs.cc b/src/libexpr/get-drvs.cc index 82e34f776..05b4897cf 100644 --- a/src/libexpr/get-drvs.cc +++ b/src/libexpr/get-drvs.cc @@ -121,12 +121,11 @@ static void getDerivations(EvalState & state, Expr e, ATerm body, pos; if (matchFunction(e, formals, body, pos)) { for (ATermIterator i(formals); i; ++i) { - Expr name, def; - if (matchNoDefFormal(*i, name)) + Expr name, def; ATerm values, def2; + if (!matchFormal(*i, name, values, def2)) abort(); + if (!matchDefaultValue(def2, def)) throw TypeError(format("cannot auto-call a function that has an argument without a default value (`%1%')") % aterm2String(name)); - else if (!matchDefFormal(*i, name, def)) - abort(); /* can't happen */ } getDerivations(state, makeCall(e, makeAttrs(ATermMap(0))), diff --git a/src/libexpr/nixexpr-ast.def b/src/libexpr/nixexpr-ast.def index a4565ab1f..4a92eaebf 100644 --- a/src/libexpr/nixexpr-ast.def +++ b/src/libexpr/nixexpr-ast.def @@ -45,8 +45,13 @@ Inherit | Expr ATermList Pos | ATerm | Scope | | Expr | -NoDefFormal | string | ATerm | -DefFormal | string Expr | ATerm | +Formal | string ValidValues DefaultValue | ATerm | + +ValidValues | ATermList | ValidValues | +UnrestrictedValues | | ValidValues | + +DefaultValue | Expr | DefaultValue | +NoDefaultValue | | DefaultValue | True | | ATerm | False | | ATerm | diff --git a/src/libexpr/nixexpr.cc b/src/libexpr/nixexpr.cc index 938670f5b..cbcaec584 100644 --- a/src/libexpr/nixexpr.cc +++ b/src/libexpr/nixexpr.cc @@ -134,9 +134,8 @@ Expr substitute(const Substitution & subs, Expr e) if (matchFunction(e, formals, body, pos)) { ATermMap map(ATgetLength(formals)); for (ATermIterator i(formals); i; ++i) { - if (!matchNoDefFormal(*i, name) && - !matchDefFormal(*i, name, def)) - abort(); + ATerm d1, d2; + if (!matchFormal(*i, name, d1, d2)) abort(); map.set(name, makeRemoved()); } Substitution subs2(&subs, &map); @@ -221,16 +220,14 @@ static void checkVarDefs2(set & done, const ATermMap & defs, Expr e) else if (matchFunction(e, formals, body, pos)) { ATermMap defs2(defs); for (ATermIterator i(formals); i; ++i) { - Expr deflt; - if (!matchNoDefFormal(*i, name) && - !matchDefFormal(*i, name, deflt)) - abort(); + Expr d1, d2; + if (!matchFormal(*i, name, d1, d2)) abort(); defs2.set(name, (ATerm) ATempty); } for (ATermIterator i(formals); i; ++i) { - Expr deflt; + Expr dummy, deflt; set done2; - if (matchDefFormal(*i, name, deflt)) + if (matchFormal(*i, name, dummy, deflt)) /* !!! check dummy */ checkVarDefs2(done2, defs2, deflt); } set done2; diff --git a/src/libexpr/nixexpr.hh b/src/libexpr/nixexpr.hh index 4dc235c8e..6e67e5ebd 100644 --- a/src/libexpr/nixexpr.hh +++ b/src/libexpr/nixexpr.hh @@ -19,6 +19,9 @@ MakeError(TypeError, EvalError) normals forms efficiently. */ typedef ATerm Expr; +typedef ATerm DefaultValue; +typedef ATerm ValidValues; + typedef ATerm Pos; diff --git a/src/libexpr/parser.cc b/src/libexpr/parser.cc index 5c2c85cd7..7adbcb618 100644 --- a/src/libexpr/parser.cc +++ b/src/libexpr/parser.cc @@ -118,10 +118,8 @@ static void checkAttrSets(ATerm e) ATermMap names(ATgetLength(formals)); for (ATermIterator i(formals); i; ++i) { ATerm name; - Expr deflt; - if (!matchNoDefFormal(*i, name) && - !matchDefFormal(*i, name, deflt)) - abort(); + ATerm d1, d2; + if (!matchFormal(*i, name, d1, d2)) abort(); if (names.get(name)) throw EvalError(format("duplicate formal function argument `%1%' at %2%") % aterm2String(name) % showPos(pos)); diff --git a/src/libexpr/parser.y b/src/libexpr/parser.y index cba390d8d..b28aa2760 100644 --- a/src/libexpr/parser.y +++ b/src/libexpr/parser.y @@ -16,6 +16,8 @@ #include "lexer-tab.h" typedef ATerm Expr; +typedef ATerm ValidValues; +typedef ATerm DefaultValue; typedef ATerm Pos; #include "nixexpr-ast.hh" @@ -203,8 +205,9 @@ formals ; formal - : ID { $$ = makeNoDefFormal($1); } - | ID '?' expr { $$ = makeDefFormal($1, $3); } + : ID { $$ = makeFormal($1, makeUnrestrictedValues(), makeNoDefaultValue()); } +// | ID ':' '[' expr_list ']' { $$ = makeDefFormal($1, $3); } + | ID '?' expr { $$ = makeFormal($1, makeUnrestrictedValues(), makeDefaultValue($3)); } ; %% diff --git a/tests/lang/parse-okay-1.exp b/tests/lang/parse-okay-1.exp index 71e55d3af..664757859 100644 --- a/tests/lang/parse-okay-1.exp +++ b/tests/lang/parse-okay-1.exp @@ -1 +1 @@ -Function([NoDefFormal("x"),NoDefFormal("y"),NoDefFormal("z")],OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),Pos("(string)",1,2)) +Function([Formal("x",UnrestrictedValues,NoDefaultValue),Formal("y",UnrestrictedValues,NoDefaultValue),Formal("z",UnrestrictedValues,NoDefaultValue)],OpPlus(OpPlus(Var("x"),Var("y")),Var("z")),Pos("(string)",1,2)) diff --git a/tests/lang/parse-okay-regression-20041027.exp b/tests/lang/parse-okay-regression-20041027.exp index f3bee28b8..38d72aa88 100644 --- a/tests/lang/parse-okay-regression-20041027.exp +++ b/tests/lang/parse-okay-regression-20041027.exp @@ -1 +1 @@ -Function([NoDefFormal("stdenv"),NoDefFormal("fetchurl")],Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1"),Pos("(string)",4,7)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2"),Pos("(string)",6,8)),Bind("md5",Str("7e935a42428d63a387b3c048be0f2756"),Pos("(string)",7,8))])),Pos("(string)",5,6))])),Pos("(string)",1,2)) +Function([Formal("stdenv",UnrestrictedValues,NoDefaultValue),Formal("fetchurl",UnrestrictedValues,NoDefaultValue)],Call(Select(Var("stdenv"),"mkDerivation"),Attrs([Bind("name",Str("libXi-6.0.1"),Pos("(string)",4,7)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://freedesktop.org/~xlibs/release/libXi-6.0.1.tar.bz2"),Pos("(string)",6,8)),Bind("md5",Str("7e935a42428d63a387b3c048be0f2756"),Pos("(string)",7,8))])),Pos("(string)",5,6))])),Pos("(string)",1,2)) diff --git a/tests/lang/parse-okay-subversion.exp b/tests/lang/parse-okay-subversion.exp index 0524c38c1..552b43259 100644 --- a/tests/lang/parse-okay-subversion.exp +++ b/tests/lang/parse-okay-subversion.exp @@ -1 +1 @@ -Function([DefFormal("localServer",Var("false")),DefFormal("httpServer",Var("false")),DefFormal("sslSupport",Var("false")),DefFormal("pythonBindings",Var("false")),DefFormal("javaSwigBindings",Var("false")),DefFormal("javahlBindings",Var("false")),NoDefFormal("stdenv"),NoDefFormal("fetchurl"),DefFormal("openssl",Var("null")),DefFormal("httpd",Var("null")),DefFormal("db4",Var("null")),NoDefFormal("expat"),DefFormal("swig",Var("null")),DefFormal("j2sdk",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("name",Str("subversion-1.1.1"),Pos("(string)",20,7)),Bind("builder",Path("/foo/bar"),Pos("(string)",22,10)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2"),Pos("(string)",24,8)),Bind("md5",Str("a180c3fe91680389c210c99def54d9e0"),Pos("(string)",25,8))])),Pos("(string)",23,6)),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),Pos("(string)",31,10)),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),Pos("(string)",33,10)),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),Pos("(string)",34,8)),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),Pos("(string)",35,6)),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),Pos("(string)",36,7)),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),Pos("(string)",37,9)),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),Pos("(string)",38,8)),Bind("expat",Var("expat"),Pos("(string)",41,10)),Bind("localServer",Var("localServer"),Pos("(string)",41,10)),Bind("httpServer",Var("httpServer"),Pos("(string)",41,10)),Bind("sslSupport",Var("sslSupport"),Pos("(string)",41,10)),Bind("pythonBindings",Var("pythonBindings"),Pos("(string)",41,10)),Bind("javaSwigBindings",Var("javaSwigBindings"),Pos("(string)",41,10)),Bind("javahlBindings",Var("javahlBindings"),Pos("(string)",41,10))])),Pos("(string)",17,7)),Pos("(string)",16,7)),Pos("(string)",15,7)),Pos("(string)",14,7)),Pos("(string)",13,7)),Pos("(string)",12,7)),Pos("(string)",11,7)),Pos("(string)",1,2)) +Function([Formal("localServer",UnrestrictedValues,DefaultValue(Var("false"))),Formal("httpServer",UnrestrictedValues,DefaultValue(Var("false"))),Formal("sslSupport",UnrestrictedValues,DefaultValue(Var("false"))),Formal("pythonBindings",UnrestrictedValues,DefaultValue(Var("false"))),Formal("javaSwigBindings",UnrestrictedValues,DefaultValue(Var("false"))),Formal("javahlBindings",UnrestrictedValues,DefaultValue(Var("false"))),Formal("stdenv",UnrestrictedValues,NoDefaultValue),Formal("fetchurl",UnrestrictedValues,NoDefaultValue),Formal("openssl",UnrestrictedValues,DefaultValue(Var("null"))),Formal("httpd",UnrestrictedValues,DefaultValue(Var("null"))),Formal("db4",UnrestrictedValues,DefaultValue(Var("null"))),Formal("expat",UnrestrictedValues,NoDefaultValue),Formal("swig",UnrestrictedValues,DefaultValue(Var("null"))),Formal("j2sdk",UnrestrictedValues,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("name",Str("subversion-1.1.1"),Pos("(string)",20,7)),Bind("builder",Path("/foo/bar"),Pos("(string)",22,10)),Bind("src",Call(Var("fetchurl"),Attrs([Bind("url",Uri("http://subversion.tigris.org/tarballs/subversion-1.1.1.tar.bz2"),Pos("(string)",24,8)),Bind("md5",Str("a180c3fe91680389c210c99def54d9e0"),Pos("(string)",25,8))])),Pos("(string)",23,6)),Bind("patches",If(Var("javahlBindings"),List([Path("/javahl.patch")]),List([])),Pos("(string)",31,10)),Bind("openssl",If(Var("sslSupport"),Var("openssl"),Var("null")),Pos("(string)",33,10)),Bind("httpd",If(Var("httpServer"),Var("httpd"),Var("null")),Pos("(string)",34,8)),Bind("db4",If(Var("localServer"),Var("db4"),Var("null")),Pos("(string)",35,6)),Bind("swig",If(OpOr(Var("pythonBindings"),Var("javaSwigBindings")),Var("swig"),Var("null")),Pos("(string)",36,7)),Bind("python",If(Var("pythonBindings"),Select(Var("swig"),"python"),Var("null")),Pos("(string)",37,9)),Bind("j2sdk",If(Var("javaSwigBindings"),Select(Var("swig"),"j2sdk"),If(Var("javahlBindings"),Var("j2sdk"),Var("null"))),Pos("(string)",38,8)),Bind("expat",Var("expat"),Pos("(string)",41,10)),Bind("localServer",Var("localServer"),Pos("(string)",41,10)),Bind("httpServer",Var("httpServer"),Pos("(string)",41,10)),Bind("sslSupport",Var("sslSupport"),Pos("(string)",41,10)),Bind("pythonBindings",Var("pythonBindings"),Pos("(string)",41,10)),Bind("javaSwigBindings",Var("javaSwigBindings"),Pos("(string)",41,10)),Bind("javahlBindings",Var("javahlBindings"),Pos("(string)",41,10))])),Pos("(string)",17,7)),Pos("(string)",16,7)),Pos("(string)",15,7)),Pos("(string)",14,7)),Pos("(string)",13,7)),Pos("(string)",12,7)),Pos("(string)",11,7)),Pos("(string)",1,2))