Context free language parser
This crate provides functionality for parsing context-free languages and was written for use with fck. The documentation will also link you to some benchmarks. These were performed on a MacBook Pro with an M1 Pro CPU. The times are mainly intended for comparisons.
Usage
This crate provides the rule!, rule_no_types!, and #[parser] macros to enable parsing context-free languages. The macros are quite powerful and can become complicated quickly, so we recommend you have a look at the documentation.
The crate also provides the Parser trait and Error struct returned when the Parser::parse function encounters an error.
Trait impl requirements
There are three trait requirements for the two types which we'll call T for the input type (the input iterator into the parse functions has type F: Iter<Item=&T>) and E for the comparison type:
E: PartialEq<E>&T: PartialEq<E>
The saved values in the resulting structs must also implement the derived traits given to the macro. For example, you can derive Copy if you store a u8 but not a String.
Examples
The examples directory contains some example files with generated expansions. These are generated using cargo-expand without the expansion of the derived traits.
All the examples have has some manual formatting of line breaks and some comments to indicate which section is currently being parser.
The structure of the examples is the same for all of them:
- Two enums for
TokenTypeandTokenalong with required trait impls - Some macro call or calls. There are comments to explain what things are roughly
mod equivalent(when not duplicated) which has the equivalent code generated by the macro
Current issues
- Errors are a bit rudimentary at the moment. If you consider the rule
(Token::T1)?, Token::T2, the first token could be eitherToken::T1orToken::T2. If neither of thee are found then the returned error will say it expectedToken::T2. A future version wil have better errors where theexpectedis aVec<E>that will be calculated for each possible error position for more useful errors