pub struct Parser<'t, 's> { /* private fields */ }Expand description
The parser state.
This struct maintains the state needed to build a rowan syntax tree from a token stream. It provides methods for token inspection, consumption, and tree building.
Implementations§
Source§impl Parser<'_, '_>
impl Parser<'_, '_>
Sourcepub const EXPR_CONTINUATION: &'static [SyntaxKind]
pub const EXPR_CONTINUATION: &'static [SyntaxKind]
Tokens that may follow a complete expression (binary/postfix operators).
Sourcepub fn parse_expr(&mut self) -> Option<CompletedMarker>
pub fn parse_expr(&mut self) -> Option<CompletedMarker>
Parse an expression.
Sourcepub fn parse_expr_with_opts(
&mut self,
opts: ExprOpts,
) -> Option<CompletedMarker>
pub fn parse_expr_with_opts( &mut self, opts: ExprOpts, ) -> Option<CompletedMarker>
Parse an expression with options.
Source§impl Parser<'_, '_>
impl Parser<'_, '_>
Sourcepub fn parse_file_items(&mut self)
pub fn parse_file_items(&mut self)
Parse a complete file.
A file may contain one or more program sections, each consisting of
optional imports followed by a program declaration. Multi-program
files appear in Leo test suites where programs are separated by
// --- Next Program --- // comments (the comment itself is trivia
and doesn’t affect parsing).
Module-level items (const, struct, fn) are also accepted
at the top level to support multi-section test files that combine
program declarations with module content separated by
// --- Next Module: path --- // comments.
Sourcepub fn parse_module_items(&mut self)
pub fn parse_module_items(&mut self)
Parse module-level items.
Module files contain only const, struct, and fn declarations
(with optional annotations). No import or program blocks.
Source§impl Parser<'_, '_>
impl Parser<'_, '_>
Sourcepub fn parse_stmt(&mut self) -> Option<CompletedMarker>
pub fn parse_stmt(&mut self) -> Option<CompletedMarker>
Parse a statement.
Sourcepub fn parse_block(&mut self) -> Option<CompletedMarker>
pub fn parse_block(&mut self) -> Option<CompletedMarker>
Parse a block: { stmts... }
Source§impl Parser<'_, '_>
impl Parser<'_, '_>
Sourcepub const PRIMITIVE_TYPE_KINDS: &'static [SyntaxKind]
pub const PRIMITIVE_TYPE_KINDS: &'static [SyntaxKind]
Primitive type keywords allowed in cast expressions.
Sourcepub fn parse_type(&mut self) -> Option<CompletedMarker>
pub fn parse_type(&mut self) -> Option<CompletedMarker>
Parse a type expression.
Returns None if the current token cannot start a type.
Sourcepub fn parse_type_with_opts(
&mut self,
opts: TypeOpts,
) -> Option<CompletedMarker>
pub fn parse_type_with_opts( &mut self, opts: TypeOpts, ) -> Option<CompletedMarker>
Parse a type expression with options.
Sourcepub fn parse_cast_type(&mut self) -> Option<CompletedMarker>
pub fn parse_cast_type(&mut self) -> Option<CompletedMarker>
Parse a cast type (primitive types and dyn record).
Sourcepub fn parse_const_param_list(&mut self)
pub fn parse_const_param_list(&mut self)
Parse a const generic parameter list (declaration site): ::[N: u32, M: u32].
Wraps the list in a CONST_PARAM_LIST node. Each parameter is
wrapped in a CONST_PARAM node containing name: Type.
Sourcepub fn parse_const_generic_args_bracket(&mut self)
pub fn parse_const_generic_args_bracket(&mut self)
Parse const generic arguments with bracket syntax (use site): ::[N] or ::[N + 1, u32].
Each argument may be an expression or a type (for intrinsics).
Wraps the list in a CONST_ARG_LIST node.
Sourcepub fn parse_const_generic_args_angle(&mut self)
pub fn parse_const_generic_args_angle(&mut self)
Parse const generic arguments with angle bracket syntax: ::<N> or ::<N, M>.
Only accepts simple IDENT/INTEGER arguments because > conflicts
with the expression parser’s greater-than operator. Angle bracket
generics are low priority (the LALRPOP grammar doesn’t use them).
Source§impl<'t, 's> Parser<'t, 's>
impl<'t, 's> Parser<'t, 's>
Sourcepub fn new(source: &'s str, tokens: &'t [Token]) -> Self
pub fn new(source: &'s str, tokens: &'t [Token]) -> Self
Create a new parser for the given source and tokens.
Sourcepub fn start(&mut self) -> Marker
pub fn start(&mut self) -> Marker
Start a new node, returning a Marker.
The marker must be completed with complete() or abandoned with
abandon(). Dropping it without doing either will panic.
Sourcepub fn current(&self) -> SyntaxKind
pub fn current(&self) -> SyntaxKind
Get the kind of the current token (or EOF if at end).
Sourcepub fn nth(&self, n: usize) -> SyntaxKind
pub fn nth(&self, n: usize) -> SyntaxKind
Look ahead by n tokens and return the kind (skipping trivia).
Sourcepub fn current_including_trivia(&self) -> SyntaxKind
pub fn current_including_trivia(&self) -> SyntaxKind
Get the kind of the current token including trivia.
Sourcepub fn at(&self, kind: SyntaxKind) -> bool
pub fn at(&self, kind: SyntaxKind) -> bool
Check if the current token matches the given kind.
Sourcepub fn at_any(&self, kinds: &[SyntaxKind]) -> bool
pub fn at_any(&self, kinds: &[SyntaxKind]) -> bool
Check if the current token matches any of the given kinds.
Sourcepub fn current_text(&self) -> &'s str
pub fn current_text(&self) -> &'s str
Get the text of the current token.
Sourcepub fn skip_trivia(&mut self)
pub fn skip_trivia(&mut self)
Skip trivia tokens, adding them to the tree.
Sourcepub fn eat(&mut self, kind: SyntaxKind) -> bool
pub fn eat(&mut self, kind: SyntaxKind) -> bool
Consume the current token if it matches the given kind. Returns true if consumed.
Sourcepub fn start_node(&mut self, kind: SyntaxKind)
pub fn start_node(&mut self, kind: SyntaxKind)
Start a new node of the given kind.
Used by error recovery. For general parsing, prefer start() and
Marker::complete().
Sourcepub fn finish_node(&mut self)
pub fn finish_node(&mut self)
Finish the current node.
Sourcepub fn checkpoint(&self) -> Checkpoint
pub fn checkpoint(&self) -> Checkpoint
Create a checkpoint for later wrapping.
Sourcepub fn start_node_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind)
pub fn start_node_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind)
Start a node at a previous checkpoint.
Sourcepub fn expect(&mut self, kind: SyntaxKind) -> bool
pub fn expect(&mut self, kind: SyntaxKind) -> bool
Expect a token of the given kind.
If the current token matches, it is consumed. Otherwise, an error is recorded but no token is consumed.
Sourcepub fn error_unexpected(&mut self, found_kind: SyntaxKind, expected: &[&str])
pub fn error_unexpected(&mut self, found_kind: SyntaxKind, expected: &[&str])
Record an “unexpected token” error with explicit expected tokens.
This sets both found and expected fields, allowing rowan.rs to emit
a ParserError::unexpected instead of ParserError::custom.
The found_kind parameter should be the SyntaxKind of the unexpected token.
Sourcepub fn error_recover(&mut self, message: &str, recovery: &[SyntaxKind])
pub fn error_recover(&mut self, message: &str, recovery: &[SyntaxKind])
Wrap unexpected tokens in an ERROR node until we reach a recovery point.
This consumes tokens until we see one of the recovery tokens or EOF. If already at a recovery token, consumes it to ensure progress is made. The error range spans the tokens wrapped in the ERROR node.
Sourcepub fn recover(&mut self, recovery: &[SyntaxKind])
pub fn recover(&mut self, recovery: &[SyntaxKind])
Skip tokens until a recovery point, wrapping them in an ERROR node.
Unlike error_recover, this does not emit an error message (useful when
the caller has already reported the error).
Balanced brace pairs ({ ... }) encountered during recovery are
consumed as a unit so that we don’t stop at an inner } that belongs
to a nested block.
Sourcepub fn error_and_bump(&mut self, message: &str)
pub fn error_and_bump(&mut self, message: &str)
Create an ERROR node containing the current token. The error range spans the single consumed token.
Sourcepub fn error_count(&self) -> usize
pub fn error_count(&self) -> usize
Get the current number of accumulated parse errors.
Auto Trait Implementations§
impl<'t, 's> Freeze for Parser<'t, 's>
impl<'t, 's> RefUnwindSafe for Parser<'t, 's>
impl<'t, 's> Send for Parser<'t, 's>
impl<'t, 's> Sync for Parser<'t, 's>
impl<'t, 's> Unpin for Parser<'t, 's>
impl<'t, 's> UnsafeUnpin for Parser<'t, 's>
impl<'t, 's> !UnwindSafe for Parser<'t, 's>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more