================ @@ -0,0 +1,713 @@ +//===-- Mustache.cpp ------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +#include "llvm/Support/Mustache.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/raw_ostream.h" +#include <sstream> + +using namespace llvm; +using namespace llvm::json; + +namespace llvm { +namespace mustache { + +class Token { +public: + enum class Type { + Text, + Variable, + Partial, + SectionOpen, + SectionClose, + InvertSectionOpen, + UnescapeVariable, + Comment, + }; + + Token(StringRef Str); + + Token(StringRef RawBody, StringRef Str, char Identifier); + + StringRef getTokenBody() const { return TokenBody; }; + + StringRef getRawBody() const { return RawBody; }; + + void setTokenBody(StringRef NewBody) { TokenBody = NewBody.str(); }; + + Accessor getAccessor() const { return Accessor; }; + + Type getType() const { return TokenType; }; + + void setIndentation(size_t NewIndentation) { Indentation = NewIndentation; }; + + size_t getIndentation() const { return Indentation; }; + + static Type getTokenType(char Identifier); + +private: + size_t Indentation; + Type TokenType; + // RawBody is the original string that was tokenized + SmallString<0> RawBody; + Accessor Accessor; + // TokenBody is the original string with the identifier removed + SmallString<0> TokenBody; +}; + +class ASTNode { +public: + enum Type { + Root, + Text, + Partial, + Variable, + UnescapeVariable, + Section, + InvertSection, + }; + + ASTNode() : T(Type::Root), ParentContext(nullptr) {}; + + ASTNode(StringRef Body, ASTNode *Parent) + : T(Type::Text), Body(Body), Parent(Parent), ParentContext(nullptr) {}; + + // Constructor for Section/InvertSection/Variable/UnescapeVariable + ASTNode(Type T, Accessor Accessor, ASTNode *Parent) + : T(T), Parent(Parent), Children({}), Accessor(Accessor), + ParentContext(nullptr) {}; + + void addChild(ASTNode *Child) { Children.emplace_back(Child); }; + + void setBody(StringRef NewBody) { Body = NewBody; }; + + void setRawBody(StringRef NewBody) { RawBody = NewBody; }; + + void setIndentation(size_t NewIndentation) { Indentation = NewIndentation; }; + + void render(const llvm::json::Value &Data, llvm::raw_ostream &OS); + + void setUpNode(llvm::BumpPtrAllocator &Alloc, StringMap<ASTNode *> &Partials, + StringMap<Lambda> &Lambdas, + StringMap<SectionLambda> &SectionLambdas, + DenseMap<char, StringRef> &Escapes); + +private: + void renderLambdas(const llvm::json::Value &Contexts, llvm::raw_ostream &OS, + Lambda &L); + + void renderSectionLambdas(const llvm::json::Value &Contexts, + llvm::raw_ostream &OS, SectionLambda &L); + + void renderPartial(const llvm::json::Value &Contexts, llvm::raw_ostream &OS, + ASTNode *Partial); + + void renderChild(const llvm::json::Value &Context, llvm::raw_ostream &OS); + + const llvm::json::Value *findContext(); + + llvm::BumpPtrAllocator *Allocator; + StringMap<ASTNode *> *Partials; + StringMap<Lambda> *Lambdas; + StringMap<SectionLambda> *SectionLambdas; + DenseMap<char, StringRef> *Escapes; + Type T; + size_t Indentation = 0; + SmallString<0> RawBody; + SmallString<0> Body; ---------------- ilovepi wrote:
As other reviewers mentioned I guess these should be `std::string`. It's my fault for misremembering the guidance in the programmers manual was only for `SmallVector`. https://github.com/llvm/llvm-project/pull/105893 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits