[−][src]Derive Macro pest_typed_tree::TypedTree
#[derive(TypedTree)] { // Attributes available to this derive: #[grammar] }
Generates helper structs for type-safe AST handling.
Structs
For each rule pest-typed-tree
will generate struct.
E.g. foo_rule
-> FooRule
, and so on.
This struct is wrapper for pest Pair
for which
as_rule() == foo_rule holds.
You can create this struct using new() constructor, and get underlying
pair or text.
Typed getters
pest-typed-tree
will generate getters that return child nodes in AST.
Consider following pest grammar:
foo = ...
bar = ...
baz = {foo ~ bar?}
Following APIs will be generated:
ⓘThis example is not tested
impl Baz { fn get_foo(&self) -> Foo; fn get_bar(&self) -> Option<Bar>; }
Converting to enum
If rule is just choice of several unique rules, pest-typed-tree
will
generate to_enum
function that returns enum with actual AST child.
For following grammar:
foo = ...
bar = ...
baz = ...
quux = {foo | bar | baz}
Following API will be available
ⓘThis example is not tested
enum QuuxChildren { Foo(Foo), Bar(Bar), Baz(Baz), } impl Quux { fn to_enum(&self) -> QuuxChildren; }
See test complex.rs
for typed getters and enum conversion usage examples.