ParserState

Struct ParserState 

Source
pub struct ParserState<'a, S: Source, L: Language> {
    pub source: S,
    pub cache: IncrementalCache<'a, L>,
    pub index: usize,
    pub errors: Vec<OakError>,
}
Expand description

Generic parsing state that encapsulates cursor for token stream and error aggregation.

This struct maintains the current parsing position and provides utilities for consuming tokens, recording errors, and building kind trees incrementally.

§Examples

use core::range::Range;
use oak_core::{SourceText, Token, parser::ParserState};

#[derive(Copy, Clone, PartialEq)]
enum K {
    A,
    B,
    Eof,
}

let source = SourceText::new("ab");
let tokens = [
    Token { kind: K::A, span: Range { start: 0, end: 1 } },
    Token { kind: K::B, span: Range { start: 1, end: 2 } },
    Token { kind: K::Eof, span: Range { start: 2, end: 2 } },
];
let mut st = ParserState::new_with_cache(&source, &tokens);
assert!(st.match_kind(&[K::A]));
assert!(st.match_kind(&[K::B]));
let out = st.finish(Ok(()));
assert!(out.diagnostics.is_empty());

Fields§

§source: S

The source text being parsed

§cache: IncrementalCache<'a, L>

The incremental cache containing tokens and previous parse results

§index: usize

Current position in the token stream

§errors: Vec<OakError>

Collection of errors encountered during parsing

Implementations§

Source§

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

Source

pub fn new_with_cache( source: S, change: usize, cache: IncrementalCache<'a, L>, ) -> Self

Creates a new parser state with the given source text and tokens.

Source

pub fn not_at_end(&self) -> bool

Checks if there are more tokens to consume.

§Returns

true if there are more tokens to parse, false otherwise

Source

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

Returns the current token without consuming it.

§Returns

An optional reference to the current token, or None if at end of stream

Source

pub fn previous(&self) -> Option<&Token<L::SyntaxKind>>

Returns the previous token (the one before the current position).

§Returns

An optional reference to the previous token, or None if at start of stream

Source

pub fn advance(&mut self) -> Option<&Token<L::SyntaxKind>>

Advances to the next token and returns it.

§Returns

An optional reference to the consumed token, or None if at end of stream

Source

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

Returns the kind of the current token without consuming it.

§Returns

An optional token kind, or None if at end of stream

Source

pub fn match_kind(&mut self, kinds: &[L::SyntaxKind]) -> bool

Checks if the current token matches any of the given kinds and consumes it if so.

§Arguments
  • kinds - Array of token kinds to match against
§Returns

true if the current token was consumed (matched), false otherwise

Source

pub fn record_error_at(&mut self, position: usize, msg: impl Into<String>)

Records a kind error at the specified byte position

§Arguments
  • position - The byte position where the error occurred
  • msg - The error message to record
Source

pub fn record_unexpected(&mut self, msg: impl Into<String>)

Records an “unexpected current kind” error

§Arguments
  • msg - The error message to record
Source

pub fn consume( &mut self, kind: L::SyntaxKind, msg: impl Into<String>, ) -> Option<Token<L::SyntaxKind>>

Consumes an expected kind; if it doesn’t match, records an error and returns None (suitable for error recovery)

§Arguments
  • kind - The expected token kind
  • msg - The error message to record if the token doesn’t match
§Returns

An optional token if the expected kind was found and consumed, None otherwise

Source

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

Finishes parsing and returns the final parse output.

This method consumes the parser state and returns a parse output containing either the successfully parsed green tree or parsing errors.

§Arguments
  • result - The parsing result (Ok for success, Err for failure)
§Returns

A parse output containing the green tree or errors

Auto Trait Implementations§

§

impl<'a, S, L> Freeze for ParserState<'a, S, L>
where S: Freeze,

§

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

§

impl<'a, S, L> Send for ParserState<'a, S, L>
where S: Send, <L as Language>::SyntaxKind: Sync,

§

impl<'a, S, L> Sync for ParserState<'a, S, L>
where S: Sync, <L as Language>::SyntaxKind: Sync,

§

impl<'a, S, L> Unpin for ParserState<'a, S, L>
where S: Unpin, <L as Language>::SyntaxKind: Unpin,

§

impl<'a, S, L> !UnwindSafe for ParserState<'a, S, L>

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,