Skip to content

[llvm] add support for mustache templating language #105893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 87 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
ac1c7b5
[clang-doc] add suport for clang-doc enum generation
PeterChou1 Jul 31, 2024
bcc4b0d
[clang-doc] remove useless code
PeterChou1 Jul 31, 2024
5fe47ca
[clang-doc] modify unittest
PeterChou1 Jul 31, 2024
28fb40f
[clang-doc] address pr comments
PeterChou1 Aug 12, 2024
0d150ea
[clang-doc] revert CommentInfo change
PeterChou1 Aug 12, 2024
e5e70b8
[clang-doc] fix test
PeterChou1 Aug 12, 2024
1c4f631
[clang-doc] fix unittest
PeterChou1 Aug 12, 2024
b8f3f9c
[clang-doc] address pr comments
PeterChou1 Aug 12, 2024
bb98aad
[clang-doc] clang-format
PeterChou1 Aug 12, 2024
c60e2b5
[llvm] implement support for mustache template language
PeterChou1 Aug 23, 2024
0a20de8
Merge branch 'main' into llvm-add-mustache
PeterChou1 Aug 23, 2024
8290c38
[llvm][Support] Finish implementation of Mustache
PeterChou1 Sep 6, 2024
07d31b4
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 Sep 6, 2024
6e893c0
[llvm][support] fix mustache test
PeterChou1 Sep 6, 2024
976593e
[llvm][support] add comments
PeterChou1 Sep 6, 2024
6a60eab
[llvm][support] add comments
PeterChou1 Sep 6, 2024
6a1fcc8
[llvm][support] use enumerate for loop
PeterChou1 Sep 6, 2024
f1c27af
Merge branch 'main' into llvm-add-mustache
PeterChou1 Sep 6, 2024
eb1e1a6
[llvm][support] clang-format
PeterChou1 Sep 6, 2024
8ff1100
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 Sep 6, 2024
f4b0520
[llvm][support] fix mustache test
PeterChou1 Sep 6, 2024
7ffaeec
[llvm][support] clang-format
PeterChou1 Sep 6, 2024
746fb97
[llvm][support] use llvm enumerate
PeterChou1 Sep 6, 2024
4944435
Merge branch 'main' into llvm-add-mustache
PeterChou1 Sep 6, 2024
bcc86fe
[llvm][support] fix unittest
PeterChou1 Sep 11, 2024
2ceb0b0
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 Sep 11, 2024
95ad7a6
[llvm][support] clang-format
PeterChou1 Sep 11, 2024
bb3b1ac
[llvm][support] address mustache comments
PeterChou1 Sep 12, 2024
d8aa85c
[llvm][support] clang-format
PeterChou1 Sep 12, 2024
0534a05
[llvm][support] clang-format
PeterChou1 Sep 12, 2024
06da7a5
[llvm][support] clang-format
PeterChou1 Sep 12, 2024
f713198
[llvm] mustache address comments
PeterChou1 Oct 8, 2024
6b4f5cd
[llvm] clang-format
PeterChou1 Oct 8, 2024
d400c29
[llvm] fix errors
PeterChou1 Oct 8, 2024
8fa5fd7
[llvm] fix more bugs
PeterChou1 Oct 8, 2024
fd7c106
[llvm] change mustache to pass by reference
PeterChou1 Oct 8, 2024
29bba68
[llvm] fix failing mustache regression test
PeterChou1 Oct 8, 2024
7eed82f
[llvm] format
PeterChou1 Oct 8, 2024
e73454d
[llvm] remove unused mustache member
PeterChou1 Oct 8, 2024
b58fbcb
[llvm] remove unused mustache member
PeterChou1 Oct 8, 2024
bb4ba40
[llvm] address more comments
PeterChou1 Oct 8, 2024
b0ce196
[llvm] address comments
PeterChou1 Oct 8, 2024
561c7eb
clang-format
PeterChou1 Oct 8, 2024
96f6990
clang-format
PeterChou1 Oct 8, 2024
a247423
[llvm] factor out internal classes
PeterChou1 Oct 10, 2024
a0e7d48
[llvm] clang-format
PeterChou1 Oct 10, 2024
4165fec
[llvm] refactor mustache to raw_ostream
PeterChou1 Oct 12, 2024
5b0a20a
[llvm] refactor to use os_stream
PeterChou1 Oct 12, 2024
ba4a6db
[llvm] clang-format
PeterChou1 Oct 12, 2024
362728f
[llvm] fix indentation bug
PeterChou1 Oct 12, 2024
74d69b3
address feedback
PeterChou1 Nov 15, 2024
5281dc4
clang format
PeterChou1 Nov 15, 2024
569df38
update
PeterChou1 Nov 15, 2024
91547a5
address more feedback
PeterChou1 Nov 15, 2024
a50461b
add static keyword
PeterChou1 Nov 15, 2024
e596a5a
rename mustache string
PeterChou1 Nov 15, 2024
7eb288d
format
PeterChou1 Nov 15, 2024
b0f2f02
address more feedback
PeterChou1 Nov 18, 2024
adb2534
address more feedback
PeterChou1 Nov 18, 2024
0bc60a6
format
PeterChou1 Nov 18, 2024
a97beca
formatting
PeterChou1 Nov 18, 2024
40cbec9
fix grammar
PeterChou1 Nov 19, 2024
e8bd889
format
PeterChou1 Nov 19, 2024
91d4217
formatting
PeterChou1 Nov 19, 2024
1198d3d
address more feedback
PeterChou1 Dec 4, 2024
ba8ee26
format code
PeterChou1 Dec 4, 2024
9895f9d
address feedback
PeterChou1 Dec 4, 2024
03ad2f4
format
PeterChou1 Dec 4, 2024
3649ae6
minor change
PeterChou1 Dec 4, 2024
72343f4
rename variable
PeterChou1 Dec 18, 2024
4729743
rename variable
PeterChou1 Dec 18, 2024
a780958
address more pr comments
PeterChou1 Jan 23, 2025
c9f0cc6
add more tests
PeterChou1 Jan 24, 2025
0c9bc1c
Merge branch 'main' into llvm-add-mustache
PeterChou1 Jan 24, 2025
dc49fd7
use std::move
PeterChou1 Jan 24, 2025
c6a9935
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 Jan 24, 2025
ec452cf
clang-format
PeterChou1 Jan 24, 2025
254bf2e
fix broken test
PeterChou1 Jan 24, 2025
83f95f0
typo
PeterChou1 Jan 24, 2025
d5f380b
address more feedback
PeterChou1 Feb 14, 2025
b5f43b7
clang-format
PeterChou1 Feb 14, 2025
f6d18b7
grammar
PeterChou1 Feb 14, 2025
d6bf982
clang-format
PeterChou1 Feb 14, 2025
1e62179
clang-format
PeterChou1 Feb 14, 2025
2191e67
address more PR feedback
PeterChou1 Feb 24, 2025
ac8d17f
formatting
PeterChou1 Feb 24, 2025
345012b
formatting
PeterChou1 Feb 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 127 additions & 0 deletions llvm/include/llvm/Support/Mustache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//===--- 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
//
// The Template class is a container class that outputs the Mustache template
// string and is the main class for users. It stores all the lambdas and the
// ASTNode Tree. When the Template is instantiated it tokenizes the Template
// String and creates a vector of Tokens. Then it calls a basic recursive
// descent parser to construct the ASTNode Tree. The ASTNodes are all stored
// in an arena allocator which is freed once the template class goes out of
// scope.
//
// Usage:
// \code
// // Creating a simple template and rendering it
// auto Template = Template("Hello, {{name}}!");
// Value Data = {{"name", "World"}};
// std::string Out;
// raw_string_ostream OS(Out);
// T.render(Data, OS);
// // Out == "Hello, World!"
//
// // Creating a template with a partial and rendering it
// auto Template = Template("{{>partial}}");
// Template.registerPartial("partial", "Hello, {{name}}!");
// Value Data = {{"name", "World"}};
// std::string Out;
// raw_string_ostream OS(Out);
// T.render(Data, OS);
// // Out == "Hello, World!"
//
// // Creating a template with a lambda and rendering it
// Value D = Object{};
// auto T = Template("Hello, {{lambda}}!");
// Lambda L = []() -> llvm::json::Value { return "World"; };
// T.registerLambda("lambda", L);
// std::string Out;
// raw_string_ostream OS(Out);
// T.render(D, OS);
// // Out == "Hello, World!"
// \endcode
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_SUPPORT_MUSTACHE
#define LLVM_SUPPORT_MUSTACHE

