ParserState

Struct ParserState 

Source
pub struct ParserState<'a, L: Language, S: Source + ?Sized = SourceText> {
    pub tokens: TokenSource<L>,
    pub sink: TreeSink<'a, L>,
    pub incremental: Option<IncrementalContext<'a, L>>,
    pub errors: Vec<OakError>,
    pub source: &'a S,
}
Expand description

High-level API for parsers, coordinating token supply and tree construction.

Fields§

§tokens: TokenSource<L>

The token stream being parsed.

§sink: TreeSink<'a, L>

The sink where the syntax tree is constructed.

§incremental: Option<IncrementalContext<'a, L>>

Optional context for incremental parsing.

§errors: Vec<OakError>

Collection of errors encountered during parsing.

§source: &'a S

We keep a reference to help with error reporting and offset calculation.

Implementations§

Source§

impl<'a, L: Language, S: Source + ?Sized> ParserState<'a, L, S>

Source

pub fn source_url(&self) -> Option<Url>

Returns the source file’s URL, if available.

Source

pub fn new( arena: &'a SyntaxArena, lex_output: LexOutput<L>, source: &'a S, capacity_hint: usize, ) -> Self

Creates a new parser state.

§Arguments
  • arena - The syntax arena to allocate nodes in.
  • lex_output - The output from the lexer, including tokens and any lexical errors.
  • source - The source text being parsed.
  • capacity_hint - Initial capacity hint for the tree sink’s child vector.
Source

pub fn nested(&self) -> ParserState<'a, L, S>

Creates a nested parser state that shares the same arena and source. This is useful for parsing sub-structures that should be independent but part of the same overall tree.

Source

pub fn peek_text(&self) -> Option<Cow<'_, str>>

Returns the text content of the current token.

Source

pub fn promote(&mut self, node: &'a GreenNode<'a, L>) -> &'a GreenNode<'a, L>

Manually promotes a node from a previous generation to the current one. This is useful for improving memory locality.

Source

pub fn set_incremental(&mut self, old: &'a GreenNode<'a, L>, edits: &[TextEdit])

Sets the incremental parsing context.

§Arguments
  • old - The root of the previous version of the syntax tree.
  • edits - The edits that were applied to the source text.
Source

pub fn current_offset(&self) -> usize

Returns the current byte offset.

Source

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

Records a syntax error with the given message.

Source

pub fn record_unexpected_token(&mut self, token: impl Into<String>)

Records an unexpected token error.

Source

pub fn record_expected(&mut self, expected: impl Into<String>)

Records an expected token error.

Source

pub fn record_expected_name(&mut self, name_kind: impl Into<String>)

Records an expected name error.

Source

pub fn record_trailing_comma_not_allowed(&mut self)

Records a trailing comma not allowed error.

Source

pub fn record_unexpected_eof(&mut self)

Records an unexpected end of file error.

Source

pub fn unexpected_eof(&mut self) -> OakError

Records an unexpected end of file error and returns it.

Source

pub fn current(&self) -> Option<&Token<L::TokenType>>

Returns the current token.

Source

pub fn peek_kind(&self) -> Option<L::TokenType>

Returns the kind of the current token.

Source

pub fn peek_at(&self, offset: usize) -> Option<&Token<L::TokenType>>

Peeks at a token at the specified offset from the current position.

Source

pub fn peek_kind_at(&self, offset: usize) -> Option<L::TokenType>

Returns the kind of the token at the specified offset from the current position.

Source

pub fn not_at_end(&self) -> bool

Checks if the parser has not yet reached the end of the token stream.

Source

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

Checks if the current token is of the specified kind.

Source

pub fn advance(&mut self)

Advances the current position to the next token.

Source

pub fn advance_until(&mut self, kind: L::TokenType)

Advances until a token of the specified kind is found, or the end of the token stream is reached.

Source

pub fn advance_until_any(&mut self, kinds: &[L::TokenType])

Advances until any token of the specified kinds is found, or the end of the token stream is reached.

Source

pub fn bump(&mut self)
where L::ElementType: From<L::TokenType>,

Consumes the current token and adds it to the syntax tree as a leaf node.

Source

pub fn eat(&mut self, kind: L::TokenType) -> bool
where L::ElementType: From<L::TokenType>,

Consumes the current token if it matches the specified kind. Returns true if the token was consumed, false otherwise.

Source

pub fn expect(&mut self, kind: L::TokenType) -> Result<(), OakError>
where L::ElementType: From<L::TokenType>,

Expects the current token to be of the specified kind, consuming it if it matches. If the token does not match, an error is recorded and returned.

Source

pub fn checkpoint(&self) -> usize

Creates a checkpoint in the tree construction process, which can be used to finish a node later.

Source

pub fn finish_at( &mut self, checkpoint: usize, kind: L::ElementType, ) -> &'a GreenNode<'a, L>

Finishes a node starting from the given checkpoint and adds it as a child.

Source

pub fn push_child(&mut self, node: &'a GreenNode<'a, L>)

Adds an existing node as a child to the current node.

Source

pub fn try_reuse(&mut self, kind: L::ElementType) -> bool

Attempts to reuse a previous syntax node at the current position. This is the core of incremental parsing.

Source

pub fn finish( self, result: Result<&'a GreenNode<'a, L>, OakError>, ) -> ParseOutput<'a, L>

Finishes the parsing process and returns the final output.

Source

pub fn incremental_node<F>( &mut self, kind: L::ElementType, f: F, ) -> Result<(), OakError>
where F: FnOnce(&mut Self) -> Result<(), OakError>,

Wraps the parsing of a node with incremental reuse support.

This method first attempts to reuse an existing node of the given kind from the previous tree. If successful, it skips the provided closure and returns Ok(()). Otherwise, it creates a checkpoint, runs the closure to parse the node’s content, and then finishes the node with the specified kind.

Source

pub fn incremental_opt<F>( &mut self, kind: L::ElementType, f: F, ) -> Result<bool, OakError>
where F: FnOnce(&mut Self) -> Result<bool, OakError>,

Wraps the parsing of an optional node with incremental reuse support.

Similar to incremental_node, but the closure returns a boolean indicating if the node was actually present.

Auto Trait Implementations§

§

impl<'a, L, S> Freeze for ParserState<'a, L, S>
where <L as Language>::TokenType: Freeze, S: ?Sized,

§

impl<'a, L, S = SourceText> !RefUnwindSafe for ParserState<'a, L, S>

§

impl<'a, L, S> Send for ParserState<'a, L, S>
where S: ?Sized,

§

impl<'a, L, S> Sync for ParserState<'a, L, S>
where S: ?Sized,

§

impl<'a, L, S> Unpin for ParserState<'a, L, S>
where <L as Language>::TokenType: Unpin, S: ?Sized,

§

impl<'a, L, S = SourceText> !UnwindSafe for ParserState<'a, L, 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, 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.