[clang-tools-extra] [llvm] [llvm] add support for mustache templating language (PR #105893)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 7 23:26:04 PDT 2024
================
@@ -0,0 +1,189 @@
+//===--- Mustache.h ---------------------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Implementation of the Mustache templating language supports version 1.4.2
+// currently relies on llvm::json::Value for data input
+// see the Mustache spec for more information
+// (https://mustache.github.io/mustache.5.html).
+//
+// Current Features Supported:
+// - Variables
+// - Sections
+// - Inverted Sections
+// - Partials
+// - Comments
+// - Lambdas
+// - Unescaped Variables
+//
+// Features Not Supported:
+// - Set Delimiter
+// - Blocks
+// - Parents
+// - Dynamic Names
+//
+// Usage:
+// \code
+// // Creating a simple template and rendering it
+// auto Template = Template::createTemplate("Hello, {{name}}!");
+// Value Data = {{"name", "World"}};
+// StringRef Rendered = Template.render(Data);
+// // Rendered == "Hello, World!"
+//
+// // Creating a template with a partial and rendering it
+// auto Template = Template::createTemplate("{{>partial}}");
+// Template.registerPartial("partial", "Hello, {{name}}!");
+// Value Data = {{"name", "World"}};
+// StringRef Rendered = Template.render(Data);
+// // Rendered == "Hello, World!"
+//
+// // Creating a template with a lambda and rendering it
+// auto Template = Template::createTemplate("{{#lambda}}Hello,
+// {{name}}!{{/lambda}}");
+// Template.registerLambda("lambda", []() { return true; });
+// Value Data = {{"name", "World"}};
+// StringRef Rendered = Template.render(Data);
+// // Rendered == "Hello, World!"
+// \endcode
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_MUSTACHE
+#define LLVM_SUPPORT_MUSTACHE
+
+#include "Error.h"
+#include "llvm/ADT/StringMap.h"
+#include "llvm/Support/JSON.h"
+#include <vector>
+
+namespace llvm {
+namespace mustache {
+
+using Accessor = std::vector<SmallString<128>>;
+using Lambda = std::function<llvm::json::Value()>;
+using SectionLambda = std::function<llvm::json::Value(StringRef)>;
+
+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(SmallString<128> NewBody) { TokenBody = NewBody; };
+
+ Accessor getAccessor() const { return Accessor; };
+
+ Type getType() const { return TokenType; };
+
+ static Type getTokenType(char Identifier);
+
+private:
+ Type TokenType;
+ SmallString<128> RawBody;
+ Accessor Accessor;
+ SmallString<128> TokenBody;
+};
+
+class ASTNode {
+public:
+ enum Type {
+ Root,
+ Text,
+ Partial,
+ Variable,
+ UnescapeVariable,
+ Section,
+ InvertSection,
+ };
+
+ ASTNode() : T(Type::Root), LocalContext(nullptr) {};
+
+ ASTNode(StringRef Body, std::shared_ptr<ASTNode> Parent)
+ : T(Type::Text), Body(Body), Parent(Parent), LocalContext(nullptr) {};
+
+ // Constructor for Section/InvertSection/Variable/UnescapeVariable
+ ASTNode(Type T, Accessor Accessor, std::shared_ptr<ASTNode> Parent)
+ : T(T), Accessor(Accessor), Parent(Parent), LocalContext(nullptr),
+ Children({}) {};
+
+ void addChild(std::shared_ptr<ASTNode> Child) {
+ Children.emplace_back(Child);
+ };
+
+ SmallString<128> getBody() const { return Body; };
+
+ void setBody(StringRef NewBody) { Body = NewBody; };
+
+ void setRawBody(StringRef NewBody) { RawBody = NewBody; };
+
+ std::shared_ptr<ASTNode> getLastChild() const {
+ return Children.empty() ? nullptr : Children.back();
+ };
+
+ SmallString<128>
----------------
PeterChou1 wrote:
I've remove arbitrary numbers now, most of the strings are now SmallString<>
https://github.com/llvm/llvm-project/pull/105893
More information about the cfe-commits
mailing list