#include "Error.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/JSON.h"
#include "llvm/Support/StringSaver.h"
#include <functional>
#include <vector>

namespace llvm::mustache {

using Lambda = std::function<llvm::json::Value()>;
using SectionLambda = std::function<llvm::json::Value(std::string)>;

class ASTNode;

// A Template represents the container for the AST and the partials
// and Lambdas that are registered with it.
class Template {
public:
Template(StringRef TemplateStr);

Template(const Template &) = delete;

Template &operator=(const Template &) = delete;

Template(Template &&Other) noexcept;

Template &operator=(Template &&Other) noexcept;

void render(const llvm::json::Value &Data, llvm::raw_ostream &OS);

void registerPartial(std::string Name, std::string Partial);

void registerLambda(std::string Name, Lambda Lambda);

void registerLambda(std::string Name, SectionLambda Lambda);

// By default the Mustache Spec Specifies that HTML special characters
// should be escaped. This function allows the user to specify which
// characters should be escaped.
void overrideEscapeCharacters(DenseMap<char, std::string> Escapes);

private:
StringMap<ASTNode *> Partials;
StringMap<Lambda> Lambdas;
StringMap<SectionLambda> SectionLambdas;
DenseMap<char, std::string> Escapes;
// The allocator for the ASTNode Tree
llvm::BumpPtrAllocator AstAllocator;
// Allocator for each render call resets after each render
llvm::BumpPtrAllocator RenderAllocator;
ASTNode *Tree;
};
} // namespace llvm::mustache

#endif // LLVM_SUPPORT_MUSTACHE
1 change: 1 addition & 0 deletions llvm/lib/Support/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ add_llvm_component_library(LLVMSupport
MD5.cpp
MSP430Attributes.cpp
MSP430AttributeParser.cpp
Mustache.cpp
NativeFormatting.cpp
OptimizedStructLayout.cpp
Optional.cpp
Expand Down
Loading
Loading