Rename asts.matchesEmpty to alwaysAdvancesOnSuccess and negate it

This makes it more clear that the function isn't about the input the
expression *matched* but about the input it *consumed* when it matched.

Based on a comment by @Mingun:

  https://github.com/pegjs/pegjs/pull/307#issuecomment-89512575
redux
David Majda 9 years ago
parent 317059760a
commit 130cbcfaa3

@ -13,50 +13,50 @@ var asts = {
return arrays.indexOf(ast.rules, function(r) { return r.name === name; });
},
matchesEmpty: function(ast, node) {
function matchesTrue() { return true; }
function matchesFalse() { return false; }
alwaysAdvancesOnSuccess: function(ast, node) {
function advancesTrue() { return true; }
function advancesFalse() { return false; }
function matchesExpression(node) {
return matches(node.expression);
function advancesExpression(node) {
return advances(node.expression);
}
var matches = visitor.build({
rule: matchesExpression,
var advances = visitor.build({
rule: advancesExpression,
choice: function(node) {
return arrays.some(node.alternatives, matches);
return arrays.every(node.alternatives, advances);
},
action: matchesExpression,
action: advancesExpression,
sequence: function(node) {
return arrays.every(node.elements, matches);
return arrays.some(node.elements, advances);
},
labeled: matchesExpression,
text: matchesExpression,
simple_and: matchesTrue,
simple_not: matchesTrue,
optional: matchesTrue,
zero_or_more: matchesTrue,
one_or_more: matchesExpression,
semantic_and: matchesTrue,
semantic_not: matchesTrue,
labeled: advancesExpression,
text: advancesExpression,
simple_and: advancesFalse,
simple_not: advancesFalse,
optional: advancesFalse,
zero_or_more: advancesFalse,
one_or_more: advancesExpression,
semantic_and: advancesFalse,
semantic_not: advancesFalse,
rule_ref: function(node) {
return matches(asts.findRule(ast, node.name));
return advances(asts.findRule(ast, node.name));
},
literal: function(node) {
return node.value === "";
return node.value !== "";
},
"class": matchesFalse,
any: matchesFalse
"class": advancesTrue,
any: advancesTrue
});
return matches(node);
return advances(node);
}
};

@ -11,13 +11,13 @@ var GrammarError = require("../../grammar-error"),
function reportInfiniteLoops(ast) {
var check = visitor.build({
zero_or_more: function(node) {
if (asts.matchesEmpty(ast, node.expression)) {
if (!asts.alwaysAdvancesOnSuccess(ast, node.expression)) {
throw new GrammarError("Infinite loop detected.", node.location);
}
},
one_or_more: function(node) {
if (asts.matchesEmpty(ast, node.expression)) {
if (!asts.alwaysAdvancesOnSuccess(ast, node.expression)) {
throw new GrammarError("Infinite loop detected.", node.location);
}
}

@ -29,7 +29,7 @@ function reportLeftRecursion(ast) {
check(element, visitedRules);
}
return asts.matchesEmpty(ast, element);
return !asts.alwaysAdvancesOnSuccess(ast, element);
});
},

Loading…
Cancel
Save