peggyjs / peggy

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

Add library mode #456

Closed hildjj closed 6 months ago

hildjj commented 7 months ago

This is an internal-only interface for calling into rules in other grammars.

This is a prerequisite for being able to import rules in grammars. The idea is:

I've got the start of the code that processes imports in another branch, but I wanted to have a discussion about this part first.

hildjj commented 7 months ago

I agree with you about passing a state object around. That might be cleaner, but probably a hair slower.

The other thing I don't like is setting up all of the constants each time a remote rule runs. A good JS runtime will probably make that not a big problem, but it's still hard to read.

I'd like to get the interface right here, we can always optimize the generated code better later. I'm going to land a few more changes on top of this before merging to see what happens.

hildjj commented 7 months ago

OK, I think this is pretty close -- it seems to work. I think this gets everything important in #292, plus several more things. @Mingun would you like me to use opcode 43 instead of 41?

frostburn commented 7 months ago

From the PR description:

Return an object that has the needed

The needed what?

hildjj commented 7 months ago

From the PR description:

Return an object that has the needed

The needed what?

sorry. Has the needed properties for parsing to continue on with the appropriate state.

hildjj commented 7 months ago

This still needs docs.

frostburn commented 7 months ago

Managed to import a rule from a package hosted on GitHub: https://github.com/frostburn/peggy-string-concat/blob/main/index.peggy

hildjj commented 7 months ago

Managed to import a rule from a package hosted on GitHub: https://github.com/frostburn/peggy-string-concat/blob/main/index.peggy

Did you check in those changes to peggy-string-concat? I was expecting to see an import statement at the top of https://github.com/frostburn/peggy-string-concat/blob/main/index.peggy which called to a compiled .js version of peggy-string.

frostburn commented 7 months ago

Managed to import a rule from a package hosted on GitHub: https://github.com/frostburn/peggy-string-concat/blob/main/index.peggy

Did you check in those changes to peggy-string-concat? I was expecting to see an import statement at the top of https://github.com/frostburn/peggy-string-concat/blob/main/index.peggy which called to a compiled .js version of peggy-string.

I'm not sure what you're asking. I changed my peggy dependencies in both repositories to hildjj:library-mode. In peggy-string there's now a module.exports at the end of the compiled file and in peggy-string-concat there's now var peg$import0 = require("peggy-string"); at the start of the compiled file.

hildjj commented 7 months ago

I'm not sure what you're asking.

I'm not either. After looking again and trying your code, it works great, exactly as expected. I appreciate your work!