Skip to content

Commit 7dc331f

Browse files
committed
fix: correct extend_to logic in parser
Previously we swapped to events in the buffer, but that might be wrong if there aer `forward_parent` links pointing to the swapped-out node. Let's do the same via parent links instead, keeping the nodes in place
1 parent 6997adf commit 7dc331f

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

crates/parser/src/event.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,6 @@ pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
9393

9494
for i in 0..events.len() {
9595
match mem::replace(&mut events[i], Event::tombstone()) {
96-
Event::Start { kind: TOMBSTONE, .. } => (),
97-
9896
Event::Start { kind, forward_parent } => {
9997
// For events[A, B, C], B is A's forward_parent, C is B's forward_parent,
10098
// in the normal control flow, the parent-child relation: `A -> B -> C`,
@@ -109,9 +107,7 @@ pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
109107
// append `A`'s forward_parent `B`
110108
fp = match mem::replace(&mut events[idx], Event::tombstone()) {
111109
Event::Start { kind, forward_parent } => {
112-
if kind != TOMBSTONE {
113-
forward_parents.push(kind);
114-
}
110+
forward_parents.push(kind);
115111
forward_parent
116112
}
117113
_ => unreachable!(),
@@ -120,7 +116,9 @@ pub(super) fn process(sink: &mut dyn TreeSink, mut events: Vec<Event>) {
120116
}
121117

122118
for kind in forward_parents.drain(..).rev() {
123-
sink.start_node(kind);
119+
if kind != TOMBSTONE {
120+
sink.start_node(kind);
121+
}
124122
}
125123
}
126124
Event::Finish => sink.finish_node(),

crates/parser/src/parser.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,14 @@ impl CompletedMarker {
340340

341341
/// Extends this completed marker *to the left* up to `m`.
342342
pub(crate) fn extend_to(self, p: &mut Parser, mut m: Marker) {
343-
assert!(m.pos <= self.pos);
344343
m.bomb.defuse();
345-
346-
p.events.swap(self.pos as usize, m.pos as usize);
344+
let idx = m.pos as usize;
345+
match &mut p.events[idx] {
346+
Event::Start { forward_parent, .. } => {
347+
*forward_parent = Some(self.pos - m.pos);
348+
}
349+
_ => unreachable!(),
350+
}
347351
}
348352

349353
pub(crate) fn kind(&self) -> SyntaxKind {

0 commit comments

Comments
 (0)