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 and column 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 expressions on lines {} and {}",a.line(),b.line());
       check(a); check(b); // Deref coercion used here
     },
   ...

The ZCParser::lbx function can be called from the semantic actions of a grammar to create LBoxed-values that include line/column information. LBox implements the Default trait if T does, so an LBox type can also serve as the absyntract syntax type for a grammar. It is also possible to use LBox<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, ZCParser::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.

Structs

Custom smart pointer that encapsulates line and column numbers along with a regular Box. Implements Deref and DerefMut so the encapsulated expression can be accessed as in a standard Box. This is intended to to be used in the formation of abstract syntax trees so that the lexical information is available for each construct after the parsing stage.

Like LBox but encapsulates an Rc. Implements Deref and emulates the Rc::clone function.