pegjs/lib/compiler/passes/remove-proxy-rules.js

43 lines
845 B
JavaScript
Raw Normal View History

"use strict";
2016-09-17 16:28:28 +02:00
// Removes proxy rules -- that is, rules that only delegate to other rule.
2018-01-30 03:38:49 +01:00
function removeProxyRules( ast, session, options ) {
function isProxyRule( node ) {
return node.type === "rule" && node.expression.type === "rule_ref";
}
2018-03-29 04:55:50 +02:00
const replaceRuleRefs = session.buildVisitor( {
2018-03-29 04:55:50 +02:00
rule_ref( node, proxy, real ) {
2018-03-29 04:55:50 +02:00
if ( node.name === proxy ) node.name = real;
2018-03-29 04:55:50 +02:00
}
2018-03-29 04:55:50 +02:00
} );
2018-03-29 04:55:50 +02:00
const allowedStartRules = options.allowedStartRules;
const rules = [];
2018-03-29 04:55:50 +02:00
for ( const rule of ast.rules ) {
if ( isProxyRule( rule ) ) {
replaceRuleRefs( ast, rule.name, rule.expression.name );
2018-03-29 04:55:50 +02:00
if ( allowedStartRules.indexOf( rule.name ) < 0 ) continue;
}
2018-03-29 04:55:50 +02:00
rules.push( rule );
2018-03-29 04:55:50 +02:00
}
2018-03-29 04:55:50 +02:00
ast.rules = rules;
}
module.exports = removeProxyRules;