From aab928de911bf0cac7cafdb5284e80c56d4703b6 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sat, 10 Dec 2016 14:52:15 +0100 Subject: [PATCH 1/5] Add support for ES modules --- bin/pegjs | 6 ++-- lib/compiler/passes/generate-js.js | 47 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/bin/pegjs b/bin/pegjs index e75de42..9c789a1 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -183,8 +183,8 @@ while (args.length > 0 && isOption(args[0])) { if (args.length === 0) { abort("Missing parameter of the --format option."); } - if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "globals" && args[0] !== "umd") { - abort("Module format must be one of \"amd\", \"commonjs\", \"globals\", and \"umd\"."); + if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "esm" && args[0] !== "globals" && args[0] !== "umd") { + abort("Module format must be one of \"amd\", \"commonjs\", \"esm\", \"globals\", and \"umd\"."); } options.format = args[0]; break; @@ -253,7 +253,7 @@ while (args.length > 0 && isOption(args[0])) { } if (Object.keys(options.dependencies).length > 0) { - if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "umd") { + if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "esm" && options.format !== "umd") { abort("Can't use the -d/--dependency option with the \"" + options.format + "\" module format."); } } diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index 4dd0295..a54494b 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1227,6 +1227,23 @@ function generateJS(ast, options) { ].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 = { bare() { return [ @@ -1273,6 +1290,36 @@ function generateJS(ast, options) { return parts.join("\n"); }, + esm() { + 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() { let dependencyVars = Object.keys(options.dependencies); let dependencyIds = dependencyVars.map(v => options.dependencies[v]); From e3b7f0c3a94b7cb6b6ed5ac9299f12721940e952 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Mon, 26 Dec 2016 14:08:24 +0100 Subject: [PATCH 2/5] Change "esm" to "es" --- bin/pegjs | 6 +++--- lib/compiler/passes/generate-js.js | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/pegjs b/bin/pegjs index 9c789a1..d097186 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -183,8 +183,8 @@ while (args.length > 0 && isOption(args[0])) { if (args.length === 0) { abort("Missing parameter of the --format option."); } - if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "esm" && args[0] !== "globals" && args[0] !== "umd") { - abort("Module format must be one of \"amd\", \"commonjs\", \"esm\", \"globals\", and \"umd\"."); + if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "es" && args[0] !== "globals" && args[0] !== "umd") { + abort("Module format must be one of \"amd\", \"commonjs\", \"es\", \"globals\", and \"umd\"."); } options.format = args[0]; break; @@ -253,7 +253,7 @@ while (args.length > 0 && isOption(args[0])) { } if (Object.keys(options.dependencies).length > 0) { - if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "esm" && options.format !== "umd") { + if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "es" && options.format !== "umd") { abort("Can't use the -d/--dependency option with the \"" + options.format + "\" module format."); } } diff --git a/lib/compiler/passes/generate-js.js b/lib/compiler/passes/generate-js.js index a54494b..4c80939 100644 --- a/lib/compiler/passes/generate-js.js +++ b/lib/compiler/passes/generate-js.js @@ -1290,7 +1290,7 @@ function generateJS(ast, options) { return parts.join("\n"); }, - esm() { + es() { let parts = []; let dependencyVars = Object.keys(options.dependencies); From c541911c04d79c86500206fbe9db40e60e5c887a Mon Sep 17 00:00:00 2001 From: fatfisz Date: Mon, 26 Dec 2016 14:16:24 +0100 Subject: [PATCH 3/5] Extract formats into array Previously there was a very long condition. --- bin/pegjs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/bin/pegjs b/bin/pegjs index d097186..2b434c9 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -116,6 +116,9 @@ let options = { 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])) { let json, id, mod; @@ -183,8 +186,8 @@ while (args.length > 0 && isOption(args[0])) { if (args.length === 0) { abort("Missing parameter of the --format option."); } - if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "es" && args[0] !== "globals" && args[0] !== "umd") { - abort("Module format must be one of \"amd\", \"commonjs\", \"es\", \"globals\", and \"umd\"."); + if (MODULE_FORMATS.indexOf(args[0]) === -1) { + abort("Module format must be one of " + MODULE_FORMATS.map(format => `"${format}"`).join(", ") + "."); } options.format = args[0]; break; @@ -253,7 +256,7 @@ while (args.length > 0 && isOption(args[0])) { } if (Object.keys(options.dependencies).length > 0) { - if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "es" && 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."); } } From d1f62596f15042bb835880a4e70b4cbbcef12a16 Mon Sep 17 00:00:00 2001 From: fatfisz Date: Mon, 26 Dec 2016 14:20:23 +0100 Subject: [PATCH 4/5] Add info about the "es" target to the README --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index eec13e4..17ea78a 100644 --- a/README.md +++ b/README.md @@ -100,8 +100,8 @@ You can tweak the generated parser with several options: `peg.generate` * `--extra-options-file` — file with additional options (in JSON format) to pass to `peg.generate` - * `--format` — format of the generated parser: `amd`, `commonjs`, `globals`, - `umd` (default: `commonjs`) + * `--format` — format of the generated parser: `amd`, `commonjs`, `es`, + `globals`, `umd` (default: `commonjs`) * `--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 @@ -142,14 +142,14 @@ object to `peg.generate`. The following options are supported: `false`) * `dependencies` — parser dependencies, the value is an object which maps 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 - `"umd"` (default: `{}`) + to load them; valid only when `format` is set to `"amd"`, `"commonjs"`, + `"es"`, or `"umd"` (default: `{}`) * `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 set to `"globals"` or `"umd"` (default: `null`) * `format` — format of the generated parser (`"amd"`, `"bare"`, `"commonjs"`, - `"globals"`, or `"umd"`); valid only when `output` is set to `"source"` - (default: `"bare"`) + `"es"`, `"globals"`, or `"umd"`); valid only when `output` is set to + `"source"` (default: `"bare"`) * `optimize`— selects between optimizing the generated parser for parsing speed (`"speed"`) or code size (`"size"`) (default: `"speed"`) * `output` — if set to `"parser"`, the method will return generated parser From 9c60380f8696fd971e9452e9ebe548f13301e55e Mon Sep 17 00:00:00 2001 From: fatfisz Date: Sun, 11 Jun 2017 19:03:33 +0200 Subject: [PATCH 5/5] Add info about es to the cmd tool --- bin/pegjs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/pegjs b/bin/pegjs index 2b434c9..fd8a04d 100755 --- a/bin/pegjs +++ b/bin/pegjs @@ -30,8 +30,8 @@ function printHelp() { 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,"); - console.log(" commonjs, globals, umd (default: commonjs)"); + console.log(" --format format of the generated parser: amd, commonjs"); + console.log(" es, globals, umd (default: commonjs)"); console.log(" -h, --help print help and exit"); console.log(" -O, --optimize select optimization for speed or size"); console.log(" (default: speed)");