From f98ee075731ab25e2ebcee0969f1b662239e3354 Mon Sep 17 00:00:00 2001 From: piegames Date: Sun, 18 Aug 2024 21:17:26 +0200 Subject: [PATCH] libexpr: rename grammar to grammar::v1 Let's make some space in the namespace for a v2 https://wiki.lix.systems/books/lix-contributors/page/nix-lang-v2 Change-Id: If56e6dbf680d931233aa822ef91c8832464471e4 --- src/libexpr/parser/grammar.hh | 60 ++++++------- src/libexpr/parser/parser.cc | 154 +++++++++++++++++----------------- 2 files changed, 107 insertions(+), 107 deletions(-) diff --git a/src/libexpr/parser/grammar.hh b/src/libexpr/parser/grammar.hh index 2c5a3d1be..6e42609c0 100644 --- a/src/libexpr/parser/grammar.hh +++ b/src/libexpr/parser/grammar.hh @@ -12,7 +12,7 @@ // eolf rules in favor of reproducing the old flex lexer as faithfully as // possible, and deferring calculation of positions to downstream users. -namespace nix::parser::grammar { +namespace nix::parser::grammar::v1 { using namespace tao::pegtl; namespace p = tao::pegtl; @@ -352,10 +352,10 @@ struct formals : semantic, _formals, seq< struct _attr { struct simple : semantic, sor {}; - struct string : semantic, seq {}; + struct string : semantic, seq {}; struct expr : semantic, seq< TAO_PEGTL_STRING("${"), seps, - must, seps, + must, seps, must> > {}; }; @@ -452,9 +452,9 @@ struct _expr { struct id : semantic, t::identifier {}; struct int_ : semantic, t::integer {}; struct float_ : semantic, t::floating {}; - struct string : semantic, seq {}; - struct ind_string : semantic, seq {}; - struct path : semantic, seq {}; + struct string : semantic, seq {}; + struct ind_string : semantic, seq {}; + struct path : semantic, seq {}; struct uri : semantic, t::uri {}; struct ancient_let : semantic, _attrset {}; struct rec_set : semantic, _attrset {}; @@ -628,34 +628,34 @@ struct nothing : p::nothing { template struct operator_semantics { - struct has_attr : grammar::op::has_attr { + struct has_attr : grammar::v1::op::has_attr { AttrPathT path; }; struct OpEntry { OpCtx ctx; uint8_t prec; - grammar::op::kind assoc; + grammar::v1::op::kind assoc; std::variant< - grammar::op::not_, - grammar::op::unary_minus, - grammar::op::implies, - grammar::op::or_, - grammar::op::and_, - grammar::op::equals, - grammar::op::not_equals, - grammar::op::less_eq, - grammar::op::greater_eq, - grammar::op::update, - grammar::op::concat, - grammar::op::less, - grammar::op::greater, - grammar::op::plus, - grammar::op::minus, - grammar::op::mul, - grammar::op::div, - grammar::op::pipe_right, - grammar::op::pipe_left, + grammar::v1::op::not_, + grammar::v1::op::unary_minus, + grammar::v1::op::implies, + grammar::v1::op::or_, + grammar::v1::op::and_, + grammar::v1::op::equals, + grammar::v1::op::not_equals, + grammar::v1::op::less_eq, + grammar::v1::op::greater_eq, + grammar::v1::op::update, + grammar::v1::op::concat, + grammar::v1::op::less, + grammar::v1::op::greater, + grammar::v1::op::plus, + grammar::v1::op::minus, + grammar::v1::op::mul, + grammar::v1::op::div, + grammar::v1::op::pipe_right, + grammar::v1::op::pipe_left, has_attr > op; }; @@ -676,7 +676,7 @@ struct operator_semantics { auto & [ctx, precedence, kind, op] = ops.back(); // NOTE this relies on associativity not being mixed within a precedence level. if ((precedence > toPrecedence) - || (kind != grammar::op::kind::leftAssoc && precedence == toPrecedence)) + || (kind != grammar::v1::op::kind::leftAssoc && precedence == toPrecedence)) break; std::visit([&, ctx=std::move(ctx)] (auto & op) { exprs.push_back(static_cast(*this).applyOp(ctx, op, args...)); @@ -694,9 +694,9 @@ struct operator_semantics { void pushOp(OpCtx ctx, auto o, auto &... args) { - if (o.kind != grammar::op::kind::unary) + if (o.kind != grammar::v1::op::kind::unary) reduce(o.precedence, args...); - if (!ops.empty() && o.kind == grammar::op::kind::nonAssoc) { + if (!ops.empty() && o.kind == grammar::v1::op::kind::nonAssoc) { auto & [_pos, _prec, _kind, _o] = ops.back(); if (_kind == o.kind && _prec == o.precedence) Self::badOperator(ctx, args...); diff --git a/src/libexpr/parser/parser.cc b/src/libexpr/parser/parser.cc index 17463056c..296b7ee57 100644 --- a/src/libexpr/parser/parser.cc +++ b/src/libexpr/parser/parser.cc @@ -19,7 +19,7 @@ #include #define ANALYZE_GRAMMAR \ ([] { \ - const std::size_t issues = tao::pegtl::analyze(); \ + const std::size_t issues = tao::pegtl::analyze(); \ assert(issues == 0); \ })() #else @@ -46,20 +46,20 @@ error_message_for(p::one<']'>) = "expecting ']'"; error_message_for(p::one<':'>) = "expecting ':'"; error_message_for(p::string<'\'', '\''>) = "expecting \"''\""; error_message_for(p::any) = "expecting any character"; -error_message_for(grammar::eof) = "expecting end of file"; -error_message_for(grammar::seps) = "expecting separators"; -error_message_for(grammar::path::forbid_prefix_triple_slash) = "too many slashes in path"; -error_message_for(grammar::path::forbid_prefix_double_slash_no_interp) = "path has a trailing slash"; -error_message_for(grammar::expr) = "expecting expression"; -error_message_for(grammar::expr::unary) = "expecting expression"; -error_message_for(grammar::binding::equal) = "expecting '='"; -error_message_for(grammar::expr::lambda::arg) = "expecting identifier"; -error_message_for(grammar::formals) = "expecting formals"; -error_message_for(grammar::attrpath) = "expecting attribute path"; -error_message_for(grammar::expr::select) = "expecting selection expression"; -error_message_for(grammar::t::kw_then) = "expecting 'then'"; -error_message_for(grammar::t::kw_else) = "expecting 'else'"; -error_message_for(grammar::t::kw_in) = "expecting 'in'"; +error_message_for(grammar::v1::eof) = "expecting end of file"; +error_message_for(grammar::v1::seps) = "expecting separators"; +error_message_for(grammar::v1::path::forbid_prefix_triple_slash) = "too many slashes in path"; +error_message_for(grammar::v1::path::forbid_prefix_double_slash_no_interp) = "path has a trailing slash"; +error_message_for(grammar::v1::expr) = "expecting expression"; +error_message_for(grammar::v1::expr::unary) = "expecting expression"; +error_message_for(grammar::v1::binding::equal) = "expecting '='"; +error_message_for(grammar::v1::expr::lambda::arg) = "expecting identifier"; +error_message_for(grammar::v1::formals) = "expecting formals"; +error_message_for(grammar::v1::attrpath) = "expecting attribute path"; +error_message_for(grammar::v1::expr::select) = "expecting selection expression"; +error_message_for(grammar::v1::t::kw_then) = "expecting 'then'"; +error_message_for(grammar::v1::t::kw_else) = "expecting 'else'"; +error_message_for(grammar::v1::t::kw_in) = "expecting 'in'"; struct SyntaxErrors { @@ -87,7 +87,7 @@ struct Control : p::must_if::control }; struct ExprState - : grammar:: + : grammar::v1:: operator_semantics>> { std::unique_ptr popExprOnly() { @@ -158,7 +158,7 @@ struct ExprState } std::pair> applyOp(PosIdx pos, auto & op, State & state) { - using Op = grammar::op; + using Op = grammar::v1::op; auto not_ = [] (auto e) { return std::make_unique(std::move(e)); @@ -224,7 +224,7 @@ public: template -struct BuildAST : grammar::nothing {}; +struct BuildAST : grammar::v1::nothing {}; struct LambdaState : SubexprState { using SubexprState::SubexprState; @@ -240,7 +240,7 @@ struct FormalsState : SubexprState { Formal formal{}; }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, FormalsState & s, State & ps) { s.formal = { .pos = ps.at(in), @@ -249,25 +249,25 @@ template<> struct BuildAST { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply0(FormalsState & s, State &) { s.formals.formals.emplace_back(std::move(s.formal)); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply0(FormalsState & s, State & ps) { s.formal.def = s->popExprOnly(); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply0(FormalsState & s, State &) { s.formals.ellipsis = true; } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(FormalsState & f, LambdaState & s, State &) { s.formals = std::make_unique(std::move(f.formals)); } @@ -282,13 +282,13 @@ struct AttrState : SubexprState { void pushAttr(T && attr, PosIdx) { attrs.emplace_back(std::forward(attr)); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, auto & s, State & ps) { s.pushAttr(ps.symbols.create(in.string_view()), ps.at(in)); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, auto & s, State & ps) { auto e = s->popExprOnly(); if (auto str = dynamic_cast(e.get())) @@ -298,7 +298,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST : BuildAST {}; +template<> struct BuildAST : BuildAST {}; struct BindingsState : SubexprState { using SubexprState::SubexprState; @@ -319,14 +319,14 @@ struct InheritState : SubexprState { void pushAttr(T && attr, PosIdx pos) { attrs.emplace_back(std::forward(attr), pos); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, InheritState & s, State & ps) { s.from = s->popExprOnly(); s.fromPos = ps.at(in); } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(InheritState & s, BindingsState & b, State & ps) { auto & attrs = b.attrs.attrs; // TODO this should not reuse generic attrpath rules. @@ -377,25 +377,25 @@ template<> struct BuildAST : change_head { } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(AttrState & a, BindingsState & s, State & ps) { s.path = std::move(a.attrs); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply0(BindingsState & s, State & ps) { s.value = s->popExprOnly(); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, BindingsState & s, State & ps) { ps.addAttr(&s.attrs, std::move(s.path), std::move(s.value), ps.at(in)); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, ExprState & s, State & ps) { if (in.string_view() == "__curPos") s.pushExpr(ps.at(in), ps.at(in)); @@ -404,7 +404,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, ExprState & s, State & ps) { int64_t v; if (std::from_chars(in.begin(), in.end(), v).ec != std::errc{}) { @@ -417,7 +417,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, ExprState & s, State & ps) { // copy the input into a temporary string so we can call stod. // can't use from_chars because libc++ (thus darwin) does not have it, @@ -514,33 +514,33 @@ struct StringState : SubexprState { } }; -template struct BuildAST> { +template struct BuildAST> { static void apply(const auto & in, StringState & s, State & ps) { s.append(ps.at(in), in.string_view()); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, StringState & s, State & ps) { s.append(ps.at(in), in.string_view()); // FIXME compat with old parser } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, StringState & s, State & ps) { s.endLiteral(); s.parts.emplace_back(ps.at(in), s->popExprOnly()); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, StringState & s, State & ps) { s.append(ps.at(in), "\\"); // FIXME compat with old parser s.append(ps.at(in), in.string_view()); } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(StringState & s, ExprState & e, State &) { e.exprs.emplace_back(noPos, s.finish()); } @@ -553,19 +553,19 @@ struct IndStringState : SubexprState { }; template -struct BuildAST> { +struct BuildAST> { static void apply(const auto & in, IndStringState & s, State & ps) { s.parts.emplace_back(ps.at(in), StringToken{in.string_view(), Indented}); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, IndStringState & s, State & ps) { s.parts.emplace_back(ps.at(in), s->popExprOnly()); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, IndStringState & s, State & ps) { switch (*in.begin()) { case 'n': s.parts.emplace_back(ps.at(in), StringToken{"\n"}); break; @@ -576,22 +576,22 @@ template<> struct BuildAST { } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success(const auto & in, IndStringState & s, ExprState & e, State & ps) { e.exprs.emplace_back(noPos, ps.stripIndentation(ps.at(in), std::move(s.parts))); } }; -template struct BuildAST> { +template struct BuildAST> { static void apply(const auto & in, StringState & s, State & ps) { s.append(ps.at(in), in.string_view()); s.endLiteral(); } }; -template<> struct BuildAST : BuildAST {}; +template<> struct BuildAST : BuildAST {}; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, StringState & s, State & ps) { Path path(absPath(in.string(), ps.basePath.path.abs())); /* add back in the trailing '/' to the first segment */ @@ -601,7 +601,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, StringState & s, State & ps) { if (evalSettings.pureEval) throw Error("the path '%s' can not be resolved in pure mode", in.string_view()); @@ -610,7 +610,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, StringState & s, State & ps) { std::vector> args{2}; args[0] = std::make_unique(ps.s.nixPath); @@ -624,7 +624,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { template static void check_slash(PosIdx end, StringState & s, State & ps) { auto e = dynamic_cast(s.parts.back().second.get()); @@ -650,11 +650,11 @@ template<> struct BuildAST : change_head { }; // strings and paths sare handled fully by the grammar-level rule for now -template<> struct BuildAST : p::maybe_nothing {}; -template<> struct BuildAST : p::maybe_nothing {}; -template<> struct BuildAST : p::maybe_nothing {}; +template<> struct BuildAST : p::maybe_nothing {}; +template<> struct BuildAST : p::maybe_nothing {}; +template<> struct BuildAST : p::maybe_nothing {}; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, ExprState & s, State & ps) { bool URLLiterals = ps.featureSettings.isEnabled(Dep::UrlLiterals); if (!URLLiterals) @@ -666,7 +666,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success(const auto & in, BindingsState & b, ExprState & s, State & ps) { // Added 2024-09-18. Turn into an error at some point in the future. // See the documentation on deprecated features for more details. @@ -684,7 +684,7 @@ template<> struct BuildAST : change_head struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success(const auto & in, BindingsState & b, ExprState & s, State & ps) { // Before inserting new attrs, check for __override and throw an error // (the error will initially be a warning to ease migration) @@ -698,7 +698,7 @@ template<> struct BuildAST : change_head } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success(const auto & in, BindingsState & b, ExprState & s, State & ps) { b.attrs.pos = ps.at(in); s.pushExpr(b.attrs.pos, std::move(b.attrs)); @@ -707,7 +707,7 @@ template<> struct BuildAST : change_head { using ListState = std::vector>; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success(const auto & in, ListState & ls, ExprState & s, State & ps) { auto e = std::make_unique(); e->elems = std::move(ls); @@ -715,7 +715,7 @@ template<> struct BuildAST : change_head { } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(ExprState & e, ListState & s, State & ps) { s.emplace_back(e.finish(ps).second); } @@ -728,25 +728,25 @@ struct SelectState : SubexprState { ExprSelect * e = nullptr; }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, SelectState & s, State & ps) { s.pos = ps.at(in); } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(AttrState & a, SelectState & s, State &) { s.e = &s->pushExpr(s.pos, s.pos, s->popExprOnly(), std::move(a.attrs), nullptr); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply0(SelectState & s, State &) { s.e->def = s->popExprOnly(); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, SelectState & s, State & ps) { std::vector> args(1); args[0] = std::make_unique(ps.at(in), ps.s.or_); @@ -754,7 +754,7 @@ template<> struct BuildAST { } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(const auto &...) {} }; @@ -765,13 +765,13 @@ struct AppState : SubexprState { ExprCall * e = nullptr; }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, AppState & s, State & ps) { s.pos = ps.at(in); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(auto & in, AppState & s, State & ps) { auto arg = s->popExprOnly(), fn = s->popExprOnly(); if ((s.e = dynamic_cast(fn.get()))) { @@ -789,34 +789,34 @@ template<> struct BuildAST { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply0(AppState & s, State & ps) { s.e->args.push_back(s->popExprOnly()); } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(const auto &...) {} }; -template struct BuildAST> { +template struct BuildAST> { static void apply(const auto & in, ExprState & s, State & ps) { s.pushOp(ps.at(in), Op{}, ps); } }; -template<> struct BuildAST> : change_head { +template<> struct BuildAST> : change_head { static void success(const auto & in, AttrState & a, ExprState & s, State & ps) { s.pushOp(ps.at(in), ExprState::has_attr{{}, std::move(a.attrs)}, ps); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, LambdaState & s, State & ps) { s.arg = ps.symbols.create(in.string_view()); } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success(const auto & in, LambdaState & l, ExprState & s, State & ps) { if (l.formals) l.formals = ps.validateFormals(std::move(l.formals), ps.at(in), l.arg); @@ -824,21 +824,21 @@ template<> struct BuildAST : change_head { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, ExprState & s, State & ps) { auto body = s.popExprOnly(), cond = s.popExprOnly(); s.pushExpr(ps.at(in), ps.at(in), std::move(cond), std::move(body)); } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, ExprState & s, State & ps) { auto body = s.popExprOnly(), scope = s.popExprOnly(); s.pushExpr(ps.at(in), ps.at(in), std::move(scope), std::move(body)); } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success(const auto & in, BindingsState & b, ExprState & s, State & ps) { if (!b.attrs.dynamicAttrs.empty()) throw ParseError({ @@ -850,14 +850,14 @@ template<> struct BuildAST : change_head { } }; -template<> struct BuildAST { +template<> struct BuildAST { static void apply(const auto & in, ExprState & s, State & ps) { auto else_ = s.popExprOnly(), then = s.popExprOnly(), cond = s.popExprOnly(); s.pushExpr(ps.at(in), ps.at(in), std::move(cond), std::move(then), std::move(else_)); } }; -template<> struct BuildAST : change_head { +template<> struct BuildAST : change_head { static void success0(ExprState & inner, ExprState & outer, State & ps) { outer.exprs.push_back(inner.finish(ps)); } @@ -893,7 +893,7 @@ Expr * EvalState::parse( p::string_input inp{std::string_view{text, length}, "input"}; try { - p::parse(inp, x, s); + p::parse(inp, x, s); } catch (p::parse_error & e) { auto pos = e.positions().back(); throw ParseError({