Skip to content

Commit

Permalink
Fix outstanding issues
Browse files Browse the repository at this point in the history
  • Loading branch information
hildjj committed Dec 26, 2023
1 parent 4ef31a8 commit 5c819a0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 29 deletions.
16 changes: 4 additions & 12 deletions bin/peggy-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ class PeggyCLI extends Command {
this.outputFile = this.progOptions.output;
this.outputJS = this.progOptions.output;

if ((this.inputFiles.indexOf("-") !== -1) && this.argv.watch) {
if ((this.inputFiles.includes("-")) && this.argv.watch) {
this.argv.watch = false; // Make error throw.
this.error("Can't watch stdin");
}
Expand All @@ -300,7 +300,7 @@ class PeggyCLI extends Command {
if (this.inputFiles.indexOf("-") === -1) {
let inFile = this.inputFiles[0];
// You might just want to run a fragment grammar as-is,
// particularyly with a specified start rule.
// particularly with a specified start rule.
const m = inFile.match(/^npm:.*\/([^/]+)$/);
if (m) {
inFile = m[1];
Expand Down Expand Up @@ -586,11 +586,7 @@ class PeggyCLI extends Command {
const dirname = path.dirname(filename);
const m = new Module(filename, module);
// This is the function that will be called by `require()` in the parser.
m.require = (
// In node 12+, createRequire is documented.
// In node 10, createRequireFromPath is the least-undocumented approach.
Module.createRequire || Module.createRequireFromPath
)(filename);
m.require = Module.createRequire(filename);
const script = new vm.Script(source, { filename });
const exec = script.runInNewContext({
// Anything that is normally in the global scope that we think
Expand Down Expand Up @@ -653,11 +649,7 @@ class PeggyCLI extends Command {
this.std.in.resume();
input.text = await readStream(this.std.in);
} else if (source.startsWith("npm:")) {
const req = (
// In node 12+, createRequire is documented.
// In node 10, createRequireFromPath is the least-undocumented approach.
Module.createRequire || Module.createRequireFromPath
)(prevSource);
const req = Module.createRequire(prevSource);
prevSource = req.resolve(source.slice(4));
input.source = prevSource;
input.text = await fs.promises.readFile(prevSource, "utf8");
Expand Down
2 changes: 1 addition & 1 deletion bin/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Watcher extends EventEmitter {
return;
}
const filename = path.join(dir, fn);
// Might be a different fil changing in one of the target dirs
// Might be a different file changing in one of the target dirs
if (resolved.has(filename)) {
if (!this.timeout) {
fs.stat(filename, (er, stats) => {
Expand Down
15 changes: 10 additions & 5 deletions docs/documentation.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,16 @@ <h3 id="generating-a-parser-command-line">Command Line</h3>
are used.</p>

<p>If you specify multiple input files, they will be folded together in the
order specified before generating a parser. <code>import</code> statements in
the top-level initializers from each of the inputs will be moved to the top of
the generated code, and all other top-level initializers will be inserted
directly after those imports, in the order of the inputs. This approach can
be used to keep libraries of often-used grammar rules in separate files.</p>
order specified before generating a parser. If generating the "es" format,
<code>import</code> statements in the top-level initializers from each of the
inputs will be moved to the top of the generated code in reverse order of the
inputs, and all other top-level initializers will be inserted directly after
those imports, also in reverse order of the inputs. Note that because the
input JavaScript is parsed using a grammar very close to the ECMAscript
grammar, if import statements are found, all comments before the first
non-import statement will move with the import statement, which may be a
little surprising. This approach can be used to keep libraries of often-used
grammar rules in separate files.</p>

<p>By default, the generated parser is in the commonjs module format. You can
override this using the <code>--format</code> option.</p>
Expand Down
2 changes: 1 addition & 1 deletion docs/js/benchmark-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/js/test-bundle.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/vendor/peggy/peggy.min.js

Large diffs are not rendered by default.

12 changes: 6 additions & 6 deletions lib/compiler/asts.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ const visitor = require("./visitor");
* @returns {T | T[]}
*/
function combinePossibleArrays(a, b) {
// First might be an array, second will not. Either might be null.
if (!(a && b)) {
return a || b;
}
const aa = Array.isArray(a) ? a : [a];
const bb = Array.isArray(b) ? b : [b];

// Put library code above code that uses it, so that variables
// will be in scope.
const res = bb.filter(x => x).concat(aa.filter(x => x)); // Remove nulls.
return res;
aa.push(b);
return aa;
}

// AST utilities.
Expand Down
7 changes: 5 additions & 2 deletions lib/compiler/passes/generate-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -865,9 +865,12 @@ function generateJS(ast, options) {
codes.push(tli);
}
}
topLevel = imps.concat(codes);
// Imports go at the end so that when reversed, they end up in front.
topLevel = codes.concat(imps);
}
for (const tli of topLevel) {
// Put library code before code using it.
const reversed = [...topLevel].reverse();
for (const tli of reversed) {
parts.push(ast2SourceNode(tli));
parts.push("");
}
Expand Down

0 comments on commit 5c819a0

Please sign in to comment.