From 756b6fc4737fd41a3476797f8b66089ab23b56f5 Mon Sep 17 00:00:00 2001 From: David Majda Date: Fri, 30 Sep 2011 17:19:14 +0200 Subject: [PATCH] Fix |resultStackDepth| computation for sequences The |1 + ...| was wrong -- sequence does not need its own variable since it reuses the one used by the first item. Part of a fix for GH-53. --- src/parser.js | 80 ++++++++++++++++++++++----------------------- src/passes.js | 2 +- test/passes-test.js | 16 ++++----- 3 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/parser.js b/src/parser.js index 0caff5a..39fb0ee 100644 --- a/src/parser.js +++ b/src/parser.js @@ -153,7 +153,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4; + var result0, result1, result2, result3; var pos0, pos1, pos2; pos0 = pos; @@ -219,7 +219,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -265,7 +265,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4, result5; + var result0, result1, result2, result3, result4; var pos0, pos1, pos2; pos0 = pos; @@ -332,7 +332,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4, result5; + var result0, result1, result2, result3; var pos0, pos1, pos2, pos3; pos0 = pos; @@ -416,7 +416,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -495,7 +495,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; pos0 = pos; @@ -550,7 +550,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -682,7 +682,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -786,7 +786,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4; + var result0, result1, result2; var pos0, pos1, pos2, pos3, pos4; pos0 = pos; @@ -899,7 +899,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; reportFailures++; @@ -944,7 +944,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; pos0 = pos; @@ -1083,7 +1083,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1131,7 +1131,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1179,7 +1179,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1227,7 +1227,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1275,7 +1275,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1323,7 +1323,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1371,7 +1371,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1419,7 +1419,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1467,7 +1467,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1515,7 +1515,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1563,7 +1563,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1611,7 +1611,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -1659,7 +1659,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; reportFailures++; @@ -1789,7 +1789,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; reportFailures++; @@ -1858,7 +1858,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; reportFailures++; @@ -1906,7 +1906,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; pos0 = pos; @@ -2008,7 +2008,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2, pos3; pos0 = pos; @@ -2087,7 +2087,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; pos0 = pos; @@ -2189,7 +2189,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2, pos3; pos0 = pos; @@ -2268,7 +2268,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4, result5, result6; + var result0, result1, result2, result3, result4, result5; var pos0, pos1, pos2; reportFailures++; @@ -2398,7 +2398,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; pos0 = pos; @@ -2532,7 +2532,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2, pos3; pos0 = pos; @@ -2611,7 +2611,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2, pos3; pos0 = pos; @@ -2715,7 +2715,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2, pos3; pos0 = pos; @@ -2772,7 +2772,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3; + var result0, result1, result2; var pos0, pos1, pos2; pos0 = pos; @@ -2828,7 +2828,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4, result5; + var result0, result1, result2, result3, result4; var pos0, pos1, pos2; pos0 = pos; @@ -2896,7 +2896,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2; + var result0, result1; var pos0, pos1, pos2; pos0 = pos; @@ -3144,7 +3144,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4, result5; + var result0, result1, result2, result3; var pos0, pos1, pos2, pos3; pos0 = pos; @@ -3250,7 +3250,7 @@ PEG.parser = (function(){ return cachedResult.result; } - var result0, result1, result2, result3, result4, result5; + var result0, result1, result2, result3; var pos0, pos1, pos2, pos3; pos0 = pos; diff --git a/src/passes.js b/src/passes.js index 71f2686..d769768 100644 --- a/src/passes.js +++ b/src/passes.js @@ -124,7 +124,7 @@ PEG.compiler.passes = { sequence: function(node) { each(node.elements, compute); - node.resultStackDepth = 1 + Math.max.apply( + node.resultStackDepth = Math.max.apply( null, map(node.elements, function(e, i) { return i + e.resultStackDepth; }) ); diff --git a/test/passes-test.js b/test/passes-test.js index 53798b4..e941dfe 100644 --- a/test/passes-test.js +++ b/test/passes-test.js @@ -169,42 +169,42 @@ test("computes stack depths", function() { /* Sequence */ { grammar: 'start = "a" "b" "c"', - resultStackDepth: 4, + resultStackDepth: 3, posStackDepth: 2 }, { grammar: 'start = "a" "b" "c"*', - resultStackDepth: 5, + resultStackDepth: 4, posStackDepth: 2 }, { grammar: 'start = "a" "b"* "c"', - resultStackDepth: 4, + resultStackDepth: 3, posStackDepth: 2 }, { grammar: 'start = "a" ("b"*)* "c"', - resultStackDepth: 5, + resultStackDepth: 4, posStackDepth: 2 }, { grammar: 'start = "a"* "b" "c"', - resultStackDepth: 4, + resultStackDepth: 3, posStackDepth: 2 }, { grammar: 'start = ("a"*)* "b" "c"', - resultStackDepth: 4, + resultStackDepth: 3, posStackDepth: 2 }, { grammar: 'start = (("a"*)*)* "b" "c"', - resultStackDepth: 5, + resultStackDepth: 4, posStackDepth: 2 }, { grammar: 'start = "a" &"b" "c"', - resultStackDepth: 4, + resultStackDepth: 3, posStackDepth: 3 },