Report full rule chain in recursive rule errors

The idea came from a PR by @Mingun:

  https://github.com/pegjs/pegjs/pull/307
redux
David Majda 9 years ago
parent 491106c347
commit bbb4f006cd

@ -37,8 +37,12 @@ function reportLeftRecursion(ast) {
rule_ref: function(node) { rule_ref: function(node) {
if (arrays.contains(visitedRules, node.name)) { if (arrays.contains(visitedRules, node.name)) {
visitedRules.push(node.name);
throw new GrammarError( throw new GrammarError(
"Possible left recursion detected for rule \"" + node.name + "\".", "Possible left recursion detected ("
+ visitedRules.join(" -> ")
+ ").",
node.location node.location
); );
} }

@ -8,7 +8,7 @@ describe("compiler pass |reportLeftRecursion|", function() {
it("reports direct left recursion", function() { it("reports direct left recursion", function() {
expect(pass).toReportError('start = start', { expect(pass).toReportError('start = start', {
message: 'Possible left recursion detected for rule \"start\".', message: 'Possible left recursion detected (start -> start).',
location: { location: {
start: { offset: 8, line: 1, column: 9 }, start: { offset: 8, line: 1, column: 9 },
end: { offset: 13, line: 1, column: 14 } end: { offset: 13, line: 1, column: 14 }
@ -21,7 +21,7 @@ describe("compiler pass |reportLeftRecursion|", function() {
'start = stop', 'start = stop',
'stop = start' 'stop = start'
].join("\n"), { ].join("\n"), {
message: 'Possible left recursion detected for rule \"start\".', message: 'Possible left recursion detected (start -> stop -> start).',
location: { location: {
start: { offset: 21, line: 2, column: 9 }, start: { offset: 21, line: 2, column: 9 },
end: { offset: 26, line: 2, column: 14 } end: { offset: 26, line: 2, column: 14 }

Loading…
Cancel
Save