#dsl #language #object-oriented #parser #string

quoth

Quoth is a scannerless (no-lexing), developer-friendly parsing library for implementing DSLs and syntax parsers in Rust

9 releases

0.1.7 Jun 21, 2024
0.1.6 Apr 4, 2024
0.1.2 Mar 27, 2024
0.0.1 Aug 21, 2023

#465 in Rust patterns

Download history 82/week @ 2024-07-23 216/week @ 2024-07-30 203/week @ 2024-08-06 92/week @ 2024-08-13 317/week @ 2024-08-20 192/week @ 2024-08-27 25/week @ 2024-09-03 88/week @ 2024-09-10 100/week @ 2024-09-17 169/week @ 2024-09-24 125/week @ 2024-10-01 110/week @ 2024-10-08 120/week @ 2024-10-15 121/week @ 2024-10-22 132/week @ 2024-10-29 67/week @ 2024-11-05

490 downloads per month
Used in 3 crates (via currencies-core)

MIT license

195KB
1.5K SLoC

Quoth

quoth raven

Crates.io docs.rs Build Status MIT License

Quoth is a scannerless parsing library (meaning there is no lexing/tokenization step) specifically designed for implementing languages and DSLs (domain specific languages) in Rust. It is based on the admittedly dtolnayian idea from syn that everything should implement the same Parse trait, however quoth takes this idea further to the point where lexing is no longer necessary, and what you are left with is something akin to "Object Oriented Parsing" where it is quite easy to compose, combine, parse, and even "unparse" Parsables in a myriad of ways.

Parsing

In quoth, everything implements Parsable, which brings with it a large set of requirements (and thus features) that are at best conventions in other parsing ecosystems. Some core features of quoth include:

  • anything that can be parsed with quoth can also be "unparsed" i.e. converted back to a string
  • because there is no tokenization step, the unmodified Span source text for any Parsable is always available and is cheap/free to access at any time during parsing
  • Span itself is very lightweight and is just a reference-counted string slice into a Source
  • because of this, ParseStream is also incredibly lightweight and provides normally expensive operations like forking virtually for free
  • in quoth, you can peek by Parsable type, but you can also peek by value, and even by regex
  • branching and ambiguity are much easier to deal with in quoth because forking is cheap and encouraged. This is a double-edged sword because it means you can efficiently parse ambiguous things that are normally inefficient to parse and hard to reason about, but now it is much easier to introduce ambiguity into your underlying grammar.

Peeking

Quoth also takes special care to make anything that is Parsable also automatically Peekable, making it even easier to implement Parsable. Furthermore, Peekable itself allows peeking on both a type-basis, and on a specific value basis. For example, you could peek "is an Ident next?" or "is an Ident with this specific value next?".

More information and docs will be coming in the next release

Dependencies

~3–4.5MB
~86K SLoC