From 5b53b53c4c94c9009f0d6c05ebf678c13740ff6c Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 23 Mar 2024 02:54:55 +0100 Subject: [PATCH] Library: solution for ''trailing prefix'' in parser-context MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * use a string-view embedded into the context-λ * on each match clip off some starting prefix from this string-view --- src/lib/text-template.hpp | 40 ++++++++++++---- tests/library/text-template-test.cpp | 19 ++++++++ wiki/thinkPad.ichthyo.mm | 70 +++++++++++++++++++++++++++- 3 files changed, 119 insertions(+), 10 deletions(-) diff --git a/src/lib/text-template.hpp b/src/lib/text-template.hpp index 990fbfc64..26b1795ce 100644 --- a/src/lib/text-template.hpp +++ b/src/lib/text-template.hpp @@ -101,7 +101,7 @@ #include "lib/nocopy.hpp" #include "lib/iter-index.hpp" #include "lib/iter-explorer.hpp" -#include "lib/format-util.hpp" +#include "lib/format-util.hpp"///////////////////OOO use format-string?? #include "lib/regex.hpp" #include "lib/util.hpp" @@ -151,23 +151,45 @@ namespace lib { }; Keyword syntaxCase{ESCAPE}; StrView lead; - StrView key; + string key; }; inline auto - parse (string input) + parse (string const& input) { - auto classify = [pre=size_t(0)] + auto classify = [rest=StrView(input)] (smatch mat) mutable -> TagSyntax { REQUIRE (not mat.empty()); - StrView lead{}; //////////////////////////////OOO find a way to move that along trailing + TagSyntax tag; + auto restAhead = mat.length() + mat.suffix().length(); + auto pre = rest.length() - restAhead; + tag.lead = rest.substr(0, pre); + rest = rest.substr(tag.lead.length()); if (mat[1].matched) - return TagSyntax{TagSyntax::ESCAPE,lead}; + return tag; + if (mat[5].matched) + tag.key = mat[5]; + if (mat[4].matched) + { // detected a logic keyword... + if ("if" == mat[4]) + tag.syntaxCase = mat[5].matched? TagSyntax::END_IF : TagSyntax::IF; + else + if ("for" == mat[4]) + tag.syntaxCase = mat[5].matched? TagSyntax::END_FOR : TagSyntax::FOR; + else + throw error::Logic("unexpected keyword"); + } + else + if (mat[3].matched) + tag.syntaxCase = TagSyntax::ELSE; + else + tag.syntaxCase = TagSyntax::KEYID; + return tag; }; - util::RegexSearchIter parser{input, ACCEPT_MARKUP}; -// return explore(parser) ///////////////////////////OOO find out why this is not forward-iterable -// .transform(classify); + + return explore (util::RegexSearchIter{input, ACCEPT_MARKUP}) + .transform(classify); } } diff --git a/tests/library/text-template-test.cpp b/tests/library/text-template-test.cpp index bbb6164dc..552ff8d4c 100644 --- a/tests/library/text-template-test.cpp +++ b/tests/library/text-template-test.cpp @@ -29,8 +29,10 @@ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp"///////////////////////TODO #include "lib/text-template.hpp" +#include "lib/format-string.hpp" #include "lib/format-cout.hpp"///////////////////////TODO #include "lib/test/diagnostic-output.hpp"///////////////////////TODO +#include "lib/stat/csv.hpp" //#include //#include @@ -39,6 +41,7 @@ //using std::array; using std::regex_search; using std::smatch; +using util::_Fmt; namespace lib { @@ -180,6 +183,22 @@ namespace test { CHECK (not mat[4].matched); CHECK (not mat[5].matched); CHECK (mat[1] == "\\$"_expect); // Sub-1 picks the escaped mark (and the remainder is no complete tag) + + + // Demonstration: can use this regular expression in a matching pipeline.... + input = "one ${two} three \\${four} ${if high} five"; + CHECK (util::join( + explore (util::RegexSearchIter{input, ACCEPT_MARKUP}) + .transform ([](smatch mat){ return mat.str(); })) + == + "${two}, \\$, ${if high}"_expect); + + auto render = [](TagSyntax& tag) -> string + { return _Fmt{"▶%s‖%d|%s‖▷"} % string{tag.lead} % uint(tag.syntaxCase) % tag.key; }; + + auto wau = parse(input) + .transform(render); +SHOW_EXPR(util::join(wau)) } diff --git a/wiki/thinkPad.ichthyo.mm b/wiki/thinkPad.ichthyo.mm index 97d12de04..d17edf4ae 100644 --- a/wiki/thinkPad.ichthyo.mm +++ b/wiki/thinkPad.ichthyo.mm @@ -113040,9 +113040,77 @@ std::cout << tmpl.render({"what", "World"}) << s - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +