Skip to content

Commit 3c745bf

Browse files
committed
Fix #205
1 parent 7ee4fcc commit 3c745bf

File tree

3 files changed

+47
-14
lines changed

3 files changed

+47
-14
lines changed

docs/native.wasm

795 Bytes
Binary file not shown.

peglib.h

+28-14
Original file line numberDiff line numberDiff line change
@@ -3810,17 +3810,8 @@ class ParserGenerator {
38103810
if (!ret) { return nullptr; }
38113811

38123812
// Check infinite loop
3813-
{
3814-
DetectInfiniteLoop vis(data.start_pos, data.start);
3815-
start_rule.accept(vis);
3816-
if (vis.has_error) {
3817-
if (log) {
3818-
auto line = line_info(s, vis.error_s);
3819-
log(line.first, line.second,
3820-
"infinite loop is detected in '" + vis.error_name + "'.");
3821-
}
3822-
return nullptr;
3823-
}
3813+
if (detect_infiniteLoop(data, start_rule, log, s)) {
3814+
return nullptr;
38243815
}
38253816

38263817
// Automatic whitespace skipping
@@ -3831,13 +3822,22 @@ class ParserGenerator {
38313822
if (IsLiteralToken::check(*ope)) { rule <= tok(ope); }
38323823
}
38333824

3834-
start_rule.whitespaceOpe =
3835-
wsp(grammar[WHITESPACE_DEFINITION_NAME].get_core_operator());
3825+
auto &rule = grammar[WHITESPACE_DEFINITION_NAME];
3826+
start_rule.whitespaceOpe = wsp(rule.get_core_operator());
3827+
3828+
if (detect_infiniteLoop(data, rule, log, s)) {
3829+
return nullptr;
3830+
}
38363831
}
38373832

38383833
// Word expression
38393834
if (grammar.count(WORD_DEFINITION_NAME)) {
3840-
start_rule.wordOpe = grammar[WORD_DEFINITION_NAME].get_core_operator();
3835+
auto &rule = grammar[WORD_DEFINITION_NAME];
3836+
start_rule.wordOpe = rule.get_core_operator();
3837+
3838+
if (detect_infiniteLoop(data, rule, log, s)) {
3839+
return nullptr;
3840+
}
38413841
}
38423842

38433843
// Apply instructions
@@ -3867,6 +3867,20 @@ class ParserGenerator {
38673867
return data.grammar;
38683868
}
38693869

3870+
bool detect_infiniteLoop(const Data &data, Definition &rule, const Log &log, const char *s) const {
3871+
DetectInfiniteLoop vis(data.start_pos, rule.name);
3872+
rule.accept(vis);
3873+
if (vis.has_error) {
3874+
if (log) {
3875+
auto line = line_info(s, vis.error_s);
3876+
log(line.first, line.second,
3877+
"infinite loop is detected in '" + vis.error_name + "'.");
3878+
}
3879+
return true;
3880+
}
3881+
return false;
3882+
}
3883+
38703884
Grammar g;
38713885
};
38723886

test/test2.cc

+19
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,25 @@ TEST(InfiniteLoopTest, Not_infinite_3) {
204204
EXPECT_TRUE(!!pg);
205205
}
206206

207+
TEST(InfiniteLoopTest, whitespace) {
208+
parser pg(R"(
209+
S <- 'hello'
210+
%whitespace <- ('')*
211+
)");
212+
213+
EXPECT_FALSE(pg);
214+
}
215+
216+
TEST(InfiniteLoopTest, word) {
217+
parser pg(R"(
218+
S <- 'hello'
219+
%whitespace <- ' '*
220+
%word <- ('')*
221+
)");
222+
223+
EXPECT_FALSE(pg);
224+
}
225+
207226
TEST(PrecedenceTest, Precedence_climbing) {
208227
parser parser(R"(
209228
START <- _ EXPRESSION

0 commit comments

Comments
 (0)