pub struct TermParserDriver<I> { /* private fields */ }Expand description
A driver that defines semantic actions for the term parser.
The TermParserDriver type implements ParserDriver and acts as the
bridge between the parser engine (Parser) and calculator-specific
semantic logic.
It provides the behavior for grammar reductions and ambiguity resolution
during parsing. Each reduction corresponds to a grammar production rule
in [ParData], and is responsible for building a term.
§Type Parameters
I: The input source (the lexer) that yieldsTermTokens. Must implementTryNextWithContext<Arena, Item = TermToken>.
§Associated Types
ParserData = ParData: Generated parser metadata containing grammar rules, production IDs, and ambiguity identifiers.Token = TermToken: The token type produced by the lexer and consumed by this parser.Parser = Parser<I, Self, Arena>: The parser engine parameterized by this driver and context.Error = TermParserError: Unified error type propagated during parsing.Context = Arena: Externally supplied context.
§Responsibilities
The parser driver performs calculator-specific actions:
resolve_ambiguity— invoked when the grammar allows multiple valid interpretations of a token sequence. The driver chooses which parse path to follow by returning an appropriateParserAction.reduce— executed when a grammar production completes. The driver can perform semantic actions such as arithmetic evaluation, updating the symbol table, or producing intermediate values.
Trait Implementations§
Source§impl<I> ParserDriver for TermParserDriver<I>
impl<I> ParserDriver for TermParserDriver<I>
Source§type ParserData = ParData
type ParserData = ParData
Parser metadata generated from the calculator grammar.
Source§type Parser = Parser<I, TermParserDriver<I>, <TermParserDriver<I> as ParserDriver>::Context>
type Parser = Parser<I, TermParserDriver<I>, <TermParserDriver<I> as ParserDriver>::Context>
Concrete parser engine type.
Source§fn resolve_ambiguity(
&mut self,
parser: &mut Self::Parser,
arena: &mut Self::Context,
ambig: <Self::ParserData as ParserData>::AmbigID,
tok2: &Self::Token,
) -> Result<ParserAction<StateID, ProdID, AmbigID>, ParlexError>
fn resolve_ambiguity( &mut self, parser: &mut Self::Parser, arena: &mut Self::Context, ambig: <Self::ParserData as ParserData>::AmbigID, tok2: &Self::Token, ) -> Result<ParserAction<StateID, ProdID, AmbigID>, ParlexError>
Resolves an ambiguity reported by the parser (e.g., shift/reduce).
Given an ambiguity identifier and the lookahead token tok2, this method
chooses the appropriate parser action (shift or reduce) according to the
operator precedence and associativity rules.
§Parameters
_arena: Arena used to allocate or inspect terms.ambig: The generated ambiguity ID (AmbigID).tok2: The lookahead token at the ambiguity point.
§Returns
The selected parser [Action] to disambiguate the current state.
§Errors
Returns an error if the ambiguity cannot be resolved consistently.
§Notes
This grammar contains only Shift/Reduce conflicts — cases where the parser can either:
- Reduce using a completed production rule, or
- Shift the next incoming token (
tok2).
Other types of conflicts (such as Reduce/Reduce) are much more difficult to handle programmatically and usually require modifying the grammar itself to eliminate ambiguity.
Source§fn reduce(
&mut self,
parser: &mut Self::Parser,
arena: &mut Self::Context,
prod_id: <Self::ParserData as ParserData>::ProdID,
token: &Self::Token,
) -> Result<(), ParlexError>
fn reduce( &mut self, parser: &mut Self::Parser, arena: &mut Self::Context, prod_id: <Self::ParserData as ParserData>::ProdID, token: &Self::Token, ) -> Result<(), ParlexError>
Performs a grammar reduction for the given production rule.
Applies the semantic action for prod_id, typically constructing or
normalizing an arena-backed Term, and pushes the resulting token
onto the parser’s value stack.
§Parameters
arena: Arena used to allocate or inspect terms.prod_id: The production being reduced (ProdID).token: The lookahead token (normally not used).
§Errors
Returns an error if the reduction fails due to arity mismatches, invalid operator metadata, or inconsistent stack state.