2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-09-17 15:08:47 +02:00
|
|
|
let GrammarError = require("../../grammar-error");
|
2016-09-22 09:25:31 +02:00
|
|
|
let asts = require("../asts");
|
|
|
|
let visitor = require("../visitor");
|
2015-04-01 12:16:27 +02:00
|
|
|
|
2016-09-17 16:28:28 +02:00
|
|
|
// 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) {
|
2016-09-08 16:04:36 +02:00
|
|
|
let check = visitor.build({
|
2015-04-01 12:16:27 +02:00
|
|
|
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;
|