1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Incremental parsing support for the Adze runtime
// This module provides efficient reparsing of edited documents
use crate::parser_v2::{ParseNode, ParserV2};
use adze_glr_core::ParseTable;
use adze_ir::Grammar;
use std::ops::Range;
/// Represents an edit to a document
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Edit {
/// The byte range that was replaced
pub old_range: Range<usize>,
/// The new text that replaces the old range
pub new_text: String,
}
impl Edit {
/// Create a new edit
pub fn new(start: usize, old_end: usize, new_text: String) -> Self {
Edit {
old_range: start..old_end,
new_text,
}
}
/// Create an edit with text
pub fn with_text(old_range: Range<usize>, new_text: String) -> Self {
Edit {
old_range,
new_text,
}
}
/// Get the new length after replacement
pub fn new_length(&self) -> usize {
self.new_text.len()
}
/// Get the change in length
pub fn delta(&self) -> isize {
self.new_text.len() as isize - self.old_range.len() as isize
}
}
/// A parse tree that can be incrementally updated
#[derive(Debug, Clone)]
pub struct IncrementalTree {
/// The root node of the parse tree
pub root: ParseNode,
/// The input text that was parsed
pub text: String,
/// Byte ranges of nodes for efficient edit mapping
node_ranges: Vec<(usize, Range<usize>)>, // (node_id, byte_range)
}
impl IncrementalTree {
/// Create a new incremental tree
pub fn new(root: ParseNode, text: String) -> Self {
let mut tree = IncrementalTree {
root,
text,
node_ranges: Vec::new(),
};
tree.compute_node_ranges();
tree
}
/// Compute byte ranges for all nodes in the tree
fn compute_node_ranges(&mut self) {
self.node_ranges.clear();
let root_clone = self.root.clone();
self.collect_ranges(&root_clone, 0);
}
/// Recursively collect node ranges
fn collect_ranges(&mut self, node: &ParseNode, mut offset: usize) -> usize {
let node_id = self.node_ranges.len();
let start = offset;
if node.children.is_empty() {
// Leaf node - use token length
let len = node.symbol.0 as usize; // Simplified - would need actual token length
self.node_ranges.push((node_id, start..start + len));
offset + len
} else {
// Internal node - sum of children
for child in &node.children {
offset = self.collect_ranges(child, offset);
}
self.node_ranges.push((node_id, start..offset));
offset
}
}
/// Apply an edit to the tree and return affected nodes
pub fn apply_edit(&mut self, edit: &Edit) -> Vec<usize> {
// Find all nodes that intersect with the edit range
let mut affected = Vec::new();
for (node_id, range) in &self.node_ranges {
if range.end > edit.old_range.start && range.start < edit.old_range.end {
affected.push(*node_id);
}
}
// Update text with the actual new text from the edit
let prefix = &self.text[..edit.old_range.start];
let suffix = &self.text[edit.old_range.end..];
self.text = format!("{}{}{}", prefix, &edit.new_text, suffix);
// Adjust node ranges after the edit
let delta = edit.delta();
if delta != 0 {
for (_, range) in &mut self.node_ranges {
if range.start >= edit.old_range.end {
range.start = (range.start as isize + delta) as usize;
range.end = (range.end as isize + delta) as usize;
} else if range.end > edit.old_range.end {
range.end = (range.end as isize + delta) as usize;
}
}
}
affected
}
}
/// Incremental parser that reuses unchanged subtrees
pub struct IncrementalParser {
parser: ParserV2,
}
impl IncrementalParser {
/// Create a new incremental parser
pub fn new(grammar: Grammar, table: ParseTable) -> Self {
IncrementalParser {
parser: ParserV2::new(grammar, table),
}
}
/// Parse with an optional old tree to reuse
pub fn parse_incremental(
&mut self,
tokens: &[crate::parser_v2::Token],
old_tree: Option<&IncrementalTree>,
edits: &[Edit],
) -> Result<IncrementalTree, crate::parser_v2::ParseError> {
if let Some(old_tree) = old_tree {
// Try to reuse parts of the old tree
self.parse_with_reuse(tokens, old_tree, edits)
} else {
// No old tree - parse from scratch
let root = self.parser.parse(tokens.to_vec())?;
let text = tokens
.iter()
.map(|t| t.text.clone())
.map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
.collect::<String>();
Ok(IncrementalTree::new(root, text))
}
}
/// Parse while trying to reuse unchanged subtrees
fn parse_with_reuse(
&mut self,
tokens: &[crate::parser_v2::Token],
old_tree: &IncrementalTree,
edits: &[Edit],
) -> Result<IncrementalTree, crate::parser_v2::ParseError> {
// For now, implement a simple strategy:
// If edits are small and localized, try to reuse unaffected subtrees
let total_edit_size: usize = edits
.iter()
.map(|e| e.old_range.len() + e.new_length())
.sum();
let text_size = old_tree.text.len();
// If edits affect less than 10% of the text, try incremental parsing
if total_edit_size < text_size / 10 {
// Note: This legacy incremental parser is deprecated.
// For production incremental parsing, use `glr_incremental` or `pure_incremental` modules
// which provide full subtree reuse with GLR-aware fork tracking.
// This module is kept only for backward compatibility behind the `legacy-parsers` feature.
}
// Full reparse
let root = self.parser.parse(tokens.to_vec())?;
let text = tokens
.iter()
.map(|t| t.text.clone())
.map(|bytes| String::from_utf8_lossy(&bytes).into_owned())
.collect::<String>();
Ok(IncrementalTree::new(root, text))
}
}
#[cfg(test)]
mod tests {
use super::*;
use adze_ir::SymbolId;
#[test]
fn test_edit_application() {
// Create a simple tree
let root = ParseNode {
symbol: SymbolId(0),
rule_id: None,
children: vec![
ParseNode {
symbol: SymbolId(1),
rule_id: None,
children: vec![],
start_byte: 0,
end_byte: 5,
text: Some(b"hello".to_vec()),
},
ParseNode {
symbol: SymbolId(2),
rule_id: None,
children: vec![],
start_byte: 6,
end_byte: 11,
text: Some(b"world".to_vec()),
},
],
start_byte: 0,
end_byte: 11,
text: None,
};
let mut tree = IncrementalTree::new(root, "hello world".to_string());
// Replace "hello" with "hi"
let edit = Edit::new(0, 5, "hi".to_string());
let affected = tree.apply_edit(&edit);
assert_eq!(tree.text, "hi world");
assert!(!affected.is_empty());
}
#[test]
fn test_edit_delta() {
// Insertion
let edit = Edit::new(5, 5, "abc".to_string());
assert_eq!(edit.delta(), 3);
// Deletion
let edit = Edit::new(5, 10, "".to_string());
assert_eq!(edit.delta(), -5);
// Replacement (same size)
let edit = Edit::new(5, 10, "hello".to_string());
assert_eq!(edit.delta(), 0);
}
#[test]
fn test_affected_nodes() {
let root = ParseNode {
symbol: SymbolId(0),
rule_id: None,
children: vec![
ParseNode {
symbol: SymbolId(1),
rule_id: None,
children: vec![],
start_byte: 0,
end_byte: 4,
text: Some(b"abcd".to_vec()),
},
ParseNode {
symbol: SymbolId(2),
rule_id: None,
children: vec![],
start_byte: 4,
end_byte: 8,
text: Some(b"efgh".to_vec()),
},
ParseNode {
symbol: SymbolId(3),
rule_id: None,
children: vec![],
start_byte: 8,
end_byte: 12,
text: Some(b"ijkl".to_vec()),
},
],
start_byte: 0,
end_byte: 12,
text: None,
};
let mut tree = IncrementalTree::new(root, "abcdefghijkl".to_string());
// Edit in the middle
let edit = Edit::new(4, 8, "XY".to_string());
let affected = tree.apply_edit(&edit);
// Should affect middle nodes
assert!(affected.len() > 0);
}
}