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: SThe source text being parsed
cache: IncrementalCache<'a, L>The incremental cache containing tokens and previous parse results
index: usizeCurrent 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>
impl<'a, S: Source, L: Language> ParserState<'a, S, L>
Sourcepub fn new_with_cache(
source: S,
change: usize,
cache: IncrementalCache<'a, L>,
) -> Self
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.
Sourcepub fn not_at_end(&self) -> bool
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
Sourcepub fn current(&self) -> Option<&Token<L::SyntaxKind>>
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
Sourcepub fn previous(&self) -> Option<&Token<L::SyntaxKind>>
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
Sourcepub fn advance(&mut self) -> Option<&Token<L::SyntaxKind>>
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
Sourcepub fn peek_kind(&self) -> Option<L::SyntaxKind>
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
Sourcepub fn match_kind(&mut self, kinds: &[L::SyntaxKind]) -> bool
pub fn match_kind(&mut self, kinds: &[L::SyntaxKind]) -> bool
Sourcepub fn record_error_at(&mut self, position: usize, msg: impl Into<String>)
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 occurredmsg- The error message to record
Sourcepub fn record_unexpected(&mut self, msg: impl Into<String>)
pub fn record_unexpected(&mut self, msg: impl Into<String>)
Sourcepub fn consume(
&mut self,
kind: L::SyntaxKind,
msg: impl Into<String>,
) -> Option<Token<L::SyntaxKind>>
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 kindmsg- 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
Sourcepub fn finish(self, result: Result<(), OakError>) -> ParseOutput<L>
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