From f82a4ebf28df6ae7f99068455800f8360114eb8b Mon Sep 17 00:00:00 2001 From: David Majda Date: Sat, 21 Aug 2010 11:55:46 +0200 Subject: [PATCH] Compiler checks and passes are named for easier reference from tests --- src/checks.js | 13 +++++++------ src/compiler.js | 17 +++++++++++++---- src/passes.js | 11 ++++++----- 3 files changed, 26 insertions(+), 15 deletions(-) diff --git a/src/checks.js b/src/checks.js index 863473f..92acd40 100644 --- a/src/checks.js +++ b/src/checks.js @@ -2,12 +2,13 @@ * 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 * function does not do anything special, otherwise it throws - * |PEG.GrammarError|. The checks are run in sequence in order of their - * definition. + * |PEG.GrammarError|. The order in which the checks are run is specified in + * |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. */ - function(ast) { + missingReferencedRules: function(ast) { function nop() {} function checkExpression(node) { check(node.expression); } @@ -55,7 +56,7 @@ PEG.compiler.checks = [ }, /* Checks that no left recursion is present. */ - function(ast) { + leftRecursion: function(ast) { function nop() {} function checkExpression(node, appliedRules) { @@ -116,4 +117,4 @@ PEG.compiler.checks = [ check(ast, []); } -]; +}; diff --git a/src/compiler.js b/src/compiler.js index e1544d2..5578daf 100644 --- a/src/compiler.js +++ b/src/compiler.js @@ -8,12 +8,21 @@ PEG.compiler = { * cause its malfunction. */ compile: function(ast) { - for (var i = 0; i < this.checks.length; i++) { - this.checks[i](ast); + var CHECK_NAMES = [ + "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++) { - ast = this.passes[i](ast); + for (var i = 0; i < PASS_NAMES.length; i++) { + ast = this.passes[PASS_NAMES[i]](ast); } var source = this.emitter(ast); diff --git a/src/passes.js b/src/passes.js index 2d303ec..4b8ef91 100644 --- a/src/passes.js +++ b/src/passes.js @@ -1,14 +1,15 @@ /* * 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 - * modified in-place by the pass. The passes are run in sequence in order of - * their definition. + * modified in-place by the pass. The order in which the passes are run is + * 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. */ - function(ast) { + proxyRules: function(ast) { function isProxyRule(node) { return node.type === "rule" && node.expression.type === "rule_ref"; } @@ -76,4 +77,4 @@ PEG.compiler.passes = [ return ast; } -]; +};