Skip to content

Commit

Permalink
Rename asts.matchesEmpty to alwaysAdvancesOnSuccess and negate it
Browse files Browse the repository at this point in the history
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:

  #307 (comment)
  • Loading branch information
dmajda committed Jul 31, 2015
1 parent 3170597 commit 130cbcf
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
48 changes: 24 additions & 24 deletions lib/compiler/asts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};

Expand Down
4 changes: 2 additions & 2 deletions lib/compiler/passes/report-infinite-loops.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/compiler/passes/report-left-recursion.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function reportLeftRecursion(ast) {
check(element, visitedRules);
}

return asts.matchesEmpty(ast, element);
return !asts.alwaysAdvancesOnSuccess(ast, element);
});
},

Expand Down

0 comments on commit 130cbcf

Please sign in to comment.