From ad27a300a8e3373d69cc284b0b35b23aba3ac358 Mon Sep 17 00:00:00 2001 From: David Majda Date: Mon, 17 Aug 2015 10:58:37 +0200 Subject: [PATCH] Fix left recursion detection in sequences Report left recursion also in cases where the recursive rule invocation is not a direct element of a sequence, but is wrapped inside an expression. Fixes #359. --- lib/compiler/passes/report-left-recursion.js | 4 +--- spec/unit/compiler/passes/report-left-recursion.spec.js | 5 +++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/compiler/passes/report-left-recursion.js b/lib/compiler/passes/report-left-recursion.js index 8decd1b..9745fce 100644 --- a/lib/compiler/passes/report-left-recursion.js +++ b/lib/compiler/passes/report-left-recursion.js @@ -29,9 +29,7 @@ function reportLeftRecursion(ast) { sequence: function(node) { arrays.every(node.elements, function(element) { - if (element.type === "rule_ref") { - check(element); - } + check(element); return !asts.alwaysAdvancesOnSuccess(ast, element); }); diff --git a/spec/unit/compiler/passes/report-left-recursion.spec.js b/spec/unit/compiler/passes/report-left-recursion.spec.js index f9cfa84..aa8fa21 100644 --- a/spec/unit/compiler/passes/report-left-recursion.spec.js +++ b/spec/unit/compiler/passes/report-left-recursion.spec.js @@ -39,6 +39,11 @@ describe("compiler pass |reportLeftRecursion|", function() { expect(pass).not.toReportError('start = "" "" "a" start'); }); + /* Regression test for #359. */ + it("reports left recursion when rule reference is wrapped in an expression", function() { + expect(pass).toReportError('start = "" start?'); + }); + it("computes expressions that always advance on success correctly", function() { expect(pass).toReportError([ 'start = a start',