From 211a1116e495ed8252ebcccb2bd243e6fb79cdee Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 30 Sep 2011 19:47:05 +0200 Subject: [PATCH] Fix stack depth computations for empty sequences Part of a fix for GH-53. --- src/passes.js | 20 ++++++++++++-------- test/passes-test.js | 5 +++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/passes.js b/src/passes.js index 4841aed..74fda0a 100644 --- a/src/passes.js +++ b/src/passes.js @@ -124,14 +124,18 @@ PEG.compiler.passes = { sequence: function(node) { each(node.elements, compute); - node.resultStackDepth = Math.max.apply( - null, - map(node.elements, function(e, i) { return i + e.resultStackDepth; }) - ); - node.posStackDepth = 1 + Math.max.apply( - null, - map(node.elements, function(e) { return e.posStackDepth; }) - ); + node.resultStackDepth = node.elements.length > 0 + ? Math.max.apply( + null, + map(node.elements, function(e, i) { return i + e.resultStackDepth; }) + ) + : 0; + node.posStackDepth = node.elements.length > 0 + ? 1 + Math.max.apply( + null, + map(node.elements, function(e) { return e.posStackDepth; }) + ) + : 1; }, labeled: computeFromExpression(0, 0), diff --git a/test/passes-test.js b/test/passes-test.js index 7e9b281..993a9c1 100644 --- a/test/passes-test.js +++ b/test/passes-test.js @@ -167,6 +167,11 @@ test("computes stack depths", function() { }, /* Sequence */ + { + grammar: 'start = ', + resultStackDepth: 1, + posStackDepth: 1 + }, { grammar: 'start = "a" "b" "c"', resultStackDepth: 3,