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 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, inline) 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 inline 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 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_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(&mut self, message: String)
pub fn error(&mut self, message: String)
Record a parse error at the current position (zero-length range).
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 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 paren_depth(&self) -> u32
pub fn paren_depth(&self) -> u32
Get the current parenthesis nesting depth.
Sourcepub fn brace_depth(&self) -> u32
pub fn brace_depth(&self) -> u32
Get the current brace nesting depth.
Sourcepub fn bracket_depth(&self) -> u32
pub fn bracket_depth(&self) -> u32
Get the current bracket nesting depth.
Sourcepub fn skip_to_balanced(&mut self, open: SyntaxKind, close: SyntaxKind) -> bool
pub fn skip_to_balanced(&mut self, open: SyntaxKind, close: SyntaxKind) -> bool
Skip tokens until we reach a balanced closing delimiter at depth 0.
This is useful for recovering from errors inside parenthesized expressions, blocks, or array literals. It consumes tokens while tracking delimiter depth, stopping when the matching close delimiter would bring us back to the starting depth.
Returns true if a balanced delimiter was found, false if EOF was reached.
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