-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
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 bcc4b0d
[clang-doc] remove useless code
PeterChou1 5fe47ca
[clang-doc] modify unittest
PeterChou1 28fb40f
[clang-doc] address pr comments
PeterChou1 0d150ea
[clang-doc] revert CommentInfo change
PeterChou1 e5e70b8
[clang-doc] fix test
PeterChou1 1c4f631
[clang-doc] fix unittest
PeterChou1 b8f3f9c
[clang-doc] address pr comments
PeterChou1 bb98aad
[clang-doc] clang-format
PeterChou1 c60e2b5
[llvm] implement support for mustache template language
PeterChou1 0a20de8
Merge branch 'main' into llvm-add-mustache
PeterChou1 8290c38
[llvm][Support] Finish implementation of Mustache
PeterChou1 07d31b4
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 6e893c0
[llvm][support] fix mustache test
PeterChou1 976593e
[llvm][support] add comments
PeterChou1 6a60eab
[llvm][support] add comments
PeterChou1 6a1fcc8
[llvm][support] use enumerate for loop
PeterChou1 f1c27af
Merge branch 'main' into llvm-add-mustache
PeterChou1 eb1e1a6
[llvm][support] clang-format
PeterChou1 8ff1100
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 f4b0520
[llvm][support] fix mustache test
PeterChou1 7ffaeec
[llvm][support] clang-format
PeterChou1 746fb97
[llvm][support] use llvm enumerate
PeterChou1 4944435
Merge branch 'main' into llvm-add-mustache
PeterChou1 bcc86fe
[llvm][support] fix unittest
PeterChou1 2ceb0b0
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 95ad7a6
[llvm][support] clang-format
PeterChou1 bb3b1ac
[llvm][support] address mustache comments
PeterChou1 d8aa85c
[llvm][support] clang-format
PeterChou1 0534a05
[llvm][support] clang-format
PeterChou1 06da7a5
[llvm][support] clang-format
PeterChou1 f713198
[llvm] mustache address comments
PeterChou1 6b4f5cd
[llvm] clang-format
PeterChou1 d400c29
[llvm] fix errors
PeterChou1 8fa5fd7
[llvm] fix more bugs
PeterChou1 fd7c106
[llvm] change mustache to pass by reference
PeterChou1 29bba68
[llvm] fix failing mustache regression test
PeterChou1 7eed82f
[llvm] format
PeterChou1 e73454d
[llvm] remove unused mustache member
PeterChou1 b58fbcb
[llvm] remove unused mustache member
PeterChou1 bb4ba40
[llvm] address more comments
PeterChou1 b0ce196
[llvm] address comments
PeterChou1 561c7eb
clang-format
PeterChou1 96f6990
clang-format
PeterChou1 a247423
[llvm] factor out internal classes
PeterChou1 a0e7d48
[llvm] clang-format
PeterChou1 4165fec
[llvm] refactor mustache to raw_ostream
PeterChou1 5b0a20a
[llvm] refactor to use os_stream
PeterChou1 ba4a6db
[llvm] clang-format
PeterChou1 362728f
[llvm] fix indentation bug
PeterChou1 74d69b3
address feedback
PeterChou1 5281dc4
clang format
PeterChou1 569df38
update
PeterChou1 91547a5
address more feedback
PeterChou1 a50461b
add static keyword
PeterChou1 e596a5a
rename mustache string
PeterChou1 7eb288d
format
PeterChou1 b0f2f02
address more feedback
PeterChou1 adb2534
address more feedback
PeterChou1 0bc60a6
format
PeterChou1 a97beca
formatting
PeterChou1 40cbec9
fix grammar
PeterChou1 e8bd889
format
PeterChou1 91d4217
formatting
PeterChou1 1198d3d
address more feedback
PeterChou1 ba8ee26
format code
PeterChou1 9895f9d
address feedback
PeterChou1 03ad2f4
format
PeterChou1 3649ae6
minor change
PeterChou1 72343f4
rename variable
PeterChou1 4729743
rename variable
PeterChou1 a780958
address more pr comments
PeterChou1 c9f0cc6
add more tests
PeterChou1 0c9bc1c
Merge branch 'main' into llvm-add-mustache
PeterChou1 dc49fd7
use std::move
PeterChou1 c6a9935
Merge branch 'llvm-add-mustache' of https://github.com/PeterChou1/llv…
PeterChou1 ec452cf
clang-format
PeterChou1 254bf2e
fix broken test
PeterChou1 83f95f0
typo
PeterChou1 d5f380b
address more feedback
PeterChou1 b5f43b7
clang-format
PeterChou1 f6d18b7
grammar
PeterChou1 d6bf982
clang-format
PeterChou1 1e62179
clang-format
PeterChou1 2191e67
address more PR feedback
PeterChou1 ac8d17f
formatting
PeterChou1 345012b
formatting
PeterChou1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
ilovepi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
AaronBallman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Allocator for each render call resets after each render | ||
llvm::BumpPtrAllocator RenderAllocator; | ||
ASTNode *Tree; | ||
}; | ||
} // namespace llvm::mustache | ||
|
||
#endif // LLVM_SUPPORT_MUSTACHE |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.