pub trait Parser<U> {
type Lexer: Lexer<U, Token: Token<TokenID = <Self::ParserData as ParserData>::TokenID>>;
type ParserData: ParserData;
// Required methods
fn ctx(&self) -> &ParserCtx<Self::Lexer, Self::ParserData, U>;
fn ctx_mut(&mut self) -> &mut ParserCtx<Self::Lexer, Self::ParserData, U>;
fn resolve_ambiguity(
&mut self,
user_data: &mut U,
ambig: <Self::ParserData as ParserData>::AmbigID,
tok2: &<Self::Lexer as Lexer<U>>::Token,
) -> Result<ParserAction<<<Self as Parser<U>>::ParserData as ParserData>::StateID, <<Self as Parser<U>>::ParserData as ParserData>::ProdID, <<Self as Parser<U>>::ParserData as ParserData>::AmbigID>>;
fn reduce(
&mut self,
user_data: &mut U,
prod_id: <Self::ParserData as ParserData>::ProdID,
token: &<Self::Lexer as Lexer<U>>::Token,
) -> Result<()>;
// Provided methods
fn stats(&self) -> ParserStats { ... }
fn tokens_peek<'a>(
&'a self,
index: usize,
) -> &'a <Self::Lexer as Lexer<U>>::Token
where U: 'a { ... }
fn tokens_mut_peek<'a>(
&'a mut self,
index: usize,
) -> &'a mut <Self::Lexer as Lexer<U>>::Token
where U: 'a { ... }
fn tokens_pop(&mut self) -> Result<<Self::Lexer as Lexer<U>>::Token> { ... }
fn tokens_push(&mut self, token: <Self::Lexer as Lexer<U>>::Token) { ... }
fn dump_state(&self, incoming: &<Self::Lexer as Lexer<U>>::Token) { ... }
fn try_collect(
&mut self,
user_data: &mut U,
) -> Result<Vec<<Self::Lexer as Lexer<U>>::Token>> { ... }
fn try_next(
&mut self,
user_data: &mut U,
) -> Result<Option<<Self::Lexer as Lexer<U>>::Token>> { ... }
}Expand description
Defines the core parser interface responsible for driving the parsing process.
The struct implementing this trait coordinates parser state, interacts with the lexer, resolves ambiguities, and applies reductions based on the parser tables. It also provides utility methods for inspecting and manipulating the parse stack.
§Type Parameters
U: User-defined data passed to parser actions.
Required Associated Types§
Sourcetype Lexer: Lexer<U, Token: Token<TokenID = <Self::ParserData as ParserData>::TokenID>>
type Lexer: Lexer<U, Token: Token<TokenID = <Self::ParserData as ParserData>::TokenID>>
The lexer used by this parser.
Must be provided by the implementor and produce tokens compatible with ParserData::TokenID.
Sourcetype ParserData: ParserData
type ParserData: ParserData
The parser data providing states, productions, tokens, and lookup tables. Must be provided by the implementor and be the struct generated by parlex-gen’s parser generator, ASLR.
Required Methods§
Sourcefn ctx(&self) -> &ParserCtx<Self::Lexer, Self::ParserData, U>
fn ctx(&self) -> &ParserCtx<Self::Lexer, Self::ParserData, U>
Returns a shared reference to the parser context.
Must be provided by the implementor and return &ParserCtx.
Sourcefn ctx_mut(&mut self) -> &mut ParserCtx<Self::Lexer, Self::ParserData, U>
fn ctx_mut(&mut self) -> &mut ParserCtx<Self::Lexer, Self::ParserData, U>
Returns a mutable reference to the parser context.
Must be provided by the implementor and return &mut ParserCtx.
Sourcefn resolve_ambiguity(
&mut self,
user_data: &mut U,
ambig: <Self::ParserData as ParserData>::AmbigID,
tok2: &<Self::Lexer as Lexer<U>>::Token,
) -> Result<ParserAction<<<Self as Parser<U>>::ParserData as ParserData>::StateID, <<Self as Parser<U>>::ParserData as ParserData>::ProdID, <<Self as Parser<U>>::ParserData as ParserData>::AmbigID>>
fn resolve_ambiguity( &mut self, user_data: &mut U, ambig: <Self::ParserData as ParserData>::AmbigID, tok2: &<Self::Lexer as Lexer<U>>::Token, ) -> Result<ParserAction<<<Self as Parser<U>>::ParserData as ParserData>::StateID, <<Self as Parser<U>>::ParserData as ParserData>::ProdID, <<Self as Parser<U>>::ParserData as ParserData>::AmbigID>>
Resolves an ambiguity reported by the parser (e.g., shift/reduce). Must be provided by the implementor.
§Parameters
user_data: User data.ambig: The 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.
Provided Methods§
Sourcefn stats(&self) -> ParserStats
fn stats(&self) -> ParserStats
Returns current parser statistics.
Sourcefn 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,
Returns a reference to a token counted from the end of the stack.
0 = last (top), 1 = second last, etc.
§Panics
Panics if index ≥ the number of tokens on the stack.
Sourcefn 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,
Returns a mutable reference to a token counted from the end of the stack.
0 = last (top), 1 = second last, etc.
§Panics
Panics if index ≥ the number of tokens on the stack.
Sourcefn tokens_pop(&mut self) -> Result<<Self::Lexer as Lexer<U>>::Token>
fn tokens_pop(&mut self) -> Result<<Self::Lexer as Lexer<U>>::Token>
Pops and returns the last (top) token from the stack.
§Errors
Returns an error if the stack is empty.
Sourcefn tokens_push(&mut self, token: <Self::Lexer as Lexer<U>>::Token)
fn tokens_push(&mut self, token: <Self::Lexer as Lexer<U>>::Token)
Pushes a token onto the stack.
Sourcefn dump_state(&self, incoming: &<Self::Lexer as Lexer<U>>::Token)
fn dump_state(&self, incoming: &<Self::Lexer as Lexer<U>>::Token)
Traces the current parser state and token stack for debugging.
Formats the stack with states and tokens, marking the incoming token with <-.