Skip to content

Commit f18412d

Browse files
committed
[llvm][mustache] Refactor template rendering
Move the rendering logic into the ASTNode, and break the logic down into individual methods.
1 parent 8be25f4 commit f18412d

File tree

1 file changed

+80
-52
lines changed

1 file changed

+80
-52
lines changed

llvm/lib/Support/Mustache.cpp

Lines changed: 80 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,14 @@ class ASTNode {
181181

182182
const llvm::json::Value *findContext();
183183

184+
void renderRoot(const json::Value &CurrentCtx, raw_ostream &OS);
185+
void renderText(raw_ostream &OS);
186+
void renderPartial(const json::Value &CurrentCtx, raw_ostream &OS);
187+
void renderVariable(const json::Value &CurrentCtx, raw_ostream &OS);
188+
void renderUnescapeVariable(const json::Value &CurrentCtx, raw_ostream &OS);
189+
void renderSection(const json::Value &CurrentCtx, raw_ostream &OS);
190+
void renderInvertSection(const json::Value &CurrentCtx, raw_ostream &OS);
191+
184192
StringMap<AstPtr> &Partials;
185193
StringMap<Lambda> &Lambdas;
186194
StringMap<SectionLambda> &SectionLambdas;
@@ -675,76 +683,96 @@ static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
675683
}
676684
}
677685

686+
void ASTNode::renderRoot(const json::Value &CurrentCtx, raw_ostream &OS) {
687+
renderChild(CurrentCtx, OS);
688+
}
689+
690+
void ASTNode::renderText(raw_ostream &OS) { OS << Body; }
691+
692+
void ASTNode::renderPartial(const json::Value &CurrentCtx, raw_ostream &OS) {
693+
auto Partial = Partials.find(AccessorValue[0]);
694+
if (Partial != Partials.end())
695+
renderPartial(CurrentCtx, OS, Partial->getValue().get());
696+
}
697+
698+
void ASTNode::renderVariable(const json::Value &CurrentCtx, raw_ostream &OS) {
699+
auto Lambda = Lambdas.find(AccessorValue[0]);
700+
if (Lambda != Lambdas.end()) {
701+
renderLambdas(CurrentCtx, OS, Lambda->getValue());
702+
} else if (const json::Value *ContextPtr = findContext()) {
703+
EscapeStringStream ES(OS, Escapes);
704+
toMustacheString(*ContextPtr, ES);
705+
}
706+
}
707+
708+
void ASTNode::renderUnescapeVariable(const json::Value &CurrentCtx,
709+
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+
toMustacheString(*ContextPtr, OS);
715+
}
716+
}
717+
718+
void ASTNode::renderSection(const json::Value &CurrentCtx, raw_ostream &OS) {
719+
auto SectionLambda = SectionLambdas.find(AccessorValue[0]);
720+
if (SectionLambda != SectionLambdas.end()) {
721+
renderSectionLambdas(CurrentCtx, OS, SectionLambda->getValue());
722+
return;
723+
}
724+
725+
const json::Value *ContextPtr = findContext();
726+
if (isContextFalsey(ContextPtr))
727+
return;
728+
729+
if (const json::Array *Arr = ContextPtr->getAsArray()) {
730+
for (const json::Value &V : *Arr)
731+
renderChild(V, OS);
732+
return;
733+
}
734+
renderChild(*ContextPtr, OS);
735+
}
736+
737+
void ASTNode::renderInvertSection(const json::Value &CurrentCtx,
738+
raw_ostream &OS) {
739+
bool IsLambda = SectionLambdas.contains(AccessorValue[0]);
740+
const json::Value *ContextPtr = findContext();
741+
if (isContextFalsey(ContextPtr) && !IsLambda) {
742+
renderChild(CurrentCtx, OS);
743+
}
744+
}
745+
678746
void ASTNode::render(const json::Value &CurrentCtx, raw_ostream &OS) {
679747
if (Ty != Root && Ty != Text && AccessorValue.empty())
680748
return;
681749
// Set the parent context to the incoming context so that we
682750
// can walk up the context tree correctly in findContext().
683751
ParentContext = &CurrentCtx;
684-
const json::Value *ContextPtr = Ty == Root ? ParentContext : findContext();
685752

686753
switch (Ty) {
687754
case Root:
688-
renderChild(CurrentCtx, OS);
755+
renderRoot(CurrentCtx, OS);
689756
return;
690757
case Text:
691-
OS << Body;
758+
renderText(OS);
692759
return;
693-
case Partial: {
694-
auto Partial = Partials.find(AccessorValue[0]);
695-
if (Partial != Partials.end())
696-
renderPartial(CurrentCtx, OS, Partial->getValue().get());
760+
case Partial:
761+
renderPartial(CurrentCtx, OS);
697762
return;
698-
}
699-
case Variable: {
700-
auto Lambda = Lambdas.find(AccessorValue[0]);
701-
if (Lambda != Lambdas.end()) {
702-
renderLambdas(CurrentCtx, OS, Lambda->getValue());
703-
} else if (ContextPtr) {
704-
EscapeStringStream ES(OS, Escapes);
705-
toMustacheString(*ContextPtr, ES);
706-
}
763+
case Variable:
764+
renderVariable(CurrentCtx, OS);
707765
return;
708-
}
709-
case UnescapeVariable: {
710-
auto Lambda = Lambdas.find(AccessorValue[0]);
711-
if (Lambda != Lambdas.end()) {
712-
renderLambdas(CurrentCtx, OS, Lambda->getValue());
713-
} else if (ContextPtr) {
714-
toMustacheString(*ContextPtr, OS);
715-
}
766+
case UnescapeVariable:
767+
renderUnescapeVariable(CurrentCtx, OS);
716768
return;
717-
}
718-
case Section: {
719-
auto SectionLambda = SectionLambdas.find(AccessorValue[0]);
720-
bool IsLambda = SectionLambda != SectionLambdas.end();
721-
722-
if (IsLambda) {
723-
renderSectionLambdas(CurrentCtx, OS, SectionLambda->getValue());
724-
return;
725-
}
726-
727-
if (isContextFalsey(ContextPtr))
728-
return;
729-
730-
if (const json::Array *Arr = ContextPtr->getAsArray()) {
731-
for (const json::Value &V : *Arr)
732-
renderChild(V, OS);
733-
return;
734-
}
735-
renderChild(*ContextPtr, OS);
769+
case Section:
770+
renderSection(CurrentCtx, OS);
736771
return;
737-
}
738-
case InvertSection: {
739-
bool IsLambda = SectionLambdas.contains(AccessorValue[0]);
740-
if (isContextFalsey(ContextPtr) && !IsLambda) {
741-
// The context for the children remains unchanged from the parent's, so
742-
// we pass this node's original incoming context.
743-
renderChild(CurrentCtx, OS);
744-
}
772+
case InvertSection:
773+
renderInvertSection(CurrentCtx, OS);
745774
return;
746775
}
747-
}
748776
llvm_unreachable("Invalid ASTNode type");
749777
}
750778

0 commit comments

Comments
 (0)