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.
|
|
|
|
*/
|
2016-07-29 18:06:16 +02:00
|
|
|
function reportInfiniteRepetition(ast) {
|
2015-04-01 12:16:27 +02:00
|
|
|
var check = visitor.build({
|
|
|
|
zero_or_more: function(node) {
|
2015-09-04 17:35:37 +02:00
|
|
|
if (!asts.alwaysConsumesOnSuccess(ast, node.expression)) {
|
2016-07-03 17:56:25 +02:00
|
|
|
throw new GrammarError(
|
2016-07-04 07:19:57 +02:00
|
|
|
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
2016-07-03 17:56:25 +02:00
|
|
|
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)) {
|
2016-07-03 17:56:25 +02:00
|
|
|
throw new GrammarError(
|
2016-07-04 07:19:57 +02:00
|
|
|
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
2016-07-03 17:56:25 +02:00
|
|
|
node.location
|
|
|
|
);
|
2015-04-01 12:16:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
check(ast);
|
|
|
|
}
|
|
|
|
|
2016-07-29 18:06:16 +02:00
|
|
|
module.exports = reportInfiniteRepetition;
|