@@ -180,6 +180,14 @@ class ASTNode {
180180
181181 const llvm::json::Value *findContext ();
182182
183+ void renderRoot (const json::Value &CurrentCtx, raw_ostream &OS);
184+ void renderText (raw_ostream &OS);
185+ void renderPartial (const json::Value &CurrentCtx, raw_ostream &OS);
186+ void renderVariable (const json::Value &CurrentCtx, raw_ostream &OS);
187+ void renderUnescapeVariable (const json::Value &CurrentCtx, raw_ostream &OS);
188+ void renderSection (const json::Value &CurrentCtx, raw_ostream &OS);
189+ void renderInvertSection (const json::Value &CurrentCtx, raw_ostream &OS);
190+
183191 StringMap<AstPtr> &Partials;
184192 StringMap<Lambda> &Lambdas;
185193 StringMap<SectionLambda> &SectionLambdas;
@@ -686,76 +694,96 @@ static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
686694 }
687695}
688696
697+ void ASTNode::renderRoot (const json::Value &CurrentCtx, raw_ostream &OS) {
698+ renderChild (CurrentCtx, OS);
699+ }
700+
701+ void ASTNode::renderText (raw_ostream &OS) { OS << Body; }
702+
703+ void ASTNode::renderPartial (const json::Value &CurrentCtx, raw_ostream &OS) {
704+ auto Partial = Partials.find (AccessorValue[0 ]);
705+ if (Partial != Partials.end ())
706+ renderPartial (CurrentCtx, OS, Partial->getValue ().get ());
707+ }
708+
709+ void ASTNode::renderVariable (const json::Value &CurrentCtx, raw_ostream &OS) {
710+ auto Lambda = Lambdas.find (AccessorValue[0 ]);
711+ if (Lambda != Lambdas.end ()) {
712+ renderLambdas (CurrentCtx, OS, Lambda->getValue ());
713+ } else if (const json::Value *ContextPtr = findContext ()) {
714+ EscapeStringStream ES (OS, Escapes);
715+ toMustacheString (*ContextPtr, ES);
716+ }
717+ }
718+
719+ void ASTNode::renderUnescapeVariable (const json::Value &CurrentCtx,
720+ raw_ostream &OS) {
721+ auto Lambda = Lambdas.find (AccessorValue[0 ]);
722+ if (Lambda != Lambdas.end ()) {
723+ renderLambdas (CurrentCtx, OS, Lambda->getValue ());
724+ } else if (const json::Value *ContextPtr = findContext ()) {
725+ toMustacheString (*ContextPtr, OS);
726+ }
727+ }
728+
729+ void ASTNode::renderSection (const json::Value &CurrentCtx, raw_ostream &OS) {
730+ auto SectionLambda = SectionLambdas.find (AccessorValue[0 ]);
731+ if (SectionLambda != SectionLambdas.end ()) {
732+ renderSectionLambdas (CurrentCtx, OS, SectionLambda->getValue ());
733+ return ;
734+ }
735+
736+ const json::Value *ContextPtr = findContext ();
737+ if (isContextFalsey (ContextPtr))
738+ return ;
739+
740+ if (const json::Array *Arr = ContextPtr->getAsArray ()) {
741+ for (const json::Value &V : *Arr)
742+ renderChild (V, OS);
743+ return ;
744+ }
745+ renderChild (*ContextPtr, OS);
746+ }
747+
748+ void ASTNode::renderInvertSection (const json::Value &CurrentCtx,
749+ raw_ostream &OS) {
750+ bool IsLambda = SectionLambdas.contains (AccessorValue[0 ]);
751+ const json::Value *ContextPtr = findContext ();
752+ if (isContextFalsey (ContextPtr) && !IsLambda) {
753+ renderChild (CurrentCtx, OS);
754+ }
755+ }
756+
689757void ASTNode::render (const json::Value &CurrentCtx, raw_ostream &OS) {
690758 if (Ty != Root && Ty != Text && AccessorValue.empty ())
691759 return ;
692760 // Set the parent context to the incoming context so that we
693761 // can walk up the context tree correctly in findContext().
694762 ParentContext = &CurrentCtx;
695- const json::Value *ContextPtr = Ty == Root ? ParentContext : findContext ();
696763
697764 switch (Ty) {
698765 case Root:
699- renderChild (CurrentCtx, OS);
766+ renderRoot (CurrentCtx, OS);
700767 return ;
701768 case Text:
702- OS << Body ;
769+ renderText (OS) ;
703770 return ;
704- case Partial: {
705- auto Partial = Partials.find (AccessorValue[0 ]);
706- if (Partial != Partials.end ())
707- renderPartial (CurrentCtx, OS, Partial->getValue ().get ());
771+ case Partial:
772+ renderPartial (CurrentCtx, OS);
708773 return ;
709- }
710- case Variable: {
711- auto Lambda = Lambdas.find (AccessorValue[0 ]);
712- if (Lambda != Lambdas.end ()) {
713- renderLambdas (CurrentCtx, OS, Lambda->getValue ());
714- } else if (ContextPtr) {
715- EscapeStringStream ES (OS, Escapes);
716- toMustacheString (*ContextPtr, ES);
717- }
774+ case Variable:
775+ renderVariable (CurrentCtx, OS);
718776 return ;
719- }
720- case UnescapeVariable: {
721- auto Lambda = Lambdas.find (AccessorValue[0 ]);
722- if (Lambda != Lambdas.end ()) {
723- renderLambdas (CurrentCtx, OS, Lambda->getValue ());
724- } else if (ContextPtr) {
725- toMustacheString (*ContextPtr, OS);
726- }
777+ case UnescapeVariable:
778+ renderUnescapeVariable (CurrentCtx, OS);
727779 return ;
728- }
729- case Section: {
730- auto SectionLambda = SectionLambdas.find (AccessorValue[0 ]);
731- bool IsLambda = SectionLambda != SectionLambdas.end ();
732-
733- if (IsLambda) {
734- renderSectionLambdas (CurrentCtx, OS, SectionLambda->getValue ());
735- return ;
736- }
737-
738- if (isContextFalsey (ContextPtr))
739- return ;
740-
741- if (const json::Array *Arr = ContextPtr->getAsArray ()) {
742- for (const json::Value &V : *Arr)
743- renderChild (V, OS);
744- return ;
745- }
746- renderChild (*ContextPtr, OS);
780+ case Section:
781+ renderSection (CurrentCtx, OS);
747782 return ;
748- }
749- case InvertSection: {
750- bool IsLambda = SectionLambdas.contains (AccessorValue[0 ]);
751- if (isContextFalsey (ContextPtr) && !IsLambda) {
752- // The context for the children remains unchanged from the parent's, so
753- // we pass this node's original incoming context.
754- renderChild (CurrentCtx, OS);
755- }
783+ case InvertSection:
784+ renderInvertSection (CurrentCtx, OS);
756785 return ;
757786 }
758- }
759787 llvm_unreachable (" Invalid ASTNode type" );
760788}
761789
0 commit comments