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

@ -11,13 +11,13 @@ var GrammarError = require("../../grammar-error"),
function reportInfiniteLoops(ast) { function reportInfiniteLoops(ast) {
var check = visitor.build({ var check = visitor.build({
zero_or_more: function(node) { 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); throw new GrammarError("Infinite loop detected.", node.location);
} }
}, },
one_or_more: function(node) { 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); throw new GrammarError("Infinite loop detected.", node.location);
} }
} }

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

Loading…
Cancel
Save