pub struct TermParser<I>where
I: FusedIterator<Item = u8>,{ /* 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) runtime library. It builds upon the TermLexer 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.
§Core Components
ParserCtx— manages parse states, actions, and reductions generated by theaslrtool.TermLexer— provides token streams ofTermTokens for the parser.Arena— stores terms compactly for minimal heap allocation.
§Typical Workflow
- Create a
TermParserfrom a byte iterator (try_new). - (Optionally) extend or redefine operator sets using [
define_opers]. - Call [
try_next_term] or [try_collect_terms] to parse one or more terms into arena-backedTerms.
Implementations§
Source§impl<I> TermParser<I>where
I: FusedIterator<Item = u8>,
Implementation of TermParser methods.
impl<I> TermParser<I>where
I: FusedIterator<Item = u8>,
Implementation of TermParser methods.
This impl defines the core construction and execution logic for the
Prolog-like term parser. It provides utilities to initialize a new
parser instance, collect or stream parsed Term values, dynamically
extend operator definitions, and normalize parsed terms.
The parser integrates with the parlex runtime library and operates over
tokens produced by the TermLexer, yielding arena-allocated Term
values suitable for further semantic processing.
§Type Parameters
I: The input source, which must implementFusedIteratoryielding bytes.
Sourcepub fn try_new(input: I, opers: Option<OperDefs>) -> Result<Self>
pub fn try_new(input: I, opers: Option<OperDefs>) -> Result<Self>
Creates a new TermParser for the given input stream.
Initializes an internal TermLexer (with optional operator definitions)
and wraps it in a ParserCtx.
§Parameters
input: A fused iterator over bytes to be parsed.opers: OptionalOperDefsdefining operator precedence and fixity.
§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.
Sourcepub fn try_next_term(&mut self, arena: &mut Arena) -> Result<Option<Term>>
pub fn try_next_term(&mut self, arena: &mut Arena) -> Result<Option<Term>>
Parses the next term from the input stream.
Returns the next complete Term, or None if end-of-input is reached.
This method performs incremental parsing suitable for stream-based term input.
§Parameters
arena: Arena for allocating internal term structures.
§Errors
Returns an error on unexpected tokens, incomplete constructs, or invalid operator combinations.
Sourcepub fn define_opers<J: FusedIterator<Item = u8>>(
&mut self,
arena: &mut Arena,
defs_input: J,
opers: Option<OperDefs>,
) -> Result<()>
pub fn define_opers<J: FusedIterator<Item = u8>>( &mut self, arena: &mut Arena, defs_input: J, opers: Option<OperDefs>, ) -> Result<()>
Defines or extends operator definitions directly from a Prolog-like
op/6 term list read from a separate input source.
This allows dynamic addition of new operator fixities and precedence rules during runtime parsing.
§Parameters
arena: Arena allocator used for constructing term structures.defs_input: Input byte iterator yielding the operator definition terms.opers: Optional initial operator table to extend. IfNone, the default operator definitions are used.
§Errors
Returns an error if parsing the operator term list fails or produces an invalid operator specification.
Trait Implementations§
Source§impl<I> Parser<Arena> for TermParser<I>where
I: FusedIterator<Item = u8>,
Implements the Parser trait for TermParser, integrating with the parlex runtime library.
impl<I> Parser<Arena> for TermParser<I>where
I: FusedIterator<Item = u8>,
Implements the Parser trait for TermParser, integrating with the parlex runtime library.
This binding connects the generated SLR parser tables (ParData) with the concrete
term parser. It exposes the parser context, statistics, and the key callbacks
required during shift/reduce parsing: ambiguity resolution and reductions.
§Associated Types
Lexer— The input lexer producingTermTokens (TermLexer<I>).ParserData— The generated parser tables and rule enums (ParData).
Source§fn ctx(&self) -> &ParserCtx<Self::Lexer, Self::ParserData, Arena>
fn ctx(&self) -> &ParserCtx<Self::Lexer, Self::ParserData, Arena>
Returns a shared reference to the internal ParserCtx.
Source§fn ctx_mut(&mut self) -> &mut ParserCtx<Self::Lexer, Self::ParserData, Arena>
fn ctx_mut(&mut self) -> &mut ParserCtx<Self::Lexer, Self::ParserData, Arena>
Returns a mutable reference to the internal ParserCtx.
Source§fn stats(&self) -> ParserStats
fn stats(&self) -> ParserStats
Returns cumulative parsing statistics (tokens, shifts, reductions, ambiguities).
Source§fn resolve_ambiguity(
&mut self,
_arena: &mut Arena,
ambig: AmbigID,
tok2: &TermToken,
) -> Result<Action>
fn resolve_ambiguity( &mut self, _arena: &mut Arena, ambig: AmbigID, tok2: &TermToken, ) -> Result<Action>
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.
Source§fn reduce(
&mut self,
arena: &mut Arena,
prod: ProdID,
token: &TermToken,
) -> Result<()>
fn reduce( &mut self, arena: &mut Arena, prod: ProdID, token: &TermToken, ) -> Result<()>
Performs a grammar reduction for the given production rule.
Applies the semantic action for prod, 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: 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.
Source§type Lexer = TermLexer<I>
type Lexer = TermLexer<I>
ParserData::TokenID.Source§type ParserData = ParData
type ParserData = ParData
Source§fn tokens_peek<'a>(
&'a self,
index: usize,
) -> &'a <Self::Lexer as Lexer<U>>::Tokenwhere
U: 'a,
fn tokens_peek<'a>(
&'a self,
index: usize,
) -> &'a <Self::Lexer as Lexer<U>>::Tokenwhere
U: 'a,
0 = last (top), 1 = second last, etc. Read moreSource§fn tokens_mut_peek<'a>(
&'a mut self,
index: usize,
) -> &'a mut <Self::Lexer as Lexer<U>>::Tokenwhere
U: 'a,
fn tokens_mut_peek<'a>(
&'a mut self,
index: usize,
) -> &'a mut <Self::Lexer as Lexer<U>>::Tokenwhere
U: 'a,
0 = last (top), 1 = second last, etc. Read moreSource§fn tokens_pop(&mut self) -> Result<<Self::Lexer as Lexer<U>>::Token, Error>
fn tokens_pop(&mut self) -> Result<<Self::Lexer as Lexer<U>>::Token, Error>
Source§fn tokens_push(&mut self, token: <Self::Lexer as Lexer<U>>::Token)
fn tokens_push(&mut self, token: <Self::Lexer as Lexer<U>>::Token)
Source§fn dump_state(&self, incoming: &<Self::Lexer as Lexer<U>>::Token)
fn dump_state(&self, incoming: &<Self::Lexer as Lexer<U>>::Token)
<-.