pegjs/lib/compiler/passes/report-infinite-loops.js
David Majda 491106c347 Report left recursion and infinite loops only as "possible"
A semantic predicate can prevent the parser to actually enter infinite
recursion or loop. This is undetectable at compile-time.
2015-09-11 14:58:53 +02:00

30 lines
822 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.alwaysConsumesOnSuccess(ast, node.expression)) {
throw new GrammarError("Possible infinite loop detected.", node.location);
}
},
one_or_more: function(node) {
if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) {
throw new GrammarError("Possible infinite loop detected.", node.location);
}
}
});
check(ast);
}
module.exports = reportInfiniteLoops;