pub trait Parser<L: Language + Send + Sync>{
// Required method
fn parse<'a, S: Source + ?Sized>(
&self,
text: &'a S,
edits: &[TextEdit],
cache: &'a mut impl ParseCache<L>,
) -> ParseOutput<'a, L>;
}Expand description
Core parser trait that defines the interface for language parsers.
This trait is responsible for converting a stream of tokens into a green tree (a lossless, immutable syntax tree). It supports incremental parsing by taking previous edits and using a cache for reuse.
§Usage Scenario
The Parser is typically used after lexical analysis to:
- Take a sequence of tokens produced by a
Lexer. - Build a
GreenNodetree representing the hierarchical structure of the source. - Handle incremental updates by reusing nodes from a previous
GreenNodetree.
§Incremental Parsing
The parse method should ideally be able to reuse nodes from a previous
parse if the source has only changed partially. This is facilitated by
the ParseCache and the provided TextEdits.
Required Methods§
Sourcefn parse<'a, S: Source + ?Sized>(
&self,
text: &'a S,
edits: &[TextEdit],
cache: &'a mut impl ParseCache<L>,
) -> ParseOutput<'a, L>
fn parse<'a, S: Source + ?Sized>( &self, text: &'a S, edits: &[TextEdit], cache: &'a mut impl ParseCache<L>, ) -> ParseOutput<'a, L>
The core parsing entry point for converting tokens into a syntax tree.
This method orchestrates the parsing process. It performs lexical analysis (if not already cached) and then builds a green tree structure. It handles incremental reuse automatically if the cache contains a previous tree.
§Arguments
text- The source text to parse.edits- A slice ofTextEdits representing changes since the last parse. Used for incremental parsing.cache- TheParseCachefor resources, incremental reuse, and diagnostics.
§Returns
A ParseOutput containing the root GreenNode and any diagnostics.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".