From e4e7fdc75a349c4fe61a7a96193f1601f3987fc7 Mon Sep 17 00:00:00 2001 From: Jacob Bohanon Date: Fri, 16 Jun 2023 09:05:05 -0400 Subject: [PATCH 1/3] allow json pointer syntax --- include/inja/config.hpp | 6 ++ include/inja/environment.hpp | 5 + include/inja/lexer.hpp | 5 +- include/inja/node.hpp | 177 ++++++++++++++++--------------- include/inja/parser.hpp | 3 +- single_include/inja/inja.hpp | 196 +++++++++++++++++++---------------- test/test-renderer.cpp | 9 ++ 7 files changed, 225 insertions(+), 176 deletions(-) diff --git a/include/inja/config.hpp b/include/inja/config.hpp index 0a8f9b7f..49341b9d 100644 --- a/include/inja/config.hpp +++ b/include/inja/config.hpp @@ -8,6 +8,8 @@ namespace inja { +enum class ElementNotation { Dot, Pointer }; + /*! * \brief Class for lexer configuration. */ @@ -28,6 +30,8 @@ struct LexerConfig { std::string comment_close_force_rstrip {"-#}"}; std::string open_chars {"#{"}; + ElementNotation notation {ElementNotation::Dot}; + bool trim_blocks {false}; bool lstrip_blocks {false}; @@ -64,6 +68,8 @@ struct LexerConfig { * \brief Class for parser configuration. */ struct ParserConfig { + ElementNotation notation {ElementNotation::Dot}; + bool search_included_templates_in_files {true}; std::function include_callback; diff --git a/include/inja/environment.hpp b/include/inja/environment.hpp index 9cab39c0..46fbc856 100644 --- a/include/inja/environment.hpp +++ b/include/inja/environment.hpp @@ -82,6 +82,11 @@ class Environment { void set_lstrip_blocks(bool lstrip_blocks) { lexer_config.lstrip_blocks = lstrip_blocks; } + /// Sets the element notation syntax + void set_element_notation(ElementNotation notation) { + parser_config.notation = notation; + lexer_config.notation = notation; + } /// Sets the element notation syntax void set_search_included_templates_in_files(bool search_in_files) { diff --git a/include/inja/lexer.hpp b/include/inja/lexer.hpp index 789c61a1..a8612617 100644 --- a/include/inja/lexer.hpp +++ b/include/inja/lexer.hpp @@ -81,7 +81,7 @@ class Lexer { } pos = tok_start + 1; - if (std::isalpha(ch)) { + if (std::isalpha(ch) || ch == '~') { minus_state = MinusState::Operator; return scan_id(); } @@ -177,12 +177,13 @@ class Lexer { } Token scan_id() { + bool isDotNotation = config.notation == ElementNotation::Dot; for (;;) { if (pos >= m_in.size()) { break; } const char ch = m_in[pos]; - if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-') { + if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-' && (isDotNotation || ch != '~')) { break; } pos += 1; diff --git a/include/inja/node.hpp b/include/inja/node.hpp index 9ad9ef6e..da3e1d03 100644 --- a/include/inja/node.hpp +++ b/include/inja/node.hpp @@ -10,6 +10,11 @@ namespace inja { +enum NotationFlag { + Dot = 0x00, + Pointer = 0x01, +}; + class NodeVisitor; class BlockNode; class TextNode; @@ -32,22 +37,22 @@ class NodeVisitor { public: virtual ~NodeVisitor() = default; - virtual void visit(const BlockNode& node) = 0; - virtual void visit(const TextNode& node) = 0; - virtual void visit(const ExpressionNode& node) = 0; - virtual void visit(const LiteralNode& node) = 0; - virtual void visit(const DataNode& node) = 0; - virtual void visit(const FunctionNode& node) = 0; - virtual void visit(const ExpressionListNode& node) = 0; - virtual void visit(const StatementNode& node) = 0; - virtual void visit(const ForStatementNode& node) = 0; - virtual void visit(const ForArrayStatementNode& node) = 0; - virtual void visit(const ForObjectStatementNode& node) = 0; - virtual void visit(const IfStatementNode& node) = 0; - virtual void visit(const IncludeStatementNode& node) = 0; - virtual void visit(const ExtendsStatementNode& node) = 0; - virtual void visit(const BlockStatementNode& node) = 0; - virtual void visit(const SetStatementNode& node) = 0; + virtual void visit(const BlockNode &node) = 0; + virtual void visit(const TextNode &node) = 0; + virtual void visit(const ExpressionNode &node) = 0; + virtual void visit(const LiteralNode &node) = 0; + virtual void visit(const DataNode &node) = 0; + virtual void visit(const FunctionNode &node) = 0; + virtual void visit(const ExpressionListNode &node) = 0; + virtual void visit(const StatementNode &node) = 0; + virtual void visit(const ForStatementNode &node) = 0; + virtual void visit(const ForArrayStatementNode &node) = 0; + virtual void visit(const ForObjectStatementNode &node) = 0; + virtual void visit(const IfStatementNode &node) = 0; + virtual void visit(const IncludeStatementNode &node) = 0; + virtual void visit(const ExtendsStatementNode &node) = 0; + virtual void visit(const BlockStatementNode &node) = 0; + virtual void visit(const SetStatementNode &node) = 0; }; /*! @@ -55,11 +60,11 @@ class NodeVisitor { */ class AstNode { public: - virtual void accept(NodeVisitor& v) const = 0; + virtual void accept(NodeVisitor &v) const = 0; size_t pos; - AstNode(size_t pos): pos(pos) {} + AstNode(size_t pos) : pos(pos) {} virtual ~AstNode() {} }; @@ -67,42 +72,35 @@ class BlockNode : public AstNode { public: std::vector> nodes; - explicit BlockNode(): AstNode(0) {} + explicit BlockNode() : AstNode(0) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class TextNode : public AstNode { public: const size_t length; - explicit TextNode(size_t pos, size_t length): AstNode(pos), length(length) {} + explicit TextNode(size_t pos, size_t length) : AstNode(pos), length(length) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ExpressionNode : public AstNode { public: - explicit ExpressionNode(size_t pos): AstNode(pos) {} + explicit ExpressionNode(size_t pos) : AstNode(pos) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class LiteralNode : public ExpressionNode { public: const json value; - explicit LiteralNode(std::string_view data_text, size_t pos): ExpressionNode(pos), value(json::parse(data_text)) {} + explicit LiteralNode(std::string_view data_text, size_t pos) + : ExpressionNode(pos), value(json::parse(data_text)) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class DataNode : public ExpressionNode { @@ -110,6 +108,15 @@ class DataNode : public ExpressionNode { const std::string name; const json::json_pointer ptr; + static std::string get_ptr(std::string_view ptr_name, NotationFlag notation) { + auto ptr = notation == NotationFlag::Dot ? convert_dot_to_ptr(ptr_name) : ptr_name.data(); + if(ptr.substr(0,1) != "/") { + ptr = "/" + ptr; + } + + return ptr; + } + static std::string convert_dot_to_ptr(std::string_view ptr_name) { std::string result; do { @@ -121,11 +128,15 @@ class DataNode : public ExpressionNode { return result; } - explicit DataNode(std::string_view ptr_name, size_t pos): ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} + explicit DataNode(std::string_view ptr_name, size_t pos) + : ExpressionNode(pos), name(ptr_name), + ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + explicit DataNode(std::string_view ptr_name, size_t pos, NotationFlag notation) + : ExpressionNode(pos), name(ptr_name), + ptr(json::json_pointer(get_ptr(ptr_name, notation))) {} + + void accept(NodeVisitor &v) const { v.visit(*this); } }; class FunctionNode : public ExpressionNode { @@ -148,8 +159,10 @@ class FunctionNode : public ExpressionNode { CallbackFunction callback; explicit FunctionNode(std::string_view name, size_t pos) - : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(0) {} - explicit FunctionNode(Op operation, size_t pos): ExpressionNode(pos), operation(operation), number_args(1) { + : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), + operation(Op::Callback), name(name), number_args(0) {} + explicit FunctionNode(Op operation, size_t pos) + : ExpressionNode(pos), operation(operation), number_args(1) { switch (operation) { case Op::Not: { number_args = 1; @@ -243,50 +256,47 @@ class FunctionNode : public ExpressionNode { } } - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ExpressionListNode : public AstNode { public: std::shared_ptr root; - explicit ExpressionListNode(): AstNode(0) {} - explicit ExpressionListNode(size_t pos): AstNode(pos) {} + explicit ExpressionListNode() : AstNode(0) {} + explicit ExpressionListNode(size_t pos) : AstNode(pos) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class StatementNode : public AstNode { public: - StatementNode(size_t pos): AstNode(pos) {} + StatementNode(size_t pos) : AstNode(pos) {} - virtual void accept(NodeVisitor& v) const = 0; + virtual void accept(NodeVisitor &v) const = 0; }; class ForStatementNode : public StatementNode { public: ExpressionListNode condition; BlockNode body; - BlockNode* const parent; + BlockNode *const parent; - ForStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent) {} + ForStatementNode(BlockNode *const parent, size_t pos) + : StatementNode(pos), parent(parent) {} - virtual void accept(NodeVisitor& v) const = 0; + virtual void accept(NodeVisitor &v) const = 0; }; class ForArrayStatementNode : public ForStatementNode { public: const std::string value; - explicit ForArrayStatementNode(const std::string& value, BlockNode* const parent, size_t pos): ForStatementNode(parent, pos), value(value) {} + explicit ForArrayStatementNode(const std::string &value, + BlockNode *const parent, size_t pos) + : ForStatementNode(parent, pos), value(value) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ForObjectStatementNode : public ForStatementNode { @@ -294,12 +304,12 @@ class ForObjectStatementNode : public ForStatementNode { const std::string key; const std::string value; - explicit ForObjectStatementNode(const std::string& key, const std::string& value, BlockNode* const parent, size_t pos) + explicit ForObjectStatementNode(const std::string &key, + const std::string &value, + BlockNode *const parent, size_t pos) : ForStatementNode(parent, pos), key(key), value(value) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class IfStatementNode : public StatementNode { @@ -307,52 +317,50 @@ class IfStatementNode : public StatementNode { ExpressionListNode condition; BlockNode true_statement; BlockNode false_statement; - BlockNode* const parent; + BlockNode *const parent; const bool is_nested; - bool has_false_statement {false}; + bool has_false_statement{false}; - explicit IfStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(false) {} - explicit IfStatementNode(bool is_nested, BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(is_nested) {} + explicit IfStatementNode(BlockNode *const parent, size_t pos) + : StatementNode(pos), parent(parent), is_nested(false) {} + explicit IfStatementNode(bool is_nested, BlockNode *const parent, size_t pos) + : StatementNode(pos), parent(parent), is_nested(is_nested) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class IncludeStatementNode : public StatementNode { public: const std::string file; - explicit IncludeStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} + explicit IncludeStatementNode(const std::string &file, size_t pos) + : StatementNode(pos), file(file) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ExtendsStatementNode : public StatementNode { public: const std::string file; - explicit ExtendsStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} + explicit ExtendsStatementNode(const std::string &file, size_t pos) + : StatementNode(pos), file(file) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class BlockStatementNode : public StatementNode { public: const std::string name; BlockNode block; - BlockNode* const parent; + BlockNode *const parent; - explicit BlockStatementNode(BlockNode* const parent, const std::string& name, size_t pos): StatementNode(pos), name(name), parent(parent) {} + explicit BlockStatementNode(BlockNode *const parent, const std::string &name, + size_t pos) + : StatementNode(pos), name(name), parent(parent) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class SetStatementNode : public StatementNode { @@ -360,11 +368,10 @@ class SetStatementNode : public StatementNode { const std::string key; ExpressionListNode expression; - explicit SetStatementNode(const std::string& key, size_t pos): StatementNode(pos), key(key) {} + explicit SetStatementNode(const std::string &key, size_t pos) + : StatementNode(pos), key(key) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; } // namespace inja diff --git a/include/inja/parser.hpp b/include/inja/parser.hpp index 0cd22336..6f35535b 100644 --- a/include/inja/parser.hpp +++ b/include/inja/parser.hpp @@ -237,7 +237,8 @@ class Parser { // Variables } else { - arguments.emplace_back(std::make_shared(static_cast(tok.text), tok.text.data() - tmpl.content.c_str())); + auto notation = this->config.notation == ElementNotation::Dot ? NotationFlag::Dot : NotationFlag::Pointer; + arguments.emplace_back(std::make_shared(static_cast(tok.text), tok.text.data() - tmpl.content.c_str(), notation)); } // Operators diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index 78b431cb..e38e1512 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -348,6 +348,11 @@ inline void replace_substring(std::string& s, const std::string& f, const std::s namespace inja { +enum NotationFlag { + Dot = 0x00, + Pointer = 0x01, +}; + class NodeVisitor; class BlockNode; class TextNode; @@ -370,22 +375,22 @@ class NodeVisitor { public: virtual ~NodeVisitor() = default; - virtual void visit(const BlockNode& node) = 0; - virtual void visit(const TextNode& node) = 0; - virtual void visit(const ExpressionNode& node) = 0; - virtual void visit(const LiteralNode& node) = 0; - virtual void visit(const DataNode& node) = 0; - virtual void visit(const FunctionNode& node) = 0; - virtual void visit(const ExpressionListNode& node) = 0; - virtual void visit(const StatementNode& node) = 0; - virtual void visit(const ForStatementNode& node) = 0; - virtual void visit(const ForArrayStatementNode& node) = 0; - virtual void visit(const ForObjectStatementNode& node) = 0; - virtual void visit(const IfStatementNode& node) = 0; - virtual void visit(const IncludeStatementNode& node) = 0; - virtual void visit(const ExtendsStatementNode& node) = 0; - virtual void visit(const BlockStatementNode& node) = 0; - virtual void visit(const SetStatementNode& node) = 0; + virtual void visit(const BlockNode &node) = 0; + virtual void visit(const TextNode &node) = 0; + virtual void visit(const ExpressionNode &node) = 0; + virtual void visit(const LiteralNode &node) = 0; + virtual void visit(const DataNode &node) = 0; + virtual void visit(const FunctionNode &node) = 0; + virtual void visit(const ExpressionListNode &node) = 0; + virtual void visit(const StatementNode &node) = 0; + virtual void visit(const ForStatementNode &node) = 0; + virtual void visit(const ForArrayStatementNode &node) = 0; + virtual void visit(const ForObjectStatementNode &node) = 0; + virtual void visit(const IfStatementNode &node) = 0; + virtual void visit(const IncludeStatementNode &node) = 0; + virtual void visit(const ExtendsStatementNode &node) = 0; + virtual void visit(const BlockStatementNode &node) = 0; + virtual void visit(const SetStatementNode &node) = 0; }; /*! @@ -393,11 +398,11 @@ class NodeVisitor { */ class AstNode { public: - virtual void accept(NodeVisitor& v) const = 0; + virtual void accept(NodeVisitor &v) const = 0; size_t pos; - AstNode(size_t pos): pos(pos) {} + AstNode(size_t pos) : pos(pos) {} virtual ~AstNode() {} }; @@ -405,42 +410,35 @@ class BlockNode : public AstNode { public: std::vector> nodes; - explicit BlockNode(): AstNode(0) {} + explicit BlockNode() : AstNode(0) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class TextNode : public AstNode { public: const size_t length; - explicit TextNode(size_t pos, size_t length): AstNode(pos), length(length) {} + explicit TextNode(size_t pos, size_t length) : AstNode(pos), length(length) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ExpressionNode : public AstNode { public: - explicit ExpressionNode(size_t pos): AstNode(pos) {} + explicit ExpressionNode(size_t pos) : AstNode(pos) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class LiteralNode : public ExpressionNode { public: const json value; - explicit LiteralNode(std::string_view data_text, size_t pos): ExpressionNode(pos), value(json::parse(data_text)) {} + explicit LiteralNode(std::string_view data_text, size_t pos) + : ExpressionNode(pos), value(json::parse(data_text)) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class DataNode : public ExpressionNode { @@ -448,6 +446,15 @@ class DataNode : public ExpressionNode { const std::string name; const json::json_pointer ptr; + static std::string get_ptr(std::string_view ptr_name, NotationFlag notation) { + auto ptr = notation == NotationFlag::Dot ? convert_dot_to_ptr(ptr_name) : ptr_name.data(); + if(ptr.substr(0,1) != "/") { + ptr = "/" + ptr; + } + + return ptr; + } + static std::string convert_dot_to_ptr(std::string_view ptr_name) { std::string result; do { @@ -459,11 +466,15 @@ class DataNode : public ExpressionNode { return result; } - explicit DataNode(std::string_view ptr_name, size_t pos): ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} + explicit DataNode(std::string_view ptr_name, size_t pos) + : ExpressionNode(pos), name(ptr_name), + ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + explicit DataNode(std::string_view ptr_name, size_t pos, NotationFlag notation) + : ExpressionNode(pos), name(ptr_name), + ptr(json::json_pointer(get_ptr(ptr_name, notation))) {} + + void accept(NodeVisitor &v) const { v.visit(*this); } }; class FunctionNode : public ExpressionNode { @@ -486,8 +497,10 @@ class FunctionNode : public ExpressionNode { CallbackFunction callback; explicit FunctionNode(std::string_view name, size_t pos) - : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(0) {} - explicit FunctionNode(Op operation, size_t pos): ExpressionNode(pos), operation(operation), number_args(1) { + : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), + operation(Op::Callback), name(name), number_args(0) {} + explicit FunctionNode(Op operation, size_t pos) + : ExpressionNode(pos), operation(operation), number_args(1) { switch (operation) { case Op::Not: { number_args = 1; @@ -581,50 +594,47 @@ class FunctionNode : public ExpressionNode { } } - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ExpressionListNode : public AstNode { public: std::shared_ptr root; - explicit ExpressionListNode(): AstNode(0) {} - explicit ExpressionListNode(size_t pos): AstNode(pos) {} + explicit ExpressionListNode() : AstNode(0) {} + explicit ExpressionListNode(size_t pos) : AstNode(pos) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class StatementNode : public AstNode { public: - StatementNode(size_t pos): AstNode(pos) {} + StatementNode(size_t pos) : AstNode(pos) {} - virtual void accept(NodeVisitor& v) const = 0; + virtual void accept(NodeVisitor &v) const = 0; }; class ForStatementNode : public StatementNode { public: ExpressionListNode condition; BlockNode body; - BlockNode* const parent; + BlockNode *const parent; - ForStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent) {} + ForStatementNode(BlockNode *const parent, size_t pos) + : StatementNode(pos), parent(parent) {} - virtual void accept(NodeVisitor& v) const = 0; + virtual void accept(NodeVisitor &v) const = 0; }; class ForArrayStatementNode : public ForStatementNode { public: const std::string value; - explicit ForArrayStatementNode(const std::string& value, BlockNode* const parent, size_t pos): ForStatementNode(parent, pos), value(value) {} + explicit ForArrayStatementNode(const std::string &value, + BlockNode *const parent, size_t pos) + : ForStatementNode(parent, pos), value(value) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ForObjectStatementNode : public ForStatementNode { @@ -632,12 +642,12 @@ class ForObjectStatementNode : public ForStatementNode { const std::string key; const std::string value; - explicit ForObjectStatementNode(const std::string& key, const std::string& value, BlockNode* const parent, size_t pos) + explicit ForObjectStatementNode(const std::string &key, + const std::string &value, + BlockNode *const parent, size_t pos) : ForStatementNode(parent, pos), key(key), value(value) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class IfStatementNode : public StatementNode { @@ -645,52 +655,50 @@ class IfStatementNode : public StatementNode { ExpressionListNode condition; BlockNode true_statement; BlockNode false_statement; - BlockNode* const parent; + BlockNode *const parent; const bool is_nested; - bool has_false_statement {false}; + bool has_false_statement{false}; - explicit IfStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(false) {} - explicit IfStatementNode(bool is_nested, BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(is_nested) {} + explicit IfStatementNode(BlockNode *const parent, size_t pos) + : StatementNode(pos), parent(parent), is_nested(false) {} + explicit IfStatementNode(bool is_nested, BlockNode *const parent, size_t pos) + : StatementNode(pos), parent(parent), is_nested(is_nested) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class IncludeStatementNode : public StatementNode { public: const std::string file; - explicit IncludeStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} + explicit IncludeStatementNode(const std::string &file, size_t pos) + : StatementNode(pos), file(file) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class ExtendsStatementNode : public StatementNode { public: const std::string file; - explicit ExtendsStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} + explicit ExtendsStatementNode(const std::string &file, size_t pos) + : StatementNode(pos), file(file) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class BlockStatementNode : public StatementNode { public: const std::string name; BlockNode block; - BlockNode* const parent; + BlockNode *const parent; - explicit BlockStatementNode(BlockNode* const parent, const std::string& name, size_t pos): StatementNode(pos), name(name), parent(parent) {} + explicit BlockStatementNode(BlockNode *const parent, const std::string &name, + size_t pos) + : StatementNode(pos), name(name), parent(parent) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; class SetStatementNode : public StatementNode { @@ -698,11 +706,10 @@ class SetStatementNode : public StatementNode { const std::string key; ExpressionListNode expression; - explicit SetStatementNode(const std::string& key, size_t pos): StatementNode(pos), key(key) {} + explicit SetStatementNode(const std::string &key, size_t pos) + : StatementNode(pos), key(key) {} - void accept(NodeVisitor& v) const { - v.visit(*this); - } + void accept(NodeVisitor &v) const { v.visit(*this); } }; } // namespace inja @@ -816,6 +823,8 @@ using TemplateStorage = std::map; namespace inja { +enum class ElementNotation { Dot, Pointer }; + /*! * \brief Class for lexer configuration. */ @@ -836,6 +845,8 @@ struct LexerConfig { std::string comment_close_force_rstrip {"-#}"}; std::string open_chars {"#{"}; + ElementNotation notation {ElementNotation::Dot}; + bool trim_blocks {false}; bool lstrip_blocks {false}; @@ -872,6 +883,8 @@ struct LexerConfig { * \brief Class for parser configuration. */ struct ParserConfig { + ElementNotation notation {ElementNotation::Dot}; + bool search_included_templates_in_files {true}; std::function include_callback; @@ -1066,7 +1079,7 @@ class Lexer { } pos = tok_start + 1; - if (std::isalpha(ch)) { + if (std::isalpha(ch) || ch == '~') { minus_state = MinusState::Operator; return scan_id(); } @@ -1162,12 +1175,13 @@ class Lexer { } Token scan_id() { + bool isDotNotation = config.notation == ElementNotation::Dot; for (;;) { if (pos >= m_in.size()) { break; } const char ch = m_in[pos]; - if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-') { + if (!std::isalnum(ch) && ch != '.' && ch != '/' && ch != '_' && ch != '-' && (isDotNotation || ch != '~')) { break; } pos += 1; @@ -1649,7 +1663,8 @@ class Parser { // Variables } else { - arguments.emplace_back(std::make_shared(static_cast(tok.text), tok.text.data() - tmpl.content.c_str())); + auto notation = this->config.notation == ElementNotation::Dot ? NotationFlag::Dot : NotationFlag::Pointer; + arguments.emplace_back(std::make_shared(static_cast(tok.text), tok.text.data() - tmpl.content.c_str(), notation)); } // Operators @@ -2771,6 +2786,11 @@ class Environment { void set_lstrip_blocks(bool lstrip_blocks) { lexer_config.lstrip_blocks = lstrip_blocks; } + /// Sets the element notation syntax + void set_element_notation(ElementNotation notation) { + parser_config.notation = notation; + lexer_config.notation = notation; + } /// Sets the element notation syntax void set_search_included_templates_in_files(bool search_in_files) { diff --git a/test/test-renderer.cpp b/test/test-renderer.cpp index 9963246a..00f4a16c 100644 --- a/test/test-renderer.cpp +++ b/test/test-renderer.cpp @@ -18,6 +18,9 @@ TEST_CASE("types") { data["relatives"]["brother"] = "Chris"; data["relatives"]["sister"] = "Jenny"; data["vars"] = {2, 3, 4, 0, -1, -2, -3}; + data["json_pointers"]["example.com"] = "online"; + data["json_pointers"]["and/or"] = "slash"; + data["json_pointers"]["and~or"] = "tilde"; SUBCASE("basic") { CHECK(env.render("", data) == ""); @@ -39,6 +42,12 @@ TEST_CASE("types") { CHECK(env.render("{{ @name }}", data) == "@name"); CHECK(env.render("{{ $name }}", data) == "$name"); + env.set_element_notation(inja::ElementNotation::Pointer); + CHECK(env.render("{{ json_pointers/example.com }}", data) == "online"); + CHECK(env.render("{{ json_pointers/and~1or }}", data) == "slash"); + CHECK(env.render("{{ json_pointers/and~0or }}", data) == "tilde"); + env.set_element_notation(inja::ElementNotation::Dot); + CHECK_THROWS_WITH(env.render("{{unknown}}", data), "[inja.exception.render_error] (at 1:3) variable 'unknown' not found"); } From 01afe106417d9047ab0b7b45a0de11eb1ce69be3 Mon Sep 17 00:00:00 2001 From: Jacob Bohanon Date: Fri, 16 Jun 2023 09:30:53 -0400 Subject: [PATCH 2/3] revert unwanted formatting changes --- include/inja/node.hpp | 158 +++++++++++++++++++---------------- single_include/inja/inja.hpp | 158 +++++++++++++++++++---------------- 2 files changed, 170 insertions(+), 146 deletions(-) diff --git a/include/inja/node.hpp b/include/inja/node.hpp index da3e1d03..87abf0d3 100644 --- a/include/inja/node.hpp +++ b/include/inja/node.hpp @@ -37,22 +37,22 @@ class NodeVisitor { public: virtual ~NodeVisitor() = default; - virtual void visit(const BlockNode &node) = 0; - virtual void visit(const TextNode &node) = 0; - virtual void visit(const ExpressionNode &node) = 0; - virtual void visit(const LiteralNode &node) = 0; - virtual void visit(const DataNode &node) = 0; - virtual void visit(const FunctionNode &node) = 0; - virtual void visit(const ExpressionListNode &node) = 0; - virtual void visit(const StatementNode &node) = 0; - virtual void visit(const ForStatementNode &node) = 0; - virtual void visit(const ForArrayStatementNode &node) = 0; - virtual void visit(const ForObjectStatementNode &node) = 0; - virtual void visit(const IfStatementNode &node) = 0; - virtual void visit(const IncludeStatementNode &node) = 0; - virtual void visit(const ExtendsStatementNode &node) = 0; - virtual void visit(const BlockStatementNode &node) = 0; - virtual void visit(const SetStatementNode &node) = 0; + virtual void visit(const BlockNode& node) = 0; + virtual void visit(const TextNode& node) = 0; + virtual void visit(const ExpressionNode& node) = 0; + virtual void visit(const LiteralNode& node) = 0; + virtual void visit(const DataNode& node) = 0; + virtual void visit(const FunctionNode& node) = 0; + virtual void visit(const ExpressionListNode& node) = 0; + virtual void visit(const StatementNode& node) = 0; + virtual void visit(const ForStatementNode& node) = 0; + virtual void visit(const ForArrayStatementNode& node) = 0; + virtual void visit(const ForObjectStatementNode& node) = 0; + virtual void visit(const IfStatementNode& node) = 0; + virtual void visit(const IncludeStatementNode& node) = 0; + virtual void visit(const ExtendsStatementNode& node) = 0; + virtual void visit(const BlockStatementNode& node) = 0; + virtual void visit(const SetStatementNode& node) = 0; }; /*! @@ -64,7 +64,7 @@ class AstNode { size_t pos; - AstNode(size_t pos) : pos(pos) {} + AstNode(size_t pos): pos(pos) {} virtual ~AstNode() {} }; @@ -72,35 +72,42 @@ class BlockNode : public AstNode { public: std::vector> nodes; - explicit BlockNode() : AstNode(0) {} + explicit BlockNode(): AstNode(0) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class TextNode : public AstNode { public: const size_t length; - explicit TextNode(size_t pos, size_t length) : AstNode(pos), length(length) {} + explicit TextNode(size_t pos, size_t length): AstNode(pos), length(length) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ExpressionNode : public AstNode { public: - explicit ExpressionNode(size_t pos) : AstNode(pos) {} + explicit ExpressionNode(size_t pos): AstNode(pos) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class LiteralNode : public ExpressionNode { public: const json value; - explicit LiteralNode(std::string_view data_text, size_t pos) - : ExpressionNode(pos), value(json::parse(data_text)) {} + explicit LiteralNode(std::string_view data_text, size_t pos): ExpressionNode(pos), value(json::parse(data_text)) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class DataNode : public ExpressionNode { @@ -128,15 +135,15 @@ class DataNode : public ExpressionNode { return result; } - explicit DataNode(std::string_view ptr_name, size_t pos) - : ExpressionNode(pos), name(ptr_name), - ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} + explicit DataNode(std::string_view ptr_name, size_t pos): ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} explicit DataNode(std::string_view ptr_name, size_t pos, NotationFlag notation) : ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(get_ptr(ptr_name, notation))) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class FunctionNode : public ExpressionNode { @@ -159,10 +166,8 @@ class FunctionNode : public ExpressionNode { CallbackFunction callback; explicit FunctionNode(std::string_view name, size_t pos) - : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), - operation(Op::Callback), name(name), number_args(0) {} - explicit FunctionNode(Op operation, size_t pos) - : ExpressionNode(pos), operation(operation), number_args(1) { + : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(0) {} + explicit FunctionNode(Op operation, size_t pos): ExpressionNode(pos), operation(operation), number_args(1) { switch (operation) { case Op::Not: { number_args = 1; @@ -256,47 +261,50 @@ class FunctionNode : public ExpressionNode { } } - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ExpressionListNode : public AstNode { public: std::shared_ptr root; - explicit ExpressionListNode() : AstNode(0) {} - explicit ExpressionListNode(size_t pos) : AstNode(pos) {} + explicit ExpressionListNode(): AstNode(0) {} + explicit ExpressionListNode(size_t pos): AstNode(pos) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class StatementNode : public AstNode { public: - StatementNode(size_t pos) : AstNode(pos) {} + StatementNode(size_t pos): AstNode(pos) {} - virtual void accept(NodeVisitor &v) const = 0; + virtual void accept(NodeVisitor& v) const = 0; }; class ForStatementNode : public StatementNode { public: ExpressionListNode condition; BlockNode body; - BlockNode *const parent; + BlockNode* const parent; - ForStatementNode(BlockNode *const parent, size_t pos) - : StatementNode(pos), parent(parent) {} + ForStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent) {} - virtual void accept(NodeVisitor &v) const = 0; + virtual void accept(NodeVisitor& v) const = 0; }; class ForArrayStatementNode : public ForStatementNode { public: const std::string value; - explicit ForArrayStatementNode(const std::string &value, - BlockNode *const parent, size_t pos) - : ForStatementNode(parent, pos), value(value) {} + explicit ForArrayStatementNode(const std::string& value, BlockNode* const parent, size_t pos): ForStatementNode(parent, pos), value(value) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ForObjectStatementNode : public ForStatementNode { @@ -304,12 +312,13 @@ class ForObjectStatementNode : public ForStatementNode { const std::string key; const std::string value; - explicit ForObjectStatementNode(const std::string &key, - const std::string &value, - BlockNode *const parent, size_t pos) + + explicit ForObjectStatementNode(const std::string& key, const std::string& value, BlockNode* const parent, size_t pos) : ForStatementNode(parent, pos), key(key), value(value) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class IfStatementNode : public StatementNode { @@ -317,50 +326,52 @@ class IfStatementNode : public StatementNode { ExpressionListNode condition; BlockNode true_statement; BlockNode false_statement; - BlockNode *const parent; + BlockNode* const parent; const bool is_nested; - bool has_false_statement{false}; + bool has_false_statement {false}; - explicit IfStatementNode(BlockNode *const parent, size_t pos) - : StatementNode(pos), parent(parent), is_nested(false) {} - explicit IfStatementNode(bool is_nested, BlockNode *const parent, size_t pos) - : StatementNode(pos), parent(parent), is_nested(is_nested) {} + explicit IfStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(false) {} + explicit IfStatementNode(bool is_nested, BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(is_nested) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class IncludeStatementNode : public StatementNode { public: const std::string file; - explicit IncludeStatementNode(const std::string &file, size_t pos) - : StatementNode(pos), file(file) {} + explicit IncludeStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ExtendsStatementNode : public StatementNode { public: const std::string file; - explicit ExtendsStatementNode(const std::string &file, size_t pos) - : StatementNode(pos), file(file) {} + explicit ExtendsStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class BlockStatementNode : public StatementNode { public: const std::string name; BlockNode block; - BlockNode *const parent; + BlockNode* const parent; - explicit BlockStatementNode(BlockNode *const parent, const std::string &name, - size_t pos) - : StatementNode(pos), name(name), parent(parent) {} + explicit BlockStatementNode(BlockNode* const parent, const std::string& name, size_t pos): StatementNode(pos), name(name), parent(parent) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class SetStatementNode : public StatementNode { @@ -368,10 +379,11 @@ class SetStatementNode : public StatementNode { const std::string key; ExpressionListNode expression; - explicit SetStatementNode(const std::string &key, size_t pos) - : StatementNode(pos), key(key) {} + explicit SetStatementNode(const std::string& key, size_t pos): StatementNode(pos), key(key) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; } // namespace inja diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index e38e1512..b9b1e2b9 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -375,22 +375,22 @@ class NodeVisitor { public: virtual ~NodeVisitor() = default; - virtual void visit(const BlockNode &node) = 0; - virtual void visit(const TextNode &node) = 0; - virtual void visit(const ExpressionNode &node) = 0; - virtual void visit(const LiteralNode &node) = 0; - virtual void visit(const DataNode &node) = 0; - virtual void visit(const FunctionNode &node) = 0; - virtual void visit(const ExpressionListNode &node) = 0; - virtual void visit(const StatementNode &node) = 0; - virtual void visit(const ForStatementNode &node) = 0; - virtual void visit(const ForArrayStatementNode &node) = 0; - virtual void visit(const ForObjectStatementNode &node) = 0; - virtual void visit(const IfStatementNode &node) = 0; - virtual void visit(const IncludeStatementNode &node) = 0; - virtual void visit(const ExtendsStatementNode &node) = 0; - virtual void visit(const BlockStatementNode &node) = 0; - virtual void visit(const SetStatementNode &node) = 0; + virtual void visit(const BlockNode& node) = 0; + virtual void visit(const TextNode& node) = 0; + virtual void visit(const ExpressionNode& node) = 0; + virtual void visit(const LiteralNode& node) = 0; + virtual void visit(const DataNode& node) = 0; + virtual void visit(const FunctionNode& node) = 0; + virtual void visit(const ExpressionListNode& node) = 0; + virtual void visit(const StatementNode& node) = 0; + virtual void visit(const ForStatementNode& node) = 0; + virtual void visit(const ForArrayStatementNode& node) = 0; + virtual void visit(const ForObjectStatementNode& node) = 0; + virtual void visit(const IfStatementNode& node) = 0; + virtual void visit(const IncludeStatementNode& node) = 0; + virtual void visit(const ExtendsStatementNode& node) = 0; + virtual void visit(const BlockStatementNode& node) = 0; + virtual void visit(const SetStatementNode& node) = 0; }; /*! @@ -402,7 +402,7 @@ class AstNode { size_t pos; - AstNode(size_t pos) : pos(pos) {} + AstNode(size_t pos): pos(pos) {} virtual ~AstNode() {} }; @@ -410,35 +410,42 @@ class BlockNode : public AstNode { public: std::vector> nodes; - explicit BlockNode() : AstNode(0) {} + explicit BlockNode(): AstNode(0) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class TextNode : public AstNode { public: const size_t length; - explicit TextNode(size_t pos, size_t length) : AstNode(pos), length(length) {} + explicit TextNode(size_t pos, size_t length): AstNode(pos), length(length) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ExpressionNode : public AstNode { public: - explicit ExpressionNode(size_t pos) : AstNode(pos) {} + explicit ExpressionNode(size_t pos): AstNode(pos) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class LiteralNode : public ExpressionNode { public: const json value; - explicit LiteralNode(std::string_view data_text, size_t pos) - : ExpressionNode(pos), value(json::parse(data_text)) {} + explicit LiteralNode(std::string_view data_text, size_t pos): ExpressionNode(pos), value(json::parse(data_text)) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class DataNode : public ExpressionNode { @@ -466,15 +473,15 @@ class DataNode : public ExpressionNode { return result; } - explicit DataNode(std::string_view ptr_name, size_t pos) - : ExpressionNode(pos), name(ptr_name), - ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} + explicit DataNode(std::string_view ptr_name, size_t pos): ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(convert_dot_to_ptr(ptr_name))) {} explicit DataNode(std::string_view ptr_name, size_t pos, NotationFlag notation) : ExpressionNode(pos), name(ptr_name), ptr(json::json_pointer(get_ptr(ptr_name, notation))) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class FunctionNode : public ExpressionNode { @@ -497,10 +504,8 @@ class FunctionNode : public ExpressionNode { CallbackFunction callback; explicit FunctionNode(std::string_view name, size_t pos) - : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), - operation(Op::Callback), name(name), number_args(0) {} - explicit FunctionNode(Op operation, size_t pos) - : ExpressionNode(pos), operation(operation), number_args(1) { + : ExpressionNode(pos), precedence(8), associativity(Associativity::Left), operation(Op::Callback), name(name), number_args(0) {} + explicit FunctionNode(Op operation, size_t pos): ExpressionNode(pos), operation(operation), number_args(1) { switch (operation) { case Op::Not: { number_args = 1; @@ -594,47 +599,50 @@ class FunctionNode : public ExpressionNode { } } - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ExpressionListNode : public AstNode { public: std::shared_ptr root; - explicit ExpressionListNode() : AstNode(0) {} - explicit ExpressionListNode(size_t pos) : AstNode(pos) {} + explicit ExpressionListNode(): AstNode(0) {} + explicit ExpressionListNode(size_t pos): AstNode(pos) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class StatementNode : public AstNode { public: - StatementNode(size_t pos) : AstNode(pos) {} + StatementNode(size_t pos): AstNode(pos) {} - virtual void accept(NodeVisitor &v) const = 0; + virtual void accept(NodeVisitor& v) const = 0; }; class ForStatementNode : public StatementNode { public: ExpressionListNode condition; BlockNode body; - BlockNode *const parent; + BlockNode* const parent; - ForStatementNode(BlockNode *const parent, size_t pos) - : StatementNode(pos), parent(parent) {} + ForStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent) {} - virtual void accept(NodeVisitor &v) const = 0; + virtual void accept(NodeVisitor& v) const = 0; }; class ForArrayStatementNode : public ForStatementNode { public: const std::string value; - explicit ForArrayStatementNode(const std::string &value, - BlockNode *const parent, size_t pos) - : ForStatementNode(parent, pos), value(value) {} + explicit ForArrayStatementNode(const std::string& value, BlockNode* const parent, size_t pos): ForStatementNode(parent, pos), value(value) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ForObjectStatementNode : public ForStatementNode { @@ -642,12 +650,13 @@ class ForObjectStatementNode : public ForStatementNode { const std::string key; const std::string value; - explicit ForObjectStatementNode(const std::string &key, - const std::string &value, - BlockNode *const parent, size_t pos) + + explicit ForObjectStatementNode(const std::string& key, const std::string& value, BlockNode* const parent, size_t pos) : ForStatementNode(parent, pos), key(key), value(value) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class IfStatementNode : public StatementNode { @@ -655,50 +664,52 @@ class IfStatementNode : public StatementNode { ExpressionListNode condition; BlockNode true_statement; BlockNode false_statement; - BlockNode *const parent; + BlockNode* const parent; const bool is_nested; - bool has_false_statement{false}; + bool has_false_statement {false}; - explicit IfStatementNode(BlockNode *const parent, size_t pos) - : StatementNode(pos), parent(parent), is_nested(false) {} - explicit IfStatementNode(bool is_nested, BlockNode *const parent, size_t pos) - : StatementNode(pos), parent(parent), is_nested(is_nested) {} + explicit IfStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(false) {} + explicit IfStatementNode(bool is_nested, BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent), is_nested(is_nested) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class IncludeStatementNode : public StatementNode { public: const std::string file; - explicit IncludeStatementNode(const std::string &file, size_t pos) - : StatementNode(pos), file(file) {} + explicit IncludeStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class ExtendsStatementNode : public StatementNode { public: const std::string file; - explicit ExtendsStatementNode(const std::string &file, size_t pos) - : StatementNode(pos), file(file) {} + explicit ExtendsStatementNode(const std::string& file, size_t pos): StatementNode(pos), file(file) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class BlockStatementNode : public StatementNode { public: const std::string name; BlockNode block; - BlockNode *const parent; + BlockNode* const parent; - explicit BlockStatementNode(BlockNode *const parent, const std::string &name, - size_t pos) - : StatementNode(pos), name(name), parent(parent) {} + explicit BlockStatementNode(BlockNode* const parent, const std::string& name, size_t pos): StatementNode(pos), name(name), parent(parent) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; class SetStatementNode : public StatementNode { @@ -706,10 +717,11 @@ class SetStatementNode : public StatementNode { const std::string key; ExpressionListNode expression; - explicit SetStatementNode(const std::string &key, size_t pos) - : StatementNode(pos), key(key) {} + explicit SetStatementNode(const std::string& key, size_t pos): StatementNode(pos), key(key) {} - void accept(NodeVisitor &v) const { v.visit(*this); } + void accept(NodeVisitor& v) const { + v.visit(*this); + } }; } // namespace inja From 5a18986825fc7e5d2916ff345c4369e4b6ea7122 Mon Sep 17 00:00:00 2001 From: Jacob Bohanon Date: Fri, 16 Jun 2023 09:33:53 -0400 Subject: [PATCH 3/3] revert unwanted formatting changes --- include/inja/node.hpp | 3 +-- single_include/inja/inja.hpp | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/inja/node.hpp b/include/inja/node.hpp index 87abf0d3..e783a59d 100644 --- a/include/inja/node.hpp +++ b/include/inja/node.hpp @@ -60,7 +60,7 @@ class NodeVisitor { */ class AstNode { public: - virtual void accept(NodeVisitor &v) const = 0; + virtual void accept(NodeVisitor& v) const = 0; size_t pos; @@ -312,7 +312,6 @@ class ForObjectStatementNode : public ForStatementNode { const std::string key; const std::string value; - explicit ForObjectStatementNode(const std::string& key, const std::string& value, BlockNode* const parent, size_t pos) : ForStatementNode(parent, pos), key(key), value(value) {} diff --git a/single_include/inja/inja.hpp b/single_include/inja/inja.hpp index b9b1e2b9..21db7e5a 100644 --- a/single_include/inja/inja.hpp +++ b/single_include/inja/inja.hpp @@ -398,7 +398,7 @@ class NodeVisitor { */ class AstNode { public: - virtual void accept(NodeVisitor &v) const = 0; + virtual void accept(NodeVisitor& v) const = 0; size_t pos; @@ -650,7 +650,6 @@ class ForObjectStatementNode : public ForStatementNode { const std::string key; const std::string value; - explicit ForObjectStatementNode(const std::string& key, const std::string& value, BlockNode* const parent, size_t pos) : ForStatementNode(parent, pos), key(key), value(value) {}