https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/159188
>From 97182cf5770867345323c9e7af7d9b69fa3e678d Mon Sep 17 00:00:00 2001 From: Paul Kirth <[email protected]> Date: Thu, 11 Sep 2025 23:45:16 -0700 Subject: [PATCH] [llvm][mustache] Refactor tokenizer for clarity This patch refactors the Mustache tokenizer by breaking the logic up with helper functions to improve clarity and simplify the code. --- llvm/lib/Support/Mustache.cpp | 49 +++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp index aa25ea476cbc1..1f1dd037552f4 100644 --- a/llvm/lib/Support/Mustache.cpp +++ b/llvm/lib/Support/Mustache.cpp @@ -550,11 +550,34 @@ class Parser { llvm::StringMap<SectionLambda> &SectionLambdas, EscapeMap &Escapes); + void parseSection(ASTNode *Parent, ASTNode::Type Ty, const Accessor &A, + llvm::StringMap<AstPtr> &Partials, + llvm::StringMap<Lambda> &Lambdas, + llvm::StringMap<SectionLambda> &SectionLambdas, + EscapeMap &Escapes); + SmallVector<Token> Tokens; size_t CurrentPtr; StringRef TemplateStr; }; +void Parser::parseSection(ASTNode *Parent, ASTNode::Type Ty, const Accessor &A, + llvm::StringMap<AstPtr> &Partials, + llvm::StringMap<Lambda> &Lambdas, + llvm::StringMap<SectionLambda> &SectionLambdas, + EscapeMap &Escapes) { + AstPtr CurrentNode = + createNode(Ty, A, Parent, Partials, Lambdas, SectionLambdas, Escapes); + size_t Start = CurrentPtr; + parseMustache(CurrentNode.get(), Partials, Lambdas, SectionLambdas, Escapes); + const size_t End = CurrentPtr - 1; + std::string RawBody; + for (std::size_t I = Start; I < End; I++) + RawBody += Tokens[I].RawBody; + CurrentNode->setRawBody(std::move(RawBody)); + Parent->addChild(std::move(CurrentNode)); +} + AstPtr Parser::parse(llvm::StringMap<AstPtr> &Partials, llvm::StringMap<Lambda> &Lambdas, llvm::StringMap<SectionLambda> &SectionLambdas, @@ -604,31 +627,13 @@ void Parser::parseMustache(ASTNode *Parent, llvm::StringMap<AstPtr> &Partials, break; } case Token::Type::SectionOpen: { - CurrentNode = createNode(ASTNode::Section, A, Parent, Partials, Lambdas, - SectionLambdas, Escapes); - size_t Start = CurrentPtr; - parseMustache(CurrentNode.get(), Partials, Lambdas, SectionLambdas, - Escapes); - const size_t End = CurrentPtr - 1; - std::string RawBody; - for (std::size_t I = Start; I < End; I++) - RawBody += Tokens[I].RawBody; - CurrentNode->setRawBody(std::move(RawBody)); - Parent->addChild(std::move(CurrentNode)); + parseSection(Parent, ASTNode::Section, A, Partials, Lambdas, + SectionLambdas, Escapes); break; } case Token::Type::InvertSectionOpen: { - CurrentNode = createNode(ASTNode::InvertSection, A, Parent, Partials, - Lambdas, SectionLambdas, Escapes); - size_t Start = CurrentPtr; - parseMustache(CurrentNode.get(), Partials, Lambdas, SectionLambdas, - Escapes); - const size_t End = CurrentPtr - 1; - std::string RawBody; - for (size_t Idx = Start; Idx < End; Idx++) - RawBody += Tokens[Idx].RawBody; - CurrentNode->setRawBody(std::move(RawBody)); - Parent->addChild(std::move(CurrentNode)); + parseSection(Parent, ASTNode::InvertSection, A, Partials, Lambdas, + SectionLambdas, Escapes); break; } case Token::Type::Comment: _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
