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 SWe keep a reference to help with error reporting and offset calculation.
Implementations§
Source§impl<'a, L: Language, S: Source + ?Sized> ParserState<'a, L, S>
impl<'a, L: Language, S: Source + ?Sized> ParserState<'a, L, S>
Sourcepub fn source_url(&self) -> Option<Url>
pub fn source_url(&self) -> Option<Url>
Returns the source file’s URL, if available.
Sourcepub fn new(
arena: &'a SyntaxArena,
lex_output: LexOutput<L>,
source: &'a S,
capacity_hint: usize,
) -> Self
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.
Sourcepub fn nested(&self) -> ParserState<'a, L, S>
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.
Sourcepub fn promote(&mut self, node: &'a GreenNode<'a, L>) -> &'a GreenNode<'a, L>
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.
Sourcepub fn set_incremental(&mut self, old: &'a GreenNode<'a, L>, edits: &[TextEdit])
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.
Sourcepub fn current_offset(&self) -> usize
pub fn current_offset(&self) -> usize
Returns the current byte offset.
Sourcepub fn syntax_error(&mut self, message: impl Into<String>) -> OakError
pub fn syntax_error(&mut self, message: impl Into<String>) -> OakError
Records a syntax error with the given message.
Sourcepub fn record_unexpected_token(&mut self, token: impl Into<String>)
pub fn record_unexpected_token(&mut self, token: impl Into<String>)
Records an unexpected token error.
Sourcepub fn record_expected(&mut self, expected: impl Into<String>)
pub fn record_expected(&mut self, expected: impl Into<String>)
Records an expected token error.
Sourcepub fn record_expected_name(&mut self, name_kind: impl Into<String>)
pub fn record_expected_name(&mut self, name_kind: impl Into<String>)
Records an expected name error.
Sourcepub fn record_trailing_comma_not_allowed(&mut self)
pub fn record_trailing_comma_not_allowed(&mut self)
Records a trailing comma not allowed error.
Sourcepub fn record_unexpected_eof(&mut self)
pub fn record_unexpected_eof(&mut self)
Records an unexpected end of file error.
Sourcepub fn unexpected_eof(&mut self) -> OakError
pub fn unexpected_eof(&mut self) -> OakError
Records an unexpected end of file error and returns it.
Sourcepub fn peek_at(&self, offset: usize) -> Option<&Token<L::TokenType>>
pub fn peek_at(&self, offset: usize) -> Option<&Token<L::TokenType>>
Peeks at a token at the specified offset from the current position.
Sourcepub fn peek_kind_at(&self, offset: usize) -> Option<L::TokenType>
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.
Sourcepub fn not_at_end(&self) -> bool
pub fn not_at_end(&self) -> bool
Checks if the parser has not yet reached the end of the token stream.
Sourcepub fn at(&self, kind: L::TokenType) -> bool
pub fn at(&self, kind: L::TokenType) -> bool
Checks if the current token is of the specified kind.
Sourcepub fn advance_until(&mut self, kind: L::TokenType)
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.
Sourcepub fn advance_until_any(&mut self, kinds: &[L::TokenType])
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.
Sourcepub fn bump(&mut self)
pub fn bump(&mut self)
Consumes the current token and adds it to the syntax tree as a leaf node.
Sourcepub fn eat(&mut self, kind: L::TokenType) -> bool
pub fn eat(&mut self, kind: L::TokenType) -> bool
Consumes the current token if it matches the specified kind.
Returns true if the token was consumed, false otherwise.
Sourcepub fn expect(&mut self, kind: L::TokenType) -> Result<(), OakError>
pub fn expect(&mut self, kind: L::TokenType) -> Result<(), OakError>
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.
Sourcepub fn checkpoint(&self) -> usize
pub fn checkpoint(&self) -> usize
Creates a checkpoint in the tree construction process, which can be used to finish a node later.
Sourcepub fn finish_at(
&mut self,
checkpoint: usize,
kind: L::ElementType,
) -> &'a GreenNode<'a, L>
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.
Sourcepub fn push_child(&mut self, node: &'a GreenNode<'a, L>)
pub fn push_child(&mut self, node: &'a GreenNode<'a, L>)
Adds an existing node as a child to the current node.
Sourcepub fn try_reuse(&mut self, kind: L::ElementType) -> bool
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.
Sourcepub fn finish(
self,
result: Result<&'a GreenNode<'a, L>, OakError>,
) -> ParseOutput<'a, L>
pub fn finish( self, result: Result<&'a GreenNode<'a, L>, OakError>, ) -> ParseOutput<'a, L>
Finishes the parsing process and returns the final output.
Sourcepub fn incremental_node<F>(
&mut self,
kind: L::ElementType,
f: F,
) -> Result<(), OakError>
pub fn incremental_node<F>( &mut self, kind: L::ElementType, f: F, ) -> 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.
Sourcepub fn incremental_opt<F>(
&mut self,
kind: L::ElementType,
f: F,
) -> Result<bool, OakError>
pub fn incremental_opt<F>( &mut self, kind: L::ElementType, f: F, ) -> 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.