Skip to content

Conversation

ilovepi
Copy link
Contributor

@ilovepi ilovepi commented Sep 16, 2025

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

Copy link
Contributor Author

ilovepi commented Sep 16, 2025

@llvmbot
Copy link
Member

llvmbot commented Sep 16, 2025

@llvm/pr-subscribers-llvm-support

Author: Paul Kirth (ilovepi)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/159188.diff

1 Files Affected:

  • (modified) llvm/lib/Support/Mustache.cpp (+27-22)
diff --git a/llvm/lib/Support/Mustache.cpp b/llvm/lib/Support/Mustache.cpp
index 43ce6adbba41a..08317a7b98902 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:

@ilovepi ilovepi force-pushed the users/ilovepi/mustache-delimiter-impl branch from 97fa89f to fbd8894 Compare September 22, 2025 17:07
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-tokenizer branch 2 times, most recently from 6f78e4d to 97182cf Compare September 22, 2025 17:56
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-delimiter-impl branch from fbd8894 to 7cea784 Compare September 22, 2025 17:56
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-tokenizer branch from 97182cf to 7a0b305 Compare September 25, 2025 22:12
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-delimiter-impl branch from 7cea784 to 630801a Compare September 25, 2025 22:12
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-tokenizer branch from 7a0b305 to 8be25f4 Compare September 26, 2025 01:55
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-delimiter-impl branch from 630801a to 794217c Compare September 26, 2025 01:55
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-delimiter-impl branch from 794217c to 87331c5 Compare September 29, 2025 17:39
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-tokenizer branch from 8be25f4 to 289f098 Compare September 29, 2025 17:39
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-delimiter-impl branch from 87331c5 to bd4ebfd Compare September 29, 2025 22:28
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-tokenizer branch from 289f098 to 41ea349 Compare September 29, 2025 22:28
Copy link
Contributor Author

ilovepi commented Sep 29, 2025

Merge activity

  • Sep 29, 11:30 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Sep 30, 12:00 AM UTC: Graphite rebased this pull request as part of a merge.
  • Sep 30, 12:27 AM UTC: Graphite rebased this pull request as part of a merge.
  • Sep 30, 12:55 AM UTC: @ilovepi merged this pull request with Graphite.

@ilovepi ilovepi force-pushed the users/ilovepi/mustache-delimiter-impl branch from bd4ebfd to 9ff3fba Compare September 29, 2025 23:32
Base automatically changed from users/ilovepi/mustache-delimiter-impl to main September 30, 2025 00:00
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-tokenizer branch from 41ea349 to 457c8d5 Compare September 30, 2025 00:00
This patch refactors the Mustache tokenizer by breaking the logic up
with helper functions to improve clarity and simplify the code.
@ilovepi ilovepi force-pushed the users/ilovepi/mustache-tokenizer branch from 457c8d5 to 0693f63 Compare September 30, 2025 00:27
@ilovepi ilovepi merged commit 6ffacae into main Sep 30, 2025
9 checks passed
@ilovepi ilovepi deleted the users/ilovepi/mustache-tokenizer branch September 30, 2025 00:55
mahesh-attarde pushed a commit to mahesh-attarde/llvm-project that referenced this pull request Oct 3, 2025
This patch refactors the Mustache tokenizer by breaking the logic up
with helper functions to improve clarity and simplify the code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants