bin/pegjs: Implement the --format option

redux
David Majda 8 years ago
parent d83e4d5a48
commit 75cd17ed58

@ -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. 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 By default, the generated parser is in the Node.js module format. You can
means it works as a Node.js or AMD module. You can also use the override this using the `--format` option.
`-e`/`--export-var` option to define a global variable into which the parser
object is assigned to when no module loader is detected.
You can tweak the generated parser with several options: 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 time in pathological cases but making the parser slower
* `--dependency` — makes the parser require a specified dependency (can be * `--dependency` — makes the parser require a specified dependency (can be
specified multiple times) 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 * `--extra-options` — additional options (in JSON format) to pass to
`peg.generate` `peg.generate`
* `--extra-options-file` — file with additional options (in JSON format) to * `--extra-options-file` — file with additional options (in JSON format) to
pass to `peg.generate` 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 * `--optimize` — selects between optimizing the generated parser for parsing
speed (`speed`) or code size (`size`) (default: `speed`) speed (`speed`) or code size (`size`) (default: `speed`)
* `--plugin` — makes PEG.js use a specified plugin (can be specified multiple * `--plugin` — makes PEG.js use a specified plugin (can be specified multiple

@ -5,6 +5,7 @@
var fs = require("fs"); var fs = require("fs");
var path = require("path"); var path = require("path");
var peg = require("../lib/peg"); var peg = require("../lib/peg");
var objects = require("../lib/utils/objects");
/* Helpers */ /* Helpers */
@ -25,11 +26,13 @@ function printHelp() {
console.log(" multiple times)"); console.log(" multiple times)");
console.log(" -e, --export-var <variable> name of a global variable into which the"); console.log(" -e, --export-var <variable> name of a global variable into which the");
console.log(" parser object is assigned to when no module"); 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 <options> additional options (in JSON format) to pass"); console.log(" --extra-options <options> additional options (in JSON format) to pass");
console.log(" to peg.generate"); console.log(" to peg.generate");
console.log(" --extra-options-file <file> file with additional options (in JSON"); console.log(" --extra-options-file <file> file with additional options (in JSON");
console.log(" format) to pass to peg.generate"); console.log(" format) to pass to peg.generate");
console.log(" --format <format> format of the generated parser: amd, global,");
console.log(" node, umd (default: node)");
console.log(" -h, --help print help and exit"); console.log(" -h, --help print help and exit");
console.log(" -O, --optimize <goal> select optimization for speed or size"); console.log(" -O, --optimize <goal> select optimization for speed or size");
console.log(" (default: speed)"); console.log(" (default: speed)");
@ -178,6 +181,17 @@ while (args.length > 0 && isOption(args[0])) {
addExtraOptions(options, json); addExtraOptions(options, json);
break; 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 "-h":
case "--help": case "--help":
printHelp(); printHelp();
@ -242,6 +256,18 @@ while (args.length > 0 && isOption(args[0])) {
nextArg(); 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 inputStream;
var outputStream; var outputStream;

Loading…
Cancel
Save