2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-25 20:19:42 +02:00
|
|
|
const GrammarError = require( "../../grammar-error" );
|
|
|
|
const asts = require( "../asts" );
|
|
|
|
const 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.
|
2017-10-25 20:19:42 +02:00
|
|
|
function reportInfiniteRepetition( ast ) {
|
|
|
|
|
|
|
|
const check = visitor.build( {
|
|
|
|
zero_or_more( node ) {
|
|
|
|
|
|
|
|
if ( ! asts.alwaysConsumesOnSuccess( ast, node.expression ) ) {
|
|
|
|
|
|
|
|
throw new GrammarError(
|
|
|
|
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
|
|
|
|
node.location
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
one_or_more( node ) {
|
|
|
|
|
|
|
|
if ( ! asts.alwaysConsumesOnSuccess( ast, node.expression ) ) {
|
|
|
|
|
|
|
|
throw new GrammarError(
|
|
|
|
"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;
|