Debian Codegraph @

lib/peg.js

raw symbols SCIP
1 "use strict";
2
3 var arrays = require("./utils/arrays"),
4 objects = require("./utils/objects");
5
6 var peg = {
7 /* PEG.js version (uses semantic versioning). */
8 VERSION: "0.10.0",
9
10 GrammarError: require("./grammar-error"),
11 parser: require("./parser"),
12 compiler: require("./compiler"),
13
14 /*
15 * Generates a parser from a specified grammar and returns it.
16 *
17 * The grammar must be a string in the format described by the metagramar in
18 * the parser.pegjs file.
19 *
20 * Throws |peg.parser.SyntaxError| if the grammar contains a syntax error or
21 * |peg.GrammarError| if it contains a semantic error. Note that not all
22 * errors are detected during the generation and some may protrude to the
23 * generated parser and cause its malfunction.
24 */
25 generate: function(grammar, options) {
26 options = options !== void 0 ? options : {};
27
28 function convertPasses(passes) {
29 var converted = {}, stage;
30
31 for (stage in passes) {
32 if (passes.hasOwnProperty(stage)) {
33 converted[stage] = objects.values(passes[stage]);
34 }
35 }
36
37 return converted;
38 }
39
40 options = objects.clone(options);
41
42 var plugins = "plugins" in options ? options.plugins : [],
43 config = {
44 parser: peg.parser,
45 passes: convertPasses(peg.compiler.passes)
46 };
47
48 arrays.each(plugins, function(p) { p.use(config, options); });
49
50 return peg.compiler.compile(
51 config.parser.parse(grammar),
52 config.passes,
53 options
54 );
55 }
56 };
57
58 module.exports = peg;