GerHobbelt / jison

bison / YACC / LEX in JavaScript (LALR(1), SLR(1), etc. lexer/parser generator)
https://gerhobbelt.github.io/jison/
MIT License
118 stars 20 forks source link

suggestion: typescript compilable to WASM #59

Closed ericprud closed 3 years ago

ericprud commented 3 years ago

cause it would go like stink. I would happy to pitch in; I'm motivated by some stuff that takes a minute to parse, which hampers user experience.

GerHobbelt commented 3 years ago

🤔 you want to compile typescript to WASM? But isn't that similar to saying you want to compile JavaScript to WASM, or am I reading this incorrectly?

(Aside: jison (jison-gho) is still on the backburner, but I'm getting a little time to fiddle with it again in a month or so, so might do another release if I get enough done. Taking on more would not be possible before 2021 though, I expect. 😢 )

ericprud commented 3 years ago

Because JavaScript isn't type-safe, the interpreter spends a lot of time testing types. You basically half to compile that work into a WASM target. AssemblyScript ("a strict variant of TypeScript" that makes typing non-optional)* is type-safe so the WASM target can dispense with that cruft.

The notion I had in mind was not to port Jison to TypeScript, though that could make some maintenance easier, but instead to write the generated parser and state tables in AssemblyScript -- basically a little printf manipulation. That could be used like any other javascript with typescript annotations (mild benefits: enforce type-safe invocation, IDE hints), or compiled to WASM.

I don't know how much of a performance increase that would buy; maybe a lot 'cause you're type-safe and closer to the metal, maybe not a lot 'cause consistent tables of integers are highly-optimized in JavaScript interpreters. At any rate, it would probably produce a smaller image in WASM. A way to test would be to hand-port some parser (I'd use ShEx and test with FHIR) and benchmark it in JavaScript and WASM.

Another option would be TurboScript, which is "based on" TypeScript without memory management (AFAIK) and a restricted set of types (which appear to line up well with what you need to build and navigate state tables and stacks). In principle, it could compile to C and JavaScript, as well as WASM, but that's not yet implemented, and the project seems to be on ice, so probably not a good source language.

MaxGraey commented 3 years ago

TurboScript was deprecated for a very long time.

GerHobbelt commented 3 years ago

I can see the usefulness of TypeScript for a project like this (doing it is another matter -- time and all that) but the jison kernel isn't WASMasble as it is (or so I guess): it's not just the tables, but also about error handling during lexing / parsing and there are callbacks, etc. there which I wouldn't know how to 'do' in WASM -- this is from reading through WASM introductions and a bit of spec scanning plus old asm.js stuff my brain picked up long time ago and distorted.

Anyway, if you want to have a look at viability, there's two main subjects to consider (at least for me):


Anyway, that's how jison-gho is "organized". Of course I'd love it if you would use jison-gho, but another odd thought of mine about WASM: wouldn't it be "easier" (for arbitrary levels of "easy" -- probably also non-trivial to get going, but anyway) if you instead took the C language route to WASM via GNU bison (and maybe flex, which is a DFA-based lexer instead of jison's regex-list based approach) and compile their output to WASM using llvm and emscriptem or what-is-the-process-for-that-these-days?

^^^ Just a rough thought, not hampered by any experience with this C-to-WASM stuff, so only riding what I recall of blogs I read about it over the years. 🤭

ericprud commented 3 years ago

Yeah, I think that compiling BISON output to WASM with LLVM would give you a pretty optimal executable. I was thinking more about the code path where lazy people (specifically, me) have existing js libraries that they want to shove into the WASM box.

Many thanks for the thorough analysis!

GerHobbelt commented 3 years ago

👍