Skip to content

Commit 87a2ce4

Browse files
authored
Merge pull request #14535 from Radvendii/parser-cleanup
parser.y cleanup
2 parents 7503062 + 90ba96a commit 87a2ce4

File tree

3 files changed

+62
-38
lines changed

3 files changed

+62
-38
lines changed

src/libexpr/include/nix/expr/nixexpr.hh

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ struct ExprOpHasAttr : Expr
339339
Expr * e;
340340
std::span<AttrName> attrPath;
341341

342-
ExprOpHasAttr(std::pmr::polymorphic_allocator<char> & alloc, Expr * e, std::vector<AttrName> attrPath)
342+
ExprOpHasAttr(std::pmr::polymorphic_allocator<char> & alloc, Expr * e, std::span<AttrName> attrPath)
343343
: e(e)
344344
, attrPath({alloc.allocate_object<AttrName>(attrPath.size()), attrPath.size()})
345345
{
@@ -433,7 +433,7 @@ struct ExprList : Expr
433433
{
434434
std::span<Expr *> elems;
435435

436-
ExprList(std::pmr::polymorphic_allocator<char> & alloc, std::vector<Expr *> exprs)
436+
ExprList(std::pmr::polymorphic_allocator<char> & alloc, std::span<Expr *> exprs)
437437
: elems({alloc.allocate_object<Expr *>(exprs.size()), exprs.size()})
438438
{
439439
std::ranges::copy(exprs, elems.begin());
@@ -562,7 +562,7 @@ public:
562562
const PosTable & positions,
563563
std::pmr::polymorphic_allocator<char> & alloc,
564564
PosIdx pos,
565-
FormalsBuilder formals,
565+
const FormalsBuilder & formals,
566566
Expr * body)
567567
: ExprLambda(positions, alloc, pos, Symbol(), formals, body) {};
568568

@@ -753,7 +753,19 @@ struct ExprConcatStrings : Expr
753753
std::pmr::polymorphic_allocator<char> & alloc,
754754
const PosIdx & pos,
755755
bool forceString,
756-
const std::vector<std::pair<PosIdx, Expr *>> & es)
756+
std::span<std::pair<PosIdx, Expr *>> es)
757+
: pos(pos)
758+
, forceString(forceString)
759+
, es({alloc.allocate_object<std::pair<PosIdx, Expr *>>(es.size()), es.size()})
760+
{
761+
std::ranges::copy(es, this->es.begin());
762+
};
763+
764+
ExprConcatStrings(
765+
std::pmr::polymorphic_allocator<char> & alloc,
766+
const PosIdx & pos,
767+
bool forceString,
768+
std::initializer_list<std::pair<PosIdx, Expr *>> es)
757769
: pos(pos)
758770
, forceString(forceString)
759771
, es({alloc.allocate_object<std::pair<PosIdx, Expr *>>(es.size()), es.size()})
@@ -833,7 +845,19 @@ public:
833845
add(std::pmr::polymorphic_allocator<char> & alloc,
834846
const PosIdx & pos,
835847
bool forceString,
836-
const std::vector<std::pair<PosIdx, Expr *>> & es)
848+
std::span<std::pair<PosIdx, Expr *>> es)
849+
requires(std::same_as<C, ExprConcatStrings>)
850+
{
851+
return alloc.new_object<C>(alloc, pos, forceString, es);
852+
}
853+
854+
template<class C>
855+
[[gnu::always_inline]]
856+
C *
857+
add(std::pmr::polymorphic_allocator<char> & alloc,
858+
const PosIdx & pos,
859+
bool forceString,
860+
std::initializer_list<std::pair<PosIdx, Expr *>> es)
837861
requires(std::same_as<C, ExprConcatStrings>)
838862
{
839863
return alloc.new_object<C>(alloc, pos, forceString, es);

src/libexpr/include/nix/expr/parser-state.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ struct ParserState
9696
ExprAttrs * attrs, AttrPath && attrPath, const ParserLocation & loc, Expr * e, const ParserLocation & exprLoc);
9797
void addAttr(ExprAttrs * attrs, AttrPath & attrPath, const Symbol & symbol, ExprAttrs::AttrDef && def);
9898
void validateFormals(FormalsBuilder & formals, PosIdx pos = noPos, Symbol arg = {});
99-
Expr * stripIndentation(const PosIdx pos, std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> && es);
99+
Expr * stripIndentation(const PosIdx pos, std::span<std::pair<PosIdx, std::variant<Expr *, StringToken>>> es);
100100
PosIdx at(const ParserLocation & loc);
101101
};
102102

@@ -239,7 +239,7 @@ inline void ParserState::validateFormals(FormalsBuilder & formals, PosIdx pos, S
239239
}
240240

