pub trait LemonTree {
type Parser;
type Token;
}Expand description
Implemented for the parser’s start symbol — the type that a successful parse produces.
You don’t implement this trait by hand: annotate a struct or enum with #[derive(LemonTree)]
and the implementation, together with a get_parser constructor, is generated for you. This
derive must be the last parser attribute in the file, because it is what triggers parser
generation (see the crate-level documentation for the full picture).
The implementation provides two associated types:
Parser— the parser, which accepts tokens and finally returns the start symbol.Token— an enum whose variants are all the terminal symbols (tokens) that appear anywhere in your grammar (in#[lem()]and#[lem_fn()]attributes).
If you annotate a struct like this:
ⓘ
#[derive(LemonTree)]
struct Unit
{
}And you have terminal symbols HELLO and WORLD, then you can:
ⓘ
let mut parser = Unit::get_parser(()); // where () is initializer for %extra_argument
// the type of parser is <Unit as LemonTree>::Parser
parser.add_token(<Unit as LemonTree>::Token::HELLO, ()).unwrap();
parser.add_token(<Unit as LemonTree>::Token::WORLD, ()).unwrap();
let resulting_unit = parser.end().unwrap(); // returns UnitRequired Associated Types§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".