@@ -348,6 +348,11 @@ inline void replace_substring(std::string& s, const std::string& f, const std::s
348
348
349
349
namespace inja {
350
350
351
+ enum NotationFlag {
352
+ Dot = 0x00 ,
353
+ Pointer = 0x01 ,
354
+ };
355
+
351
356
class NodeVisitor ;
352
357
class BlockNode ;
353
358
class TextNode ;
@@ -448,6 +453,15 @@ class DataNode : public ExpressionNode {
448
453
const std::string name;
449
454
const json::json_pointer ptr;
450
455
456
+ static std::string get_ptr (std::string_view ptr_name, NotationFlag notation) {
457
+ auto ptr = notation == NotationFlag::Dot ? convert_dot_to_ptr (ptr_name) : ptr_name.data ();
458
+ if (ptr.substr (0 ,1 ) != " /" ) {
459
+ ptr = " /" + ptr;
460
+ }
461
+
462
+ return ptr;
463
+ }
464
+
451
465
static std::string convert_dot_to_ptr (std::string_view ptr_name) {
452
466
std::string result;
453
467
do {
@@ -461,6 +475,10 @@ class DataNode : public ExpressionNode {
461
475
462
476
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))) {}
463
477
478
+ explicit DataNode (std::string_view ptr_name, size_t pos, NotationFlag notation)
479
+ : ExpressionNode(pos), name(ptr_name),
480
+ ptr(json::json_pointer(get_ptr(ptr_name, notation))) {}
481
+
464
482
void accept (NodeVisitor& v) const {
465
483
v.visit (*this );
466
484
}
@@ -816,6 +834,8 @@ using TemplateStorage = std::map<std::string, Template>;
816
834
817
835
namespace inja {
818
836
837
+ enum class ElementNotation { Dot, Pointer };
838
+
819
839
/* !
820
840
* \brief Class for lexer configuration.
821
841
*/
@@ -836,6 +856,8 @@ struct LexerConfig {
836
856
std::string comment_close_force_rstrip {" -#}" };
837
857
std::string open_chars {" #{" };
838
858
859
+ ElementNotation notation {ElementNotation::Dot};
860
+
839
861
bool trim_blocks {false };
840
862
bool lstrip_blocks {false };
841
863
@@ -872,6 +894,8 @@ struct LexerConfig {
872
894
* \brief Class for parser configuration.
873
895
*/
874
896
struct ParserConfig {
897
+ ElementNotation notation {ElementNotation::Dot};
898
+
875
899
bool search_included_templates_in_files {true };
876
900
877
901
std::function<Template(const std::string&, const std::string&)> include_callback;
@@ -1066,7 +1090,7 @@ class Lexer {
1066
1090
}
1067
1091
1068
1092
pos = tok_start + 1 ;
1069
- if (std::isalpha (ch)) {
1093
+ if (std::isalpha (ch) || ch == ' ~ ' ) {
1070
1094
minus_state = MinusState::Operator;
1071
1095
return scan_id ();
1072
1096
}
@@ -1162,12 +1186,13 @@ class Lexer {
1162
1186
}
1163
1187
1164
1188
Token scan_id () {
1189
+ bool isDotNotation = config.notation == ElementNotation::Dot;
1165
1190
for (;;) {
1166
1191
if (pos >= m_in.size ()) {
1167
1192
break ;
1168
1193
}
1169
1194
const char ch = m_in[pos];
1170
- if (!std::isalnum (ch) && ch != ' .' && ch != ' /' && ch != ' _' && ch != ' -' ) {
1195
+ if (!std::isalnum (ch) && ch != ' .' && ch != ' /' && ch != ' _' && ch != ' -' && (isDotNotation || ch != ' ~ ' ) ) {
1171
1196
break ;
1172
1197
}
1173
1198
pos += 1 ;
@@ -1649,7 +1674,8 @@ class Parser {
1649
1674
1650
1675
// Variables
1651
1676
} else {
1652
- arguments.emplace_back (std::make_shared<DataNode>(static_cast <std::string>(tok.text ), tok.text .data () - tmpl.content .c_str ()));
1677
+ auto notation = this ->config .notation == ElementNotation::Dot ? NotationFlag::Dot : NotationFlag::Pointer;
1678
+ arguments.emplace_back (std::make_shared<DataNode>(static_cast <std::string>(tok.text ), tok.text .data () - tmpl.content .c_str (), notation));
1653
1679
}
1654
1680
1655
1681
// Operators
@@ -2771,6 +2797,11 @@ class Environment {
2771
2797
void set_lstrip_blocks (bool lstrip_blocks) {
2772
2798
lexer_config.lstrip_blocks = lstrip_blocks;
2773
2799
}
2800
+ // / Sets the element notation syntax
2801
+ void set_element_notation (ElementNotation notation) {
2802
+ parser_config.notation = notation;
2803
+ lexer_config.notation = notation;
2804
+ }
2774
2805
2775
2806
// / Sets the element notation syntax
2776
2807
void set_search_included_templates_in_files (bool search_in_files) {
0 commit comments