- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[llvm][mustache] Refactor tokenizer for clarity #159188
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
                
     Merged
            
            
          Conversation
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
    
  This was referenced Sep 16, 2025 
      
  This was referenced Sep 16, 2025 
      
  This was referenced Sep 16, 2025 
      
| 
          
 @llvm/pr-subscribers-llvm-support Author: Paul Kirth (ilovepi) ChangesThis patch refactors the Mustache tokenizer by breaking the logic up Full diff: https://github.com/llvm/llvm-project/pull/159188.diff 1 Files Affected: 
 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:
 | 
    
97fa89f    to
    fbd8894      
    Compare
  
    6f78e4d    to
    97182cf      
    Compare
  
    fbd8894    to
    7cea784      
    Compare
  
    97182cf    to
    7a0b305      
    Compare
  
    7cea784    to
    630801a      
    Compare
  
    7a0b305    to
    8be25f4      
    Compare
  
    630801a    to
    794217c      
    Compare
  
    
              
                    evelez7
  
              
              approved these changes
              
                  
                    Sep 29, 2025 
                  
              
              
            
            
794217c    to
    87331c5      
    Compare
  
    8be25f4    to
    289f098      
    Compare
  
    87331c5    to
    bd4ebfd      
    Compare
  
    289f098    to
    41ea349      
    Compare
  
    
          Merge activity
  | 
    
bd4ebfd    to
    9ff3fba      
    Compare
  
        
          Base automatically changed from
    
      users/ilovepi/mustache-delimiter-impl
     to
    
      main
    
    September 30, 2025 00:00     
    
41ea349    to
    457c8d5      
    Compare
  
    This patch refactors the Mustache tokenizer by breaking the logic up with helper functions to improve clarity and simplify the code.
457c8d5    to
    0693f63      
    Compare
  
    
    
  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
  
      
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
This patch refactors the Mustache tokenizer by breaking the logic up
with helper functions to improve clarity and simplify the code.