Skip to main content

BaseParser

Struct BaseParser 

Source
pub struct BaseParser<S> { /* private fields */ }

Implementations§

Source§

impl<S> BaseParser<S>
where S: TokenSource,

Source

pub fn new(input: CommonTokenStream<S>, data: RecognizerData) -> Self

Creates a parser base over a buffered token stream and recognizer metadata.

Source

pub const fn input(&mut self) -> &mut CommonTokenStream<S>

Source

pub const fn token_stream(&self) -> &CommonTokenStream<S>

Returns the token stream owned by this parser.

Source

pub fn into_token_stream(self) -> CommonTokenStream<S>

Consumes this parser and returns its token stream.

Source

pub const fn number_of_syntax_errors(&self) -> usize

Returns the number of parser syntax errors recorded by committed parse paths so far.

Source

pub const fn record_generated_syntax_error(&mut self)

Records a syntax error that generated parser code returns as fatal before it can recover into the current rule context.

Source

pub fn report_token_source_errors(&mut self)

Emits diagnostics buffered by the token stream while generated parser code was fetching lexer tokens directly.

Source

pub const fn generated_diagnostics_checkpoint( &self, ) -> GeneratedDiagnosticsCheckpoint

Captures generated-parser diagnostics and syntax-error count before a speculative generated rule path.

Source

pub fn restore_generated_diagnostics( &mut self, marker: GeneratedDiagnosticsCheckpoint, )

Restores generated-parser diagnostics after a speculative rule path failed.

Source

pub fn report_generated_parser_diagnostics(&mut self)

Emits diagnostics recorded by committed generated parser recovery.

Source

pub fn record_generated_ambiguity_diagnostic( &mut self, atn: &Atn, state_number: usize, start_index: usize, stop_index: usize, alts: &[usize], )

Buffers ANTLR-style ambiguity diagnostics discovered by generated decision code.

Source

pub fn record_generated_prediction_diagnostic( &mut self, atn: &Atn, state_number: usize, prediction: &ParserAtnPrediction, )

Buffers ANTLR-style diagnostic-listener messages produced by generated parser calls to the adaptive simulator.

Source

pub fn la(&mut self, offset: isize) -> i32

Source

pub fn consume(&mut self)

Source

pub fn set_int_member(&mut self, member: usize, value: i64)

Sets a generated integer member value used by target-template tests.

Source

pub fn int_member(&self, member: usize) -> Option<i64>

Reads a generated integer member value.

Source

pub fn int_members_checkpoint(&self) -> BTreeMap<usize, i64>

Captures generated integer members before speculative generated parser execution.

Source

pub fn restore_int_members(&mut self, members: BTreeMap<usize, i64>)

Restores generated integer members after generated parser fallback.

Source

pub fn add_int_member(&mut self, member: usize, delta: i64) -> i64

Adds delta to a generated integer member and returns the new value.

Source

pub fn match_token(&mut self, token_type: i32) -> Result<ParseTree, AntlrError>

Matches and consumes the current token when it has the expected token type.

On success the consumed token is wrapped as a terminal parse-tree node. On mismatch the error carries vocabulary display names so diagnostics are stable across literal and symbolic token naming.

Source

pub fn match_token_recovering( &mut self, token_type: i32, follow_state: usize, atn: &Atn, ) -> Result<GeneratedMatch, AntlrError>

Matches a token from generated recursive-descent code, including ANTLR’s single-token insertion recovery when the active rule context can legally continue at the current input symbol.

Source

pub fn match_set_recovering( &mut self, intervals: &[(i32, i32)], follow_state: usize, atn: &Atn, ) -> Result<GeneratedMatch, AntlrError>

Source

pub fn match_not_set_recovering( &mut self, intervals: &[(i32, i32)], min_vocabulary: i32, max_vocabulary: i32, follow_state: usize, atn: &Atn, ) -> Result<GeneratedMatch, AntlrError>

Source

pub fn match_eof(&mut self) -> Result<ParseTree, AntlrError>

Source

pub fn match_set( &mut self, intervals: &[(i32, i32)], ) -> Result<ParseTree, AntlrError>

Source

pub fn match_not_set( &mut self, intervals: &[(i32, i32)], min_vocabulary: i32, max_vocabulary: i32, ) -> Result<ParseTree, AntlrError>

Source

pub const fn rule_node(&self, context: ParserRuleContext) -> ParseTree

Source

pub fn enter_rule( &mut self, state: isize, rule_index: usize, ) -> ParserRuleContext

Enters a generated parser rule and returns the context object the generated method should populate.

Source

pub fn push_invoking_state(&mut self, invoking_state: isize) -> usize

Records the ATN source state for the next generated rule invocation.

