Compiler checks and passes are named for easier reference from tests
This commit is contained in:
parent
906488027b
commit
f82a4ebf28
|
@ -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, []);
|
||||
}
|
||||
];
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
];
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue