peggyjs / peggy

Peggy: Parser generator for JavaScript
https://peggyjs.org/
MIT License
936 stars 64 forks source link

Command line to generate the same output as the "online" version? #188

Open tomqwpl opened 3 years ago

tomqwpl commented 3 years ago

I'm trying to work out how to use the peggy command line to generate the same output as the online version. So far I've been unable.

It looks like the online version prepends the "module.exports = " bit on to the front of whatever it is that the parser generates, that bit isn't being generated by the parser generator itself. But even accounting for that, I can't find a set of command line options that make the rest get generated in the same way. "--format=globals" seems to generate the closest output that I've seen, but even this isn't quite right.

I'm not that expert in javascript, I'm just trying to set up automation of the build process rather than actually developing the parser, or the javascript that makes use of it, so forgive me if my question seems dumb. I've tried looking at the source code of the online version and the source code of the command line, but found neither particularly illuminating.

What gets you closes, is the "--format=globals" output, but the command line generates this:

// Generated by Peggy 1.2.0.
//
// https://peggyjs.org/
(function(root) {
  "use strict";
...
  root.null = {
    SyntaxError: peg$SyntaxError,
    parse: peg$parse
  };
})(this);

The online version generates:

module.exports = // Generated by Peggy 1.2.0.
//
// https://peggyjs.org/
(function() {
  "use strict";
...
  return {
    SyntaxError: peg$SyntaxError,
    parse: peg$parse
  };
})();

Thanks.

hildjj commented 3 years ago

The way that the online version does it is super-manual, here:

https://github.com/peggyjs/peggy/blob/05fcaff534ceb3d2072d7df0d4fc5f8570567999/docs/js/online.js#L200

So there's not currently a command line option that does exactly that. Depending on what you're trying to accomplish --format umd --export-var myParser might be useful. That will allow the .js file to be imported, required, or used with window.myParser in the browser.

No, I'm not sure why the online version does that, so let's keep this bug open for the online version to be normalized in some way.