Expand description
§parlex-calc
A small demonstration crate built on parlex, providing a complete, minimal example of a lexer–parser pipeline for a calculator language.
This crate showcases how to connect a CalcLexer and CalcParser
using token spans and a shared symbol table. It’s intended for educational
and testing purposes rather than production use.
§Overview
The parlex-calc crate defines the following components:
lexer— a tokenizing module that converts raw input into a stream ofCalcTokenitems, each annotated with a [Span] indicating its position in the source text.parser— a grammar-based parser that consumes lexer tokens to produce an abstract syntax tree (AST) or evaluate expressions directly.symtab— a symbol table abstraction (SymTab) that manages variable bindings and supports expression evaluation.token— shared token definitions, includingCalcToken,TokenValue, andTokenID.
§Example
use try_next::{IterInput, TryNextWithContext};
use parlex::span;
use parlex_calc::{CalcParser, CalcToken, SymTab, TokenID, TokenValue};
// Input source string
let source = "a = 1 + 2 * 3";
// Initialize parser
let mut symtab = SymTab::new();
let input = IterInput::from(source.bytes());
let mut parser = CalcParser::try_new(input).unwrap();
// Run the lexer–parser pipeline
let Ok(Some(token)) = parser.try_next_with_context(&mut symtab) else {panic!("expected token")};
assert!(matches!(
token,
CalcToken {
token_id: TokenID::Stat,
span: span!(0, 0, 0, 13),
value: TokenValue::Number(7)
}
));§Modules
lexer— lexical analysis (tokenization)parser— grammar-based parsingsymtab— symbol table and evaluation contexttoken— token definitions and span utilities
§Re-exports
To simplify integration, the crate re-exports its main entry points:
CalcLexer, CalcLexerDriver, CalcParser, CalcParserDriver,
SymTab, SymTabError, CalcToken, TokenValue, TokenIDThese are the primary types needed to embed parlex-calc in tests,
examples, or small interpreters.
Re-exports§
pub use lexer::CalcLexer;pub use lexer::CalcLexerDriver;pub use parser::parser_data::TokenID;pub use parser::CalcParser;pub use parser::CalcParserDriver;pub use symtab::SymTab;pub use symtab::SymTabError;pub use token::CalcToken;pub use token::TokenValue;
Modules§
- lexer
- Calculator Lexer
- parser
- Calculator Parser
- symtab
- A minimal, flat symbol table built on
indexmap::IndexMap. - token
- Calculator Tokens