Plugin API: Implement the --plugin option

Implements part of GH-106.
redux
David Majda 12 years ago
parent e4b5588327
commit e1af175af8

@ -77,6 +77,8 @@ You can tweak the generated parser with several options:
time in pathological cases but making the parser slower
* `--allowed-start-rules` — comma-separated list of rules the parser will be
allowed to start parsing from (default: the first rule in the grammar)
* `--plugin` — makes PEG.js use a specified plugin (can be specified multiple
times)
### JavaScript API

@ -2,6 +2,7 @@
var util = require("util");
var fs = require("fs");
var path = require("path");
var PEG = require("../lib/peg");
/* Helpers */
@ -31,6 +32,8 @@ function printHelp() {
util.puts(" grammar)");
util.puts(" -o, --optimize <goal> select optimization for speed or size (default:");
util.puts(" speed)");
util.puts(" --plugin <plugin> use a specified plugin (can be specified");
util.puts(" multiple times)");
util.puts(" -v, --version print version information and exit");
util.puts(" -h, --help print help and exit");
}
@ -75,7 +78,8 @@ var exportVar = "module.exports";
var options = {
cache: false,
output: "source",
optimize: "speed"
optimize: "speed",
plugins: []
};
while (args.length > 0 && isOption(args[0])) {
@ -115,6 +119,22 @@ while (args.length > 0 && isOption(args[0])) {
options.optimize = args[0];
break;
case "--plugin":
nextArg();
if (args.length === 0) {
abort("Missing parameter of the --plugin option.");
}
var id = /^(\.\/|\.\.\/)/.test(args[0]) ? path.resolve(args[0]) : args[0];
try {
var module = require(id);
} catch (e) {
if (e.code !== "MODULE_NOT_FOUND") { throw e; }
abort("Can't load module \"" + id + "\".");
}
options.plugins.push(module);
break;
case "-v":
case "--version":
printVersion();

Loading…
Cancel
Save