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().

§shared_state: Rc<RefCell<ET>>

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>

source

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.

source

pub fn get_tokenizer(&mut self) -> &mut TT

returns a mutatble borrow of the parser’s tokenizer

source

pub fn swap_tokenizer(&mut self, newtk: TT) -> TT

replaces the parser’s tokenizer with a new tokenizer, and returns the previous tokenizer

source

pub fn current_line(&self) -> usize

returns the current line number

source

pub fn current_column(&self) -> usize

returns the current column number

source

pub fn current_position(&self) -> usize

returns the current absolute byte position according to tokenizer

source

pub fn previous_position(&self) -> usize

returns the previous position (before shift) according to tokenizer

source

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.

source

pub fn stop(&mut self)

this function can be called from within the “semantic” actions attached to production rules to terminate parsing.

source

pub fn report(&mut self, errmsg: &str)

may be called from grammar semantic actions to report error. this report function will print to stdout.

source

pub fn report_error(&mut self, errmsg: &str, showlc: bool)

same as BaseParser::report but with option to display line/column

source

pub fn bad_pattern(&mut self, pattern: &str) -> AT

this function is only exported to support the generated code

source

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.

source

pub fn popstack_as_lbox(&mut self) -> LBox<AT>

source

pub fn error_occurred(&self) -> bool

can be called to determine if an error occurred during parsing. The parser will not panic.

source

pub fn lb<T>(&self, e: T) -> LBox<T>

creates a LBox smart pointer that includes line/column information; should be called from the semantic actions of a grammar rule, e.g.

   E --> E:a + E:b {PlusExpr(parser.lb(a),parser.lb(b))}
source

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.

source

pub fn lrc<T>(&self, e: T) -> LRc<T>

similar to BaseParser::lb, but creates a LRc instead of LBox

source

pub fn lrca<T: 'static>(&'t self, e: T) -> LRc<dyn Any>

similar to BaseParser::lba but creates a LRc

source

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

pub fn lbox<T>(&self, i: usize, e: T) -> LBox<T>

alias for Self::lbx

source

pub fn lc<T>(&self, i: usize, e: T) -> LC<T>

creates LC enclosing e using line/column information associated with right-hand side symbols, numbered left-to-right starting at 0

source

pub fn lrcn<T>(&self, i: usize, e: T) -> LRc<T>

Like lbx but creates an LRc

source§

impl<'t, AT: Default, ET: Default, TT: Tokenizer<'t, AT>> BaseParser<'t, AT, ET, TT>

source

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.

source

pub fn reset(&mut self)

resets parser, including external state

source

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

source

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>

source

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.

source

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.

source

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.

Auto Trait Implementations§

§

impl<'ilt, AT, ET, TT> !Freeze for BaseParser<'ilt, AT, ET, TT>

§

impl<'ilt, AT, ET, TT> !RefUnwindSafe for BaseParser<'ilt, AT, ET, TT>

§

impl<'ilt, AT, ET, TT> !Send for BaseParser<'ilt, AT, ET, TT>

§

impl<'ilt, AT, ET, TT> !Sync for BaseParser<'ilt, AT, ET, TT>

§

impl<'ilt, AT, ET, TT> Unpin for BaseParser<'ilt, AT, ET, TT>
where ET: Unpin, TT: Unpin, AT: Unpin,

§

impl<'ilt, AT, ET, TT> !UnwindSafe for BaseParser<'ilt, AT, ET, TT>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.