2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2015-04-01 12:16:27 +02:00
|
|
|
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) {
|
2015-07-31 13:48:46 +02:00
|
|
|
if (!asts.alwaysAdvancesOnSuccess(ast, node.expression)) {
|
2015-04-06 10:34:07 +02:00
|
|
|
throw new GrammarError("Infinite loop detected.", node.location);
|
2015-04-01 12:16:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
one_or_more: function(node) {
|
2015-07-31 13:48:46 +02:00
|
|
|
if (!asts.alwaysAdvancesOnSuccess(ast, node.expression)) {
|
2015-04-06 10:34:07 +02:00
|
|
|
throw new GrammarError("Infinite loop detected.", node.location);
|
2015-04-01 12:16:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
check(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = reportInfiniteLoops;
|