Parser

Trait Parser 

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

Source

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.

Source

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§

Source

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.

Source

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.

Source

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.

Source

fn reduce( &mut self, user_data: &mut U, prod_id: <Self::ParserData as ParserData>::ProdID, token: &<Self::Lexer as Lexer<U>>::Token, ) -> Result<()>

Performs a grammar reduction for the given production rule. Must be provided by the implementor.

§Parameters
  • user_data: User data.
  • prod: The production being reduced (ProdID).
  • token: The lookahead token (normally not used).
§Errors

Returns an error if the reduction fails.

Provided Methods§

Source

fn stats(&self) -> ParserStats

Returns current parser statistics.

Source

fn tokens_peek<'a>( &'a self, index: usize, ) -> &'a <Self::Lexer as Lexer<U>>::Token
where 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.

Source

fn tokens_mut_peek<'a>( &'a mut self, index: usize, ) -> &'a mut <Self::Lexer as Lexer<U>>::Token
where 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.

Source

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.

Source

fn tokens_push(&mut self, token: <Self::Lexer as Lexer<U>>::Token)

Pushes a token onto the stack.

Source

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

Source

fn try_collect( &mut self, user_data: &mut U, ) -> Result<Vec<<Self::Lexer as Lexer<U>>::Token>>

Attempts to collect all tokens until exhaustion.

Source

fn try_next( &mut self, user_data: &mut U, ) -> Result<Option<<Self::Lexer as Lexer<U>>::Token>>

Attempts to parse the input and produce the next reduced (accepted) token.

Implementors§