@@ -187,14 +187,14 @@ class ASTNode {
187187 void render (const llvm::json::Value &Data, MustacheOutputStream &OS);
188188
189189private:
190- void renderLambdas (const llvm::json::Value &Contexts, MustacheOutputStream &OS,
191- Lambda &L);
190+ void renderLambdas (const llvm::json::Value &Contexts,
191+ MustacheOutputStream &OS, Lambda &L);
192192
193193 void renderSectionLambdas (const llvm::json::Value &Contexts,
194194 MustacheOutputStream &OS, SectionLambda &L);
195195
196- void renderPartial (const llvm::json::Value &Contexts, MustacheOutputStream &OS,
197- ASTNode *Partial);
196+ void renderPartial (const llvm::json::Value &Contexts,
197+ MustacheOutputStream &OS, ASTNode *Partial);
198198
199199 void renderChild (const llvm::json::Value &Context, MustacheOutputStream &OS);
200200
@@ -204,9 +204,11 @@ class ASTNode {
204204 void renderText (MustacheOutputStream &OS);
205205 void renderPartial (const json::Value &CurrentCtx, MustacheOutputStream &OS);
206206 void renderVariable (const json::Value &CurrentCtx, MustacheOutputStream &OS);
207- void renderUnescapeVariable (const json::Value &CurrentCtx, MustacheOutputStream &OS);
207+ void renderUnescapeVariable (const json::Value &CurrentCtx,
208+ MustacheOutputStream &OS);
208209 void renderSection (const json::Value &CurrentCtx, MustacheOutputStream &OS);
209- void renderInvertSection (const json::Value &CurrentCtx, MustacheOutputStream &OS);
210+ void renderInvertSection (const json::Value &CurrentCtx,
211+ MustacheOutputStream &OS);
210212
211213 MustacheContext &Ctx;
212214 Type Ty;
@@ -330,6 +332,36 @@ struct Tag {
330332 size_t StartPosition = StringRef::npos;
331333};
332334
335+ static const char *tagKindToString (Tag::Kind K) {
336+ switch (K) {
337+ case Tag::Kind::None:
338+ return " None" ;
339+ case Tag::Kind::Normal:
340+ return " Normal" ;
341+ case Tag::Kind::Triple:
342+ return " Triple" ;
343+ }
344+ llvm_unreachable (" Unknown Tag::Kind" );
345+ }
346+
347+ static const char *jsonKindToString (json::Value::Kind K) {
348+ switch (K) {
349+ case json::Value::Kind::Null:
350+ return " JSON_KIND_NULL" ;
351+ case json::Value::Kind::Boolean:
352+ return " JSON_KIND_BOOLEAN" ;
353+ case json::Value::Kind::Number:
354+ return " JSON_KIND_NUMBER" ;
355+ case json::Value::Kind::String:
356+ return " JSON_KIND_STRING" ;
357+ case json::Value::Kind::Array:
358+ return " JSON_KIND_ARRAY" ;
359+ case json::Value::Kind::Object:
360+ return " JSON_KIND_OBJECT" ;
361+ }
362+ llvm_unreachable (" Unknown json::Value::Kind" );
363+ }
364+
333365static Tag findNextTag (StringRef Template, size_t StartPos, StringRef Open,
334366 StringRef Close) {
335367 const StringLiteral TripleOpen (" {{{" );
@@ -374,19 +406,17 @@ static Tag findNextTag(StringRef Template, size_t StartPos, StringRef Open,
374406
375407static std::optional<std::pair<StringRef, StringRef>>
376408processTag (const Tag &T, SmallVectorImpl<Token> &Tokens) {
377- LLVM_DEBUG (dbgs () << " Found tag: \" " << T.FullMatch << " \" , Content: \" "
378- << T. Content << " \ "\n" );
409+ LLVM_DEBUG (dbgs () << " [Tag] " << T.FullMatch << " , Content: " << T. Content
410+ << " , Kind: " << tagKindToString (T. TagKind ) << " \n " );
379411 if (T.TagKind == Tag::Kind::Triple) {
380412 Tokens.emplace_back (T.FullMatch .str (), " &" + T.Content .str (), ' &' );
381- LLVM_DEBUG (dbgs () << " Created UnescapeVariable token.\n " );
382413 return std::nullopt ;
383414 }
384415 StringRef Interpolated = T.Content ;
385416 std::string RawBody = T.FullMatch .str ();
386417 if (!Interpolated.trim ().starts_with (" =" )) {
387418 char Front = Interpolated.empty () ? ' ' : Interpolated.trim ().front ();
388419 Tokens.emplace_back (RawBody, Interpolated.str (), Front);
389- LLVM_DEBUG (dbgs () << " Created tag token of type '" << Front << " '\n " );
390420 return std::nullopt ;
391421 }
392422 Tokens.emplace_back (RawBody, Interpolated.str (), ' =' );
@@ -396,8 +426,8 @@ processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
396426 DelimSpec = DelimSpec.trim ();
397427
398428 auto [NewOpen, NewClose] = DelimSpec.split (' ' );
399- LLVM_DEBUG (dbgs () << " Found Set Delimiter tag. NewOpen=' " << NewOpen
400- << " ' , NewClose=' " << NewClose << " ' \n " );
429+ LLVM_DEBUG (dbgs () << " [ Set Delimiter] NewOpen: " << NewOpen
430+ << " , NewClose: " << NewClose << " \n " );
401431 return std::make_pair (NewOpen, NewClose);
402432}
403433
@@ -406,30 +436,27 @@ processTag(const Tag &T, SmallVectorImpl<Token> &Tokens) {
406436// but we don't support that here. An unescape variable
407437// is represented only by {{& variable}}.
408438static SmallVector<Token> tokenize (StringRef Template) {
409- LLVM_DEBUG (dbgs () << " Tokenizing template: \" " << Template << " \"\n " );
439+ LLVM_DEBUG (dbgs () << " [Tokenize Template] \" " << Template << " \"\n " );
410440 SmallVector<Token> Tokens;
411441 SmallString<8 > Open (" {{" );
412442 SmallString<8 > Close (" }}" );
413443 size_t Start = 0 ;
414444
415445 while (Start < Template.size ()) {
416- LLVM_DEBUG (dbgs () << " Loop start. Start=" << Start << " , Open='" << Open
446+ LLVM_DEBUG (dbgs () << " [Tokenize Loop] Start=" << Start << " , Open='" << Open
417447 << " ', Close='" << Close << " '\n " );
418448 Tag T = findNextTag (Template, Start, Open, Close);
419449
420450 if (T.TagKind == Tag::Kind::None) {
421451 // No more tags, the rest is text.
422452 Tokens.emplace_back (Template.substr (Start).str ());
423- LLVM_DEBUG (dbgs () << " No more tags. Created final Text token: \" "
424- << Template.substr (Start) << " \"\n " );
425453 break ;
426454 }
427455
428456 // Add the text before the tag.
429457 if (T.StartPosition > Start) {
430458 StringRef Text = Template.substr (Start, T.StartPosition - Start);
431459 Tokens.emplace_back (Text.str ());
432- LLVM_DEBUG (dbgs () << " Created Text token: \" " << Text << " \"\n " );
433460 }
434461
435462 if (auto NewDelims = processTag (T, Tokens)) {
@@ -480,15 +507,13 @@ static SmallVector<Token> tokenize(StringRef Template) {
480507 if ((!HasTextBehind && !HasTextAhead) || (!HasTextBehind && Idx == LastIdx))
481508 stripTokenBefore (Tokens, Idx, CurrentToken, CurrentType);
482509 }
483- LLVM_DEBUG (dbgs () << " Tokenizing finished.\n " );
484510 return Tokens;
485511}
486512
487513// Custom stream to escape strings.
488514class EscapeStringStream : public MustacheOutputStream {
489515public:
490- explicit EscapeStringStream (raw_ostream &WrappedStream,
491- EscapeMap &Escape)
516+ explicit EscapeStringStream (raw_ostream &WrappedStream, EscapeMap &Escape)
492517 : Escape(Escape), WrappedStream(WrappedStream) {
493518 SetUnbuffered ();
494519 }
@@ -532,7 +557,7 @@ class AddIndentationStringStream : public MustacheOutputStream {
532557 Indent.resize (Indentation, ' ' );
533558
534559 for (char C : Data) {
535- LLVM_DEBUG (dbgs () << " IndentationStream: NeedsIndent=" << NeedsIndent
560+ LLVM_DEBUG (dbgs () << " [ IndentationStream] NeedsIndent=" << NeedsIndent
536561 << " , C='" << C << " ', Indentation=" << Indentation
537562 << " \n " );
538563 if (NeedsIndent && C != ' \n ' ) {
@@ -641,7 +666,8 @@ void Parser::parseMustache(ASTNode *Parent) {
641666 }
642667}
643668static void toMustacheString (const json::Value &Data, raw_ostream &OS) {
644- LLVM_DEBUG (dbgs () << " toMustacheString: kind=" << (int )Data.kind () << " \n " );
669+ LLVM_DEBUG (dbgs () << " [To Mustache String] Kind: " << jsonKindToString (Data.kind ())
670+ << " , Data: " << Data << " \n " );
645671 switch (Data.kind ()) {
646672 case json::Value::Null:
647673 return ;
@@ -654,7 +680,6 @@ static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
654680 }
655681 case json::Value::String: {
656682 auto Str = *Data.getAsString ();
657- LLVM_DEBUG (dbgs () << " --> writing string: \" " << Str << " \"\n " );
658683 OS << Str.str ();
659684 return ;
660685 }
@@ -674,21 +699,24 @@ static void toMustacheString(const json::Value &Data, raw_ostream &OS) {
674699 }
675700}
676701
677- void ASTNode::renderRoot (const json::Value &CurrentCtx, MustacheOutputStream &OS) {
702+ void ASTNode::renderRoot (const json::Value &CurrentCtx,
703+ MustacheOutputStream &OS) {
678704 renderChild (CurrentCtx, OS);
679705}
680706
681707void ASTNode::renderText (MustacheOutputStream &OS) { OS << Body; }
682708
683- void ASTNode::renderPartial (const json::Value &CurrentCtx, MustacheOutputStream &OS) {
684- LLVM_DEBUG (dbgs () << " renderPartial: Accessor=" << AccessorValue[0 ]
709+ void ASTNode::renderPartial (const json::Value &CurrentCtx,
710+ MustacheOutputStream &OS) {
711+ LLVM_DEBUG (dbgs () << " [Render Partial] Accessor=" << AccessorValue[0 ]
685712 << " , Indentation=" << Indentation << " \n " );
686713 auto Partial = Ctx.Partials .find (AccessorValue[0 ]);
687714 if (Partial != Ctx.Partials .end ())
688715 renderPartial (CurrentCtx, OS, Partial->getValue ().get ());
689716}
690717
691- void ASTNode::renderVariable (const json::Value &CurrentCtx, MustacheOutputStream &OS) {
718+ void ASTNode::renderVariable (const json::Value &CurrentCtx,
719+ MustacheOutputStream &OS) {
692720 auto Lambda = Ctx.Lambdas .find (AccessorValue[0 ]);
693721 if (Lambda != Ctx.Lambdas .end ()) {
694722 renderLambdas (CurrentCtx, OS, Lambda->getValue ());
@@ -700,21 +728,20 @@ void ASTNode::renderVariable(const json::Value &CurrentCtx, MustacheOutputStream
700728
701729void ASTNode::renderUnescapeVariable (const json::Value &CurrentCtx,
702730 MustacheOutputStream &OS) {
703- LLVM_DEBUG (dbgs () << " renderUnescapeVariable: Accessor=" << AccessorValue[0 ]
731+ LLVM_DEBUG (dbgs () << " [Render UnescapeVariable] Accessor=" << AccessorValue[0 ]
704732 << " \n " );
705733 auto Lambda = Ctx.Lambdas .find (AccessorValue[0 ]);
706734 if (Lambda != Ctx.Lambdas .end ()) {
707735 renderLambdas (CurrentCtx, OS, Lambda->getValue ());
708736 } else if (const json::Value *ContextPtr = findContext ()) {
709- LLVM_DEBUG (dbgs () << " --> Found context value, writing to stream.\n " );
710737 OS.suspendIndentation ();
711738 toMustacheString (*ContextPtr, OS);
712739 OS.resumeIndentation ();
713740 }
714741}
715742
716743void ASTNode::renderSection (const json::Value &CurrentCtx,
717- MustacheOutputStream &OS) {
744+ MustacheOutputStream &OS) {
718745 auto SectionLambda = Ctx.SectionLambdas .find (AccessorValue[0 ]);
719746 if (SectionLambda != Ctx.SectionLambdas .end ()) {
720747 renderSectionLambdas (CurrentCtx, OS, SectionLambda->getValue ());
@@ -776,8 +803,6 @@ void ASTNode::render(const llvm::json::Value &Data, MustacheOutputStream &OS) {
776803}
777804
778805const json::Value *ASTNode::findContext () {
779- LLVM_DEBUG (dbgs () << " findContext: AccessorValue[0]=" << AccessorValue[0 ]
780- << " \n " );
781806 // The mustache spec allows for dot notation to access nested values
782807 // a single dot refers to the current context.
783808 // We attempt to find the JSON context in the current node, if it is not
@@ -792,24 +817,12 @@ const json::Value *ASTNode::findContext() {
792817 StringRef CurrentAccessor = AccessorValue[0 ];
793818 ASTNode *CurrentParent = Parent;
794819
795- LLVM_DEBUG (dbgs () << " findContext: ParentContext: " ;
796- if (ParentContext) ParentContext->print (dbgs ());
797- else dbgs () << " nullptr" ;
798- dbgs () << " \n " );
799-
800820 while (!CurrentContext || !CurrentContext->get (CurrentAccessor)) {
801- LLVM_DEBUG (dbgs () << " findContext: climbing parent\n " );
802821 if (CurrentParent->Ty != Root) {
803822 CurrentContext = CurrentParent->ParentContext ->getAsObject ();
804823 CurrentParent = CurrentParent->Parent ;
805- LLVM_DEBUG (dbgs () << " findContext: new ParentContext: " ;
806- if (CurrentParent->ParentContext )
807- CurrentParent->ParentContext ->print (dbgs ());
808- else dbgs () << " nullptr" ;
809- dbgs () << " \n " );
810824 continue ;
811825 }
812- LLVM_DEBUG (dbgs () << " findContext: reached root, not found\n " );
813826 return nullptr ;
814827 }
815828 const json::Value *Context = nullptr ;
@@ -825,28 +838,24 @@ const json::Value *ASTNode::findContext() {
825838 Context = CurrentValue;
826839 }
827840 }
828- LLVM_DEBUG (dbgs () << " findContext: found value: " ;
829- if (Context) Context->print (dbgs ());
830- else dbgs () << " nullptr" ;
831- dbgs () << " \n " );
832841 return Context;
833842}
834843
835- void ASTNode::renderChild (const json::Value &Contexts, MustacheOutputStream &OS) {
844+ void ASTNode::renderChild (const json::Value &Contexts,
845+ MustacheOutputStream &OS) {
836846 for (AstPtr &Child : Children)
837847 Child->render (Contexts, OS);
838848}
839849
840- void ASTNode::renderPartial (const json::Value &Contexts, MustacheOutputStream &OS,
841- ASTNode *Partial) {
842- LLVM_DEBUG (dbgs () << " renderPartial (helper): Indentation=" << Indentation
843- << " \n " );
850+ void ASTNode::renderPartial (const json::Value &Contexts,
851+ MustacheOutputStream &OS, ASTNode *Partial) {
852+ LLVM_DEBUG (dbgs () << " [Render Partial Indentation] " << Indentation << " \n " );
844853 AddIndentationStringStream IS (OS, Indentation);
845854 Partial->render (Contexts, IS);
846855}
847856
848- void ASTNode::renderLambdas (const json::Value &Contexts, MustacheOutputStream &OS,
849- Lambda &L) {
857+ void ASTNode::renderLambdas (const json::Value &Contexts,
858+ MustacheOutputStream &OS, Lambda &L) {
850859 json::Value LambdaResult = L ();
851860 std::string LambdaStr;
852861 raw_string_ostream Output (LambdaStr);
0 commit comments