Skip to content

Commit 0795e26

Browse files
committed
Resolve #233
1 parent 738be51 commit 0795e26

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

peglib.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,9 +768,11 @@ class Context {
768768

769769
std::vector<bool> cut_stack;
770770

771+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
771772
std::vector<std::map<std::string, std::unordered_set<std::string>>>
772773
symbol_tables_stack;
773774
size_t symbol_tables_stack_size = 0;
775+
#endif
774776

775777
const size_t def_count;
776778
const bool enablePackratParsing;
@@ -802,7 +804,9 @@ class Context {
802804
push_args({});
803805
push_capture_scope();
804806

807+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
805808
push_symbol_tables();
809+
#endif
806810
}
807811

808812
~Context() {
@@ -812,8 +816,10 @@ class Context {
812816
assert(!capture_scope_stack_size);
813817
assert(cut_stack.empty());
814818

819+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
815820
pop_symbol_tables();
816821
assert(!symbol_tables_stack_size);
822+
#endif
817823
}
818824

819825
Context(const Context &) = delete;
@@ -854,14 +860,18 @@ class Context {
854860

855861
SemanticValues &push() {
856862
push_capture_scope();
863+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
857864
push_symbol_tables();
865+
#endif
858866
return push_semantic_values_scope();
859867
}
860868

861869
void pop() {
862870
pop_capture_scope();
863871
pop_semantic_values_scope();
872+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
864873
pop_symbol_tables();
874+
#endif
865875
}
866876

867877
// Semantic values
@@ -924,6 +934,7 @@ class Context {
924934
}
925935
}
926936

937+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
927938
// Symbol tables
928939
void push_symbol_tables() {
929940
assert(symbol_tables_stack_size <= symbol_tables_stack.size());
@@ -968,6 +979,7 @@ class Context {
968979
}
969980
return false;
970981
}
982+
#endif
971983

972984
// Error
973985
void set_error_pos(const char *a_s, const char *literal = nullptr);
@@ -1060,7 +1072,9 @@ class PrioritizedChoice : public Ope {
10601072
vs.choice_count_ = opes_.size();
10611073
vs.choice_ = id;
10621074
c.shift_capture_values();
1075+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
10631076
c.shift_symbol_tables();
1077+
#endif
10641078
break;
10651079
} else if (!c.cut_stack.empty() && c.cut_stack.back()) {
10661080
break;
@@ -1100,7 +1114,9 @@ class Repetition : public Ope {
11001114
if (success(len)) {
11011115
vs.append(chvs);
11021116
c.shift_capture_values();
1117+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
11031118
c.shift_symbol_tables();
1119+
#endif
11041120
} else {
11051121
return len;
11061122
}
@@ -1117,7 +1133,9 @@ class Repetition : public Ope {
11171133
if (success(len)) {
11181134
vs.append(chvs);
11191135
c.shift_capture_values();
1136+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
11201137
c.shift_symbol_tables();
1138+
#endif
11211139
} else {
11221140
break;
11231141
}
@@ -2398,9 +2416,11 @@ class Definition {
23982416

23992417
bool eoi_check = true;
24002418

2419+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
24012420
bool declare_symbol = false;
24022421
bool check_symbol = false;
24032422
std::string symbol_table_name;
2423+
#endif
24042424

24052425
private:
24062426
friend class Reference;
@@ -2775,6 +2795,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
27752795
c.error_info.message = msg;
27762796
}
27772797
len = static_cast<size_t>(-1);
2798+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
27782799
} else if (outer_->declare_symbol) {
27792800
assert(outer_->is_token());
27802801
auto symbol = chvs.token_to_string();
@@ -2791,6 +2812,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
27912812
msg = "'" + symbol + "' doesn't exist.";
27922813
len = static_cast<size_t>(-1);
27932814
}
2815+
#endif
27942816
}
27952817

27962818
if (success(len)) {
@@ -3443,8 +3465,10 @@ class ParserGenerator {
34433465
g["EndBlacket"]);
34443466
g["InstructionItem"] <= cho(g["PrecedenceClimbing"], g["ErrorMessage"],
34453467
g["NoAstOpt"]
3468+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
34463469
,
34473470
g["DeclareSymbol"], g["CheckSymbol"]
3471+
#endif
34483472
);
34493473
~g["InstructionItemSeparator"] <= seq(chr(';'), g["Spacing"]);
34503474

@@ -3478,11 +3502,13 @@ class ParserGenerator {
34783502
// No Ast node optimazation instruction
34793503
g["NoAstOpt"] <= seq(lit("no_ast_opt"), g["SpacesZom"]);
34803504

3505+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
34813506
// Symbol table instruction
34823507
g["DeclareSymbol"] <= seq(lit("declare_symbol"), g["SpacesZom"],
34833508
g["Identifier"], g["SpacesZom"]);
34843509
g["CheckSymbol"] <= seq(lit("check_symbol"), g["SpacesZom"],
34853510
g["Identifier"], g["SpacesZom"]);
3511+
#endif
34863512

34873513
// Set definition names
34883514
for (auto &x : g) {
@@ -3886,6 +3912,7 @@ class ParserGenerator {
38863912
return instruction;
38873913
};
38883914

3915+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
38893916
g["DeclareSymbol"] = [](const SemanticValues &vs) {
38903917
Instruction instruction;
38913918
instruction.type = "declare_symbol";
@@ -3901,6 +3928,7 @@ class ParserGenerator {
39013928
instruction.sv = vs.sv();
39023929
return instruction;
39033930
};
3931+
#endif
39043932

39053933
g["Instruction"] = [](const SemanticValues &vs) {
39063934
return vs.transform<Instruction>();
@@ -4152,12 +4180,14 @@ class ParserGenerator {
41524180
rule.error_message = std::any_cast<std::string>(instruction.data);
41534181
} else if (instruction.type == "no_ast_opt") {
41544182
rule.no_ast_opt = true;
4183+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
41554184
} else if (instruction.type == "declare_symbol") {
41564185
rule.declare_symbol = true;
41574186
rule.symbol_table_name = std::any_cast<std::string>(instruction.data);
41584187
} else if (instruction.type == "check_symbol") {
41594188
rule.check_symbol = true;
41604189
rule.symbol_table_name = std::any_cast<std::string>(instruction.data);
4190+
#endif
41614191
}
41624192
}
41634193
}

test/test2.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,7 @@ TEST(PredicateTest, Semantic_predicate_test) {
861861
EXPECT_FALSE(parser.parse("200", val));
862862
}
863863

864+
#ifdef CPPPEGLIB_SYMBOL_TABLE_SUPPORT
864865
TEST(SymbolTableTest, symbol_instruction_test) {
865866
parser parser(R"(S <- (Decl / Ref)*
866867
Decl <- 'decl' symbol
@@ -1054,6 +1055,7 @@ decl aaa
10541055
EXPECT_FALSE(parser.parse(source, ast));
10551056
}
10561057
}
1058+
#endif
10571059

10581060
TEST(UnicodeTest, Japanese_character) {
10591061
peg::parser parser(u8R"(

0 commit comments

Comments
 (0)