skvadrik / re2c

Lexer generator for C, C++, Go and Rust.
https://re2c.org
Other
1.07k stars 169 forks source link

Javascript Support #420

Open F35H opened 1 year ago

F35H commented 1 year ago

I plan on using this for use with Javascript. The syntax is so similar it's worth using the same tool.

With --loop-switch it's almost perfect* - the string obviously can't be a pointer though.

The other settings can be used easily as well replacing goto with a break or continue.

I might as well leave an issue here for the future.

*Many times I speak mid-idea. Likely the most efficient for anything is bit-vectors, but this will always be contextual and dependent on context.

**Hilarious, but because of the way continue statements work in javascript, --loop-switch is probably the only one viable right now without copious amounts of work in comparison.

skvadrik commented 1 year ago

Hi @F35H , thanks for the heads up. There is an idea to add syntax files (user-supplied configs) that will make it easier to use re2c with different language backends. This is not implemented yet, but I'm slowly rewriting codegen subsystem to make it possible.

skvadrik commented 1 year ago

There is no need for a string to be a byte stream. The details can be encapsulated behind generic API and you can provide suitable definitions for YYPEEK, YYSKIP and other primitives using the string type and index operator.

F35H commented 1 year ago

I got it to work appropriately [and viably].

The main thing that makes it difficult for javascript [beyond things such as types, pointers, etc. ] is that continue statements only work with labels that are defined above them only work in loops. It essentially makes it where --loop-switch is the only real option without having to over-analyze each case.

Other than that, it's just a matter of replacing variables where appropriate and organizing "blocks" into functions. Cntrl-H is absolutely your friend [although, now that I've tried it, free-form style with YYPEEK, YYSKIP, etc. is also clearly your friend].

skvadrik commented 2 months ago

Experimental JS support was added on syntax-files branch: https://github.com/skvadrik/re2c/commit/74ace0811c34afed2ad0d087fed6e78b1fa6b4c8. See https://github.com/skvadrik/re2c/tree/syntax-files/examples/js for examples (they use nodejs as the examples run in a non-browser environment).