remove ParserFormals

this is a proper subset of Formals anyway, so let's just use those and
avoid the extra allocations and moves.

(cherry picked from commit f07388bf985c2440413f398cf93d5f5840d1ec8c)
Change-Id: I4508c9c9c918cbaaed649dc753eb86f5cafc7ab6
This commit is contained in:
eldritch horrors 2024-03-08 05:12:59 +01:00
parent 609a8e0d94
commit 2cea973e29

View file

@ -62,11 +62,6 @@ namespace nix {
std::optional<ErrorInfo> error; std::optional<ErrorInfo> error;
}; };
struct ParserFormals {
std::vector<Formal> formals;
bool ellipsis = false;
};
} }
// using C a struct allows us to avoid having to define the special // using C a struct allows us to avoid having to define the special
@ -178,7 +173,7 @@ static void addAttr(ExprAttrs * attrs, AttrPath && attrPath,
} }
static Formals * toFormals(ParseData & data, ParserFormals * formals, static Formals * validateFormals(ParseData & data, Formals * formals,
PosIdx pos = noPos, Symbol arg = {}) PosIdx pos = noPos, Symbol arg = {})
{ {
std::sort(formals->formals.begin(), formals->formals.end(), std::sort(formals->formals.begin(), formals->formals.end(),
@ -199,18 +194,13 @@ static Formals * toFormals(ParseData & data, ParserFormals * formals,
.errPos = data.state.positions[duplicate->second] .errPos = data.state.positions[duplicate->second]
}); });
Formals result; if (arg && formals->has(arg))
result.ellipsis = formals->ellipsis;
result.formals = std::move(formals->formals);
if (arg && result.has(arg))
throw ParseError({ throw ParseError({
.msg = hintfmt("duplicate formal function argument '%1%'", data.symbols[arg]), .msg = hintfmt("duplicate formal function argument '%1%'", data.symbols[arg]),
.errPos = data.state.positions[pos] .errPos = data.state.positions[pos]
}); });
delete formals; return formals;
return new Formals(std::move(result));
} }
@ -338,7 +328,7 @@ void yyerror(YYLTYPE * loc, yyscan_t scanner, ParseData * data, const char * err
nix::Expr * e; nix::Expr * e;
nix::ExprList * list; nix::ExprList * list;
nix::ExprAttrs * attrs; nix::ExprAttrs * attrs;
nix::ParserFormals * formals; nix::Formals * formals;
nix::Formal * formal; nix::Formal * formal;
nix::NixInt n; nix::NixInt n;
nix::NixFloat nf; nix::NixFloat nf;
@ -396,16 +386,16 @@ expr_function
: ID ':' expr_function : ID ':' expr_function
{ $$ = new ExprLambda(CUR_POS, data->symbols.create($1), 0, $3); } { $$ = new ExprLambda(CUR_POS, data->symbols.create($1), 0, $3); }
| '{' formals '}' ':' expr_function | '{' formals '}' ':' expr_function
{ $$ = new ExprLambda(CUR_POS, toFormals(*data, $2), $5); } { $$ = new ExprLambda(CUR_POS, validateFormals(*data, $2), $5); }
| '{' formals '}' '@' ID ':' expr_function | '{' formals '}' '@' ID ':' expr_function
{ {
auto arg = data->symbols.create($5); auto arg = data->symbols.create($5);
$$ = new ExprLambda(CUR_POS, arg, toFormals(*data, $2, CUR_POS, arg), $7); $$ = new ExprLambda(CUR_POS, arg, validateFormals(*data, $2, CUR_POS, arg), $7);
} }
| ID '@' '{' formals '}' ':' expr_function | ID '@' '{' formals '}' ':' expr_function
{ {
auto arg = data->symbols.create($1); auto arg = data->symbols.create($1);
$$ = new ExprLambda(CUR_POS, arg, toFormals(*data, $4, CUR_POS, arg), $7); $$ = new ExprLambda(CUR_POS, arg, validateFormals(*data, $4, CUR_POS, arg), $7);
} }
| ASSERT expr ';' expr_function | ASSERT expr ';' expr_function
{ $$ = new ExprAssert(CUR_POS, $2, $4); } { $$ = new ExprAssert(CUR_POS, $2, $4); }
@ -649,11 +639,11 @@ formals
: formal ',' formals : formal ',' formals
{ $$ = $3; $$->formals.emplace_back(*$1); delete $1; } { $$ = $3; $$->formals.emplace_back(*$1); delete $1; }
| formal | formal
{ $$ = new ParserFormals; $$->formals.emplace_back(*$1); $$->ellipsis = false; delete $1; } { $$ = new Formals; $$->formals.emplace_back(*$1); $$->ellipsis = false; delete $1; }
| |
{ $$ = new ParserFormals; $$->ellipsis = false; } { $$ = new Formals; $$->ellipsis = false; }
| ELLIPSIS | ELLIPSIS
{ $$ = new ParserFormals; $$->ellipsis = true; } { $$ = new Formals; $$->ellipsis = true; }
; ;
formal formal