From 75cd17ed58e413ddd453f011fd7b316fdbdbe235 Mon Sep 17 00:00:00 2001 From: David Majda Date: Thu, 14 Jul 2016 16:16:29 +0200 Subject: [PATCH] bin/pegjs: Implement the --format option --- README.md | 10 ++++++---- bin/pegjs | 34 ++++++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 51e0304..774ef3a 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,8 @@ $ pegjs -o arithmetics-parser.js arithmetics.pegjs If you omit both input and output file, standard input and output are used. -The generated parser is in the [UMD](https://github.com/umdjs/umd) format, which -means it works as a Node.js or AMD module. You can also use the -`-e`/`--export-var` option to define a global variable into which the parser -object is assigned to when no module loader is detected. +By default, the generated parser is in the Node.js module format. You can +override this using the `--format` option. You can tweak the generated parser with several options: @@ -96,10 +94,14 @@ You can tweak the generated parser with several options: time in pathological cases but making the parser slower * `--dependency` — makes the parser require a specified dependency (can be specified multiple times) + * `--export-var` — name of a global variable into which the parser object is + assigned to when no module loader is detected * `--extra-options` — additional options (in JSON format) to pass to `peg.generate` * `--extra-options-file` — file with additional options (in JSON format) to pass to `peg.generate` + * `--format` — format of the generated parser: `amd`, `global`, `node`, `umd` + (default: `node`) * `--optimize` — selects between optimizing the generated parser for parsing speed (`speed`) or code size (`size`) (default: `speed`) * `--plugin` — makes PEG.js use a specified plugin (can be specified multiple diff --git a/bin/pegjs b/bin/pegjs index 968b6fd..e1c045f 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -2,9 +2,10 @@ "use strict"; -var fs = require("fs"); -var path = require("path"); -var peg = require("../lib/peg"); +var fs = require("fs"); +var path = require("path"); +var peg = require("../lib/peg"); +var objects = require("../lib/utils/objects"); /* Helpers */ @@ -25,11 +26,13 @@ function printHelp() { console.log(" multiple times)"); console.log(" -e, --export-var name of a global variable into which the"); console.log(" parser object is assigned to when no module"); - console.log(" loader is detected (default: \"\")"); + console.log(" loader is detected"); console.log(" --extra-options additional options (in JSON format) to pass"); console.log(" to peg.generate"); console.log(" --extra-options-file file with additional options (in JSON"); console.log(" format) to pass to peg.generate"); + console.log(" --format format of the generated parser: amd, global,"); + console.log(" node, umd (default: node)"); console.log(" -h, --help print help and exit"); console.log(" -O, --optimize select optimization for speed or size"); console.log(" (default: speed)"); @@ -178,6 +181,17 @@ while (args.length > 0 && isOption(args[0])) { addExtraOptions(options, json); break; + case "--format": + nextArg(); + if (args.length === 0) { + abort("Missing parameter of the --format option."); + } + if (args[0] !== "amd" && args[0] !== "global" && args[0] !== "node" && args[0] !== "umd") { + abort("Module format must be one of \"amd\", \"global\", \"node\", and \"umd\"."); + } + options.format = args[0]; + break; + case "-h": case "--help": printHelp(); @@ -242,6 +256,18 @@ while (args.length > 0 && isOption(args[0])) { nextArg(); } +if (objects.keys(options.dependencies).length > 0) { + if (options.format !== "amd" && options.format !== "node" && options.format !== "umd") { + abort("Can't use the -d/--dependency option with the \"" + options.format + "\" module format."); + } +} + +if (options.exportVar !== null) { + if (options.format !== "global" && options.format !== "umd") { + abort("Can't use the -e/--export-var option with the \"" + options.format + "\" module format."); + } +} + var inputStream; var outputStream;