Browse Source

peg.generate: Implement { format: "node" }

redux
David Majda 5 years ago
parent
commit
f633f697c9
  1. 7
      README.md
  2. 35
      lib/compiler/passes/generate-js.js

7
README.md

@ -140,12 +140,13 @@ 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 `"umd"` (default: `{}`)
to load them; valid only when `format` is set to `"node"` 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 `"umd"` (default: `null`)
* `format` — format of the genreated parser (`"bare"` or `"umd"`); valid only
when `output` is set to `"source"` (default: `"bare"`)
* `format` — format of the genreated parser (`"bare"`, `"node"`, 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

35
lib/compiler/passes/generate-js.js

@ -1267,6 +1267,41 @@ function generateJS(ast, options) {
].join('\n');
},
node: function() {
var parts = [],
dependencyVars = objects.keys(options.dependencies),
requires = arrays.map(
dependencyVars,
function(variable) {
return variable
+ ' = require("'
+ js.stringEscape(options.dependencies[variable])
+ '")';
}
);
parts.push([
generateGeneratedByComment(),
'',
'"use strict";',
''
].join('\n'));
if (requires.length > 0) {
parts.push('var ' + requires.join(', ') + ';');
parts.push('');
}
parts.push([
toplevelCode,
'',
'module.exports = ' + generateParserObject() + ';',
''
].join('\n'));
return parts.join('\n');
},
umd: function() {
var parts = [],
dependencyIds = objects.values(options.dependencies),

Loading…
Cancel
Save