This commit is contained in:
yhirose 2022-07-31 15:39:38 -04:00
parent 604fe1808e
commit 4198d81bc6
2 changed files with 33 additions and 1 deletions

View File

@ -7,6 +7,14 @@
#pragma once
/*
* Configuration
*/
#ifndef CPPPEGLIB_HEURISTIC_ERROR_TOKEN_MAX_CHAR_COUNT
#define CPPPEGLIB_HEURISTIC_ERROR_TOKEN_MAX_CHAR_COUNT 32
#endif
#include <algorithm>
#include <any>
#include <cassert>
@ -702,7 +710,7 @@ private:
}
}
size_t count = 8;
size_t count = CPPPEGLIB_HEURISTIC_ERROR_TOKEN_MAX_CHAR_COUNT;
size_t j = 0;
while (count > 0 && j < i) {
j += codepoint_length(&pos[j], i - j);

View File

@ -1041,3 +1041,27 @@ TEST(GeneralTest, InvalidCutOperator) {
ret = parser.parse("b");
EXPECT_FALSE(ret);
}
TEST(GeneralTest, HeuristicErrorTokenTest) {
auto parser = peg::parser(R"(
program <- enum+
enum <- 'enum' enum_kind^untyped_enum
enum_kind <- 'sequence' / 'bitmask'
%whitespace <- [ \r\t\n]*
%word <- [a-zA-Z0-9_]
untyped_enum <- '' { message "invalid/missing enum type, expected one of 'sequence' or 'bitmask', got '%t'"}
)");
parser.log = [&](size_t ln, size_t col, const std::string &msg) {
EXPECT_EQ(1, ln);
EXPECT_EQ(6, col);
EXPECT_EQ("invalid/missing enum type, expected one of 'sequence' or "
"'bitmask', got 'sequencer'",
msg);
};
auto ret = parser.parse("enum sequencer");
EXPECT_FALSE(ret);
}