From 4198d81bc653c343c321f7fb1a2edf17efff1cdd Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 31 Jul 2022 15:39:38 -0400 Subject: [PATCH] Fix #238 --- peglib.h | 10 +++++++++- test/test1.cc | 24 ++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/peglib.h b/peglib.h index 6be2b14..fb66894 100644 --- a/peglib.h +++ b/peglib.h @@ -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 #include #include @@ -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); diff --git a/test/test1.cc b/test/test1.cc index 77404c0..1e3d4bd 100644 --- a/test/test1.cc +++ b/test/test1.cc @@ -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); +}