oak_core/builder/
mod.rs

1use crate::{GreenBuilder, IncrementalCache, Language, OakDiagnostics, source::Source};
2
3/// Builder trait for constructing typed kind trees from source text.
4///
5/// This trait provides a unified interface for building complete kind trees
6/// from source text, supporting both full parsing and incremental updates.
7/// It coordinates the lexing, parsing, and AST construction phases.
8pub trait Builder<L: Language> {
9    /// Builds a complete kind tree from the given source text.
10    ///
11    /// This method performs a full parse of the source text, creating a new
12    /// kind tree from scratch. It uses a default cache configuration.
13    ///
14    /// # Arguments
15    ///
16    /// * `text` - The source text to parse
17    ///
18    /// # Returns
19    ///
20    /// A diagnostics result containing either the parsed kind tree or errors
21    fn build(&self, text: impl Source) -> OakDiagnostics<L::TypedRoot> {
22        let mut pool = GreenBuilder::new(0);
23        let cache = IncrementalCache::new(&mut pool);
24        self.build_incremental(text, 0, cache)
25    }
26
27    /// Builds a kind tree incrementally using an existing cache.
28    ///
29    /// This method enables efficient re-parsing by reusing information from
30    /// previous parsing operations, only processing the changed portions
31    /// of the source text.
32    ///
33    /// # Arguments
34    ///
35    /// * `text` - The source text to parse
36    /// * `changed` - The number of bytes that have changed since the last parse
37    /// * `cache` - The incremental cache containing previous parsing results
38    ///
39    /// # Returns
40    ///
41    /// A diagnostics result containing either the parsed kind tree or errors
42    fn build_incremental(&self, text: impl Source, changed: usize, cache: IncrementalCache<L>) -> OakDiagnostics<L::TypedRoot>;
43}