The Simplest Parser Library (TSPL)
TSPL is the The Simplest Parser Library that works in Rust.
Concept
In pure functional languages like Haskell, a Parser can be represented as a function:
Parser<A> ::= String -> Reply<A, Error>
This allows us to implement a Monad instance for Parser<A>
, letting us use the do-notation
to
create simple and elegant parsers for our own types. Sadly, Rust doesn't have an equivalent. Yet,
we can easily emulate it by:
-
Using structs and
impl
to manage the cursor state internally. -
Returning a
Result
, which allows us to use Rust's?
to emulate monadic blocks.
This library merely exposes some functions to implement parsers that way, and nothing else.
Example
As an example, let's create a λ-Term parser using TSPL.
- Implement the type you want to create a parser for.
- Define your grammar. We'll use the following:
<term> ::= <lam> | <app> | <var>
<lam> ::= "λ" <name> " " <term>
<app> ::= "(" <term> " " <term> ")"
<var> ::= alphanumeric_string
- Create a new Parser with the
new_parser()!
macro.
TSPL new_parser!;
- Create an
impl TermParser
, with your grammar:
- Use your parser!
The complete example is available in ./examples/lambda_term.rs. Run it with:
cargo run --example lambda_term
Credit
This design is based on T6's new parser for HVM-Core, and is much cleaner than the old HOPA approach.