Compiler checks and passes are named for easier reference from tests

redux
David Majda 14 years ago
parent 906488027b
commit f82a4ebf28

@ -2,12 +2,13 @@
* Checks made on the grammar AST before compilation. Each check is a function * Checks made on the grammar AST before compilation. Each check is a function
* that is passed the AST and does not return anything. If the check passes, the * that is passed the AST and does not return anything. If the check passes, the
* function does not do anything special, otherwise it throws * function does not do anything special, otherwise it throws
* |PEG.GrammarError|. The checks are run in sequence in order of their * |PEG.GrammarError|. The order in which the checks are run is specified in
* definition. * |PEG.compiler.compile| and should be the same as the order of definitions
* here.
*/ */
PEG.compiler.checks = [ PEG.compiler.checks = {
/* Checks that all referenced rules exist. */ /* Checks that all referenced rules exist. */
function(ast) { missingReferencedRules: function(ast) {
function nop() {} function nop() {}
function checkExpression(node) { check(node.expression); } function checkExpression(node) { check(node.expression); }
@ -55,7 +56,7 @@ PEG.compiler.checks = [
}, },
/* Checks that no left recursion is present. */ /* Checks that no left recursion is present. */
function(ast) { leftRecursion: function(ast) {
function nop() {} function nop() {}
function checkExpression(node, appliedRules) { function checkExpression(node, appliedRules) {
@ -116,4 +117,4 @@ PEG.compiler.checks = [
check(ast, []); check(ast, []);
} }
]; };

@ -8,12 +8,21 @@ PEG.compiler = {
* cause its malfunction. * cause its malfunction.
*/ */
compile: function(ast) { compile: function(ast) {
for (var i = 0; i < this.checks.length; i++) { var CHECK_NAMES = [
this.checks[i](ast); "missingReferencedRules",
"leftRecursion"
];
var PASS_NAMES = [
"proxyRules"
];
for (var i = 0; i < CHECK_NAMES.length; i++) {
this.checks[CHECK_NAMES[i]](ast);
} }
for (var i = 0; i < this.passes.length; i++) { for (var i = 0; i < PASS_NAMES.length; i++) {
ast = this.passes[i](ast); ast = this.passes[PASS_NAMES[i]](ast);
} }
var source = this.emitter(ast); var source = this.emitter(ast);

@ -1,14 +1,15 @@
/* /*
* Optimalization passes made on the grammar AST before compilation. Each pass * Optimalization passes made on the grammar AST before compilation. Each pass
* is a function that is passed the AST and returns a new AST. The AST can be * is a function that is passed the AST and returns a new AST. The AST can be
* modified in-place by the pass. The passes are run in sequence in order of * modified in-place by the pass. The order in which the passes are run is
* their definition. * specified in |PEG.compiler.compile| and should be the same as the order of
* definitions here.
*/ */
PEG.compiler.passes = [ PEG.compiler.passes = {
/* /*
* Removes proxy rules -- that is, rules that only delegate to other rule. * Removes proxy rules -- that is, rules that only delegate to other rule.
*/ */
function(ast) { proxyRules: function(ast) {
function isProxyRule(node) { function isProxyRule(node) {
return node.type === "rule" && node.expression.type === "rule_ref"; return node.type === "rule" && node.expression.type === "rule_ref";
} }
@ -76,4 +77,4 @@ PEG.compiler.passes = [
return ast; return ast;
} }
]; };

Loading…
Cancel
Save