@@ -197,7 +197,7 @@ struct Bytecode {
197
197
uint32_t flags: 2 ;
198
198
199
199
json value;
200
- std::string_view str;
200
+ std::string str;
201
201
202
202
Bytecode (): args(0 ), flags(0 ) {}
203
203
explicit Bytecode (Op op, unsigned int args = 0 ): op(op), args(args), flags(0 ) {}
@@ -601,21 +601,28 @@ class Lexer {
601
601
602
602
Token scan_id () {
603
603
for (;;) {
604
- if (m_pos >= m_in.size ()) break ;
604
+ if (m_pos >= m_in.size ()) {
605
+ break ;
606
+ }
605
607
char ch = m_in[m_pos];
606
- if (!std::isalnum (ch) && ch != ' .' && ch != ' /' && ch != ' _' && ch != ' -' ) break ;
608
+ if (!std::isalnum (ch) && ch != ' .' && ch != ' /' && ch != ' _' && ch != ' -' ) {
609
+ break ;
610
+ }
607
611
m_pos += 1 ;
608
612
}
609
613
return make_token (Token::Kind::Id);
610
614
}
611
615
612
616
Token scan_number () {
613
617
for (;;) {
614
- if (m_pos >= m_in.size ()) break ;
618
+ if (m_pos >= m_in.size ()) {
619
+ break ;
620
+ }
615
621
char ch = m_in[m_pos];
616
622
// be very permissive in lexer (we'll catch errors when conversion happens)
617
- if (!std::isdigit (ch) && ch != ' .' && ch != ' e' && ch != ' E' && ch != ' +' && ch != ' -' )
623
+ if (!std::isdigit (ch) && ch != ' .' && ch != ' e' && ch != ' E' && ch != ' +' && ch != ' -' ) {
618
624
break ;
625
+ }
619
626
m_pos += 1 ;
620
627
}
621
628
return make_token (Token::Kind::Number);
@@ -626,12 +633,13 @@ class Lexer {
626
633
for (;;) {
627
634
if (m_pos >= m_in.size ()) break ;
628
635
char ch = m_in[m_pos++];
629
- if (ch == ' \\ ' )
636
+ if (ch == ' \\ ' ) {
630
637
escape = true ;
631
- else if (!escape && ch == m_in[m_tok_start])
638
+ } else if (!escape && ch == m_in[m_tok_start]) {
632
639
break ;
633
- else
640
+ } else {
634
641
escape = false ;
642
+ }
635
643
}
636
644
return make_token (Token::Kind::String);
637
645
}
@@ -658,29 +666,9 @@ class Lexer {
658
666
659
667
namespace inja {
660
668
661
- class Template {
662
- friend class Parser ;
663
- friend class Renderer ;
664
-
669
+ struct Template {
665
670
std::vector<Bytecode> bytecodes;
666
671
std::string content;
667
-
668
- public:
669
- Template () {}
670
- Template (const Template& oth): bytecodes(oth.bytecodes), content(oth.content) {}
671
- Template (Template&& oth): bytecodes(std::move(oth.bytecodes)), content(std::move(oth.content)) {}
672
-
673
- Template& operator =(const Template& oth) {
674
- bytecodes = oth.bytecodes ;
675
- content = oth.content ;
676
- return *this ;
677
- }
678
-
679
- Template& operator =(Template&& oth) {
680
- bytecodes = std::move (oth.bytecodes );
681
- content = std::move (oth.content );
682
- return *this ;
683
- }
684
672
};
685
673
686
674
using TemplateStorage = std::map<std::string, Template>;
@@ -1064,12 +1052,6 @@ class Parser {
1064
1052
}
1065
1053
// sys::path::remove_dots(pathname, true, sys::path::Style::posix);
1066
1054
1067
- // parse it only if it's new
1068
- // TemplateStorage::iterator included;
1069
- // bool is_new {true};
1070
- // std::tie(included, is_new) = m_included_templates.emplace(pathname);
1071
- // if (is_new) included->second = parse_template(pathname);
1072
-
1073
1055
Template include_template = parse_template (pathname);
1074
1056
m_included_templates.emplace (pathname, include_template);
1075
1057
@@ -1322,18 +1304,20 @@ class Renderer {
1322
1304
std::vector<const json*>& get_args (const Bytecode& bc) {
1323
1305
m_tmp_args.clear ();
1324
1306
1325
- bool hasImm = ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop);
1307
+ bool has_imm = ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop);
1326
1308
1327
1309
// get args from stack
1328
1310
unsigned int pop_args = bc.args ;
1329
- if (hasImm) --pop_args;
1311
+ if (has_imm) {
1312
+ pop_args -= 1 ;
1313
+ }
1330
1314
1331
1315
for (auto i = std::prev (m_stack.end (), pop_args); i != m_stack.end (); i++) {
1332
1316
m_tmp_args.push_back (&(*i));
1333
1317
}
1334
1318
1335
1319
// get immediate arg
1336
- if (hasImm ) {
1320
+ if (has_imm ) {
1337
1321
m_tmp_args.push_back (get_imm (bc));
1338
1322
}
1339
1323
@@ -1342,9 +1326,12 @@ class Renderer {
1342
1326
1343
1327
void pop_args (const Bytecode& bc) {
1344
1328
unsigned int popArgs = bc.args ;
1345
- if ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop)
1346
- --popArgs;
1347
- for (unsigned int i = 0 ; i < popArgs; ++i) m_stack.pop_back ();
1329
+ if ((bc.flags & Bytecode::Flag::ValueMask) != Bytecode::Flag::ValuePop) {
1330
+ popArgs -= 1 ;
1331
+ }
1332
+ for (unsigned int i = 0 ; i < popArgs; ++i) {
1333
+ m_stack.pop_back ();
1334
+ }
1348
1335
}
1349
1336
1350
1337
const json* get_imm (const Bytecode& bc) {
@@ -1463,11 +1450,13 @@ class Renderer {
1463
1450
const auto & bc = tmpl.bytecodes [i];
1464
1451
1465
1452
switch (bc.op ) {
1466
- case Bytecode::Op::Nop:
1453
+ case Bytecode::Op::Nop: {
1467
1454
break ;
1468
- case Bytecode::Op::PrintText:
1455
+ }
1456
+ case Bytecode::Op::PrintText: {
1469
1457
os << bc.str ;
1470
1458
break ;
1459
+ }
1471
1460
case Bytecode::Op::PrintValue: {
1472
1461
const json& val = *get_args (bc)[0 ];
1473
1462
if (val.is_string ())
@@ -1478,9 +1467,10 @@ class Renderer {
1478
1467
pop_args (bc);
1479
1468
break ;
1480
1469
}
1481
- case Bytecode::Op::Push:
1470
+ case Bytecode::Op::Push: {
1482
1471
m_stack.emplace_back (*get_imm (bc));
1483
1472
break ;
1473
+ }
1484
1474
case Bytecode::Op::Upper: {
1485
1475
auto result = get_args (bc)[0 ]->get <std::string>();
1486
1476
std::transform (result.begin (), result.end (), result.begin (), ::toupper);
@@ -1737,9 +1727,10 @@ class Renderer {
1737
1727
m_stack.emplace_back (std::move (result));
1738
1728
break ;
1739
1729
}
1740
- case Bytecode::Op::Jump:
1730
+ case Bytecode::Op::Jump: {
1741
1731
i = bc.args - 1 ; // -1 due to ++i in loop
1742
1732
break ;
1733
+ }
1743
1734
case Bytecode::Op::ConditionalJump: {
1744
1735
if (!truthy (m_stack.back ())) {
1745
1736
i = bc.args - 1 ; // -1 due to ++i in loop
0 commit comments