pub struct TermParser<I>{ /* private fields */ }Expand description
Prolog-like term parser with operator precedence and associativity handling.
The TermParser drives the parsing of Prolog-style terms using the
parlex SLR(1) core library. It builds upon the TermTokenParser for tokenization
and produces Term values stored in an Arena for efficient allocation.
Operator definitions are resolved dynamically through an OperDefs table,
allowing user-defined or default operators to control how expressions are
grouped and nested according to their fixity, precedence, and
associativity.
/// # Input / Output
- Input: any byte stream
IimplementingTryNextWithContext<Arena, Item = u8>. - Output: completed parsing units as
TermTokenvalues.
§End Tokens and Multiple Sentences
The underlying parser emits an explicit tokens 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 terms grammar also accepts an empty term, which is returned
as a token with Value::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 term.
- It then emits an additional token representing the empty term
(
Value::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.
§Errors
All failures are surfaced through a composed
[ParserError<LexerError<I::Error, CalcError>, CalcError, CalcToken>]:
I::Error— errors from the input source,- [
TermParserError] — lexical/semantic errors (e.g., UTF-8, integer parsing, symbol-table issues).
§Example
let mut arena = Arena::try_with_default_opers().unwrap();
let input = IterInput::from("hello = 1 .\n foo =\n [5, 3, 2].\n (world, hello, 10).\n\n1000".bytes());
let mut parser = TermParser::try_new(input).unwrap();
let vs = parser.try_collect_with_context(&mut arena).unwrap();
assert_eq!(vs.len(), 4);Implementations§
Source§impl<I> TermParser<I>
impl<I> TermParser<I>
Sourcepub fn try_new(input: I) -> Result<Self, ParlexError>
pub fn try_new(input: I) -> Result<Self, ParlexError>
Creates a new TermParser for the given input stream and operator definitions.
§Parameters
input: A fused iterator over bytes to be parsed.arena: A term arena, used to initialized default operator defs.
§Returns
A fully initialized TermParser ready to parse Prolog-like terms.
§Errors
Returns an error if the lexer context cannot be initialized or if the generated parser tables fail to load.
Trait Implementations§
Source§impl<I> TryNextWithContext<Arena, (LexerStats, ParserStats)> for TermParser<I>
impl<I> TryNextWithContext<Arena, (LexerStats, ParserStats)> for TermParser<I>
Source§type Error = ParlexError
type Error = ParlexError
Unified error type.
Source§fn try_next_with_context(
&mut self,
context: &mut Arena,
) -> Result<Option<Term>, ParlexError>
fn try_next_with_context( &mut self, context: &mut Arena, ) -> Result<Option<Term>, ParlexError>
Advances the parser and returns the next token, or None at end of input.
The provided context (an Arena) may be mutated by rule
actions (for example, to intern terms). This method is fallible;
both input and lexical errors are converted into Self::Error.
§End of Input
When the lexer reaches the end of the input stream, it will typically
emit a final TokenID::End token before returning None.
This explicit End token is expected by the Parlex parser to signal successful termination of a complete parsing unit. Consumers should treat this token as a logical end-of-sentence or end-of-expression marker, depending on the grammar.
If the input contains multiple independent sentences or expressions,
the lexer may emit multiple End tokens—one after each completed unit.
In such cases, the parser can restart or resume parsing after each End
to produce multiple parse results from a single input stream.
Once all input has been consumed, the lexer returns None.