this post was submitted on 06 Apr 2024
26 points (96.4% liked)

Rust Programming

8163 readers
2 users here now

founded 5 years ago
MODERATORS
 

Been working on this a few months. It's inspired by previous generations of parser generators, and by my own previous work generating ast lexers from grammar files. This integrates seamlessly with the type system, allowing you to declare your syntax, extract only the data you want as variables, and evaluate them easily. And just from a simple description of your syntax, you'll get beautiful errors which visually point out structures in the input.

With this I have been able to implement a (mostly complete) JSON parser in just 12 lines of parsing logic, and a pmdas-respecting expression parser in just 6 (with one helper function to apply individual operators).

Examples available on the github repo, also now available on crates.io!

top 4 comments
sorted by: hot top controversial new old
[–] Chaphasilor@feddit.nl 2 points 7 months ago (1 children)

So what, I can write a parser in JavaScript in a single line
\s

Jokes aside, that does look neat and like a really clever solution that is very fammiliar right from the start!

[–] redempt@lemmy.world 2 points 7 months ago

Thank you! It's hopefully intuitive to anyone who knows regex or BNF already

[–] fzz@programming.dev 1 points 4 months ago* (last edited 4 months ago) (1 children)

Looking to example code in the README I have to say that it is neat! ❤️‍🔥 But know what? Could be awesome to support char-literals in the parser! macro. Currently in that example str-literals used as single-char strings. I mean this for example:

num: num=<"-"? '0'-'9'+ …

Why there dash is str but not a char? Also what about escapes, unicode sequences and binary literals?

[–] redempt@lemmy.world 2 points 4 months ago

There's no real need for character literals. They would behave exactly the same as string literals but only support a single character. And you can use escape sequences in the string literal, of course.