Rustlr is an LR-style parser generator for Rust. Advanced features include:
- Option to automatically generate the AST datatypes and semantic actions, with manual overrides possible.
- Recognizes regex-style operators
?, which simplify the writing of grammars and allow better ASTs to be created.
- Experimental features include the ability to signal delayed reduction transformations on a grammar.
- The ability to train the parser interactively for better error reporting
- External state access allows parsers go to beyond CFGs
A tutorial is separately available that will explain the format of grammars and how to generate and deploy parsers for several examples. The documentation found here should be used as a technical reference.
Rustlr should be installed as an executable (cargo install rustlr). Many of the items exported by this crate are only required by the parsers that are generated, and are not intended to be used in other programs. However, rustlr uses traits and trait objects to loosely couple the various components of the runtime parser so that custom interfaces, such as those for graphical IDEs, can built around a basic ZCParser::parse_core function.
As a simplified, self-contained example of how to use rustlr, given this grammar with file name “brackets.grammar”,
generates a LALR parser as a rust program. This program includes a ‘make_parser’ function and a ‘bracketslexer’ structure which represents the lexical scanner. The program also contains a ‘load_extras’ function, which can be modified by interactive training to give more helpful error messages other than the generic “unexpected symbol..”.
pub use lexer_interface::*;
pub use generic_absyn::*;
pub use runtime_parser::RuntimeParser;
pub use runtime_parser::RProduction;
pub use zc_parser::ZCParser;
pub use zc_parser::ZCRProduction;
Generic Abstract Syntax Support Module
This module is deprecated by the crate::zc_parser module and is only kept for compatibility with existing parsers.
This module implements a zero-copy version of the runtime parser that uses the LR statemachine generated by rustlr. It will (for now), live side along with the original parser implemented as crate::RuntimeParser. Since Version 0.2.3, this module can now generate a basic lexical scanner based on crate::RawToken and crate::StrTokenizer.
macro for downcasting
LBox<dyn Any> to
LBox<A> for some
concrete type A. Must be called
from within the semantic actions of grammar productions. Warning:
unwrap is called within the macro
macro for creating
LBox<dyn Any> structures that can encapsulate any type
as abstract syntax. Must be called from within the semantic actions of
a grammar production rule.
just extract value from LBox
this enum is only exported because it’s used by the generated parsers. There is no reason to use it in other programs.
this function is only exported because it’s used by the generated parsers.