pub struct CalcParser<I>{
parser: Parser<CalcLexer<I>, CalcParserDriver<CalcLexer<I>>, SymTab>,
}Expand description
The calculator parser, a wrapper that couples:
- the calculator lexer (
CalcLexer) producingCalcTokens, and - the calculator parser driver (
CalcParserDriver) implementing reductions and ambiguity resolution for the calculator grammar.
CalcParser<I> exposes an iterator-like interface via
TryNextWithContext, yielding completed parse results (e.g., one per
“sentence” or top-level expression) while using a shared SymTab as
context. Internally it owns a generic Parser that pulls tokens
from CalcLexer and executes semantic actions in CalcParserDriver.
§Input / Output
- Input: any byte stream
IimplementingTryNextWithContext<SymTab, Item = u8>. - Output: completed parsing units as
CalcTokenvalues (typically grammar-level results like expressions/statements).
§End Tokens and Multiple Sentences
The underlying lexer typically emits an explicit TokenID::End token at
the end of a parsing unit (end of “sentence” or expression). The parser
uses this to finalize and emit one result. If the input contains multiple
independent sentences, you will receive multiple results — one per End —
and None only after all input is consumed.
§Empty Statements
The calculator grammar also accepts an empty statement, which is returned
as a token with TokenValue::None.
This occurs, for example, when the last statement in the input is terminated
by a semicolon (;) but followed by no further expression. In that case:
- The parser first emits the token for the preceding completed statement.
- It then emits an additional token representing the empty statement
(
TokenValue::None). - Finally, it returns
None, indicating the end of the input stream.
This design allows the parser to fully reflect the structure of the input, including empty or separator-only statements.
§Errors
All failures are surfaced through a composed
[ParserError<LexerError<I::Error, CalcError>, CalcError, CalcToken>]:
I::Error— errors from the input source,- [
CalcError] — lexical/semantic errors (e.g., UTF-8, integer parsing, symbol-table issues).
§Example
let mut symtab = SymTab::new();
let input = IterInput::from("hello = 1;\n foo =\n 5 + 3 * 2;\n (world + hello + 10) * -2;\n\n1000 - - -123".bytes());
let mut parser = CalcParser::try_new(input).unwrap();
let vs = parser.try_collect_with_context(&mut symtab).unwrap();
assert_eq!(vs.len(), 4);
assert_eq!(symtab.len(), 3);Fields§
§parser: Parser<CalcLexer<I>, CalcParserDriver<CalcLexer<I>>, SymTab>Implementations§
Source§impl<I> CalcParser<I>
impl<I> CalcParser<I>
pub fn try_new(input: I) -> Result<Self, ParlexError>
Trait Implementations§
Source§impl<I> TryNextWithContext<SymTab, (LexerStats, ParserStats)> for CalcParser<I>
impl<I> TryNextWithContext<SymTab, (LexerStats, ParserStats)> for CalcParser<I>
Source§fn try_next_with_context(
&mut self,
context: &mut SymTab,
) -> Result<Option<CalcToken>, ParlexError>
fn try_next_with_context( &mut self, context: &mut SymTab, ) -> Result<Option<CalcToken>, ParlexError>
Returns the next fully reduced unit (Stat), or None at end of input.
The underlying lexer typically emits an explicit TokenID::End at
unit boundaries (e.g., semicolon-terminated statements). The parser
finalizes and yields one Stat per such boundary.