241241
inline Expr *
242-
ParserState::stripIndentation(const PosIdx pos, std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>> && es)
242+
ParserState::stripIndentation(const PosIdx pos, std::span<std::pair<PosIdx, std::variant<Expr *, StringToken>>> es)
243243
{
244244
if (es.empty())
245245
return exprs.add<ExprString>(""_sds);
@@ -343,7 +343,7 @@ ParserState::stripIndentation(const PosIdx pos, std::vector<std::pair<PosIdx, st
343343
auto * const result = (es2)[0].second;
344344
return result;
345345
}
346-
return exprs.add<ExprConcatStrings>(exprs.alloc, pos, true, std::move(es2));
346+
return exprs.add<ExprConcatStrings>(exprs.alloc, pos, true, es2);
347347
}
348348

349349
inline PosIdx LexerState::at(const ParserLocation & loc)

src/libexpr/parser.y

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -126,26 +126,26 @@ static Expr * makeCall(Exprs & exprs, PosIdx pos, Expr * fn, Expr * arg) {
126126

127127
%define api.value.type variant
128128

129-
%type <nix::Expr *> start expr expr_function expr_if expr_op
130-
%type <nix::Expr *> expr_select expr_simple expr_app
131-
%type <nix::Expr *> expr_pipe_from expr_pipe_into
129+
%type <Expr *> start expr expr_function expr_if expr_op
130+
%type <Expr *> expr_select expr_simple expr_app
131+
%type <Expr *> expr_pipe_from expr_pipe_into
132132
%type <std::vector<Expr *>> list
133-
%type <nix::ExprAttrs *> binds binds1
134-
%type <nix::FormalsBuilder> formals formal_set
135-
%type <nix::Formal> formal
136-
%type <std::vector<nix::AttrName>> attrpath
137-
%type <std::vector<std::pair<nix::AttrName, nix::PosIdx>>> attrs
138-
%type <std::vector<std::pair<nix::PosIdx, nix::Expr *>>> string_parts_interpolated
139-
%type <std::vector<std::pair<nix::PosIdx, std::variant<nix::Expr *, nix::StringToken>>>> ind_string_parts
140-
%type <nix::Expr *> path_start
141-
%type <std::variant<nix::Expr *, std::string_view>> string_parts string_attr
142-
%type <nix::StringToken> attr
143-
%token <nix::StringToken> ID
144-
%token <nix::StringToken> STR IND_STR
145-
%token <nix::NixInt> INT_LIT
146-
%token <nix::NixFloat> FLOAT_LIT
147-
%token <nix::StringToken> PATH HPATH SPATH PATH_END
148-
%token <nix::StringToken> URI
133+
%type <ExprAttrs *> binds binds1
134+
%type <FormalsBuilder> formals formal_set
135+
%type <Formal> formal
136+
%type <std::vector<AttrName>> attrpath
137+
%type <std::vector<std::pair<AttrName, PosIdx>>> attrs
138+
%type <std::vector<std::pair<PosIdx, Expr *>>> string_parts_interpolated
139+
%type <std::vector<std::pair<PosIdx, std::variant<Expr *, StringToken>>>> ind_string_parts
140+
%type <Expr *> path_start
141+
%type <std::variant<Expr *, std::string_view>> string_parts string_attr
142+
%type <StringToken> attr
143+
%token <StringToken> ID
144+
%token <StringToken> STR IND_STR
145+
%token <NixInt> INT_LIT
146+
%token <NixFloat> FLOAT_LIT
147+
%token <StringToken> PATH HPATH SPATH PATH_END
148+
%token <StringToken> URI
149149
%token IF THEN ELSE ASSERT WITH LET IN_KW REC INHERIT EQ NEQ AND OR IMPL OR_KW
150150
%token PIPE_FROM PIPE_INTO /* <| and |> */
151151
%token DOLLAR_CURLY /* == ${ */
@@ -186,23 +186,23 @@ expr_function
186186
| formal_set ':' expr_function[body]
187187
{
188188
state->validateFormals($formal_set);
189-
auto me = state->exprs.add<ExprLambda>(state->positions, state->exprs.alloc, CUR_POS, std::move($formal_set), $body);
189+
auto me = state->exprs.add<ExprLambda>(state->positions, state->exprs.alloc, CUR_POS, $formal_set, $body);
190190
$$ = me;
191191
SET_DOC_POS(me, @1);
192192
}
193193
| formal_set '@' ID ':' expr_function[body]
194194
{
195195
auto arg = state->symbols.create($ID);
196196
state->validateFormals($formal_set, CUR_POS, arg);
197-
auto me = state->exprs.add<ExprLambda>(state->positions, state->exprs.alloc, CUR_POS, arg, std::move($formal_set), $body);
197+
auto me = state->exprs.add<ExprLambda>(state->positions, state->exprs.alloc, CUR_POS, arg, $formal_set, $body);
198198
$$ = me;
199199
SET_DOC_POS(me, @1);
200200
}
201201
| ID '@' formal_set ':' expr_function[body]
202202
{
203203
auto arg = state->symbols.create($ID);
204204
state->validateFormals($formal_set, CUR_POS, arg);
205-
auto me = state->exprs.add<ExprLambda>(state->positions, state->exprs.alloc, CUR_POS, arg, std::move($formal_set), $body);
205+
auto me = state->exprs.add<ExprLambda>(state->positions, state->exprs.alloc, CUR_POS, arg, $formal_set, $body);
206206
$$ = me;
207207
SET_DOC_POS(me, @1);
208208
}
@@ -251,7 +251,7 @@ expr_op
251251
| expr_op OR expr_op { $$ = state->exprs.add<ExprOpOr>(state->at(@2), $1, $3); }
252252
| expr_op IMPL expr_op { $$ = state->exprs.add<ExprOpImpl>(state->at(@2), $1, $3); }
253253
| expr_op UPDATE expr_op { $$ = state->exprs.add<ExprOpUpdate>(state->at(@2), $1, $3); }
254-
| expr_op '?' attrpath { $$ = state->exprs.add<ExprOpHasAttr>(state->exprs.alloc, $1, std::move($3)); }
254+
| expr_op '?' attrpath { $$ = state->exprs.add<ExprOpHasAttr>(state->exprs.alloc, $1, $3); }
255255
| expr_op '+' expr_op
256256
{ $$ = state->exprs.add<ExprConcatStrings>(state->exprs.alloc, state->at(@2), false, {{state->at(@1), $1}, {state->at(@3), $3}}); }
257257
| expr_op '-' expr_op { $$ = state->exprs.add<ExprCall>(state->at(@2), state->exprs.add<ExprVar>(state->s.sub), {$1, $3}); }
@@ -272,9 +272,9 @@ expr_app
272272

273273
expr_select
274274
: expr_simple '.' attrpath
275-
{ $$ = state->exprs.add<ExprSelect>(state->exprs.alloc, CUR_POS, $1, std::move($3), nullptr); }
275+
{ $$ = state->exprs.add<ExprSelect>(state->exprs.alloc, CUR_POS, $1, $3, nullptr); }
276276
| expr_simple '.' attrpath OR_KW expr_select
277-
{ $$ = state->exprs.add<ExprSelect>(state->exprs.alloc, CUR_POS, $1, std::move($3), $5); $5->warnIfCursedOr(state->symbols, state->positions); }
277+
{ $$ = state->exprs.add<ExprSelect>(state->exprs.alloc, CUR_POS, $1, $3, $5); $5->warnIfCursedOr(state->symbols, state->positions); }
278278
| /* Backwards compatibility: because Nixpkgs has a function named ‘or’,
279279
allow stuff like ‘map or [...]’. This production is problematic (see
280280
https://github.com/NixOS/nix/issues/11118) and will be refactored in the
@@ -304,12 +304,12 @@ expr_simple
304304
$2);
305305
}
306306
| IND_STRING_OPEN ind_string_parts IND_STRING_CLOSE {
307-
$$ = state->stripIndentation(CUR_POS, std::move($2));
307+
$$ = state->stripIndentation(CUR_POS, $2);
308308
}
309309
| path_start PATH_END
310310
| path_start string_parts_interpolated PATH_END {
311311
$2.insert($2.begin(), {state->at(@1), $1});
312-
$$ = state->exprs.add<ExprConcatStrings>(state->exprs.alloc, CUR_POS, false, std::move($2));
312+
$$ = state->exprs.add<ExprConcatStrings>(state->exprs.alloc, CUR_POS, false, $2);
313313
}
314314
| SPATH {
315315
std::string_view path($1.p + 1, $1.l - 2);
@@ -338,12 +338,12 @@ expr_simple
338338
{ $2->pos = CUR_POS; $$ = $2; }
339339
| '{' '}'
340340
{ $$ = state->exprs.add<ExprAttrs>(CUR_POS); }
341-
| '[' list ']' { $$ = state->exprs.add<ExprList>(state->exprs.alloc, std::move($2)); }
341+
| '[' list ']' { $$ = state->exprs.add<ExprList>(state->exprs.alloc, $2); }
342342
;
343343

344344
string_parts
345345
: STR { $$ = $1; }
346-
| string_parts_interpolated { $$ = state->exprs.add<ExprConcatStrings>(state->exprs.alloc, CUR_POS, true, std::move($1)); }
346+
| string_parts_interpolated { $$ = state->exprs.add<ExprConcatStrings>(state->exprs.alloc, CUR_POS, true, $1); }
347347
| { $$ = std::string_view(); }
348348
;
349349

@@ -425,7 +425,7 @@ binds1
425425
if (!$accum->inheritFromExprs)
426426
$accum->inheritFromExprs = std::make_unique<std::vector<Expr *>>();
427427
$accum->inheritFromExprs->push_back($expr);
428-
auto from = new nix::ExprInheritFrom(state->at(@expr), $accum->inheritFromExprs->size() - 1);
428+
auto from = state->exprs.add<ExprInheritFrom>(state->at(@expr), $accum->inheritFromExprs->size() - 1);
429429
for (auto & [i, iPos] : $attrs) {
430430
if ($accum->attrs.find(i.symbol) != $accum->attrs.end())
431431
state->dupAttr(i.symbol, iPos, $accum->attrs[i.symbol].pos);

0 commit comments

Comments
 (0)