2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
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.
|
2018-01-30 03:38:49 +01:00
|
|
|
function reportInfiniteRepetition( ast, session ) {
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-01-30 03:38:49 +01:00
|
|
|
const check = session.buildVisitor( {
|
2017-10-25 20:19:42 +02:00
|
|
|
zero_or_more( node ) {
|
|
|
|
|
2018-01-26 08:49:34 +01:00
|
|
|
if ( ! ast.alwaysConsumesOnSuccess( node.expression ) ) {
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-02-02 05:32:39 +01:00
|
|
|
session.error(
|
2017-10-25 20:19:42 +02:00
|
|
|
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
|
|
|
node.location
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
one_or_more( node ) {
|
|
|
|
|
2018-01-26 08:49:34 +01:00
|
|
|
if ( ! ast.alwaysConsumesOnSuccess( node.expression ) ) {
|
2017-10-25 20:19:42 +02:00
|
|
|
|
2018-02-02 05:32:39 +01:00
|
|
|
session.error(
|
2017-10-25 20:19:42 +02:00
|
|
|
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
|
|
|
node.location
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
check( ast );
|
|
|
|
|
2015-04-01 12:16:27 +02:00
|
|
|
}
|
|
|
|
|
2016-07-29 18:06:16 +02:00
|
|
|
module.exports = reportInfiniteRepetition;
|