From e1af175af837df1fed8476865c3149bfeef36497 Mon Sep 17 00:00:00 2001 From: David Majda Date: Sun, 13 Jan 2013 12:06:29 +0100 Subject: [PATCH] Plugin API: Implement the --plugin option Implements part of GH-106. --- README.md | 2 ++ bin/pegjs | 22 +++++++++++++++++++++- 2 files changed, 23 insertions(+), 1 deletion(-) 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();