diff --git a/README.md b/README.md index 68b1f4c..4191d52 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/bin/pegjs b/bin/pegjs index fd3c586..a20aec8 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -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 select optimization for speed or size (default:"); util.puts(" speed)"); + util.puts(" --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();