ANTLR’s full-context prediction reconstructs caller follow states from each active rule context’s invoking state. Generated Rust rule methods are plain functions, so the caller supplies that ATN state just before making a rule call; enter_rule consumes it when the callee starts.

Source

pub fn discard_invoking_state(&mut self, marker: usize)

Discards an invoking-state marker if the callee did not consume it.

Source

pub fn exit_rule(&mut self)

Exits the current generated parser rule.

Source

pub fn prediction_context(&mut self, atn: &Atn) -> Rc<PredictionContext>

Converts the active generated-parser rule stack into an ANTLR prediction context for full-context adaptive prediction.

Source

pub fn add_parse_child(&self, context: &mut ParserRuleContext, child: ParseTree)

Adds a generated parser child only when parse-tree construction is enabled. The match is recorded on the context either way (via add_child, or note_matched_child when trees are off) so generated recovery can tell whether the rule has matched anything yet without depending on children.

Source

pub fn finish_rule( &mut self, context: ParserRuleContext, consumed_eof: bool, ) -> ParseTree

Finishes a generated parser rule and returns its parse-tree node.

Source

pub fn recover_generated_rule( &mut self, context: &mut ParserRuleContext, atn: &Atn, error: AntlrError, )

Recovers a generated rule catch block after a committed mismatch.

ANTLR’s generated parsers catch recognition errors inside each rule, report the original error, then consume unexpected tokens until the caller’s recovery set can resume. Tokens consumed during recovery become error nodes in the current rule context.

Source

pub fn finish_recursion_rule( &mut self, context: ParserRuleContext, consumed_eof: bool, ) -> ParseTree

Finishes a generated left-recursive parser rule and returns its parse-tree node.

Source

pub fn enter_recursion_rule( &mut self, state: isize, rule_index: usize, precedence: i32, ) -> ParserRuleContext

Enters a generated left-recursive rule at precedence.

Source

pub fn push_new_recursion_context( &mut self, state: isize, rule_index: usize, ) -> ParserRuleContext

Replaces the current context while expanding a left-recursive rule.

Source

pub fn push_new_recursion_context_with_previous( &mut self, state: isize, rule_index: usize, current: &mut ParserRuleContext, )

Wraps the previous left-recursive context before parsing the next recursive operator alternative.

Source

pub fn unroll_recursion_context(&mut self)

Leaves a generated left-recursive rule.

Source

pub fn left_recursive_loop_enter_matches( &mut self, atn: &Atn, state_number: usize, precedence: i32, ) -> bool

Checks whether a generated left-recursive loop has an operator alternative that can start at the current token under the active precedence. The operator block still performs adaptive prediction; this guard only decides whether the loop should enter or exit.

Source

pub fn precpred(&self, precedence: i32) -> bool

Implements generated precpred(_ctx, k) checks.

Source

pub fn parser_semantic_predicate_matches( &mut self, predicates: &[(usize, usize, ParserPredicate)], rule_index: usize, pred_index: usize, ) -> bool

Evaluates a generated parser semantic predicate at the current input position.

Source

pub fn parser_semantic_predicate_matches_with_local( &mut self, predicates: &[(usize, usize, ParserPredicate)], rule_index: usize, pred_index: usize, local_int_arg: i32, ) -> bool

Evaluates a generated parser semantic predicate with the current integer rule argument exposed as $_p/$i metadata where applicable.

Source

pub fn parser_semantic_predicate_matches_with_context_and_local( &mut self, predicates: &[(usize, usize, ParserPredicate)], rule_index: usize, pred_index: usize, context: &ParserRuleContext, local_int_arg: i32, ) -> bool

Evaluates a generated parser semantic predicate with access to the current generated rule context.

Source

pub fn parser_semantic_predicate_failure_message( &self, rule_index: usize, pred_index: usize, predicates: &[(usize, usize, ParserPredicate)], ) -> Option<&'static str>

Returns a generated fail-option message for a parser semantic predicate coordinate.

Source

pub fn match_wildcard(&mut self) -> Result<ParseTree, AntlrError>

Matches any non-EOF token.

Source

pub fn sync(&mut self, state: isize) -> Result<(), AntlrError>

Generated parser synchronization hook. The current interpreter owns recovery; direct generated methods can call this as a no-op until the generated recovery strategy is expanded.

Source

pub fn sync_decision( &mut self, atn: &Atn, state_number: usize, current_context_empty: bool, loop_back: bool, ) -> Result<Vec<ParseTree>, AntlrError>

Synchronizes a generated parser decision against the ATN lookahead set.

ANTLR generated parsers call the error strategy before optional and loop decisions. When the current token cannot start any alternative, follow a nullable exit, or be deleted before a later synchronization token, the generated Rust method reports that decision-level mismatch instead of descending into a child rule that cannot start at the current token.

