-
Notifications
You must be signed in to change notification settings - Fork 65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How do you export parser Typescript types along with the parser? #187
Comments
I personally rolled back to |
thank you, did this as well. |
Actually, this is related to typescript. |
the way I'm currently approaching this is by hand-creating my AST types, and basically hand enforcing they line up with the output of the parser https://github.com/ShaderFrog/glsl-parser/blob/main/src/ast/node.ts And exporting a typescript definition file for the parser, which includes those nodes in the produced program https://github.com/ShaderFrog/glsl-parser/blob/main/src/parser/parser.d.ts This sucks in the sense that the type safety could be off if I don't manually line up the parser code with the typescript defs. Am I doing it wrong? I don't understand if there's a preferred method for doing this |
The problem is, it can't be solved on peggy side. If you do something like
types for AST are
but right hand sides are only possible to extract from TS code with some There's not many ways to fix it:
Neither thing is possible to do in peggy while keeping backwards compatibility. You're better off implementing own parser generator. The thing might looks something like this. |
I work around part of this issue by importing and re-exporting my parser object in typescript. Basically something like
I use |
here are a few options (assuming the output of peggy is
I just wrote a quick example of 3) here: https://gist.github.com/hildjj/0643e6b75d1ef6a83cf27531cdc3040f It is completely reasonable for you to want this to be easier, so I'm going to leave this open as a feature request. Will think about it after the upcoming release. |
One possible approach if someone takes this on:
|
I have been working on this issue, particularly the issues that @polkovnikov-ph brought up and I have a working prototype that automatically generates typescript types from a Peggy grammar. Source is here: https://github.com/siefkenj/peggy-to-ts Playground is here: https://siefkenj.github.io/peggy-to-ts I would be happy to modify this code as needed if the Peggy maintainers were interested in integrating it. |
@reverofevil this is a tangent, but: (this is ignoring the swath of languages that compile to JS but don't resemble it) |
I'm using peggy to generate a parser that I'm publishing to npm. The project the parser is in uses typescript.
I build the output file from peggy
./dist/parser.js
. In my project, I also have./parser.d.ts
, where I give the parser generated by peggy its type:I compile typescript also the
./dist
folder. My hope was that the parser.js from peggy and the parser.d.ts file from typescript would line up in the export.However, after an npm publish of package, there is no parser.d.ts. And on import of
package/dist/parser
in my other project, it complains of no types for the parser.I'm hoping this is a solved problem and I'm barking up the wrong tree.
The text was updated successfully, but these errors were encountered: