Plugin API: Implement the |passes| parameter for |PEG.compiler.compile|

The |passes| parameter will allow to pass the list of passes from
|PEG.buildParser|. This will be used by plugins. The old way via setting
the |appliedPassNames| property is removed.

Implements part of GH-106.
redux
David Majda 12 years ago
parent 3b3798fa39
commit d02098eebe

@ -16,27 +16,14 @@ module.exports = {
generateJavascript: require("./compiler/passes/generate-javascript") generateJavascript: require("./compiler/passes/generate-javascript")
}, },
/*
* Names of passes that will get run during the compilation (in the specified
* order).
*/
appliedPassNames: [
"reportMissingRules",
"reportLeftRecursion",
"removeProxyRules",
"generateBytecode",
"generateJavascript"
],
/* /*
* Generates a parser from a specified grammar AST. Throws |PEG.GrammarError| * Generates a parser from a specified grammar AST. Throws |PEG.GrammarError|
* if the AST contains a semantic error. Note that not all errors are detected * if the AST contains a semantic error. Note that not all errors are detected
* during the generation and some may protrude to the generated parser and * during the generation and some may protrude to the generated parser and
* cause its malfunction. * cause its malfunction.
*/ */
compile: function(ast) { compile: function(ast, passes) {
var that = this, var options = arguments.length > 2 ? utils.clone(arguments[2]) : {};
options = arguments.length > 1 ? utils.clone(arguments[1]) : {};
utils.defaults(options, { utils.defaults(options, {
allowedStartRules: [ast.rules[0].name], allowedStartRules: [ast.rules[0].name],
@ -45,9 +32,7 @@ module.exports = {
output: "parser" output: "parser"
}); });
utils.each(this.appliedPassNames, function(passName) { utils.each(passes, function(p) { p(ast, options); });
that.passes[passName](ast, options);
});
switch (options.output) { switch (options.output) {
case "parser": return eval(ast.code); case "parser": return eval(ast.code);

@ -1,3 +1,5 @@
var utils = require("./utils");
module.exports = { module.exports = {
/* PEG.js version (uses semantic versioning). */ /* PEG.js version (uses semantic versioning). */
VERSION: "0.7.0", VERSION: "0.7.0",
@ -18,6 +20,10 @@ module.exports = {
* generated parser and cause its malfunction. * generated parser and cause its malfunction.
*/ */
buildParser: function(grammar, options) { buildParser: function(grammar, options) {
return this.compiler.compile(this.parser.parse(grammar), options); return this.compiler.compile(
this.parser.parse(grammar),
utils.values(this.compiler.passes),
options
);
} }
}; };

Loading…
Cancel
Save