pegjs/lib/compiler/passes/report-infinite-loops.js
David Majda 130cbcfaa3 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
2015-07-31 13:48:46 +02:00

30 lines
804 B
JavaScript

"use strict";
var GrammarError = require("../../grammar-error"),
asts = require("../asts"),
visitor = require("../visitor");
/*
* Reports expressions that don't consume any input inside |*| or |+| in the
* grammar, which prevents infinite loops in the generated parser.
*/
function reportInfiniteLoops(ast) {
var check = visitor.build({
zero_or_more: function(node) {
if (!asts.alwaysAdvancesOnSuccess(ast, node.expression)) {
throw new GrammarError("Infinite loop detected.", node.location);
}
},
one_or_more: function(node) {
if (!asts.alwaysAdvancesOnSuccess(ast, node.expression)) {
throw new GrammarError("Infinite loop detected.", node.location);
}
}
});
check(ast);
}
module.exports = reportInfiniteLoops;