diff --git a/README.md b/README.md index f18e9ac..221871f 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ To generate the parser from JavaScript code, include the `lib/compiler.js` file To generate the parser from a command line, you need to have Java installed (so that [Rhino](http://www.mozilla.org/rhino/) — which is included in PEG.js — can run). Use the `bin/pegjs` script on Unix or `bin/pegjs.bat` batch file on Windows: - $ bin/pegjs arithmeticsParser examples/arithmetics.pegjs + $ bin/pegjs --export-var arithmeticsParser examples/arithmetics.pegjs This command will create the parser from the `examples/arithmetics.pegjs` file and put in into the `examples/arithmetics.js` file. The parser object will be available in the `arithmeticsParser` global variable. To learn more about the generator usage, use the `--help` option. diff --git a/Rakefile b/Rakefile index 6179379..94e67de 100644 --- a/Rakefile +++ b/Rakefile @@ -27,7 +27,7 @@ def preprocess(input, base_dir) end file PARSER_OUT_FILE => PARSER_SRC_FILE do - system "#{PEGJS} PEG.parser #{PARSER_SRC_FILE} #{PARSER_OUT_FILE}" + system "#{PEGJS} --export-var PEG.parser #{PARSER_SRC_FILE} #{PARSER_OUT_FILE}" end file PEGJS_OUT_FILE => SRC_FILES do diff --git a/bin/pegjs-main.js b/bin/pegjs-main.js index 8f53c0d..36cd0ad 100644 --- a/bin/pegjs-main.js +++ b/bin/pegjs-main.js @@ -52,16 +52,17 @@ function printHelp() { print("Usage: pegjs [options] [--] [] []"); print(""); print("Generates a parser from the PEG grammar specified in the and"); - print("writes it to the . The parser object will be stored in a variable"); - print("named ."); + print("writes it to the ."); print(""); print("If the is omitted, its name is generated by changing the"); print(" extension to \".js\". If both and are"); print("omitted, standard input and output are used."); print(""); print("Options:"); - print(" -v, --version print version information and exit"); - print(" -h, --help print help and exit"); + print(" -e, --export-var name of the variable where the parser object"); + print(" will be stored (default: \"exports.parser\")"); + print(" -v, --version print version information and exit"); + print(" -h, --help print help and exit"); } function nextArg() { @@ -81,6 +82,9 @@ function abort(message) { exitFailure(); } +/* This makes the generated parser a CommonJS module by default. */ +var exportVar = "exports.parser"; + /* * The trimmed first argument is the script path -- see the beginning of this * file. @@ -89,6 +93,16 @@ var args = Array.prototype.slice.call(arguments, 1); while (args.length > 0 && isOption(args[0])) { switch (args[0]) { + case "-e": + case "--export-var": + nextArg(); + exportVar = args[0]; + break; + + case "--version": + printVersion(); + exitSuccess(); + break; case "-v": case "--version": printVersion(); @@ -111,12 +125,6 @@ while (args.length > 0 && isOption(args[0])) { nextArg(); } -if (args.length === 0) { - abort("Too few arguments."); -} -var parserVar = args[0]; -nextArg(); - switch (args.length) { case 0: var inputFile = FILE_STDIN; @@ -144,4 +152,4 @@ try { abort(e.message); } } -writeFile(outputFile, parserVar + " = " + parser.toSource() + ";\n"); +writeFile(outputFile, exportVar + " = " + parser.toSource() + ";\n");