Skip to main content

oak_core/builder/
mod.rs

1use crate::{
2    Language,
3    errors::OakDiagnostics,
4    source::{Source, TextEdit},
5    tree::GreenNode,
6};
7
8/// Output of a builder operation.
9pub type BuildOutput<L: Language> = OakDiagnostics<L::TypedRoot>;
10
11/// Trait for building higher-level structures (like ASTs) from source text.
12pub trait Builder<L: Language> {
13    /// Builds the structure from the source text.
14    fn build<'a, S: Source + ?Sized>(&self, text: &S, edits: &[TextEdit], cache: &'a mut impl BuilderCache<L>) -> BuildOutput<L>;
15}
16
17/// Cache trait for builder operations used by lexers and parsers.
18///
19/// This trait defines the interface for building green tree nodes incrementally.
20/// It provides methods for adding tokens and nodes to the tree structure.
21pub trait BuilderCache<L: Language>: crate::parser::ParseCache<L> {
22    /// Gets a cached typed node for a given green node.
23    fn get_typed_node<T: 'static + Send + Sync + Clone>(&self, green: &GreenNode<L>) -> Option<T>;
24
25    /// Caches a typed node for a given green node.
26    fn set_typed_node<T: 'static + Send + Sync>(&mut self, green: &GreenNode<L>, typed: T);
27}
28
29impl<'a, L: Language, C: BuilderCache<L> + ?Sized> BuilderCache<L> for &'a mut C {
30    fn get_typed_node<T: 'static + Send + Sync + Clone>(&self, green: &GreenNode<L>) -> Option<T> {
31        (**self).get_typed_node(green)
32    }
33
34    fn set_typed_node<T: 'static + Send + Sync>(&mut self, green: &GreenNode<L>, typed: T) {
35        (**self).set_typed_node(green, typed)
36    }
37}
38
39impl<L: Language + Send + Sync> BuilderCache<L> for crate::parser::ParseSession<L> {
40    fn get_typed_node<T: 'static + Send + Sync + Clone>(&self, green: &GreenNode<L>) -> Option<T> {
41        self.get_typed_node(green)
42    }
43
44    fn set_typed_node<T: 'static + Send + Sync>(&mut self, green: &GreenNode<L>, typed: T) {
45        self.set_typed_node(green, typed)
46    }
47}