CalcParser

Struct CalcParser 

Source
pub struct CalcParser<I>
where I: TryNextWithContext<SymTab, Item = u8, Error: Display + 'static>,
{ parser: Parser<CalcLexer<I>, CalcParserDriver<CalcLexer<I>>, SymTab>, }
Expand description

The calculator parser, a wrapper that couples:

  • the calculator lexer (CalcLexer) producing CalcTokens, 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

§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:

  1. The parser first emits the token for the preceding completed statement.
  2. It then emits an additional token representing the empty statement (TokenValue::None).
  3. 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>
where I: TryNextWithContext<SymTab, Item = u8, Error: Display + 'static>,

Source

pub fn try_new(input: I) -> Result<Self, ParlexError>

Trait Implementations§

Source§

impl<I> TryNextWithContext<SymTab, (LexerStats, ParserStats)> for CalcParser<I>
where I: TryNextWithContext<SymTab, Item = u8, Error: Display + 'static>,

Source§

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.

Source§

type Item = CalcToken

The type of items yielded by this source.
Source§

type Error = ParlexError

The error type that may be returned when producing the next item fails.
Source§

fn stats(&self) -> (LexerStats, ParserStats)

Source§

fn try_collect_with_context( &mut self, context: &mut C, ) -> Result<Vec<Self::Item>, Self::Error>

Collects all remaining items into a Vec, using the given context. Read more

Auto Trait Implementations§

§

impl<I> Freeze for CalcParser<I>
where I: Freeze,

§

impl<I> RefUnwindSafe for CalcParser<I>
where I: RefUnwindSafe,

§

impl<I> Send for CalcParser<I>
where I: Send,

§

impl<I> Sync for CalcParser<I>
where I: Sync,

§

impl<I> Unpin for CalcParser<I>
where I: Unpin,

§

impl<I> UnwindSafe for CalcParser<I>
where I: UnwindSafe,

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>,

Source§

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>,

Source§

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.