pub struct BaseParser<S> { /* private fields */ }Implementations§
Source§impl<S> BaseParser<S>where
S: TokenSource,
impl<S> BaseParser<S>where
S: TokenSource,
Sourcepub fn new(input: CommonTokenStream<S>, data: RecognizerData) -> Self
pub fn new(input: CommonTokenStream<S>, data: RecognizerData) -> Self
Creates a parser base over a buffered token stream and recognizer metadata.
pub const fn input(&mut self) -> &mut CommonTokenStream<S>
Sourcepub const fn token_stream(&self) -> &CommonTokenStream<S>
pub const fn token_stream(&self) -> &CommonTokenStream<S>
Returns the token stream owned by this parser.
Sourcepub fn into_token_stream(self) -> CommonTokenStream<S>
pub fn into_token_stream(self) -> CommonTokenStream<S>
Consumes this parser and returns its token stream.
Sourcepub const fn number_of_syntax_errors(&self) -> usize
pub const fn number_of_syntax_errors(&self) -> usize
Returns the number of parser syntax errors recorded by committed parse paths so far.
Sourcepub const fn record_generated_syntax_error(&mut self)
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.
Sourcepub fn report_token_source_errors(&mut self)
pub fn report_token_source_errors(&mut self)
Emits diagnostics buffered by the token stream while generated parser code was fetching lexer tokens directly.
Sourcepub const fn generated_diagnostics_checkpoint(
&self,
) -> GeneratedDiagnosticsCheckpoint
pub const fn generated_diagnostics_checkpoint( &self, ) -> GeneratedDiagnosticsCheckpoint
Captures generated-parser diagnostics and syntax-error count before a speculative generated rule path.
Sourcepub fn restore_generated_diagnostics(
&mut self,
marker: GeneratedDiagnosticsCheckpoint,
)
pub fn restore_generated_diagnostics( &mut self, marker: GeneratedDiagnosticsCheckpoint, )
Restores generated-parser diagnostics after a speculative rule path failed.
Sourcepub fn report_generated_parser_diagnostics(&mut self)
pub fn report_generated_parser_diagnostics(&mut self)
Emits diagnostics recorded by committed generated parser recovery.
Sourcepub fn record_generated_ambiguity_diagnostic(
&mut self,
atn: &Atn,
state_number: usize,
start_index: usize,
stop_index: usize,
alts: &[usize],
)
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.
Sourcepub fn record_generated_prediction_diagnostic(
&mut self,
atn: &Atn,
state_number: usize,
prediction: &ParserAtnPrediction,
)
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.
pub fn la(&mut self, offset: isize) -> i32
pub fn consume(&mut self)
Sourcepub fn set_int_member(&mut self, member: usize, value: i64)
pub fn set_int_member(&mut self, member: usize, value: i64)
Sets a generated integer member value used by target-template tests.
Sourcepub fn int_member(&self, member: usize) -> Option<i64>
pub fn int_member(&self, member: usize) -> Option<i64>
Reads a generated integer member value.
Sourcepub fn int_members_checkpoint(&self) -> BTreeMap<usize, i64>
pub fn int_members_checkpoint(&self) -> BTreeMap<usize, i64>
Captures generated integer members before speculative generated parser execution.
Sourcepub fn restore_int_members(&mut self, members: BTreeMap<usize, i64>)
pub fn restore_int_members(&mut self, members: BTreeMap<usize, i64>)
Restores generated integer members after generated parser fallback.
Sourcepub fn add_int_member(&mut self, member: usize, delta: i64) -> i64
pub fn add_int_member(&mut self, member: usize, delta: i64) -> i64
Adds delta to a generated integer member and returns the new value.
Sourcepub fn match_token(&mut self, token_type: i32) -> Result<ParseTree, AntlrError>
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.
Sourcepub fn match_token_recovering(
&mut self,
token_type: i32,
follow_state: usize,
atn: &Atn,
) -> Result<GeneratedMatch, AntlrError>
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.
pub fn match_set_recovering( &mut self, intervals: &[(i32, i32)], follow_state: usize, atn: &Atn, ) -> Result<GeneratedMatch, AntlrError>
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>
pub fn match_eof(&mut self) -> Result<ParseTree, AntlrError>
pub fn match_set( &mut self, intervals: &[(i32, i32)], ) -> Result<ParseTree, AntlrError>
pub fn match_not_set( &mut self, intervals: &[(i32, i32)], min_vocabulary: i32, max_vocabulary: i32, ) -> Result<ParseTree, AntlrError>
pub const fn rule_node(&self, context: ParserRuleContext) -> ParseTree
Sourcepub fn enter_rule(
&mut self,
state: isize,
rule_index: usize,
) -> ParserRuleContext
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.
Sourcepub fn push_invoking_state(&mut self, invoking_state: isize) -> usize
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.
Sourcepub fn discard_invoking_state(&mut self, marker: usize)
pub fn discard_invoking_state(&mut self, marker: usize)
Discards an invoking-state marker if the callee did not consume it.
Sourcepub fn prediction_context(&mut self, atn: &Atn) -> Rc<PredictionContext>
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.
Sourcepub fn add_parse_child(&self, context: &mut ParserRuleContext, child: ParseTree)
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.
Sourcepub fn finish_rule(
&mut self,
context: ParserRuleContext,
consumed_eof: bool,
) -> ParseTree
pub fn finish_rule( &mut self, context: ParserRuleContext, consumed_eof: bool, ) -> ParseTree
Finishes a generated parser rule and returns its parse-tree node.
Sourcepub fn recover_generated_rule(
&mut self,
context: &mut ParserRuleContext,
atn: &Atn,
error: AntlrError,
)
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.
Sourcepub fn finish_recursion_rule(
&mut self,
context: ParserRuleContext,
consumed_eof: bool,
) -> ParseTree
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.
Sourcepub fn enter_recursion_rule(
&mut self,
state: isize,
rule_index: usize,
precedence: i32,
) -> ParserRuleContext
pub fn enter_recursion_rule( &mut self, state: isize, rule_index: usize, precedence: i32, ) -> ParserRuleContext
Enters a generated left-recursive rule at precedence.
Sourcepub fn push_new_recursion_context(
&mut self,
state: isize,
rule_index: usize,
) -> ParserRuleContext
pub fn push_new_recursion_context( &mut self, state: isize, rule_index: usize, ) -> ParserRuleContext
Replaces the current context while expanding a left-recursive rule.
Sourcepub fn push_new_recursion_context_with_previous(
&mut self,
state: isize,
rule_index: usize,
current: &mut ParserRuleContext,
)
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.
Sourcepub fn unroll_recursion_context(&mut self)
pub fn unroll_recursion_context(&mut self)
Leaves a generated left-recursive rule.
Sourcepub fn left_recursive_loop_enter_matches(
&mut self,
atn: &Atn,
state_number: usize,
precedence: i32,
) -> bool
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.
Sourcepub fn parser_semantic_predicate_matches(
&mut self,
predicates: &[(usize, usize, ParserPredicate)],
rule_index: usize,
pred_index: usize,
) -> bool
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.
Sourcepub fn parser_semantic_predicate_matches_with_local(
&mut self,
predicates: &[(usize, usize, ParserPredicate)],
rule_index: usize,
pred_index: usize,
local_int_arg: i32,
) -> bool
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.
Sourcepub 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
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.
Sourcepub fn parser_semantic_predicate_failure_message(
&self,
rule_index: usize,
pred_index: usize,
predicates: &[(usize, usize, ParserPredicate)],
) -> Option<&'static str>
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.
Sourcepub fn match_wildcard(&mut self) -> Result<ParseTree, AntlrError>
pub fn match_wildcard(&mut self) -> Result<ParseTree, AntlrError>
Matches any non-EOF token.
Sourcepub fn sync(&mut self, state: isize) -> Result<(), AntlrError>
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.
Sourcepub fn sync_decision(
&mut self,
atn: &Atn,
state_number: usize,
current_context_empty: bool,
loop_back: bool,
) -> Result<Vec<ParseTree>, AntlrError>
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.
Sourcepub fn ll1_decision_prediction(
&mut self,
atn: &Atn,
state_number: usize,
) -> Option<ParserAtnPrediction>
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.
Sourcepub fn no_viable_alternative_error(&mut self, start_index: usize) -> AntlrError
pub fn no_viable_alternative_error(&mut self, start_index: usize) -> AntlrError
Builds a generated no-viable-alternative parser error.
Sourcepub fn no_viable_alternative_error_at(
&mut self,
start_index: usize,
error_index: usize,
) -> AntlrError
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.
Sourcepub fn failed_predicate_error(
&mut self,
message: impl Into<String>,
) -> AntlrError
pub fn failed_predicate_error( &mut self, message: impl Into<String>, ) -> AntlrError
Builds a generated failed-predicate parser error.
Sourcepub fn failed_predicate_option_error(
&mut self,
rule_index: usize,
message: impl Into<String>,
) -> AntlrError
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.
Sourcepub fn parser_action_at_current(
&mut self,
source_state: usize,
rule_index: usize,
start_index: usize,
consumed_eof: bool,
) -> ParserAction
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.
Sourcepub fn parse_atn_rule_adaptive_or_fallback<'atn>(
&mut self,
atn: &'atn Atn,
simulator: &mut ParserAtnSimulator<'atn>,
rule_index: usize,
) -> Result<ParseTree, AntlrError>
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.
Sourcepub fn parse_atn_rule(
&mut self,
atn: &Atn,
rule_index: usize,
) -> Result<ParseTree, AntlrError>
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.
Sourcepub fn parse_atn_rule_with_precedence(
&mut self,
atn: &Atn,
rule_index: usize,
precedence: i32,
) -> Result<ParseTree, AntlrError>
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.
Sourcepub fn parse_atn_rule_with_actions(
&mut self,
atn: &Atn,
rule_index: usize,
) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>
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.
Sourcepub fn parse_atn_rule_with_action_inits(
&mut self,
atn: &Atn,
rule_index: usize,
init_action_rules: &[usize],
) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>
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.
Sourcepub 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>
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.
Sourcepub fn parse_atn_rule_with_runtime_options(
&mut self,
atn: &Atn,
rule_index: usize,
options: ParserRuntimeOptions<'_>,
) -> Result<(ParseTree, Vec<ParserAction>), AntlrError>
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.
Sourcepub 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>
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.
Sourcepub fn parse_interpreted_rule(
&mut self,
rule_index: usize,
) -> Result<ParseTree, AntlrError>
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.
Sourcepub fn after_action_stop_index(&mut self, current_index: usize) -> Option<usize>
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.
Sourcepub fn after_action_stop_index_for_tree(
&mut self,
tree: &ParseTree,
current_index: usize,
) -> Option<usize>
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.
Sourcepub fn after_action_start_index_for_tree(
&self,
tree: &ParseTree,
fallback_index: usize,
) -> usize
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.
Sourcepub fn text_interval(&mut self, start: usize, stop: Option<usize>) -> String
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.
Sourcepub fn expected_tokens_at_state(&self, atn: &Atn, state_number: usize) -> String
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.
Sourcepub fn token_display_at(&mut self, index: usize) -> Option<String>
pub fn token_display_at(&mut self, index: usize) -> Option<String>
Formats a buffered token in ANTLR’s diagnostic token display form.