From c852ae60da16b890f77e9e1b274d31dced73ae66 Mon Sep 17 00:00:00 2001 From: piegames Date: Tue, 1 Oct 2024 18:41:54 +0200 Subject: libexpr: Rewrite stripIndentation for indented strings This commit should faithfully reproduce the old behavior down to the bugs. The new code is a lot more readable, all quirks are well documented, and it is overall much more maintainable. Change-Id: I629585918e4f2b7d296b6b8330235cdc90b7bade --- src/libexpr/parser/parser-impl1.inc.cc | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) (limited to 'src/libexpr/parser/parser-impl1.inc.cc') diff --git a/src/libexpr/parser/parser-impl1.inc.cc b/src/libexpr/parser/parser-impl1.inc.cc index 0d41324b3..c65fb3ddc 100644 --- a/src/libexpr/parser/parser-impl1.inc.cc +++ b/src/libexpr/parser/parser-impl1.inc.cc @@ -533,36 +533,48 @@ template<> struct BuildAST : change_head { struct IndStringState : SubexprState { using SubexprState::SubexprState; - std::vector, StringToken>>> parts; + std::vector lines; }; -template -struct BuildAST> { +template<> struct BuildAST { static void apply(const auto & in, IndStringState & s, State & ps) { - s.parts.emplace_back(ps.at(in), StringToken{in.string_view(), Indented}); + s.lines.push_back(IndStringLine { in.string_view(), ps.at(in) }); + } +}; + +template +struct BuildAST> { + static void apply(const auto & in, IndStringState & s, State & ps) { + s.lines.back().parts.emplace_back(ps.at(in), StringToken{ in.string_view(), CanMerge }); } }; template<> struct BuildAST { static void apply(const auto & in, IndStringState & s, State & ps) { - s.parts.emplace_back(ps.at(in), s->popExprOnly()); + s.lines.back().parts.emplace_back(ps.at(in), s->popExprOnly()); } }; 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; - case 'r': s.parts.emplace_back(ps.at(in), StringToken{"\r"}); break; - case 't': s.parts.emplace_back(ps.at(in), StringToken{"\t"}); break; - default: s.parts.emplace_back(ps.at(in), StringToken{in.string_view()}); break; + case 'n': s.lines.back().parts.emplace_back(ps.at(in), StringToken{"\n"}); break; + case 'r': s.lines.back().parts.emplace_back(ps.at(in), StringToken{"\r"}); break; + case 't': s.lines.back().parts.emplace_back(ps.at(in), StringToken{"\t"}); break; + default: s.lines.back().parts.emplace_back(ps.at(in), StringToken{in.string_view()}); break; } } }; +template<> struct BuildAST { + static void apply(const auto & in, IndStringState & s, State & ps) { + s.lines.back().hasContent = true; + } +}; + 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))); + e.exprs.emplace_back(noPos, ps.stripIndentation(ps.at(in), std::move(s.lines))); } }; -- cgit v1.2.3