Skip to content

Commit 23b6e25

Browse files
committed
Continued working on new parsing system
1 parent b48f471 commit 23b6e25

File tree

1 file changed

+185
-14
lines changed

1 file changed

+185
-14
lines changed

src/components/rg/src/main.rs

Lines changed: 185 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#![allow(unused)]
2-
use std::{collections::HashMap, sync::Arc};
2+
use std::{
3+
cmp::max,
4+
collections::{HashMap, VecDeque},
5+
sync::Arc,
6+
};
37

48
#[derive(Clone, Debug)]
59
pub struct GreenNodeId(usize);
@@ -15,11 +19,32 @@ pub enum GreenKind {
1519
Number,
1620
String,
1721
Array,
18-
KeyValue,
19-
Object,
2022
Value,
2123
}
2224

25+
#[derive(Copy, Clone, Debug)]
26+
pub enum Reparse {
27+
Value,
28+
Array,
29+
}
30+
31+
impl GreenKind {
32+
pub fn can_reparse(&self) -> Option<Reparse> {
33+
match self {
34+
GreenKind::Error => None,
35+
GreenKind::Whitespace => None,
36+
GreenKind::Punct(_) => None,
37+
GreenKind::Null => None,
38+
GreenKind::True => None,
39+
GreenKind::False => None,
40+
GreenKind::Number => None,
41+
GreenKind::String => None,
42+
GreenKind::Array => Some(Reparse::Array),
43+
GreenKind::Value => Some(Reparse::Value),
44+
}
45+
}
46+
}
47+
2348
#[derive(Clone, Debug)]
2449
pub struct GreenNode {
2550
id: GreenNodeId,
@@ -100,9 +125,7 @@ impl GreenTree {
100125
}
101126

102127
pub fn parse_root(&mut self, content: &str) -> Arc<GreenNode> {
103-
let Some(parsed) = self.parse_value(content) else {
104-
return self.new_error(content.into());
105-
};
128+
let parsed = self.parse_value(content);
106129

107130
if parsed.consumed != content.len() {
108131
let error = self.new_error(content[parsed.consumed..].into());
@@ -208,7 +231,7 @@ impl GreenTree {
208231
})
209232
}
210233

211-
pub fn parse_value(&mut self, mut content: &str) -> Option<ParseResult> {
234+
pub fn parse_value(&mut self, mut content: &str) -> ParseResult {
212235
let mut children = Vec::new();
213236

214237
if let Some(ws) = self.parse_whitespace(&content) {
@@ -247,10 +270,10 @@ impl GreenTree {
247270
content = &content[ws.consumed..];
248271
}
249272

250-
Some(ParseResult {
273+
ParseResult {
251274
consumed: children.iter().map(|child| child.content_bytes).sum(),
252275
green: self.new_parent(GreenKind::Value, children),
253-
})
276+
}
254277
}
255278

256279
pub fn parse_array(&mut self, mut content: &str) -> Option<ParseResult> {
@@ -283,9 +306,7 @@ impl GreenTree {
283306
});
284307
}
285308

286-
let Some(value) = self.parse_value(content) else {
287-
break;
288-
};
309+
let value = self.parse_value(content);
289310

290311
children.push(value.green);
291312
content = &content[value.consumed..];
@@ -311,6 +332,61 @@ impl GreenTree {
311332
green: self.new_parent(GreenKind::Array, children),
312333
})
313334
}
335+
336+
pub fn reparse(
337+
&mut self,
338+
root: &Arc<RedNode>,
339+
old_content: &str,
340+
edit_index: usize,
341+
delete: usize,
342+
insert: &str,
343+
) -> (Arc<GreenNode>, String) {
344+
let content = format!(
345+
"{}{}{}",
346+
&old_content[..edit_index],
347+
insert,
348+
&old_content[edit_index + delete..]
349+
);
350+
351+
//let new_root = reparse_range(root, 0, edit_index, edit_index + delete);
352+
//dbg!(new_root);
353+
354+
/*
355+
let (parent, path, start_index, end_index) =
356+
find_lowest_affected(&root, 0, edit_index, edit_index + delete);
357+
358+
let Some(reparse) = parent.kind.can_reparse() else {
359+
return (self.parse_root(&content), content);
360+
};
361+
362+
let new_node = self.reparse_as(reparse, &content[start_index..end_index]);
363+
364+
dbg!(
365+
parent,
366+
path.iter().map(|x| x.0).collect::<Vec<_>>(),
367+
reparse,
368+
new_node,
369+
);
370+
371+
let new_root = todo!();
372+
*/
373+
374+
//(new_root, content)
375+
todo!()
376+
}
377+
378+
pub fn reparse_as(&mut self, reparse: Reparse, content: &str) -> ParseResult {
379+
match reparse {
380+
Reparse::Value => self.parse_value(content),
381+
Reparse::Array => {
382+
if let Some(array) = self.parse_array(content) {
383+
array
384+
} else {
385+
self.parse_value(content)
386+
}
387+
}
388+
}
389+
}
314390
}
315391