Source

pub fn ll1_decision_prediction( &mut self, atn: &Atn, state_number: usize, ) -> Option<ParserAtnPrediction>

Returns a generated-parser prediction when one token of lookahead uniquely selects an alternative for state_number.

This mirrors the interpreter’s LL(1) commit point and lets generated recursive-descent methods avoid invoking the adaptive simulator for simple optional/block/loop decisions.

Source

pub fn no_viable_alternative_error(&mut self, start_index: usize) -> AntlrError

Builds a generated no-viable-alternative parser error.

Source

pub fn no_viable_alternative_error_at( &mut self, start_index: usize, error_index: usize, ) -> AntlrError

Builds a generated no-viable-alternative parser error at the simulator’s failing lookahead index. adaptive_predict restores the input cursor before returning, so generated parsers have to pass the recorded index explicitly to preserve ANTLR’s LL(k) diagnostic span.

Source

pub fn failed_predicate_error( &mut self, message: impl Into<String>, ) -> AntlrError

Builds a generated failed-predicate parser error.

Source

pub fn failed_predicate_option_error( &mut self, rule_index: usize, message: impl Into<String>, ) -> AntlrError

Builds a generated parser error for a semantic predicate with ANTLR’s <fail='...'> option.

Source

pub fn parser_action_at_current( &mut self, source_state: usize, rule_index: usize, start_index: usize, consumed_eof: bool, ) -> ParserAction

Builds a generated parser-action event at the current input position.

Source

pub fn parse_atn_rule_adaptive_or_fallback<'atn>( &mut self, atn: &'atn Atn, simulator: &mut ParserAtnSimulator<'atn>, rule_index: usize, ) -> Result<ParseTree, AntlrError>

Attempts to execute a whole generated rule by committing simulator decisions directly. Unsupported constructs or decisions that need full-context / predicate evaluation restore the input cursor and fall back to Self::parse_atn_rule.

Source

pub fn parse_atn_rule( &mut self, atn: &Atn, rule_index: usize, ) -> Result<ParseTree, AntlrError>

Parses a generated rule by interpreting the parser ATN from the rule’s start state to its stop state.

The recognizer backtracks across alternatives and loop exits using token stream indices instead of committing to input consumption immediately. Once a viable ATN path is found, the parser commits the accepted token interval and returns a rule node whose children mirror every grammar rule invocation reached on that path, matching ANTLR’s parse-tree shape.

Source

pub fn parse_atn_rule_with_precedence( &mut self, atn: &Atn, rule_index: usize, precedence: i32, ) -> Result<ParseTree, AntlrError>

Parses a generated rule by interpreting the parser ATN with an initial left-recursive precedence threshold.

Source

pub fn parse_atn_rule_with_actions( &mut self, atn: &Atn, rule_index: usize, ) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>

Parses a generated rule and returns semantic actions reached on the selected ATN path.

This slower path preserves action ordering and token intervals for generated code that replays target-specific action templates after the recognizer has chosen one viable parse path.

Source

pub fn parse_atn_rule_with_action_inits( &mut self, atn: &Atn, rule_index: usize, init_action_rules: &[usize], ) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>

Parses a generated rule and emits ATN actions plus selected rule-init actions reached on the chosen path.

Generated parsers use this when a grammar contains rule-level @init templates that must run for nested rule invocations. The runtime keeps the action list path-sensitive, so init templates are replayed only for rules that were actually entered by the selected parse.

Source

pub fn parse_atn_rule_with_action_options( &mut self, atn: &Atn, rule_index: usize, init_action_rules: &[usize], track_alt_numbers: bool, ) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>

Parses a generated rule with optional semantic-action replay features.

track_alt_numbers is used by grammars that opt into ANTLR’s alt-numbered context behavior. It keeps ordinary parse-tree rendering unchanged for grammars that do not request that target template.

Source

