Skip to main content

Parser

Trait Parser 

Source
pub trait Parser<L: Language + Send + Sync>
where L::ElementType: From<L::TokenType>,
{ // 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:

  1. Take a sequence of tokens produced by a Lexer.
  2. Build a GreenNode tree representing the hierarchical structure of the source.
  3. Handle incremental updates by reusing nodes from a previous GreenNode tree.

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

Source

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 of TextEdits representing changes since the last parse. Used for incremental parsing.
  • cache - The ParseCache for 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", so this trait is not object safe.

Implementors§