From e264d69bfb73c3a969c743c11ab3632e6c7a7f9e Mon Sep 17 00:00:00 2001 From: Tartasprint Date: Fri, 18 Oct 2024 02:58:36 +0200 Subject: [PATCH 1/3] Band-Aid: fix one of the left recursion bugs (fixes #1047) --- meta/src/validator.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meta/src/validator.rs b/meta/src/validator.rs index eff89969..937fa2de 100644 --- a/meta/src/validator.rs +++ b/meta/src/validator.rs @@ -667,7 +667,7 @@ fn left_recursion<'a, 'i: 'a>(rules: HashMap>) -> Vec None } ParserExpr::Seq(ref lhs, ref rhs) => { - if is_non_failing(&lhs.expr, rules, &mut vec![trace.last().unwrap().clone()]) { + if is_non_failing(&lhs.expr, rules, &mut vec![trace.last().unwrap().clone()]) || is_non_progressing(&lhs.expr, rules, &mut vec![trace.last().unwrap().clone()]){ check_expr(rhs, rules, trace) } else { check_expr(lhs, rules, trace) From c93aae856712497514d4f80093c17786127eee7c Mon Sep 17 00:00:00 2001 From: Tartasprint Date: Fri, 18 Oct 2024 03:24:34 +0200 Subject: [PATCH 2/3] Forgot formatting --- meta/src/validator.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/meta/src/validator.rs b/meta/src/validator.rs index 937fa2de..8ac93585 100644 --- a/meta/src/validator.rs +++ b/meta/src/validator.rs @@ -667,7 +667,13 @@ fn left_recursion<'a, 'i: 'a>(rules: HashMap>) -> Vec None } ParserExpr::Seq(ref lhs, ref rhs) => { - if is_non_failing(&lhs.expr, rules, &mut vec![trace.last().unwrap().clone()]) || is_non_progressing(&lhs.expr, rules, &mut vec![trace.last().unwrap().clone()]){ + if is_non_failing(&lhs.expr, rules, &mut vec![trace.last().unwrap().clone()]) + || is_non_progressing( + &lhs.expr, + rules, + &mut vec![trace.last().unwrap().clone()], + ) + { check_expr(rhs, rules, trace) } else { check_expr(lhs, rules, trace) From 3ae5eba1187979aa9b5a5148b30a047e2a4acf50 Mon Sep 17 00:00:00 2001 From: Tartasprint Date: Fri, 18 Oct 2024 11:27:28 +0200 Subject: [PATCH 3/3] Added test for validating left recursion in the case of a sequence starting by a non progressing expression. --- meta/src/validator.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/meta/src/validator.rs b/meta/src/validator.rs index 8ac93585..26ce300f 100644 --- a/meta/src/validator.rs +++ b/meta/src/validator.rs @@ -1817,6 +1817,22 @@ mod tests { #[test] #[should_panic(expected = "grammar error + --> 1:14 + | +1 | a = { !\"a\" ~ a } + | ^ + | + = rule a is left-recursive (a -> a); pest::pratt_parser might be useful in this case")] + fn non_progressing_left_recursion() { + let input = "a = { !\"a\" ~ a }"; + unwrap_or_report(consume_rules( + PestParser::parse(Rule::grammar_rules, input).unwrap(), + )); + } + + #[test] + #[should_panic(expected = "grammar error + --> 1:7 | 1 | a = { \"a\"* | \"a\" | \"b\" }