2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2016-10-10 13:38:39 +02:00
|
|
|
let GrammarError = require("./grammar-error");
|
|
|
|
let compiler = require("./compiler");
|
|
|
|
let parser = require("./parser");
|
|
|
|
|
2016-09-08 16:04:36 +02:00
|
|
|
let peg = {
|
2016-09-17 16:28:28 +02:00
|
|
|
// PEG.js version (uses semantic versioning).
|
2016-08-18 14:33:46 +02:00
|
|
|
VERSION: "0.10.0",
|
2010-11-14 17:11:36 +01:00
|
|
|
|
2016-10-10 13:38:39 +02:00
|
|
|
GrammarError: GrammarError,
|
|
|
|
parser: parser,
|
|
|
|
compiler: compiler,
|
2012-11-10 09:47:22 +01:00
|
|
|
|
2016-09-17 16:28:28 +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.
|
2016-10-07 17:03:10 +02:00
|
|
|
generate(grammar, options) {
|
2016-09-01 14:12:16 +02:00
|
|
|
options = options !== undefined ? options : {};
|
2016-03-18 16:39:19 +01:00
|
|
|
|
2013-01-13 11:17:44 +01:00
|
|
|
function convertPasses(passes) {
|
2016-09-09 11:43:06 +02:00
|
|
|
let converted = {};
|
2013-01-13 11:17:44 +01:00
|
|
|
|
2016-09-14 16:08:14 +02:00
|
|
|
Object.keys(passes).forEach(stage => {
|
|
|
|
converted[stage] = Object.keys(passes[stage])
|
|
|
|
.map(name => passes[stage][name]);
|
|
|
|
});
|
2013-01-13 11:17:44 +01:00
|
|
|
|
|
|
|
return converted;
|
|
|
|
}
|
|
|
|
|
2016-09-17 15:08:47 +02:00
|
|
|
let plugins = "plugins" in options ? options.plugins : [];
|
2016-09-22 09:25:31 +02:00
|
|
|
let config = {
|
2016-10-04 11:00:25 +02:00
|
|
|
parser: peg.parser,
|
|
|
|
passes: convertPasses(peg.compiler.passes)
|
|
|
|
};
|
2013-01-11 20:55:33 +01:00
|
|
|
|
2016-09-09 13:27:24 +02:00
|
|
|
plugins.forEach(p => { p.use(config, options); });
|
2013-01-11 20:55:33 +01:00
|
|
|
|
2016-07-04 07:56:08 +02:00
|
|
|
return peg.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
|
|
|
}
|
|
|
|
};
|
2014-05-04 14:11:44 +02:00
|
|
|
|
2016-05-04 12:37:13 +02:00
|
|
|
module.exports = peg;
|