316392
#[derive(Clone, Debug)]
@@ -320,12 +396,95 @@ pub struct RedNode {
320396
absolute_position: usize,
321397
}
322398

399+
impl RedNode {
400+
pub fn new_arc(
401+
parent: Option<Arc<RedNode>>,
402+
green: Arc<GreenNode>,
403+
absolute_position: usize,
404+
) -> Arc<Self> {
405+
Arc::new(Self {
406+
green,
407+
parent,
408+
absolute_position,
409+
})
410+
}
411+
412+
pub fn parent(&self) -> Option<&Arc<Self>> {
413+
self.parent.as_ref()
414+
}
415+
416+
pub fn children(self: &Arc<Self>) -> impl Iterator<Item = Arc<RedNode>> {
417+
self.green
418+
.children
419+
.iter()
420+
.scan(self.absolute_position, |acc, child| {
421+
let start = *acc;
422+
*acc += child.content_bytes;
423+
Some((start, child))
424+
})
425+
.map(|(start, child)| RedNode::new_arc(Some(self.clone()), child.clone(), start))
426+
}
427+
}
428+
323429
#[derive(Clone, Debug)]
324430
pub struct ParseResult {
325431
green: Arc<GreenNode>,
326432
consumed: usize,
327433
}
328434

435+
fn find_lowest_affected(
436+
node: &Arc<GreenNode>,
437+
node_absolute_index: usize,
438+
min_absolute_index: usize,
439+
max_absolute_index: usize,
440+
) -> (
441+
Arc<GreenNode>,
442+
VecDeque<(usize, Arc<GreenNode>)>,
443+
usize,
444+
usize,
445+
) {
446+
let mut index = node_absolute_index;
447+
448+
for (i, child) in node.children.iter().enumerate() {
449+
let node_left = index;
450+
let node_right = index + child.content_bytes;
451+
452+
let left_in = node_left <= min_absolute_index;
453+
let right_in = max_absolute_index <= node_right;
454+
let contained = left_in && right_in;
455+
456+
if contained {
457+
if child.kind.can_reparse().is_none() {
458+
return (
459+
node.clone(),
460+
VecDeque::new(),
461+
node_absolute_index,
462+
node_absolute_index + node.content_bytes,
463+
);
464+
}
465+
466+
// Fits within child
467+
let (parent, mut path, start_index, end_index) =
468+
find_lowest_affected(child, index, min_absolute_index, max_absolute_index);
469+
470+
path.push_front((i, node.clone()));
471+
return (parent, path, start_index, end_index);
472+
} else if max_absolute_index < node_left {
473+
// Does not fit within a single child
474+
break;
475+
} else {
476+
index += child.content_bytes;
477+
}
478+
}
479+
480+
(
481+
node.clone(),
482+
VecDeque::new(),
483+
node_absolute_index,
484+
node_absolute_index + node.content_bytes,
485+
)
486+
}
487+
329488
fn main() {
330489
let content = r#"
331490
["rust", "parser",
@@ -338,7 +497,19 @@ fn main() {
338497

339498
let mut green_tree = GreenTree::default();
340499
println!("Original source: {}", content);
341-
let root = green_tree.parse_root(&content);
500+
let root = RedNode::new_arc(None, green_tree.parse_root(&content), 0);
342501
println!("Original green tree:");
343-
root.print(0);
502+
root.green.print(0);
503+
504+
let old_text = "\"parser\"";
505+
let edit_index = content.find(old_text).unwrap();
506+
let delete_len = old_text.len();
507+
let insert_text = "\"lexer\"";
508+
509+
println!("Changing {:?} to {:?}", old_text, insert_text);
510+
511+
let (new_root, new_content) =
512+
green_tree.reparse(&root, content, edit_index, delete_len, insert_text);
513+
514+
dbg!(new_root, new_content);
344515
}

0 commit comments

Comments
 (0)