Fix stack depth computations for empty sequences

Part of a fix for GH-53.
redux
David Majda 13 years ago
parent afdcb6fc4f
commit 211a1116e4

@ -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),

@ -167,6 +167,11 @@ test("computes stack depths", function() {
},
/* Sequence */
{
grammar: 'start = ',
resultStackDepth: 1,
posStackDepth: 1
},
{
grammar: 'start = "a" "b" "c"',
resultStackDepth: 3,

Loading…
Cancel
Save