pegjs/lib/peg.js
David Majda e4b5588327 Plugin API: Split compiler passes into stages
The compiler passes are now split into three stages:

  * check -- passes that check for various error conditions

  * transform -- passes that transform the AST (e.g. to perform
    optimizations)

  * generate -- passes that are related to code generation

Splitting the passes into stages is important for plugins. For example,
if a plugin wants to add a new optimization pass, it can add it at the
end of the "transform" stage without any knowledge about other passes it
contains. Similarly, if it wants to generate something else than the
default code generator does from the AST, it can just replace all passes
in the "generate" stage by its own one(s).

More generally, the stages make it possible to write plugins that do not
depend on names and actions of specific passes (which I consider
internal and subject of change), just on the definition of stages (which
I consider a public API with to which semver rules apply).

Implements part of GH-106.
2013-01-13 19:08:06 +01:00

49 lines
1.4 KiB
JavaScript

var utils = require("./utils");
module.exports = {
/* PEG.js version (uses semantic versioning). */
VERSION: "0.7.0",
GrammarError: require("./grammar-error"),
parser: require("./parser"),
compiler: require("./compiler"),
/*
* Generates a parser from a specified grammar and returns it.
*
* The grammar must be a string in the format described by the metagramar in
* the parser.pegjs file.
*
* Throws |PEG.parser.SyntaxError| if the grammar contains a syntax error or
* |PEG.GrammarError| if it contains a semantic error. Note that not all
* errors are detected during the generation and some may protrude to the
* generated parser and cause its malfunction.
*/
buildParser: function(grammar) {
function convertPasses(passes) {
var converted = {}, stage;
for (stage in passes) {
converted[stage] = utils.values(passes[stage]);
}
return converted;
}
var options = arguments.length > 1 ? utils.clone(arguments[1]) : {},
plugins = "plugins" in options ? options.plugins : [],
config = {
parser: this.parser,
passes: convertPasses(this.compiler.passes)
};
utils.each(plugins, function(p) { p.use(config, options); });
return this.compiler.compile(
config.parser.parse(grammar),
config.passes,
options
);
}
};