Skip to main content

Parser

Struct Parser 

Source
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<'_, '_>

Source

pub const EXPR_CONTINUATION: &'static [SyntaxKind]

Tokens that may follow a complete expression (binary/postfix operators).

Source

pub fn parse_expr(&mut self) -> Option<CompletedMarker>

Parse an expression.

Source

pub fn parse_expr_with_opts( &mut self, opts: ExprOpts, ) -> Option<CompletedMarker>

Parse an expression with options.

Source§

impl Parser<'_, '_>

Source

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.

Source

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<'_, '_>

Source

pub fn parse_stmt(&mut self) -> Option<CompletedMarker>

Parse a statement.

Source

pub fn parse_block(&mut self) -> Option<CompletedMarker>

Parse a block: { stmts... }

Source§

impl Parser<'_, '_>

Source

pub const PRIMITIVE_TYPE_KINDS: &'static [SyntaxKind]

Primitive type keywords allowed in cast expressions.

Source

pub fn parse_type(&mut self) -> Option<CompletedMarker>

Parse a type expression.

Returns None if the current token cannot start a type.

Source

pub fn parse_type_with_opts( &mut self, opts: TypeOpts, ) -> Option<CompletedMarker>

Parse a type expression with options.

Source

pub fn parse_cast_type(&mut self) -> Option<CompletedMarker>

Parse a cast type (primitive types and dyn record).

Source

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.

Source

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.

Source

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>

Source

pub fn new(source: &'s str, tokens: &'t [Token]) -> Self

Create a new parser for the given source and tokens.

Source

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.

Source

pub fn current(&self) -> SyntaxKind

Get the kind of the current token (or EOF if at end).

Source

pub fn nth(&self, n: usize) -> SyntaxKind

Look ahead by n tokens and return the kind (skipping trivia).

Source

pub fn current_including_trivia(&self) -> SyntaxKind

Get the kind of the current token including trivia.

Source

pub fn at(&self, kind: SyntaxKind) -> bool

Check if the current token matches the given kind.

Source

pub fn at_any(&self, kinds: &[SyntaxKind]) -> bool

Check if the current token matches any of the given kinds.

Source

pub fn at_eof(&self) -> bool

Check if we’re at the end of the token stream.

Source

pub fn current_text(&self) -> &'s str

Get the text of the current token.

Source

pub fn bump(&mut self)

Consume the current token and add it to the tree.

Source

pub fn skip_trivia(&mut self)

Skip trivia tokens, adding them to the tree.

Source

pub fn eat(&mut self, kind: SyntaxKind) -> bool

Consume the current token if it matches the given kind. Returns true if consumed.

Source

pub fn bump_any(&mut self)

Consume any trivia and then the next token, regardless of kind.

Source

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().

Source

pub fn finish_node(&mut self)

Finish the current node.

Source

pub fn checkpoint(&self) -> Checkpoint

Create a checkpoint for later wrapping.

Source

pub fn start_node_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind)

Start a node at a previous checkpoint.

Source

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.

Source

pub fn error(&mut self, message: impl Display)

Record a parse error at the current position.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn error_count(&self) -> usize

Get the current number of accumulated parse errors.

Source

pub fn finish(self, lex_errors: Vec<LexError>) -> Parse

Finish parsing and return the parse result.

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.