@@ -138,26 +138,17 @@ class ASTNode {
138
138
InvertSection,
139
139
};
140
140
141
- ASTNode (llvm::StringMap<AstPtr> &Partials, llvm::StringMap<Lambda> &Lambdas,
142
- llvm::StringMap<SectionLambda> &SectionLambdas, EscapeMap &Escapes)
143
- : Partials(Partials), Lambdas(Lambdas), SectionLambdas(SectionLambdas),
144
- Escapes (Escapes), Ty(Type::Root), Parent(nullptr ),
145
- ParentContext(nullptr ) {}
141
+ ASTNode (MustacheContext &Ctx)
142
+ : Ctx(Ctx), Ty(Type::Root), Parent(nullptr ), ParentContext(nullptr ) {}
146
143
147
- ASTNode (std::string Body, ASTNode *Parent, llvm::StringMap<AstPtr> &Partials,
148
- llvm::StringMap<Lambda> &Lambdas,
149
- llvm::StringMap<SectionLambda> &SectionLambdas, EscapeMap &Escapes)
150
- : Partials(Partials), Lambdas(Lambdas), SectionLambdas(SectionLambdas),
151
- Escapes(Escapes), Ty(Type::Text), Body(std::move(Body)), Parent(Parent),
144
+ ASTNode (MustacheContext &Ctx, std::string Body, ASTNode *Parent)
145
+ : Ctx(Ctx), Ty(Type::Text), Body(std::move(Body)), Parent(Parent),
152
146
ParentContext (nullptr ) {}
153
147
154
148
// Constructor for Section/InvertSection/Variable/UnescapeVariable Nodes
155
- ASTNode (Type Ty, Accessor Accessor, ASTNode *Parent,
156
- llvm::StringMap<AstPtr> &Partials, llvm::StringMap<Lambda> &Lambdas,
157
- llvm::StringMap<SectionLambda> &SectionLambdas, EscapeMap &Escapes)
158
- : Partials(Partials), Lambdas(Lambdas), SectionLambdas(SectionLambdas),
159
- Escapes(Escapes), Ty(Ty), Parent(Parent),
160
- AccessorValue(std::move(Accessor)), ParentContext(nullptr ) {}
149
+ ASTNode (MustacheContext &Ctx, Type Ty, Accessor Accessor, ASTNode *Parent)
150
+ : Ctx(Ctx), Ty(Ty), Parent(Parent), AccessorValue(std::move(Accessor)),
151
+ ParentContext(nullptr ) {}
161
152
162
153
void addChild (AstPtr Child) { Children.emplace_back (std::move (Child)); };
163
154
@@ -189,10 +180,7 @@ class ASTNode {
189
180
void renderSection (const json::Value &CurrentCtx, raw_ostream &OS);
190
181
void renderInvertSection (const json::Value &CurrentCtx, raw_ostream &OS);
191
182
192
- StringMap<AstPtr> &Partials;
193
- StringMap<Lambda> &Lambdas;
194
- StringMap<SectionLambda> &SectionLambdas;
195
- EscapeMap &Escapes;
183
+ MustacheContext &Ctx;
196
184
Type Ty;
197
185
size_t Indentation = 0 ;
198
186
std::string RawBody;
@@ -205,29 +193,18 @@ class ASTNode {
205
193
};
206
194
207
195
// A wrapper for arena allocator for ASTNodes
208
- static AstPtr createRootNode (llvm::StringMap<AstPtr> &Partials,
209
- llvm::StringMap<Lambda> &Lambdas,
210
- llvm::StringMap<SectionLambda> &SectionLambdas,
211
- EscapeMap &Escapes) {
212
- return std::make_unique<ASTNode>(Partials, Lambdas, SectionLambdas, Escapes);
196
+ static AstPtr createRootNode (MustacheContext &Ctx) {
197
+ return std::make_unique<ASTNode>(Ctx);
213
198
}
214
199
215
- static AstPtr createNode (ASTNode::Type T, Accessor A, ASTNode *Parent,
216
- llvm::StringMap<AstPtr> &Partials,
217
- llvm::StringMap<Lambda> &Lambdas,
218
- llvm::StringMap<SectionLambda> &SectionLambdas,
219
- EscapeMap &Escapes) {
220
- return std::make_unique<ASTNode>(T, std::move (A), Parent, Partials, Lambdas,
221
- SectionLambdas, Escapes);
200
+ static AstPtr createNode (MustacheContext &Ctx, ASTNode::Type T, Accessor A,
201
+ ASTNode *Parent) {
202
+ return std::make_unique<ASTNode>(Ctx, T, std::move (A), Parent);
222
203
}
223
204
224
- static AstPtr createTextNode (std::string Body, ASTNode *Parent,
225
- llvm::StringMap<AstPtr> &Partials,
226
- llvm::StringMap<Lambda> &Lambdas,
227
- llvm::StringMap<SectionLambda> &SectionLambdas,
228
- EscapeMap &Escapes) {
229
- return std::make_unique<ASTNode>(std::move (Body), Parent, Partials, Lambdas,
230
- SectionLambdas, Escapes);
205
+ static AstPtr createTextNode (MustacheContext &Ctx, std::string Body,
206
+ ASTNode *Parent) {
207
+ return std::make_unique<ASTNode>(Ctx, std::move (Body), Parent);
231
208
}
232
209
233
210
// Function to check if there is meaningful text behind.
@@ -556,39 +533,26 @@ class AddIndentationStringStream : public raw_ostream {
556
533
557
534
class Parser {
558
535
public:
559
- Parser (StringRef TemplateStr) : TemplateStr(TemplateStr) {}
536
+ Parser (StringRef TemplateStr, MustacheContext &Ctx)
537
+ : Ctx(Ctx), TemplateStr(TemplateStr) {}
560
538
561
- AstPtr parse (llvm::StringMap<AstPtr> &Partials,
562
- llvm::StringMap<Lambda> &Lambdas,
563
- llvm::StringMap<SectionLambda> &SectionLambdas,
564
- EscapeMap &Escapes);
539
+ AstPtr parse ();
565
540
566
541
private:
567
- void parseMustache (ASTNode *Parent, llvm::StringMap<AstPtr> &Partials,
568
- llvm::StringMap<Lambda> &Lambdas,
569
- llvm::StringMap<SectionLambda> &SectionLambdas,
570
- EscapeMap &Escapes);
571
-
572
- void parseSection (ASTNode *Parent, ASTNode::Type Ty, const Accessor &A,
573
- llvm::StringMap<AstPtr> &Partials,
574
- llvm::StringMap<Lambda> &Lambdas,
575
- llvm::StringMap<SectionLambda> &SectionLambdas,
576
- EscapeMap &Escapes);
542
+ void parseMustache (ASTNode *Parent);
543
+ void parseSection (ASTNode *Parent, ASTNode::Type Ty, const Accessor &A);
577
544
545
+ MustacheContext &Ctx;
578
546
SmallVector<Token> Tokens;
579
547
size_t CurrentPtr;
580
548
StringRef TemplateStr;
581
549
};
582
550
583
- void Parser::parseSection (ASTNode *Parent, ASTNode::Type Ty, const Accessor &A,
584
- llvm::StringMap<AstPtr> &Partials,
585
- llvm::StringMap<Lambda> &Lambdas,
586
- llvm::StringMap<SectionLambda> &SectionLambdas,
587
- EscapeMap &Escapes) {
588
- AstPtr CurrentNode =
589
- createNode (Ty, A, Parent, Partials, Lambdas, SectionLambdas, Escapes);
551
+ void Parser::parseSection (ASTNode *Parent, ASTNode::Type Ty,
552
+ const Accessor &A) {
553
+ AstPtr CurrentNode = createNode (Ctx, Ty, A, Parent);
590
554
size_t Start = CurrentPtr;
591
- parseMustache (CurrentNode.get (), Partials, Lambdas, SectionLambdas, Escapes );
555
+ parseMustache (CurrentNode.get ());
592
556
const size_t End = CurrentPtr - 1 ;
593
557
std::string RawBody;
594
558
for (std::size_t I = Start; I < End; I++)
@@ -597,21 +561,15 @@ void Parser::parseSection(ASTNode *Parent, ASTNode::Type Ty, const Accessor &A,
597
561
Parent->addChild (std::move (CurrentNode));
598
562
}
599
563
600
- AstPtr Parser::parse (llvm::StringMap<AstPtr> &Partials,
601
- llvm::StringMap<Lambda> &Lambdas,
602
- llvm::StringMap<SectionLambda> &SectionLambdas,
603
- EscapeMap &Escapes) {
564
+ AstPtr Parser::parse () {
604
565
Tokens = tokenize (TemplateStr);
605
566
CurrentPtr = 0 ;
606
- AstPtr RootNode = createRootNode (Partials, Lambdas, SectionLambdas, Escapes );
607
- parseMustache (RootNode.get (), Partials, Lambdas, SectionLambdas, Escapes );
567
+ AstPtr RootNode = createRootNode (Ctx );
568
+ parseMustache (RootNode.get ());
608
569
return RootNode;
609
570
}
610
571
611
- void Parser::parseMustache (ASTNode *Parent, llvm::StringMap<AstPtr> &Partials,
612
- llvm::StringMap<Lambda> &Lambdas,
613
- llvm::StringMap<SectionLambda> &SectionLambdas,
614
- EscapeMap &Escapes) {
572
+ void Parser::parseMustache (ASTNode *Parent) {
615
573
616
574
while (CurrentPtr < Tokens.size ()) {
617
575
Token CurrentToken = Tokens[CurrentPtr];
@@ -621,38 +579,34 @@ void Parser::parseMustache(ASTNode *Parent, llvm::StringMap<AstPtr> &Partials,
621
579
622
580
switch (CurrentToken.getType ()) {
623
581
case Token::Type::Text: {
624
- CurrentNode = createTextNode ( std::move (CurrentToken. TokenBody ), Parent,
625
- Partials, Lambdas, SectionLambdas, Escapes );
582
+ CurrentNode =
583
+ createTextNode (Ctx, std::move (CurrentToken. TokenBody ), Parent );
626
584
Parent->addChild (std::move (CurrentNode));
627
585
break ;
628
586
}
629
587
case Token::Type::Variable: {
630
- CurrentNode = createNode (ASTNode::Variable, std::move (A), Parent,
631
- Partials, Lambdas, SectionLambdas, Escapes);
588
+ CurrentNode = createNode (Ctx, ASTNode::Variable, std::move (A), Parent);
632
589
Parent->addChild (std::move (CurrentNode));
633
590
break ;
634
591
}
635
592
case Token::Type::UnescapeVariable: {
636
- CurrentNode = createNode (ASTNode::UnescapeVariable, std::move (A), Parent,
637
- Partials, Lambdas, SectionLambdas, Escapes );
593
+ CurrentNode =
594
+ createNode (Ctx, ASTNode::UnescapeVariable, std::move (A), Parent );
638
595
Parent->addChild (std::move (CurrentNode));
639
596
break ;
640
597
}
641
598
case Token::Type::Partial: {
642
- CurrentNode = createNode (ASTNode::Partial, std::move (A), Parent, Partials,
643
- Lambdas, SectionLambdas, Escapes);
599
+ CurrentNode = createNode (Ctx, ASTNode::Partial, std::move (A), Parent);
644
600
CurrentNode->setIndentation (CurrentToken.getIndentation ());
645
601
Parent->addChild (std::move (CurrentNode));
646
602
break ;
647
603
}
648
604
case Token::Type::SectionOpen: {
649
- parseSection (Parent, ASTNode::Section, A, Partials, Lambdas,
650
- SectionLambdas, Escapes);
605
+ parseSection (Parent, ASTNode::Section, A);
651
606
break ;
652
607
}
653
608
case Token::Type::InvertSectionOpen: {
654
- parseSection (Parent, ASTNode::InvertSection, A, Partials, Lambdas,
655
- SectionLambdas, Escapes);
609
+ parseSection (Parent, ASTNode::InvertSection, A);
656
610
break ;
657
611
}
658
612
case Token::Type::Comment:
@@ -702,34 +656,34 @@ void ASTNode::renderRoot(const json::Value &CurrentCtx, raw_ostream &OS) {
702
656
void ASTNode::renderText (raw_ostream &OS) { OS << Body; }
703
657
704
658
void ASTNode::renderPartial (const json::Value &CurrentCtx, raw_ostream &OS) {
705
- auto Partial = Partials.find (AccessorValue[0 ]);
706
- if (Partial != Partials.end ())
659
+ auto Partial = Ctx. Partials .find (AccessorValue[0 ]);
660
+ if (Partial != Ctx. Partials .end ())
707
661
renderPartial (CurrentCtx, OS, Partial->getValue ().get ());
708
662
}
709
663
710
664
void ASTNode::renderVariable (const json::Value &CurrentCtx, raw_ostream &OS) {
711
- auto Lambda = Lambdas.find (AccessorValue[0 ]);
712
- if (Lambda != Lambdas.end ()) {
665
+ auto Lambda = Ctx. Lambdas .find (AccessorValue[0 ]);
666
+ if (Lambda != Ctx. Lambdas .end ()) {
713
667
renderLambdas (CurrentCtx, OS, Lambda->getValue ());
714
668
} else if (const json::Value *ContextPtr = findContext ()) {
715
- EscapeStringStream ES (OS, Escapes);
669
+ EscapeStringStream ES (OS, Ctx. Escapes );
716
670
toMustacheString (*ContextPtr, ES);
717
671
}
718
672
}
719
673
720
674
void ASTNode::renderUnescapeVariable (const json::Value &CurrentCtx,
721
675
raw_ostream &OS) {
722
- auto Lambda = Lambdas.find (AccessorValue[0 ]);
723
- if (Lambda != Lambdas.end ()) {
676
+ auto Lambda = Ctx. Lambdas .find (AccessorValue[0 ]);
677
+ if (Lambda != Ctx. Lambdas .end ()) {
724
678
renderLambdas (CurrentCtx, OS, Lambda->getValue ());
725
679
} else if (const json::Value *ContextPtr = findContext ()) {
726
680
toMustacheString (*ContextPtr, OS);
727
681
}
728
682
}
729
683
730
684
void ASTNode::renderSection (const json::Value &CurrentCtx, raw_ostream &OS) {
731
- auto SectionLambda = SectionLambdas.find (AccessorValue[0 ]);
732
- if (SectionLambda != SectionLambdas.end ()) {
685
+ auto SectionLambda = Ctx. SectionLambdas .find (AccessorValue[0 ]);
686
+ if (SectionLambda != Ctx. SectionLambdas .end ()) {
733
687
renderSectionLambdas (CurrentCtx, OS, SectionLambda->getValue ());
734
688
return ;
735
689
}
@@ -748,7 +702,7 @@ void ASTNode::renderSection(const json::Value &CurrentCtx, raw_ostream &OS) {
748
702
749
703
void ASTNode::renderInvertSection (const json::Value &CurrentCtx,
750
704
raw_ostream &OS) {
751
- bool IsLambda = SectionLambdas.contains (AccessorValue[0 ]);
705
+ bool IsLambda = Ctx. SectionLambdas .contains (AccessorValue[0 ]);
752
706
const json::Value *ContextPtr = findContext ();
753
707
if (isContextFalsey (ContextPtr) && !IsLambda) {
754
708
renderChild (CurrentCtx, OS);
@@ -844,10 +798,10 @@ void ASTNode::renderLambdas(const json::Value &Contexts, llvm::raw_ostream &OS,
844
798
std::string LambdaStr;
845
799
raw_string_ostream Output (LambdaStr);
846
800
toMustacheString (LambdaResult, Output);
847
- Parser P = Parser (LambdaStr);
848
- AstPtr LambdaNode = P.parse (Partials, Lambdas, SectionLambdas, Escapes );
801
+ Parser P (LambdaStr, Ctx );
802
+ AstPtr LambdaNode = P.parse ();
849
803
850
- EscapeStringStream ES (OS, Escapes);
804
+ EscapeStringStream ES (OS, Ctx. Escapes );
851
805
if (Ty == Variable) {
852
806
LambdaNode->render (Contexts, ES);
853
807
return ;
@@ -863,8 +817,8 @@ void ASTNode::renderSectionLambdas(const json::Value &Contexts,
863
817
std::string LambdaStr;
864
818
raw_string_ostream Output (LambdaStr);
865
819
toMustacheString (Return, Output);
866
- Parser P = Parser (LambdaStr);
867
- AstPtr LambdaNode = P.parse (Partials, Lambdas, SectionLambdas, Escapes );
820
+ Parser P (LambdaStr, Ctx );
821
+ AstPtr LambdaNode = P.parse ();
868
822
LambdaNode->render (Contexts, OS);
869
823
}
870
824
@@ -873,22 +827,26 @@ void Template::render(const json::Value &Data, llvm::raw_ostream &OS) {
873
827
}
874
828
875
829
void Template::registerPartial (std::string Name, std::string Partial) {
876
- Parser P = Parser (Partial);
877
- AstPtr PartialTree = P.parse (Partials, Lambdas, SectionLambdas, Escapes );
878
- Partials.insert (std::make_pair (Name, std::move (PartialTree)));
830
+ Parser P (Partial, Ctx );
831
+ AstPtr PartialTree = P.parse ();
832
+ Ctx. Partials .insert (std::make_pair (Name, std::move (PartialTree)));
879
833
}
880
834
881
- void Template::registerLambda (std::string Name, Lambda L) { Lambdas[Name] = L; }
835
+ void Template::registerLambda (std::string Name, Lambda L) {
836
+ Ctx.Lambdas [Name] = L;
837
+ }
882
838
883
839
void Template::registerLambda (std::string Name, SectionLambda L) {
884
- SectionLambdas[Name] = L;
840
+ Ctx. SectionLambdas [Name] = L;
885
841
}
886
842
887
- void Template::overrideEscapeCharacters (EscapeMap E) { Escapes = std::move (E); }
843
+ void Template::overrideEscapeCharacters (EscapeMap E) {
844
+ Ctx.Escapes = std::move (E);
845
+ }
888
846
889
847
Template::Template (StringRef TemplateStr) {
890
- Parser P = Parser (TemplateStr);
891
- Tree = P.parse (Partials, Lambdas, SectionLambdas, Escapes );
848
+ Parser P (TemplateStr, Ctx );
849
+ Tree = P.parse ();
892
850
// The default behavior is to escape html entities.
893
851
const EscapeMap HtmlEntities = {{' &' , " &" },
894
852
{' <' , " <" },
@@ -899,18 +857,13 @@ Template::Template(StringRef TemplateStr) {
899
857
}
900
858
901
859
Template::Template (Template &&Other) noexcept
902
- : Partials(std::move(Other.Partials)), Lambdas(std::move(Other.Lambdas)),
903
- SectionLambdas(std::move(Other.SectionLambdas)),
904
- Escapes(std::move(Other.Escapes)), Tree(std::move(Other.Tree)) {}
860
+ : Ctx(std::move(Other.Ctx)), Tree(std::move(Other.Tree)) {}
905
861
906
862
Template::~Template () = default ;
907
863
908
864
Template &Template::operator =(Template &&Other) noexcept {
909
865
if (this != &Other) {
910
- Partials = std::move (Other.Partials );
911
- Lambdas = std::move (Other.Lambdas );
912
- SectionLambdas = std::move (Other.SectionLambdas );
913
- Escapes = std::move (Other.Escapes );
866
+ Ctx = std::move (Other.Ctx );
914
867
Tree = std::move (Other.Tree );
915
868
Other.Tree = nullptr ;
916
869
}
0 commit comments