Module rustlr::generic_absyn [−][src]
Expand description
Generic Abstract Syntax Support Module
Rustlr allows any type that implements the Default trait to be used as the abstract syntax type (grammar directive absyntype). However, this module defines custom smart pointers LBox and LRc that simplify the construction of abstract syntax trees. LBox/LRc keep the line, column and source id numbers of each syntatic construct, as these are often needed during later stages of code analysis post-parsing.
For example, an abstract syntax type can be defined by
enum Expr {
Val(i64),
PlusExpr(LBox<Expr>,LBox<Expr>),
...
}
fn check(e:&Expr) {
match e {
PlusExpr(a,b) => {
println!("checking subexpressions on line {} and {}",a.line,b.line);
check(a); check(b); // Deref coercion used here
},
...
The RuntimeParser::lb function can be called from the semantic actions
of a grammar
to create LBoxed-values that include line/column information. LBoxLBox<dyn Any>
as the abstract syntax type
along with the LBox::upcast and LBox::downcast functions and
convenience macros lbup and lbdown.
Sufficient functionality has also been implemented to allow the use of
LBox<dyn Any>
as the abstract syntax type of Grammars.
This effectively allows grammar symbols to carray values of different types
as Any-trait objects. The functions LBox::upcast, LBox::downcast, RuntimeParser::lba,
and the convenience macros lbup, lbdown and lbget
are intended to support this usage. A simplified, sample grammar using
LBox<dyn Any>
as the abstract syntax type returned by the parser is
found here,
which generates this LALR parser.
Equivalent functions are available for LRc.