[llvm] 6ffacae - [llvm][mustache] Refactor tokenizer for clarity (#159188)

via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 29 17:55:13 PDT 2025


Author: Paul Kirth
Date: 2025-09-29T17:55:08-07:00
New Revision: 6ffacae996b7eecf3859d7c83c95e7d7d95bc03f

URL: https://github.com/llvm/llvm-project/commit/6ffacae996b7eecf3859d7c83c95e7d7d95bc03f
DIFF: https://github.com/llvm/llvm-project/commit/6ffacae996b7eecf3859d7c83c95e7d7d95bc03f.diff

LOG: [llvm][mustache] Refactor tokenizer for clarity (#159188)

This patch refactors the Mustache tokenizer by breaking the logic up
with helper functions to improve clarity and simplify the code.

Added: 
    

Modified: 
    llvm/lib/Support/Mustache.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 9834bf6f9361f..597288f26c4ac 100644
--- a/llvm/lib/Support/Mustache.cpp
+++ b/llvm/lib/Support/Mustache.cpp
@@ -560,11 +560,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,
@@ -614,31 +637,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:


        


More information about the llvm-commits mailing list