Merge pull request #481 from fatfisz/esm-support

Add support for ES modules
master
Futago-za Ryuu 7 years ago committed by GitHub
commit 6939d1eb8d

@ -100,8 +100,8 @@ You can tweak the generated parser with several options:
`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`, `commonjs`, `globals`, * `--format` — format of the generated parser: `amd`, `commonjs`, `es`,
`umd` (default: `commonjs`) `globals`, `umd` (default: `commonjs`)
* `--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
@ -142,14 +142,14 @@ object to `peg.generate`. The following options are supported:
`false`) `false`)
* `dependencies` — parser dependencies, the value is an object which maps * `dependencies` — parser dependencies, the value is an object which maps
variables used to access the dependencies in the parser to module IDs used variables used to access the dependencies in the parser to module IDs used
to load them; valid only when `format` is set to `"amd"`, `"commonjs"`, or to load them; valid only when `format` is set to `"amd"`, `"commonjs"`,
`"umd"` (default: `{}`) `"es"`, or `"umd"` (default: `{}`)
* `exportVar` — name of a global variable into which the parser object is * `exportVar` — name of a global variable into which the parser object is
assigned to when no module loader is detected; valid only when `format` is assigned to when no module loader is detected; valid only when `format` is
set to `"globals"` or `"umd"` (default: `null`) set to `"globals"` or `"umd"` (default: `null`)
* `format` — format of the generated parser (`"amd"`, `"bare"`, `"commonjs"`, * `format` — format of the generated parser (`"amd"`, `"bare"`, `"commonjs"`,
`"globals"`, or `"umd"`); valid only when `output` is set to `"source"` `"es"`, `"globals"`, or `"umd"`); valid only when `output` is set to
(default: `"bare"`) `"source"` (default: `"bare"`)
* `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"`)
* `output` — if set to `"parser"`, the method will return generated parser * `output` — if set to `"parser"`, the method will return generated parser

@ -30,8 +30,8 @@ function printHelp() {
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,"); console.log(" --format <format> format of the generated parser: amd, commonjs");
console.log(" commonjs, globals, umd (default: commonjs)"); console.log(" es, globals, umd (default: commonjs)");
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)");
@ -116,6 +116,9 @@ let options = {
trace: false trace: false
}; };
const MODULE_FORMATS = ["amd", "commonjs", "es", "globals", "umd"];
const MODULE_FORMATS_WITH_DEPS = ["amd", "commonjs", "es", "umd"];
while (args.length > 0 && isOption(args[0])) { while (args.length > 0 && isOption(args[0])) {
let json, id, mod; let json, id, mod;
@ -183,8 +186,8 @@ while (args.length > 0 && isOption(args[0])) {
if (args.length === 0) { if (args.length === 0) {
abort("Missing parameter of the --format option."); abort("Missing parameter of the --format option.");
} }
if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "globals" && args[0] !== "umd") { if (MODULE_FORMATS.indexOf(args[0]) === -1) {
abort("Module format must be one of \"amd\", \"commonjs\", \"globals\", and \"umd\"."); abort("Module format must be one of " + MODULE_FORMATS.map(format => `"${format}"`).join(", ") + ".");
} }
options.format = args[0]; options.format = args[0];
break; break;
@ -253,7 +256,7 @@ while (args.length > 0 && isOption(args[0])) {
} }
if (Object.keys(options.dependencies).length > 0) { if (Object.keys(options.dependencies).length > 0) {
if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "umd") { if (MODULE_FORMATS_WITH_DEPS.indexOf(options.format) === -1) {
abort("Can't use the -d/--dependency option with the \"" + options.format + "\" module format."); abort("Can't use the -d/--dependency option with the \"" + options.format + "\" module format.");
} }
} }

@ -1227,6 +1227,23 @@ function generateJS(ast, options) {
].join("\n"); ].join("\n");
} }
function generateParserExports() {
return options.trace
? [
"{",
" peg$SyntaxError as SyntaxError,",
" peg$DefaultTracer as DefaultTracer,",
" peg$parse as parse",
"}"
].join("\n")
: [
"{",
" peg$SyntaxError as SyntaxError,",
" peg$parse as parse",
"}"
].join("\n");
}
let generators = { let generators = {
bare() { bare() {
return [ return [
@ -1273,6 +1290,36 @@ function generateJS(ast, options) {
return parts.join("\n"); return parts.join("\n");
}, },
es() {
let parts = [];
let dependencyVars = Object.keys(options.dependencies);
parts.push(
generateGeneratedByComment(),
""
);
if (dependencyVars.length > 0) {
dependencyVars.forEach(variable => {
parts.push("import " + variable
+ " from \""
+ js.stringEscape(options.dependencies[variable])
+ "\";"
);
});
parts.push("");
}
parts.push(
toplevelCode,
"",
"export " + generateParserExports() + ";",
""
);
return parts.join("\n");
},
amd() { amd() {
let dependencyVars = Object.keys(options.dependencies); let dependencyVars = Object.keys(options.dependencies);
let dependencyIds = dependencyVars.map(v => options.dependencies[v]); let dependencyIds = dependencyVars.map(v => options.dependencies[v]);

Loading…
Cancel
Save