Skip to main content

SemanticHooks

Trait SemanticHooks 

Source
pub trait SemanticHooks {
    const ENABLES_LEXER_LIFECYCLE: bool = true;

    // Provided methods
    fn observes_parser_predicates(&self) -> bool { ... }
    fn sempred<S>(
        &mut self,
        ctx: &mut ParserSemCtx<'_, S>,
        rule_index: usize,
        pred_index: usize,
    ) -> Option<bool>
       where S: TokenSource { ... }
    fn action<S>(
        &mut self,
        ctx: &mut ParserSemCtx<'_, S>,
        action: ParserAction,
    ) -> bool
       where S: TokenSource { ... }
    fn lexer_sempred<I>(
        &mut self,
        ctx: &mut LexerSemCtx<'_, I>,
        rule_index: usize,
        pred_index: usize,
    ) -> Option<bool>
       where I: CharStream { ... }
    fn lexer_action<I>(
        &mut self,
        ctx: &mut LexerSemCtx<'_, I>,
        action: LexerCustomAction,
    ) -> bool
       where I: CharStream { ... }
    fn lexer_reset<I>(&mut self, ctx: &mut LexerLifecycleCtx<'_, I>)
       where I: CharStream { ... }
    fn lexer_before_token<I>(&mut self, ctx: &mut LexerLifecycleCtx<'_, I>)
       where I: CharStream { ... }
    fn lexer_after_accept<I>(&mut self, ctx: &mut LexerLifecycleCtx<'_, I>)
       where I: CharStream { ... }
    fn lexer_token_emitted(&mut self, token: TokenView<'_>) { ... }
}
Expand description

User extension point for parser semantic predicates and actions that the metadata generator did not translate into built-in runtime metadata.

Returning None/false says “not handled”, so the runtime falls through to the configured UnknownSemanticPolicy. Predicate hooks may run during speculative prediction and must be replay-safe.

Provided Associated Constants§

Source

const ENABLES_LEXER_LIFECYCLE: bool = true

Whether generated lexers should route lifecycle callbacks through this hook object.

User hook implementations opt in by default. NoSemanticHooks overrides this to keep generated lexers on the direct no-extension token path.

Provided Methods§

Source

fn observes_parser_predicates(&self) -> bool

Whether this hook object may observe parser predicate transitions.

Custom hooks default to conservative predicate handling so the fast recognizer does not bypass a sempred implementation.

Source

fn sempred<S>( &mut self, ctx: &mut ParserSemCtx<'_, S>, rule_index: usize, pred_index: usize, ) -> Option<bool>
where S: TokenSource,

Source

fn action<S>( &mut self, ctx: &mut ParserSemCtx<'_, S>, action: ParserAction, ) -> bool
where S: TokenSource,

Source

fn lexer_sempred<I>( &mut self, ctx: &mut LexerSemCtx<'_, I>, rule_index: usize, pred_index: usize, ) -> Option<bool>
where I: CharStream,

Source

fn lexer_action<I>( &mut self, ctx: &mut LexerSemCtx<'_, I>, action: LexerCustomAction, ) -> bool
where I: CharStream,

Runs a lexer custom action on the committed lexing path. Returns whether the hook handled the action.

The action runs post-accept, so ctx carries a mutable lexer borrow: a hook may change lexer state, including LexerSemCtx::set_type, LexerSemCtx::set_channel, mode changes, input consumption, and queued prefix tokens, just like the closure-based custom_action API. (The speculative predicate context in Self::lexer_sempred is a shared borrow, so those mutators are inert there.)

Source

fn lexer_reset<I>(&mut self, ctx: &mut LexerLifecycleCtx<'_, I>)
where I: CharStream,

Runs after runtime-owned lexer state has been reset for reuse.

Implementations should clear extension-owned transient state here.

Source

fn lexer_before_token<I>(&mut self, ctx: &mut LexerLifecycleCtx<'_, I>)
where I: CharStream,

Runs before the runtime returns a queued token or starts a new ATN token match.

The callback also runs between internal skip/more matches, so it observes every point where another ATN match may start.

Source

fn lexer_after_accept<I>(&mut self, ctx: &mut LexerLifecycleCtx<'_, I>)
where I: CharStream,

Runs after the accepted path’s portable and custom actions, but before the token span is finalized and emitted.

Accepted paths that selected skip or more are included, and the hook may observe or override that pending token type.

This callback has no synthetic ATN coordinate. It therefore also runs for accepted rules that contain no action or predicate.

Source

fn lexer_token_emitted(&mut self, token: TokenView<'_>)

Observes a token after committed lexer actions and portable commands have run and the token has been emitted, immediately before it is returned to the token stream.

Hidden and custom-channel tokens are included. skip and intermediate more matches do not produce callbacks.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§