Struct rustlr::base_parser::BaseParser
source · pub struct BaseParser<'ilt, AT: Default, ET: Default, TT: Tokenizer<'ilt, AT>> {Show 14 fields
pub exstate: ET,
pub shared_state: Rc<RefCell<ET>>,
pub RSM: Vec<HashMap<&'static str, Stateaction>>,
pub Rules: Vec<BaseProduction<'ilt, AT, ET, TT>>,
pub stack: Vec<StackedItem<AT>>,
pub resynch: HashSet<&'static str>,
pub Errsym: &'static str,
pub linenum: usize,
pub column: usize,
pub position: usize,
pub prev_position: usize,
pub src_id: usize,
pub Symset: HashSet<&'static str>,
pub tokenizer: TT,
/* private fields */
}
Expand description
This is the structure created by the generated parser. The generated parser program will contain a make_parser function that returns this structure. Most of the pub items are, however, only exported to support the operation of the parser, and should not be accessed directly. Only the functions BaseParser::parse, BaseParser::report, BaseParser::abort, BaseParser::error_occurred, BaseParser::get_tokenizer and BaseParser::swap_tokenizer should be called directly from user programs. Only the field BaseParser::exstate should be accessed by user programs.
Fields§
§exstate: ET
this is the “external state” structure, with type ET defined by the grammar. The semantic actions associated with each grammar rule, which are written in the grammar, have ref mut access to the BaseParser structure, which allows them to read and change the external state object. This gives the parsers greater flexibility and capability, including the ability to parse some non-context free languages. See this sample grammar. The exstate is initialized to ET::default().
External state that can be shared
RSM: Vec<HashMap<&'static str, Stateaction>>
used only by generated parser: do not reference
Rules: Vec<BaseProduction<'ilt, AT, ET, TT>>
do not reference
stack: Vec<StackedItem<AT>>
do not reference
resynch: HashSet<&'static str>
§Errsym: &'static str
§linenum: usize
axiom: linenum and column represents the starting position of the topmost StackedItem.
column: usize
§position: usize
§prev_position: usize
§src_id: usize
§Symset: HashSet<&'static str>
Hashset containing all grammar symbols (terminal and non-terminal). This is used for error reporting and training.
tokenizer: TT
Implementations§
source§impl<'t, AT: Default, ET: Default, TT: Tokenizer<'t, AT>> BaseParser<'t, AT, ET, TT>
impl<'t, AT: Default, ET: Default, TT: Tokenizer<'t, AT>> BaseParser<'t, AT, ET, TT>
sourcepub fn new(rlen: usize, slen: usize, tk: TT) -> Self
pub fn new(rlen: usize, slen: usize, tk: TT) -> Self
this is only called by the make_parser function in the machine-generated parser program. Do not call this function in other places as it only generates a skeleton.
sourcepub fn get_tokenizer(&mut self) -> &mut TT
pub fn get_tokenizer(&mut self) -> &mut TT
returns a mutatble borrow of the parser’s tokenizer
sourcepub fn swap_tokenizer(&mut self, newtk: TT) -> TT
pub fn swap_tokenizer(&mut self, newtk: TT) -> TT
replaces the parser’s tokenizer with a new tokenizer, and returns the previous tokenizer
sourcepub fn current_line(&self) -> usize
pub fn current_line(&self) -> usize
returns the current line number
sourcepub fn current_column(&self) -> usize
pub fn current_column(&self) -> usize
returns the current column number
sourcepub fn current_position(&self) -> usize
pub fn current_position(&self) -> usize
returns the current absolute byte position according to tokenizer
sourcepub fn previous_position(&self) -> usize
pub fn previous_position(&self) -> usize
returns the previous position (before shift) according to tokenizer
sourcepub fn abort(&mut self, msg: &str)
pub fn abort(&mut self, msg: &str)
this function can be called from within the semantic actions attached to grammar production rules that are executed for each “reduce” action of the parser.
sourcepub fn stop(&mut self)
pub fn stop(&mut self)
this function can be called from within the “semantic” actions attached to production rules to terminate parsing.
sourcepub fn report(&mut self, errmsg: &str)
pub fn report(&mut self, errmsg: &str)
may be called from grammar semantic actions to report error. this report function will print to stdout.
sourcepub fn report_error(&mut self, errmsg: &str, showlc: bool)
pub fn report_error(&mut self, errmsg: &str, showlc: bool)
same as BaseParser::report but with option to display line/column
sourcepub fn bad_pattern(&mut self, pattern: &str) -> AT
pub fn bad_pattern(&mut self, pattern: &str) -> AT
this function is only exported to support the generated code
sourcepub fn popstack(&mut self) -> StackedItem<AT>
pub fn popstack(&mut self) -> StackedItem<AT>
this function is called from the generated semantic actions and should most definitely not be called from elsewhere as it would corrupt the base parser.
pub fn popstack_as_lbox(&mut self) -> LBox<AT>
sourcepub fn error_occurred(&self) -> bool
pub fn error_occurred(&self) -> bool
can be called to determine if an error occurred during parsing. The parser will not panic.
sourcepub fn lba<T: 'static>(&self, e: T) -> LBox<dyn Any>
pub fn lba<T: 'static>(&self, e: T) -> LBox<dyn Any>
creates a LBox<dyn Any>
, which allows attributes of different types to
be associated with grammar symbols. Use in conjuction with LBox::downcast, LBox::upcast and the lbdown, lbup macros.
sourcepub fn lrc<T>(&self, e: T) -> LRc<T>
pub fn lrc<T>(&self, e: T) -> LRc<T>
similar to BaseParser::lb, but creates a LRc instead of LBox
sourcepub fn lrca<T: 'static>(&'t self, e: T) -> LRc<dyn Any>
pub fn lrca<T: 'static>(&'t self, e: T) -> LRc<dyn Any>
similar to BaseParser::lba but creates a LRc
sourcepub fn lbx<T>(&self, i: usize, e: T) -> LBox<T>
pub fn lbx<T>(&self, i: usize, e: T) -> LBox<T>
creates LBox enclosing e using line/column information associated with right-hand side symbols, numbered left-to-right starting at 0
source§impl<'t, AT: Default, ET: Default, TT: Tokenizer<'t, AT>> BaseParser<'t, AT, ET, TT>
impl<'t, AT: Default, ET: Default, TT: Tokenizer<'t, AT>> BaseParser<'t, AT, ET, TT>
sourcepub fn error_recover(
&mut self,
lookahead: &mut TerminalToken<'t, AT>
) -> Option<Stateaction>
pub fn error_recover( &mut self, lookahead: &mut TerminalToken<'t, AT> ) -> Option<Stateaction>
Error recovery routine of rustlr, separate from error_reporter. This function will modify the parser and lookahead symbol and return either the next action the parser should take (if recovery succeeded) or None if recovery failed.
sourcepub fn get_err_report(&self) -> &str
pub fn get_err_report(&self) -> &str
Retrieves recorded error report. This function will return an empty string if BaseParser::set_err_report is not called. It will also return an empty string if there was no error
sourcepub fn set_err_report(&mut self, onof: bool)
pub fn set_err_report(&mut self, onof: bool)
When given true as argument, this option will disable the output of parser errors to stderr, and instead log them internally until retrieved with BaseParser::get_err_report. Each call to this function will clear the previous report and begin a new one. If the bool argument is false, it will erase and turn off error logging and print all parser errors to stderr. This function does not affect interactive training, which uses stdio.
source§impl<'t, AT: Default, ET: Default, TT: Tokenizer<'t, AT>> BaseParser<'t, AT, ET, TT>
impl<'t, AT: Default, ET: Default, TT: Tokenizer<'t, AT>> BaseParser<'t, AT, ET, TT>
sourcepub fn parse(&mut self) -> AT
pub fn parse(&mut self) -> AT
This function is exported because it’s required at runtime.
Under normal circumstances, this function should not be called
directly. In the recommended auto mode, which generates the abstract
syntax and lexical tokenizer, call instead the parse_with
function
that is specific to each grammar.
sourcepub fn parse_train(&mut self, parserfile: &str) -> AT
pub fn parse_train(&mut self, parserfile: &str) -> AT
This function is exported because it’s required at runtime.
Under normal circumstances, this function should not be called
directly. In the recommended auto mode, which generates the abstract
syntax and lexical tokenizer, call instead the parse_train_with
function
that is specific to each grammar.
sourcepub fn train_from_script(&mut self, parserfile: &str, scriptfile: &str)
pub fn train_from_script(&mut self, parserfile: &str, scriptfile: &str)
trains parser from a training script created by interactive training. This is intended to be used after a grammar has been modified and the parser is regenerated with different state numbers. It is the user’s responsibility to keep consistent the parser file, script file, and sample input that was used when the script was created. The script contains the line and column numbers of each error encountered, along with either the unexpected symbol that caused the error, or the reserved ANY_ERROR symbol if the error message is to be applied to all unexpected symbols. These entries must match, in sequence, the errors encountered during retraining - it is therefore recommended that the same tokenizer be used during retraining so that the same line/column information are given. The trainer will augment the parser (parserfile) with new Error entries, overriding any previous ones. It is also recommended that the user examines the “load_extras” function that appears at the end of the augmented parser. The train_from_script function does not return a value, unlike BaseParser::parse and BaseParser::parse_train.