1
0
mirror of https://github.com/yhirose/cpp-peglib.git synced 2025-01-08 17:05:30 +00:00
This commit is contained in:
yhirose 2022-09-03 07:09:31 -04:00
parent a8e5aa0e08
commit 0861c0065e
5 changed files with 46 additions and 199 deletions

Binary file not shown.

View File

@ -10,10 +10,11 @@ int main(void) {
parser parser(R"(
# Grammar for Calculator...
Additive <- Multitive '+' Additive / Multitive
Multitive <- Primary '*' Multitive / Primary
Multitive <- Primary '*' Multitive^cond / Primary
Primary <- '(' Additive ')' / Number
Number <- < [0-9]+ >
%whitespace <- [ \t]*
cond <- '' { error_message "missing multitative" }
)");
assert(static_cast<bool>(parser) == true);
@ -44,8 +45,9 @@ int main(void) {
// (4) Parse
parser.enable_packrat_parsing(); // Enable packrat parsing.
int val;
parser.parse(" (1 + 2) * 3 ", val);
int val = 0;
parser.parse(" (1 + 2) * ", val);
assert(val == 9);
// assert(val == 9);
assert(val == 0);
}

View File

@ -2730,7 +2730,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &vs,
}
if (success(len)) {
a_val = reduce(chvs, dt);
if (!c.recovered) { a_val = reduce(chvs, dt); }
} else {
if (c.log && !msg.empty() && c.error_info.message_pos < s) {
c.error_info.message_pos = s;
@ -2945,7 +2945,7 @@ inline size_t Recovery::parse_core(const char *s, size_t n,
}
// Recovery
size_t len = static_cast<size_t>(-1);
auto len = static_cast<size_t>(-1);
{
auto save_log = c.log;
c.log = nullptr;

View File

@ -516,6 +516,41 @@ TEST(GeneralTest, Simple_calculator_test) {
EXPECT_EQ(9, val);
}
TEST(GeneralTest, Simple_calculator_with_recovery_test) {
parser parser(R"(
Additive <- Multitive '+' Additive / Multitive
Multitive <- Primary '*' Multitive^cond / Primary
Primary <- '(' Additive ')' / Number
Number <- < [0-9]+ >
%whitespace <- [ \t]*
cond <- '' { error_message "missing multitative" }
)");
parser["Additive"] = [](const SemanticValues &vs) {
switch (vs.choice()) {
case 0: return std::any_cast<int>(vs[0]) + std::any_cast<int>(vs[1]);
default: return std::any_cast<int>(vs[0]);
}
};
parser["Multitive"] = [](const SemanticValues &vs) {
switch (vs.choice()) {
case 0: return std::any_cast<int>(vs[0]) * std::any_cast<int>(vs[1]);
default: return std::any_cast<int>(vs[0]);
}
};
parser["Number"] = [](const SemanticValues &vs) {
return vs.token_to_number<int>();
};
int val = 0;
auto ret = parser.parse(" (1 + 2) * ", val);
EXPECT_FALSE(ret);
EXPECT_EQ(0, val);
}
TEST(GeneralTest, Calculator_test) {
// Construct grammer
Definition EXPRESSION, TERM, FACTOR, TERM_OPERATOR, FACTOR_OPERATOR, NUMBER;

View File

@ -1618,55 +1618,7 @@ rrr | sss
ast));
EXPECT_EQ(i, errors.size());
ast = pg.optimize_ast(ast);
EXPECT_EQ(R"(+ START
+ SECTION
- HEADER/0[CATEGORY] (Section 1)
+ ENTRIES
+ ENTRY/0
- ONE_WAY/0[WORD] (111)
- PHRASE/0[WORD] (222)
- PHRASE/0[WORD] (333)
+ ENTRY/2
+ ENTRY/0
- ONE_WAY/0[WORD] (ccc)
- PHRASE/0[WORD] (ddd)
+ SECTION
- HEADER/0[CATEGORY] (Section 2)
+ ENTRIES
+ ENTRY/2
+ ENTRY/1
- PHRASE/0[WORD] (fff)
- PHRASE/0[WORD] (ggg)
+ SECTION
- HEADER/0[CATEGORY] (Section 3)
+ ENTRIES
+ ENTRY/1
- PHRASE/0[WORD] (hhh)
- PHRASE/0[WORD] (iii)
+ SECTION
- HEADER/0[CATEGORY] (Section )
+ ENTRIES
+ ENTRY/1
- PHRASE/0[WORD] (ppp)
- PHRASE/0[WORD] (qqq)
+ SECTION
- HEADER/0[CATEGORY] (Section 4)
+ ENTRIES
+ ENTRY/1
- PHRASE/0[WORD] (jjj)
- PHRASE/0[WORD] (kkk)
+ ENTRY/2
+ SECTION
- HEADER/0[CATEGORY] (Section 5)
+ ENTRIES
+ ENTRY/1
- PHRASE/0[WORD] (rrr)
- PHRASE/0[WORD] (sss)
)",
ast_to_s(ast));
EXPECT_FALSE(ast);
}
TEST(ErrorTest, Error_recovery_2) {
@ -1824,110 +1776,7 @@ sss | ttt
ast));
EXPECT_EQ(i, errors.size());
ast = pg.optimize_ast(ast);
EXPECT_EQ(R"(+ START
+ SECTION
- HEADER/0[CATEGORY] (Section 1)
+ ENTRIES
+ ENTRY/0
+ ONE_WAY/0[PHRASE]
- WORD (111)
+ PHRASE
- WORD (222)
+ PHRASE
- WORD (333)
+ ENTRY/1
+ PHRASE
- WORD (AAA)
- WORD (BB*)
+ PHRASE
- WORD (CCC)
+ ENTRY/1
+ PHRASE
- WORD (AAA)
- WORD (B?B)
+ PHRASE
- WORD (CCC)
+ ENTRY/1
+ PHRASE
- WORD (aaa)
+ PHRASE
- WORD (bbb)
+ ENTRY/0
+ ONE_WAY/0[PHRASE]
- WORD (ccc)
+ PHRASE
- WORD (ddd)
+ SECTION
- HEADER/0[CATEGORY] (Section 2)
+ ENTRIES
+ ENTRY/1
+ PHRASE
- WORD (eee)
+ ENTRY/1
+ PHRASE
- WORD (fff)
+ PHRASE
- WORD (ggg)
+ ENTRY/1
+ PHRASE
- WORD (fff)
+ PHRASE
- WORD (ggg)
- WORD (111)
+ SECTION
- HEADER/0[CATEGORY] (Section 3)
+ ENTRIES
+ ENTRY/1
+ PHRASE
- WORD (hhh)
+ PHRASE
- WORD (iii)
+ SECTION
- HEADER/0[CATEGORY] (Section )
+ ENTRIES
+ ENTRY/1
+ PHRASE
- WORD (ppp)
+ PHRASE
- WORD (qqq)
+ SECTION
- HEADER/0[CATEGORY] (Section 4)
+ ENTRIES
+ ENTRY/1
+ PHRASE
- WORD (jjj)
+ PHRASE
- WORD (kkk)
+ ENTRY/0
+ ONE_WAY/0[PHRASE]
- WORD (lll)
+ PHRASE
- WORD (mmm)
+ PHRASE
- WORD (nnn)
+ SECTION
- HEADER/0[CATEGORY] (Section 5)
+ ENTRIES
+ ENTRY/1
+ PHRASE
- WORD (ppp)
- WORD (qqq)
+ PHRASE
- WORD (rrr)
+ ENTRY/1
+ PHRASE
- WORD (Section)
- WORD (6)
+ ENTRY/1
+ PHRASE
- WORD (sss)
+ PHRASE
- WORD (ttt)
)",
ast_to_s(ast));
EXPECT_FALSE(ast);
}
TEST(ErrorTest, Error_recovery_Java) {
@ -2024,46 +1873,7 @@ SkipToRCUR ← (!RCUR (LCUR SkipToRCUR / .))* RCUR
)",
ast));
ast = pg.optimize_ast(ast);
EXPECT_EQ(R"(+ Prog
- PUBLIC (public)
- CLASS (class)
- NAME (Example)
- PUBLIC (public)
- STATIC (static)
- VOID (void)
- MAIN (main)
- STRING (String)
- NAME (args)
+ BlockStmt
+ Stmt/3[DecStmt]
- INT (int)
- NAME (n)
- Exp/0[NUMBER] (5)
+ Stmt/3[DecStmt]
- INT (int)
- NAME (f)
- Exp/0[NUMBER] (1)
+ Stmt/1[WhileStmt]
+ Exp/0[RelExp]
- AddExp/0[NUMBER] (0)
- AddExp/0[NAME] (n)
+ Stmt/5[BlockStmt]
+ Stmt/4[AssignStmt]
- NAME (f)
+ Exp/0[MulExp]
- AtomExp/2[NAME] (f)
- TIMES (*)
- AtomExp/2[NAME] (n)
+ Stmt/4[AssignStmt]
- NAME (n)
+ Exp/0[AddExp]
- MulExp/0[NAME] (n)
- MINUS (-)
- MulExp/0[NUMBER] (1)
)",
ast_to_s(ast));
EXPECT_FALSE(ast);
}
TEST(ErrorTest, Error_message_with_rule_instruction) {