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-09-04 17:35:37 +02:00
|
|
|
if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) {
|
2015-09-11 14:58:53 +02:00
|
|
|
throw new GrammarError("Possible infinite loop detected.", node.location);
|
2015-04-01 12:16:27 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
one_or_more: function(node) {
|
2015-09-04 17:35:37 +02:00
|
|
|
if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) {
|
2015-09-11 14:58:53 +02:00
|
|
|
throw new GrammarError("Possible infinite loop detected.", node.location);
|
2015-04-01 12:16:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
check(ast);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = reportInfiniteLoops;
|