I’ve recently used NimbleParsec for parsing a programming language for a game/challenge, just for fun. It might not have been the best solution, but it was pretty good at getting the job done!
That got me wondering if anyone else has used NimbleParsec for parsing a programming language. And if so, how did you handle pretty-printing parsing errors? (I didn’t handle at all haha)
I’d love to see some examples to learn from them – and perhaps even add some examples to the lib’s documentation.
Also, if you know a more adequate lexer/parser for parsing programming langs in Elixir, please do let me know. I’d love some recommendations.
Now try and use lex/yacc from OTP, and you can write your own programming language that runs on OTP
I haven’t used it for parsing an existing language but I’ve used it to parse DSLs. For example, I wrote a schema-less JSON document API on top of Elasticsearch and wrote a query language using NimbleParsec.
Can you provide any more detail about what you mean by pretty-printing exactly? I’ll provide some personal experience here though: I’ve found piping the result of
NimbleParsec.wrap_and_tag/3 to the
NimbleParsec.label/3 function is really useful for improving error messages. In combination with the line+column integers in the error tuple returned by
defparsec-defined functions, I’ve managed to produce concise, easy-to-debug error messages
I’m unable to upload images, but here’s the link to screen shot of a “pretty-printed” error message Screenshot-2023-09-14-at-18-07-34 hosted at ImgBB — ImgBB
I think “prettry-print” wasn’t my best choice of wording there, I meant more like being able to provide fine grained error messages as one would expect from a compiler’s parser. using
label/3 didn’t seem much helpful there (probably mb for misusing it).
In case it interests you, here’s the repo I’m working on: GitHub - rwillians/rinha-de-compilers--elixir-transcompiler: Source-to-Source Transcompiler from `.rinha` to Elixir. Runs on ERTS (Erlang Runtime System).
It’s a “source-to-source transcompiler” (fancy wording – I’m parsing from another lang, transpiling the ast to elixir then compiling). It’s just for fun, for a challenge.
Nice! I wasn’t aware of those, I’ll look into it.
To slightly correct @D4no0 it’s