-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinja.hpp
2937 lines (2506 loc) · 90 KB
/
inja.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
___ _ Version 3.4.0
|_ _|_ __ (_) __ _ https://github.com/pantor/inja
| || '_ \ | |/ _` | Licensed under the MIT License <http://opensource.org/licenses/MIT>.
| || | | || | (_| |
|___|_| |_|/ |\__,_| Copyright (c) 2018-2023 Lars Berscheid
|__/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef INCLUDE_INJA_INJA_HPP_
#define INCLUDE_INJA_INJA_HPP_
#include "json.hpp"
namespace inja {
#ifndef INJA_DATA_TYPE
using json = nlohmann::json;
#else
using json = INJA_DATA_TYPE;
#endif
} // namespace inja
#if (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) && !defined(INJA_NOEXCEPTION)
#ifndef INJA_THROW
#define INJA_THROW(exception) throw exception
#endif
#else
#include <cstdlib>
#ifndef INJA_THROW
#define INJA_THROW(exception) \
std::abort(); \
std::ignore = exception
#endif
#ifndef INJA_NOEXCEPTION
#define INJA_NOEXCEPTION
#endif
#endif
// #include "environment.hpp"
#ifndef INCLUDE_INJA_ENVIRONMENT_HPP_
#define INCLUDE_INJA_ENVIRONMENT_HPP_
#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>
// #include "config.hpp"
#ifndef INCLUDE_INJA_CONFIG_HPP_
#define INCLUDE_INJA_CONFIG_HPP_
#include <functional>
#include <string>
// #include "template.hpp"
#ifndef INCLUDE_INJA_TEMPLATE_HPP_
#define INCLUDE_INJA_TEMPLATE_HPP_
#include <map>
#include <memory>
#include <string>
#include <vector>
// #include "node.hpp"
#ifndef INCLUDE_INJA_NODE_HPP_
#define INCLUDE_INJA_NODE_HPP_
#include <string>
#include <string_view>
#include <utility>
// #include "function_storage.hpp"
#ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_
#define INCLUDE_INJA_FUNCTION_STORAGE_HPP_
#include <string_view>
#include <vector>
namespace inja {
using Arguments = std::vector<const json*>;
using CallbackFunction = std::function<json(Arguments& args)>;
using VoidCallbackFunction = std::function<void(Arguments& args)>;
/*!
* \brief Class for builtin functions and user-defined callbacks.
*/
class FunctionStorage {
public:
enum class Operation {
Not,
And,
Or,
In,
Equal,
NotEqual,
Greater,
GreaterEqual,
Less,
LessEqual,
Add,
Subtract,
Multiplication,
Division,
Power,
Modulo,
AtId,
At,
Default,
DivisibleBy,
Even,
Exists,
ExistsInObject,
First,
Float,
Int,
IsArray,
IsBoolean,
IsFloat,
IsInteger,
IsNumber,
IsObject,
IsString,
Last,
Length,
Lower,
Max,
Min,
Odd,
Range,
Round,
Sort,
Upper,
Super,
Join,
Callback,
None,
};
struct FunctionData {
explicit FunctionData(const Operation& op, const CallbackFunction& cb = CallbackFunction {}): operation(op), callback(cb) {}
const Operation operation;
const CallbackFunction callback;
};
private:
const int VARIADIC {-1};
std::map<std::pair<std::string, int>, FunctionData> function_storage = {
{std::make_pair("at", 2), FunctionData {Operation::At}},
{std::make_pair("default", 2), FunctionData {Operation::Default}},
{std::make_pair("divisibleBy", 2), FunctionData {Operation::DivisibleBy}},
{std::make_pair("even", 1), FunctionData {Operation::Even}},
{std::make_pair("exists", 1), FunctionData {Operation::Exists}},
{std::make_pair("existsIn", 2), FunctionData {Operation::ExistsInObject}},
{std::make_pair("first", 1), FunctionData {Operation::First}},
{std::make_pair("float", 1), FunctionData {Operation::Float}},
{std::make_pair("int", 1), FunctionData {Operation::Int}},
{std::make_pair("isArray", 1), FunctionData {Operation::IsArray}},
{std::make_pair("isBoolean", 1), FunctionData {Operation::IsBoolean}},
{std::make_pair("isFloat", 1), FunctionData {Operation::IsFloat}},
{std::make_pair("isInteger", 1), FunctionData {Operation::IsInteger}},
{std::make_pair("isNumber", 1), FunctionData {Operation::IsNumber}},
{std::make_pair("isObject", 1), FunctionData {Operation::IsObject}},
{std::make_pair("isString", 1), FunctionData {Operation::IsString}},
{std::make_pair("last", 1), FunctionData {Operation::Last}},
{std::make_pair("length", 1), FunctionData {Operation::Length}},
{std::make_pair("lower", 1), FunctionData {Operation::Lower}},
{std::make_pair("max", 1), FunctionData {Operation::Max}},
{std::make_pair("min", 1), FunctionData {Operation::Min}},
{std::make_pair("odd", 1), FunctionData {Operation::Odd}},
{std::make_pair("range", 1), FunctionData {Operation::Range}},
{std::make_pair("round", 2), FunctionData {Operation::Round}},
{std::make_pair("sort", 1), FunctionData {Operation::Sort}},
{std::make_pair("upper", 1), FunctionData {Operation::Upper}},
{std::make_pair("super", 0), FunctionData {Operation::Super}},
{std::make_pair("super", 1), FunctionData {Operation::Super}},
{std::make_pair("join", 2), FunctionData {Operation::Join}},
};
public:
void add_builtin(std::string_view name, int num_args, Operation op) {
function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData {op});
}
void add_callback(std::string_view name, int num_args, const CallbackFunction& callback) {
function_storage.emplace(std::make_pair(static_cast<std::string>(name), num_args), FunctionData {Operation::Callback, callback});
}
FunctionData find_function(std::string_view name, int num_args) const {
auto it = function_storage.find(std::make_pair(static_cast<std::string>(name), num_args));
if (it != function_storage.end()) {
return it->second;
// Find variadic function
} else if (num_args > 0) {
it = function_storage.find(std::make_pair(static_cast<std::string>(name), VARIADIC));
if (it != function_storage.end()) {
return it->second;
}
}
return FunctionData {Operation::None};
}
};
} // namespace inja
#endif // INCLUDE_INJA_FUNCTION_STORAGE_HPP_
// #include "utils.hpp"
#ifndef INCLUDE_INJA_UTILS_HPP_
#define INCLUDE_INJA_UTILS_HPP_
#include <algorithm>
#include <fstream>
#include <string>
#include <string_view>
#include <utility>
// #include "exceptions.hpp"
#ifndef INCLUDE_INJA_EXCEPTIONS_HPP_
#define INCLUDE_INJA_EXCEPTIONS_HPP_
#include <stdexcept>
#include <string>
namespace inja {
struct SourceLocation {
size_t line;
size_t column;
};
struct InjaError : public std::runtime_error {
const std::string type;
const std::string message;
const SourceLocation location;
explicit InjaError(const std::string& type, const std::string& message)
: std::runtime_error("[inja.exception." + type + "] " + message), type(type), message(message), location({0, 0}) {}
explicit InjaError(const std::string& type, const std::string& message, SourceLocation location)
: std::runtime_error("[inja.exception." + type + "] (at " + std::to_string(location.line) + ":" + std::to_string(location.column) + ") " + message),
type(type), message(message), location(location) {}
};
struct ParserError : public InjaError {
explicit ParserError(const std::string& message, SourceLocation location): InjaError("parser_error", message, location) {}
};
struct RenderError : public InjaError {
explicit RenderError(const std::string& message, SourceLocation location): InjaError("render_error", message, location) {}
};
struct FileError : public InjaError {
explicit FileError(const std::string& message): InjaError("file_error", message) {}
explicit FileError(const std::string& message, SourceLocation location): InjaError("file_error", message, location) {}
};
struct DataError : public InjaError {
explicit DataError(const std::string& message, SourceLocation location): InjaError("data_error", message, location) {}
};
} // namespace inja
#endif // INCLUDE_INJA_EXCEPTIONS_HPP_
namespace inja {
namespace string_view {
inline std::string_view slice(std::string_view view, size_t start, size_t end) {
start = std::min(start, view.size());
end = std::min(std::max(start, end), view.size());
return view.substr(start, end - start);
}
inline std::pair<std::string_view, std::string_view> split(std::string_view view, char Separator) {
size_t idx = view.find(Separator);
if (idx == std::string_view::npos) {
return std::make_pair(view, std::string_view());
}
return std::make_pair(slice(view, 0, idx), slice(view, idx + 1, std::string_view::npos));
}
inline bool starts_with(std::string_view view, std::string_view prefix) {
return (view.size() >= prefix.size() && view.compare(0, prefix.size(), prefix) == 0);
}
} // namespace string_view
inline SourceLocation get_source_location(std::string_view content, size_t pos) {
// Get line and offset position (starts at 1:1)
auto sliced = string_view::slice(content, 0, pos);
std::size_t last_newline = sliced.rfind("\n");
if (last_newline == std::string_view::npos) {
return {1, sliced.length() + 1};
}
// Count newlines
size_t count_lines = 0;
size_t search_start = 0;
while (search_start <= sliced.size()) {
search_start = sliced.find("\n", search_start) + 1;
if (search_start == 0) {
break;
}
count_lines += 1;
}
return {count_lines + 1, sliced.length() - last_newline};
}
inline void replace_substring(std::string& s, const std::string& f, const std::string& t) {
if (f.empty()) {
return;
}
for (auto pos = s.find(f); // find first occurrence of f
pos != std::string::npos; // make sure f was found
s.replace(pos, f.size(), t), // replace with t, and
pos = s.find(f, pos + t.size())) // find next occurrence of f
{}
}
} // namespace inja
#endif // INCLUDE_INJA_UTILS_HPP_
namespace inja {
class NodeVisitor;
class BlockNode;
class TextNode;
class ExpressionNode;
class LiteralNode;
class DataNode;
class FunctionNode;
class ExpressionListNode;
class StatementNode;
class ForStatementNode;
class ForArrayStatementNode;
class ForObjectStatementNode;
class IfStatementNode;
class IncludeStatementNode;
class ExtendsStatementNode;
class BlockStatementNode;
class SetStatementNode;
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;
};
/*!
* \brief Base node class for the abstract syntax tree (AST).
*/
class AstNode {
public:
virtual void accept(NodeVisitor& v) const = 0;
size_t pos;
AstNode(size_t pos): pos(pos) {}
virtual ~AstNode() {}
};
class BlockNode : public AstNode {
public:
std::vector<std::shared_ptr<AstNode>> nodes;
explicit BlockNode(): AstNode(0) {}
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) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
class ExpressionNode : public AstNode {
public:
explicit ExpressionNode(size_t pos): AstNode(pos) {}
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)) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
class DataNode : public ExpressionNode {
public:
const std::string name;
const json::json_pointer ptr;
static std::string convert_dot_to_ptr(std::string_view ptr_name) {
std::string result;
do {
std::string_view part;
std::tie(part, ptr_name) = string_view::split(ptr_name, '.');
result.push_back('/');
result.append(part.begin(), part.end());
} while (!ptr_name.empty());
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))) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
class FunctionNode : public ExpressionNode {
using Op = FunctionStorage::Operation;
public:
enum class Associativity {
Left,
Right,
};
unsigned int precedence;
Associativity associativity;
Op operation;
std::string name;
int number_args; // Can also be negative -> -1 for unknown number
std::vector<std::shared_ptr<ExpressionNode>> arguments;
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) {
switch (operation) {
case Op::Not: {
number_args = 1;
precedence = 4;
associativity = Associativity::Left;
} break;
case Op::And: {
number_args = 2;
precedence = 1;
associativity = Associativity::Left;
} break;
case Op::Or: {
number_args = 2;
precedence = 1;
associativity = Associativity::Left;
} break;
case Op::In: {
number_args = 2;
precedence = 2;
associativity = Associativity::Left;
} break;
case Op::Equal: {
number_args = 2;
precedence = 2;
associativity = Associativity::Left;
} break;
case Op::NotEqual: {
number_args = 2;
precedence = 2;
associativity = Associativity::Left;
} break;
case Op::Greater: {
number_args = 2;
precedence = 2;
associativity = Associativity::Left;
} break;
case Op::GreaterEqual: {
number_args = 2;
precedence = 2;
associativity = Associativity::Left;
} break;
case Op::Less: {
number_args = 2;
precedence = 2;
associativity = Associativity::Left;
} break;
case Op::LessEqual: {
number_args = 2;
precedence = 2;
associativity = Associativity::Left;
} break;
case Op::Add: {
number_args = 2;
precedence = 3;
associativity = Associativity::Left;
} break;
case Op::Subtract: {
number_args = 2;
precedence = 3;
associativity = Associativity::Left;
} break;
case Op::Multiplication: {
number_args = 2;
precedence = 4;
associativity = Associativity::Left;
} break;
case Op::Division: {
number_args = 2;
precedence = 4;
associativity = Associativity::Left;
} break;
case Op::Power: {
number_args = 2;
precedence = 5;
associativity = Associativity::Right;
} break;
case Op::Modulo: {
number_args = 2;
precedence = 4;
associativity = Associativity::Left;
} break;
case Op::AtId: {
number_args = 2;
precedence = 8;
associativity = Associativity::Left;
} break;
default: {
precedence = 1;
associativity = Associativity::Left;
}
}
}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
class ExpressionListNode : public AstNode {
public:
std::shared_ptr<ExpressionNode> root;
explicit ExpressionListNode(): AstNode(0) {}
explicit ExpressionListNode(size_t pos): AstNode(pos) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
class StatementNode : public AstNode {
public:
StatementNode(size_t pos): AstNode(pos) {}
virtual void accept(NodeVisitor& v) const = 0;
};
class ForStatementNode : public StatementNode {
public:
ExpressionListNode condition;
BlockNode body;
BlockNode* const parent;
ForStatementNode(BlockNode* const parent, size_t pos): StatementNode(pos), parent(parent) {}
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) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
class ForObjectStatementNode : public ForStatementNode {
public:
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) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
class IfStatementNode : public StatementNode {
public:
ExpressionListNode condition;
BlockNode true_statement;
BlockNode false_statement;
BlockNode* const parent;
const bool is_nested;
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) {}
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) {}
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) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
};
};
class BlockStatementNode : public StatementNode {
public:
const std::string name;
BlockNode block;
BlockNode* const 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);
};
};
class SetStatementNode : public StatementNode {
public:
const std::string key;
ExpressionListNode expression;
explicit SetStatementNode(const std::string& key, size_t pos): StatementNode(pos), key(key) {}
void accept(NodeVisitor& v) const {
v.visit(*this);
}
};
} // namespace inja
#endif // INCLUDE_INJA_NODE_HPP_
// #include "statistics.hpp"
#ifndef INCLUDE_INJA_STATISTICS_HPP_
#define INCLUDE_INJA_STATISTICS_HPP_
// #include "node.hpp"
namespace inja {
/*!
* \brief A class for counting statistics on a Template.
*/
class StatisticsVisitor : public NodeVisitor {
void visit(const BlockNode& node) {
for (auto& n : node.nodes) {
n->accept(*this);
}
}
void visit(const TextNode&) {}
void visit(const ExpressionNode&) {}
void visit(const LiteralNode&) {}
void visit(const DataNode&) {
variable_counter += 1;
}
void visit(const FunctionNode& node) {
for (auto& n : node.arguments) {
n->accept(*this);
}
}
void visit(const ExpressionListNode& node) {
node.root->accept(*this);
}
void visit(const StatementNode&) {}
void visit(const ForStatementNode&) {}
void visit(const ForArrayStatementNode& node) {
node.condition.accept(*this);
node.body.accept(*this);
}
void visit(const ForObjectStatementNode& node) {
node.condition.accept(*this);
node.body.accept(*this);
}
void visit(const IfStatementNode& node) {
node.condition.accept(*this);
node.true_statement.accept(*this);
node.false_statement.accept(*this);
}
void visit(const IncludeStatementNode&) {}
void visit(const ExtendsStatementNode&) {}
void visit(const BlockStatementNode& node) {
node.block.accept(*this);
}
void visit(const SetStatementNode&) {}
public:
unsigned int variable_counter;
explicit StatisticsVisitor(): variable_counter(0) {}
};
} // namespace inja
#endif // INCLUDE_INJA_STATISTICS_HPP_
namespace inja {
/*!
* \brief The main inja Template.
*/
struct Template {
BlockNode root;
std::string content;
std::map<std::string, std::shared_ptr<BlockStatementNode>> block_storage;
explicit Template() {}
explicit Template(const std::string& content): content(content) {}
/// Return number of variables (total number, not distinct ones) in the template
int count_variables() {
auto statistic_visitor = StatisticsVisitor();
root.accept(statistic_visitor);
return statistic_visitor.variable_counter;
}
};
using TemplateStorage = std::map<std::string, Template>;
} // namespace inja
#endif // INCLUDE_INJA_TEMPLATE_HPP_
namespace inja {
/*!
* \brief Class for lexer configuration.
*/
struct LexerConfig {
std::string statement_open {"{%"};
std::string statement_open_no_lstrip {"{%+"};
std::string statement_open_force_lstrip {"{%-"};
std::string statement_close {"%}"};
std::string statement_close_force_rstrip {"-%}"};
std::string line_statement {"##"};
std::string expression_open {"{{"};
std::string expression_open_force_lstrip {"{{-"};
std::string expression_close {"}}"};
std::string expression_close_force_rstrip {"-}}"};
std::string comment_open {"{#"};
std::string comment_open_force_lstrip {"{#-"};
std::string comment_close {"#}"};
std::string comment_close_force_rstrip {"-#}"};
std::string open_chars {"#{"};
bool trim_blocks {false};
bool lstrip_blocks {false};
void update_open_chars() {
open_chars = "";
if (open_chars.find(line_statement[0]) == std::string::npos) {
open_chars += line_statement[0];
}
if (open_chars.find(statement_open[0]) == std::string::npos) {
open_chars += statement_open[0];
}
if (open_chars.find(statement_open_no_lstrip[0]) == std::string::npos) {
open_chars += statement_open_no_lstrip[0];
}
if (open_chars.find(statement_open_force_lstrip[0]) == std::string::npos) {
open_chars += statement_open_force_lstrip[0];
}
if (open_chars.find(expression_open[0]) == std::string::npos) {
open_chars += expression_open[0];
}
if (open_chars.find(expression_open_force_lstrip[0]) == std::string::npos) {
open_chars += expression_open_force_lstrip[0];
}
if (open_chars.find(comment_open[0]) == std::string::npos) {
open_chars += comment_open[0];
}
if (open_chars.find(comment_open_force_lstrip[0]) == std::string::npos) {
open_chars += comment_open_force_lstrip[0];
}
}
};
/*!
* \brief Class for parser configuration.
*/
struct ParserConfig {
bool search_included_templates_in_files {true};
std::function<Template(const std::string&, const std::string&)> include_callback;
};
/*!
* \brief Class for render configuration.
*/
struct RenderConfig {
bool throw_at_missing_includes {true};
};
} // namespace inja
#endif // INCLUDE_INJA_CONFIG_HPP_
// #include "function_storage.hpp"
// #include "parser.hpp"
#ifndef INCLUDE_INJA_PARSER_HPP_
#define INCLUDE_INJA_PARSER_HPP_
#include <limits>
#include <stack>
#include <string>
#include <utility>
#include <vector>
// #include "config.hpp"
// #include "exceptions.hpp"
// #include "function_storage.hpp"
// #include "lexer.hpp"
#ifndef INCLUDE_INJA_LEXER_HPP_
#define INCLUDE_INJA_LEXER_HPP_
#include <cctype>
#include <locale>
// #include "config.hpp"
// #include "token.hpp"
#ifndef INCLUDE_INJA_TOKEN_HPP_
#define INCLUDE_INJA_TOKEN_HPP_
#include <string>
#include <string_view>
namespace inja {
/*!
* \brief Helper-class for the inja Lexer.
*/
struct Token {
enum class Kind {
Text,
ExpressionOpen, // {{
ExpressionClose, // }}
LineStatementOpen, // ##
LineStatementClose, // \n
StatementOpen, // {%
StatementClose, // %}
CommentOpen, // {#
CommentClose, // #}
Id, // this, this.foo
Number, // 1, 2, -1, 5.2, -5.3
String, // "this"
Plus, // +
Minus, // -
Times, // *
Slash, // /
Percent, // %
Power, // ^
Comma, // ,
Dot, // .
Colon, // :
LeftParen, // (
RightParen, // )
LeftBracket, // [
RightBracket, // ]
LeftBrace, // {
RightBrace, // }
Equal, // ==
NotEqual, // !=
GreaterThan, // >
GreaterEqual, // >=
LessThan, // <
LessEqual, // <=
Unknown,
Eof,
};
Kind kind {Kind::Unknown};
std::string_view text;
explicit constexpr Token() = default;
explicit constexpr Token(Kind kind, std::string_view text): kind(kind), text(text) {}
std::string describe() const {
switch (kind) {
case Kind::Text:
return "<text>";
case Kind::LineStatementClose:
return "<eol>";
case Kind::Eof:
return "<eof>";
default:
return static_cast<std::string>(text);
}
}
};
} // namespace inja
#endif // INCLUDE_INJA_TOKEN_HPP_
// #include "utils.hpp"
namespace inja {
/*!
* \brief Class for lexing an inja Template.
*/