mirror of
https://github.com/NixOS/nix
synced 2025-06-27 08:31:16 +02:00
* Refactoring to support domain checks.
This commit is contained in:
parent
9c3099d328
commit
57751fdb55
10 changed files with 36 additions and 29 deletions
|
@ -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%'")
|
||||
|
|
|
@ -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))),
|
||||
|
|
|
@ -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 |
|
||||
|
|
|
@ -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<Expr> & 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<Expr> done2;
|
||||
if (matchDefFormal(*i, name, deflt))
|
||||
if (matchFormal(*i, name, dummy, deflt)) /* !!! check dummy */
|
||||
checkVarDefs2(done2, defs2, deflt);
|
||||
}
|
||||
set<Expr> done2;
|
||||
|
|
|
@ -19,6 +19,9 @@ MakeError(TypeError, EvalError)
|
|||
normals forms efficiently. */
|
||||
typedef ATerm Expr;
|
||||
|
||||
typedef ATerm DefaultValue;
|
||||
typedef ATerm ValidValues;
|
||||
|
||||
typedef ATerm Pos;
|
||||
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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)); }
|
||||
;
|
||||
|
||||
%%
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue