1use std::num::NonZeroUsize;
2
3use crate::{
4 cow_mut::CowMut,
5 green::{node_cache::NodeCache, GreenElement, GreenNode, SyntaxKind},
6 NodeOrToken,
7};
8
9#[derive(Clone, Copy, Debug)]
11pub struct Checkpoint(NonZeroUsize);
12
13impl Checkpoint {
14 fn new(inner: usize) -> Self {
15 Self(NonZeroUsize::new(inner + 1).unwrap())
16 }
17
18 fn into_inner(self) -> usize {
19 self.0.get() - 1
20 }
21}
22
23#[derive(Default, Debug)]
25pub struct GreenNodeBuilder<'cache> {
26 cache: CowMut<'cache, NodeCache>,
27 parents: Vec<(SyntaxKind, usize)>,
28 children: Vec<(u64, GreenElement)>,
29}
30
31impl GreenNodeBuilder<'_> {
32 pub fn new() -> GreenNodeBuilder<'static> {
34 GreenNodeBuilder::default()
35 }
36
37 pub fn with_cache(cache: &mut NodeCache) -> GreenNodeBuilder<'_> {
40 GreenNodeBuilder {
41 cache: CowMut::Borrowed(cache),
42 parents: Vec::new(),
43 children: Vec::new(),
44 }
45 }
46
47 #[inline]
49 pub fn token(&mut self, kind: SyntaxKind, text: &str) {
50 let (hash, token) = self.cache.token(kind, text);
51 self.children.push((hash, token.into()));
52 }
53
54 #[inline]
56 pub fn start_node(&mut self, kind: SyntaxKind) {
57 let len = self.children.len();
58 self.parents.push((kind, len));
59 }
60
61 #[inline]
64 pub fn finish_node(&mut self) {
65 let (kind, first_child) = self.parents.pop().unwrap();
66 let (hash, node) = self.cache.node(kind, &mut self.children, first_child);
67 self.children.push((hash, node.into()));
68 }
69
70 #[inline]
96 pub fn checkpoint(&self) -> Checkpoint {
97 Checkpoint::new(self.children.len())
98 }
99
100 #[inline]
103 pub fn start_node_at(&mut self, checkpoint: Checkpoint, kind: SyntaxKind) {
104 let checkpoint = checkpoint.into_inner();
105 assert!(
106 checkpoint <= self.children.len(),
107 "checkpoint no longer valid, was finish_node called early?"
108 );
109
110 if let Some(&(_, first_child)) = self.parents.last() {
111 assert!(
112 checkpoint >= first_child,
113 "checkpoint no longer valid, was an unmatched start_node_at called?"
114 );
115 }
116
117 self.parents.push((kind, checkpoint));
118 }
119
120 #[inline]
124 pub fn finish(mut self) -> GreenNode {
125 assert_eq!(self.children.len(), 1);
126 match self.children.pop().unwrap().1 {
127 NodeOrToken::Node(node) => node,
128 NodeOrToken::Token(_) => panic!(),
129 }
130 }
131}