Make parsers generated by /bin/pegjs CommonJS modules by default

redux
David Majda 14 years ago
parent 2d38c5cab3
commit a12a24fca1

@ -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.

@ -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

@ -52,16 +52,17 @@ function printHelp() {
print("Usage: pegjs [options] [--] <parser_var> [<input_file>] [<output_file>]");
print("");
print("Generates a parser from the PEG grammar specified in the <input_file> and");
print("writes it to the <output_file>. The parser object will be stored in a variable");
print("named <parser_var>.");
print("writes it to the <output_file>.");
print("");
print("If the <output_file> is omitted, its name is generated by changing the");
print("<input_file> extension to \".js\". If both <input_file> and <output_file> 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 <variable> 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");

Loading…
Cancel
Save