pegjs/lib/compiler/passes/report-infinite-repetition.js

40 lines
1,012 B
JavaScript
Raw Normal View History

"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 ) {
2018-01-30 03:38:49 +01:00
const check = session.buildVisitor( {
zero_or_more( node ) {
2018-01-26 08:49:34 +01:00
if ( ! ast.alwaysConsumesOnSuccess( node.expression ) ) {
2018-02-02 05:32:39 +01:00
session.error(
"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 ) ) {
2018-02-02 05:32:39 +01:00
session.error(
"Possible infinite loop when parsing (repetition used with an expression that may not consume any input).",
node.location
);
}
}
} );
check( ast );
}
module.exports = reportInfiniteRepetition;