hegemonic / catharsis

A JavaScript parser for Google Closure Compiler and JSDoc type expressions.
MIT License
54 stars 14 forks source link

Rewrite Catharsis as a Pratt parser #54

Open hegemonic opened 6 years ago

hegemonic commented 6 years ago

(Disclaimer: I'll probably never actually do this.)

Catharsis's parser is currently generated by PEG.js from a parsing expression grammar. That seemed like a good choice when I made it. Over time, though, as I've implemented more and more undocumented things that Closure Compiler supports, the grammar has become quite brittle and difficult to change.

In the unlikely event that I'm ever so inclined, it would be interesting to try to reimplement Catharsis as a Pratt parser, which should be more flexible while still being reasonably straightforward to implement by hand.

brettz9 commented 4 years ago

FWIW, I see there is https://www.npmjs.com/package/pratt-parser

Btw, also regarding the project future, until such time as you may move to such a parser, are you still accepting PRs here? I'd like to add a routine for having the Unicode rules be auto-generated from the https://github.com/mathiasbynens/node-unicode-data for the latest Unicode support, and also to make use of much more succinct regex-like ranges as PegJS supports.

Also would be interested to know if you'd accept PRs to add support for a TypeScript mode to parse TS expressions...

simonseyock commented 3 years ago

@brettz9 because I saw this post and I was curious to learn more about pratt-parsers, I just wrote one that parses jsdoc types. I used the carthasis test suites to test my Parser and might use the jsdoctypeparser ones as well. So far it passes all catharsis tests but one in the main test/spec directory but not all of the subdirectories.

I looked at catharsis and at jsdoctypeparser for inspiration for the output. So far the results are pretty near to catharsis and I also wrote a catharsis compat mode - to be able to use the test suites directly. I might write one for jsdoctypeparser as well.

I uploaded the project to https://github.com/simonseyock/jsdoc-type-pratt-parser, but did not write any documentation yet. If this is interesting to you I am grateful for any input. Also I am willing to write some documentation XD

I did not think much about modes (closure, jsdoc, typescript) and any other functionality apart from parsing.

brettz9 commented 3 years ago

Excellent! I'd love to be able to use such an engine in eslint-plugin-jsdoc so that we can be compatible with jsdoc (or have a mode that is compatible), as well as support any closure-specific types and TypeScript types. (It does admittedly get hard to keep compatibility in some cases though, e.g., one doesn't want to hold back Unicode usage for the whole project if jsdoc won't be updating; but passing most or all of its tests should at least be a good sign).

So, yes, I'm very interested. Just have to give a warning that my health isn't great, so I can be flaky and unreliable at times, and at other times, if I have energy, may be able to plow forward for a time.

I've watched your repo, and if you get to adding some docs, hope you can file an alpha release or file an issue or something.