pegjs/lib/peg.js

56 lines
1.5 KiB
JavaScript
Raw Normal View History

"use strict";
const GrammarError = require( "./grammar-error" );
2018-01-28 03:00:28 +01:00
const ast = require( "./ast" );
const compiler = require( "./compiler" );
const parser = require( "./parser" );
const util = require( "./util" );
const peg = {
// PEG.js version (uses semantic versioning).
2017-12-18 02:23:17 +01:00
VERSION: "0.11.0-dev",
GrammarError: GrammarError,
2018-01-28 03:00:28 +01:00
ast: ast,
parser: parser,
compiler: compiler,
util: util,
// 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 : {};
2018-01-30 03:38:49 +01:00
const session = new compiler.Session( {
2018-01-28 03:18:02 +01:00
passes: util.convertPasses( compiler.passes )
2018-01-30 03:38:49 +01:00
} );
2018-03-29 02:20:57 +02:00
if ( Array.isArray( options.plugins ) )
2018-03-29 02:20:57 +02:00
options.plugins.forEach( p => {
2018-03-29 02:20:57 +02:00
if ( typeof p.use !== "function" ) return;
p.use( session, options );
} );
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,
options
);
}
};
module.exports = peg;