| 1 |
"use strict"; |
| 2 |
|
| 3 |
var arrays = require("./utils/arrays"), |
| 4 |
objects = require("./utils/objects"); |
| 5 |
|
| 6 |
var peg = { |
| 7 |
|
| 8 |
VERSION: "0.10.0", |
| 9 |
|
| 10 |
GrammarError: require("./grammar-error"), |
| 11 |
parser: require("./parser"), |
| 12 |
compiler: require("./compiler"), |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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; |