Fixed regression problem caused by #217

This commit is contained in:
yhirose 2022-08-19 22:31:04 -04:00
parent 9f275133bb
commit 9c3497e350
4 changed files with 21 additions and 5 deletions

View File

@ -624,7 +624,7 @@ public class Example {
} }
> peglint java.peg sample.java > peglint java.peg sample.java
sample.java:5:12: syntax error, unexpected '<', expecting <NAME>, <NUMBER>, <WhileStmt>. sample.java:5:12: syntax error, unexpected '<', expecting '(', <NUMBER>, <NAME>.
sample.java:8:5: missing semicolon in assignment. sample.java:8:5: missing semicolon in assignment.
sample.java:8:6: invalid statement sample.java:8:6: invalid statement
``` ```

Binary file not shown.

View File

@ -3045,7 +3045,14 @@ inline size_t Recovery::parse_core(const char *s, size_t n,
len = rule.parse(s, n, dummy_vs, c, dummy_dt); len = rule.parse(s, n, dummy_vs, c, dummy_dt);
} }
if (success(len)) { c.recovered = true; } if (success(len)) {
c.recovered = true;
if (c.log) {
c.error_info.output_log(c.log, c.s, c.l);
c.error_info.clear();
}
}
// Cut // Cut
if (!c.cut_stack.empty()) { if (!c.cut_stack.empty()) {

View File

@ -1452,6 +1452,7 @@ TEST(ErrorTest, Default_error_handling_1) {
}; };
EXPECT_FALSE(pg.parse(" @ aaa typo ")); EXPECT_FALSE(pg.parse(" @ aaa typo "));
EXPECT_EQ(i, errors.size());
} }
TEST(ErrorTest, Default_error_handling_2) { TEST(ErrorTest, Default_error_handling_2) {
@ -1477,6 +1478,7 @@ TEST(ErrorTest, Default_error_handling_2) {
}; };
EXPECT_FALSE(pg.parse(" @ aaa typo ")); EXPECT_FALSE(pg.parse(" @ aaa typo "));
EXPECT_EQ(i, errors.size());
} }
TEST(ErrorTest, Default_error_handling_fiblang) { TEST(ErrorTest, Default_error_handling_fiblang) {
@ -1525,6 +1527,7 @@ TEST(ErrorTest, Default_error_handling_fiblang) {
for n frm 1 to 30 for n frm 1 to 30
puts(fib(n)) puts(fib(n))
)")); )"));
EXPECT_EQ(i, errors.size());
} }
TEST(ErrorTest, Error_recovery_1) { TEST(ErrorTest, Error_recovery_1) {
@ -1603,6 +1606,8 @@ rrr | sss
)", )",
ast)); ast));
EXPECT_EQ(i, errors.size());
ast = pg.optimize_ast(ast); ast = pg.optimize_ast(ast);
EXPECT_EQ(R"(+ START EXPECT_EQ(R"(+ START
@ -1655,20 +1660,20 @@ rrr | sss
TEST(ErrorTest, Error_recovery_2) { TEST(ErrorTest, Error_recovery_2) {
parser pg(R"( parser pg(R"(
START <- ENTRY (',' ENTRY)* _* START <- ENTRY ((',' ENTRY) / %recover((!(',' / Space) .)+))* (_ / %recover(.*))
ENTRY <- '[' ITEM (',' ITEM)* ']' ENTRY <- '[' ITEM (',' ITEM)* ']'
ITEM <- WORD / NUM / %recover((!(',' / ']') .)+) ITEM <- WORD / NUM / %recover((!(',' / ']') .)+)
NUM <- [0-9]+ ![a-z] NUM <- [0-9]+ ![a-z]
WORD <- '"' [a-z]+ '"' WORD <- '"' [a-z]+ '"'
~_ <- Space ~_ <- Space*
Space <- [ \n] Space <- [ \n]
)"); )");
EXPECT_TRUE(!!pg); EXPECT_TRUE(!!pg);
std::vector<std::string> errors{ std::vector<std::string> errors{
R"(1:6: syntax error, unexpected ']', expecting <Space>.)", R"(1:6: syntax error, unexpected ']', expecting ','.)",
R"(1:18: syntax error, unexpected 'z', expecting <NUM>.)", R"(1:18: syntax error, unexpected 'z', expecting <NUM>.)",
R"(1:24: syntax error, unexpected ',', expecting '"'.)", R"(1:24: syntax error, unexpected ',', expecting '"'.)",
R"(1:31: syntax error, unexpected 'ccc', expecting '"', <NUM>.)", R"(1:31: syntax error, unexpected 'ccc', expecting '"', <NUM>.)",
@ -1692,6 +1697,7 @@ TEST(ErrorTest, Error_recovery_2) {
pg.parse(R"([000]],[111],[222z,"aaa,"bbb",ccc"],[ddd",444,555,"eee],[ pg.parse(R"([000]],[111],[222z,"aaa,"bbb",ccc"],[ddd",444,555,"eee],[
)", )",
ast)); ast));
EXPECT_EQ(i, errors.size());
EXPECT_FALSE(ast); EXPECT_FALSE(ast);
} }
@ -1761,6 +1767,7 @@ skip_puncs <- [|=]* _
R"(21:17: Use of invalid operator)", R"(21:17: Use of invalid operator)",
R"(24:10: Use of invalid operator combination)", R"(24:10: Use of invalid operator combination)",
R"(26:10: Missing OR operator (|))", R"(26:10: Missing OR operator (|))",
R"(28:3: Missing opening/closing square bracket)",
}; };
size_t i = 0; size_t i = 0;
@ -1803,6 +1810,8 @@ sss | ttt
)", )",
ast)); ast));
EXPECT_EQ(i, errors.size());
ast = pg.optimize_ast(ast); ast = pg.optimize_ast(ast);
EXPECT_EQ(R"(+ START EXPECT_EQ(R"(+ START