2013-01-11 20:35:18 +01:00
|
|
|
var utils = require("./utils");
|
|
|
|
|
2012-11-10 09:47:22 +01:00
|
|
|
module.exports = {
|
2012-04-16 15:35:54 +02:00
|
|
|
/* PEG.js version (uses semantic versioning). */
|
2013-12-24 08:24:35 +01:00
|
|
|
VERSION: "0.8.0",
|
2010-11-14 17:11:36 +01:00
|
|
|
|
2012-11-10 09:47:22 +01:00
|
|
|
GrammarError: require("./grammar-error"),
|
|
|
|
parser: require("./parser"),
|
|
|
|
compiler: require("./compiler"),
|
|
|
|
|
2010-07-25 17:54:09 +02:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-01-11 20:55:33 +01:00
|
|
|
buildParser: function(grammar) {
|
2013-01-13 11:17:44 +01:00
|
|
|
function convertPasses(passes) {
|
|
|
|
var converted = {}, stage;
|
|
|
|
|
|
|
|
for (stage in passes) {
|
2013-12-14 21:46:47 +01:00
|
|
|
if (passes.hasOwnProperty(stage)) {
|
|
|
|
converted[stage] = utils.values(passes[stage]);
|
|
|
|
}
|
2013-01-13 11:17:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
2013-01-11 20:55:33 +01:00
|
|
|
var options = arguments.length > 1 ? utils.clone(arguments[1]) : {},
|
|
|
|
plugins = "plugins" in options ? options.plugins : [],
|
|
|
|
config = {
|
|
|
|
parser: this.parser,
|
2013-01-13 11:17:44 +01:00
|
|
|
passes: convertPasses(this.compiler.passes)
|
2013-01-11 20:55:33 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
utils.each(plugins, function(p) { p.use(config, options); });
|
|
|
|
|
2013-01-11 20:35:18 +01:00
|
|
|
return this.compiler.compile(
|
2013-01-11 20:55:33 +01:00
|
|
|
config.parser.parse(grammar),
|
|
|
|
config.passes,
|
2013-01-11 20:35:18 +01:00
|
|
|
options
|
|
|
|
);
|
2010-07-25 17:54:09 +02:00
|
|
|
}
|
|
|
|
};
|