From 6997adfee7a317ed758fa26e86298c7f1e4c01f0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 25 Sep 2021 18:07:51 +0300 Subject: [PATCH] simplify attribute parsing --- crates/parser/src/grammar/expressions.rs | 14 +++------- crates/parser/src/parser.rs | 33 +++++++++--------------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/crates/parser/src/grammar/expressions.rs b/crates/parser/src/grammar/expressions.rs index ba64f58e6dc..30d3d4421ac 100644 --- a/crates/parser/src/grammar/expressions.rs +++ b/crates/parser/src/grammar/expressions.rs @@ -27,11 +27,7 @@ pub(super) fn expr_with_attrs(p: &mut Parser) -> bool { let success = cm.is_some(); match (has_attrs, cm) { - (true, Some(cm)) => { - let kind = cm.kind(); - cm.undo_completion(p).abandon(p); - m.complete(p, kind); - } + (true, Some(cm)) => cm.extend_to(p, m), _ => m.abandon(p), } @@ -92,11 +88,9 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) { // { #[A] bar!()? } // #[B] &() // } - if let Some(cm) = cm { - cm.undo_completion(p).abandon(p); - m.complete(p, kind); - } else { - m.abandon(p); + match cm { + Some(cm) => cm.extend_to(p, m), + None => m.abandon(p), } } else { // test no_semi_after_block diff --git a/crates/parser/src/parser.rs b/crates/parser/src/parser.rs index a3aca3d591d..f6b8308c57a 100644 --- a/crates/parser/src/parser.rs +++ b/crates/parser/src/parser.rs @@ -285,9 +285,8 @@ impl Marker { } _ => unreachable!(), } - let finish_pos = p.events.len() as u32; p.push_event(Event::Finish); - CompletedMarker::new(self.pos, finish_pos, kind) + CompletedMarker::new(self.pos, kind) } /// Abandons the syntax tree node. All its children @@ -305,14 +304,13 @@ impl Marker { } pub(crate) struct CompletedMarker { - start_pos: u32, - finish_pos: u32, + pos: u32, kind: SyntaxKind, } impl CompletedMarker { - fn new(start_pos: u32, finish_pos: u32, kind: SyntaxKind) -> Self { - CompletedMarker { start_pos, finish_pos, kind } + fn new(pos: u32, kind: SyntaxKind) -> Self { + CompletedMarker { pos, kind } } /// This method allows to create a new node which starts @@ -330,29 +328,22 @@ impl CompletedMarker { /// distance to `NEWSTART` into forward_parent(=2 in this case); pub(crate) fn precede(self, p: &mut Parser) -> Marker { let new_pos = p.start(); - let idx = self.start_pos as usize; + let idx = self.pos as usize; match &mut p.events[idx] { Event::Start { forward_parent, .. } => { - *forward_parent = Some(new_pos.pos - self.start_pos); + *forward_parent = Some(new_pos.pos - self.pos); } _ => unreachable!(), } new_pos } - /// Undo this completion and turns into a `Marker` - pub(crate) fn undo_completion(self, p: &mut Parser) -> Marker { - let start_idx = self.start_pos as usize; - let finish_idx = self.finish_pos as usize; - match &mut p.events[start_idx] { - Event::Start { kind, forward_parent: None } => *kind = TOMBSTONE, - _ => unreachable!(), - } - match &mut p.events[finish_idx] { - slot @ Event::Finish => *slot = Event::tombstone(), - _ => unreachable!(), - } - Marker::new(self.start_pos) + /// Extends this completed marker *to the left* up to `m`. + pub(crate) fn extend_to(self, p: &mut Parser, mut m: Marker) { + assert!(m.pos <= self.pos); + m.bomb.defuse(); + + p.events.swap(self.pos as usize, m.pos as usize); } pub(crate) fn kind(&self) -> SyntaxKind {