rusty_lr_core 3.39.1

core library for rusty_lr
Documentation
/// A trait for data stack in the parser.
///
/// Since each non-terminal could have different ruletypes,
/// this effectively handles those rule types into separated `Vec` stack,
/// instead of using enum of rule types (since it would be costful at memory aspects if the size differs significantly).
/// For people who is curious about the implementation details,
/// you should see the actual generated `DataStack` structs, like `GrammarDataStack` in `rusty_lr_parser/src/parser/parser_expanded.rs`.
pub trait DataStack: Sized + Default {
    /// Type for terminal symbols
    type Term;
    /// Type for non-terminal symbols - this must be enum type that was auto-generated by rusty_lr
    type NonTerm: crate::parser::nonterminal::NonTerminal;
    /// Type for user data that is passed to the parser from the user.
    type UserData;
    /// Type for `Err` variant returned by reduce action
    type ReduceActionError;
    /// The value of the start symbol
    type StartType;
    /// Type for location of the token
    type Location: crate::Location;

    fn pop_start(&mut self) -> Option<Self::StartType>;
    fn pop(&mut self);
    fn push_terminal(&mut self, term: Self::Term);
    fn push_empty(&mut self);

    fn clear(&mut self);
    fn reserve(&mut self, additional: usize);
    fn with_capacity(capacity: usize) -> Self {
        let mut self_: Self = Default::default();
        self_.reserve(capacity);
        self_
    }

    fn split_off(&mut self, at: usize) -> Self;
    fn append(&mut self, other: &mut Self);

    /// Performs a reduce action with the given rule index.
    /// Returns false if the empty tag was pushed by this reduce action, true otherwise.
    fn reduce_action(
        // the child tokens for the reduction
        // the caller (usually from generated code) must pops all of the tokens used for this reduce_action
        data_stack: &mut Self,
        location_stack: &mut Vec<Self::Location>,
        push_data: bool,

        // the index of the production rule to reduce
        rule_index: usize,

        // for runtime-conflict-resolve.
        // if this variable is set to false in the action, the shift action will not be performed. (GLR parser)
        shift: &mut bool,
        // the lookahead token that caused this reduce action
        lookahead: &crate::TerminalSymbol<Self::Term>,
        // user input data
        userdata: &mut Self::UserData,
        // location of this non-terminal, e.g. `@$`
        location0: &mut Self::Location,
    ) -> Result<(), Self::ReduceActionError>;
}