2015-06-08 20:21:19 +02:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-25 20:19:42 +02:00
|
|
|
const GrammarError = require( "./grammar-error" );
|
2018-01-28 03:00:28 +01:00
|
|
|
const ast = require( "./ast" );
|
2017-10-25 20:19:42 +02:00
|
|
|
const compiler = require( "./compiler" );
|
|
|
|
const parser = require( "./parser" );
|
2018-01-14 21:44:53 +01:00
|
|
|
const util = require( "./util" );
|
2017-10-25 20:19:42 +02:00
|
|
|
|
|
|
|
const peg = {
|
|
|
|
// PEG.js version (uses semantic versioning).
|
2017-12-18 02:23:17 +01:00
|
|
|
VERSION: "0.11.0-dev",
|
2017-10-25 20:19:42 +02:00
|
|
|
|
|
|
|
GrammarError: GrammarError,
|
2018-01-28 03:00:28 +01:00
|
|
|
ast: ast,
|
2017-10-25 20:19:42 +02:00
|
|
|
parser: parser,
|
|
|
|
compiler: compiler,
|
2018-01-14 21:44:53 +01:00
|
|
|
util: util,
|
2017-10-25 20:19:42 +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.
|
|
|
|
generate( grammar, options ) {
|
|
|
|
|
|
|
|
options = typeof options !== "undefined" ? options : {};
|
|
|
|
|
|
|
|
const plugins = "plugins" in options ? options.plugins : [];
|
2018-01-30 03:38:49 +01:00
|
|
|
const session = new compiler.Session( {
|
|
|
|
grammar: grammar,
|
2018-01-28 03:18:02 +01:00
|
|
|
passes: util.convertPasses( compiler.passes )
|
2018-01-30 03:38:49 +01:00
|
|
|
} );
|
2017-10-25 20:19:42 +02:00
|
|
|
|
|
|
|
plugins.forEach( p => {
|
|
|
|
|
2018-01-30 03:38:49 +01:00
|
|
|
p.use( session, options );
|
2017-10-25 20:19:42 +02:00
|
|
|
|
|
|
|
} );
|
|
|
|
|
2018-01-28 03:18:02 +01:00
|
|
|
return compiler.compile(
|
2018-01-30 03:38:49 +01:00
|
|
|
session.parse( grammar, options.parser || {} ),
|
|
|
|
session,
|
2017-10-25 20:19:42 +02: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;
|