pub fn parse_atn_rule_with_runtime_options( &mut self, atn: &Atn, rule_index: usize, options: ParserRuntimeOptions<'_>, ) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>

Parses a generated rule with action replay and parser predicate support.

predicates maps serialized (rule_index, pred_index) coordinates to target-template predicate semantics emitted by the generator. Missing entries are treated as true so unsupported predicate-free grammars keep the previous unconditional transition behavior.

Source

pub fn parse_atn_rule_with_runtime_options_and_precedence( &mut self, atn: &Atn, rule_index: usize, precedence: i32, options: ParserRuntimeOptions<'_>, ) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>

Parses a generated rule with action replay, parser predicate support, and an initial left-recursive precedence threshold.

Source

pub fn parse_interpreted_rule( &mut self, rule_index: usize, ) -> Result<ParseTree, AntlrError>

Temporary parser entry used by generated parser methods while the parser ATN simulator is being implemented.

This keeps generated parser crates buildable and gives us a stable method surface for every grammar rule. It intentionally accepts all remaining tokens into one rule context; it is not the final parser semantics.

Source

pub fn after_action_stop_index(&mut self, current_index: usize) -> Option<usize>

Stop-token index for a rule’s @after action, matching the boundary that finish_rule records on the rule context.

A rule that matched EOF leaves the cursor parked on the EOF token (CommonTokenStream::consume does not advance past EOF), so the stop is the current index rather than the previous visible token. Without this, $stop/$text in an @after action on a rule like r: a* EOF; would report the token before EOF (or None for empty input), diverging from the rule context that finish_rule builds.

NOTE: this infers consumed_eof from the cursor, which is wrong when a rule ends right before EOF without matching it (the cursor is parked on EOF, but the rule did not consume it). Prefer Self::after_action_stop_index_for_tree, which reuses the stop token the rule context already recorded with the real flag. Kept for callers without the rule tree in hand.

Source

pub fn after_action_stop_index_for_tree( &mut self, tree: &ParseTree, current_index: usize, ) -> Option<usize>

Stop-token index for a rule’s @after action, taken from the stop token the rule context already recorded.

finish_rule computes the rule stop with the real consumed_eof flag, so reading it back keeps $stop/$text in an @after action aligned with the rule context — even when the rule ends immediately before EOF without matching it (cursor parked on EOF, but consumed_eof is false). Falls back to the cursor-based inference only when the tree carries no rule stop.

Source

pub fn after_action_start_index_for_tree( &self, tree: &ParseTree, fallback_index: usize, ) -> usize

Start-token index for a rule’s @after action, taken from the start token the rule context already recorded.

enter_rule sets the rule context start to the first visible token (it skips leading hidden-channel tokens), so reading it back keeps $start / $text in an @after action aligned with the rule context — even when the rule begins after a hidden prefix (e.g. leading whitespace) that the raw pre-rule cursor still points at. Falls back to fallback_index only when the tree carries no rule start.

Source

pub fn text_interval(&mut self, start: usize, stop: Option<usize>) -> String

Returns token text for a buffered token interval used by generated $text actions.

ANTLR treats EOF as a range boundary rather than printable input text, even when an action interval explicitly stops at the EOF token.

Source

pub fn expected_tokens_at_state(&self, atn: &Atn, state_number: usize) -> String

Formats the tokens expected from an ATN state using ANTLR display names.

Source

pub fn token_display_at(&mut self, index: usize) -> Option<String>

Formats a buffered token in ANTLR’s diagnostic token display form.

Trait Implementations§

Source§

impl<S: Debug> Debug for BaseParser<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S> Parser for BaseParser<S>
where S: TokenSource,

Source§

fn build_parse_trees(&self) -> bool

Reports whether generated parser rules should build parse-tree nodes while recognizing input.
Source§

fn set_build_parse_trees(&mut self, build: bool)

Enables or disables parse-tree construction for subsequent rule calls.
Source§

fn number_of_syntax_errors(&self) -> usize

Returns the number of parser syntax errors recorded by committed parse paths so far.
Source§

fn report_diagnostic_errors(&self) -> bool

Reports whether prediction diagnostic-listener messages are emitted during parser ATN recognition.
Source§

fn set_report_diagnostic_errors(&mut self, report: bool)

Enables or disables ANTLR-style prediction diagnostics for subsequent rule calls.
Source§

fn prediction_mode(&self) -> PredictionMode

Reports the prediction strategy used when selecting among alternatives.
Source§

fn set_prediction_mode(&mut self, mode: PredictionMode)

Sets the prediction strategy for subsequent rule calls.
Source§

impl<S> Recognizer for BaseParser<S>
where S: TokenSource,

Source§

fn data(&self) -> &RecognizerData

Source§

fn data_mut(&mut self) -> &mut RecognizerData

Source§

fn grammar_file_name(&self) -> &str

Source§

fn rule_names(&self) -> &[String]

Source§

fn channel_names(&self) -> &[String]

Source§

fn mode_names(&self) -> &[String]

Source§

fn vocabulary(&self) -> &Vocabulary

Source§

fn state(&self) -> isize

Source§

fn set_state(&mut self, state: isize)

Source§

fn sempred(&mut self, _rule_index: usize, _pred_index: usize) -> bool

Source§

fn action(&mut self, _rule_index: usize, _action_index: usize)

Auto Trait Implementations§

§

impl<S> !Send for BaseParser<S>

§

impl<S> !Sync for BaseParser<S>

§

impl<S> Freeze for BaseParser<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for BaseParser<S>
where S: RefUnwindSafe,

§

impl<S> Unpin for BaseParser<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for BaseParser<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for BaseParser<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.