simplify attribute parsing

This commit is contained in:
Aleksey Kladov 2021-09-25 18:07:51 +03:00
parent 13da3d93f9
commit 6997adfee7
2 changed files with 16 additions and 31 deletions

View file

@ -27,11 +27,7 @@ pub(super) fn expr_with_attrs(p: &mut Parser) -> bool {
let success = cm.is_some(); let success = cm.is_some();
match (has_attrs, cm) { match (has_attrs, cm) {
(true, Some(cm)) => { (true, Some(cm)) => cm.extend_to(p, m),
let kind = cm.kind();
cm.undo_completion(p).abandon(p);
m.complete(p, kind);
}
_ => m.abandon(p), _ => m.abandon(p),
} }
@ -92,11 +88,9 @@ pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi, prefer_expr: bool) {
// { #[A] bar!()? } // { #[A] bar!()? }
// #[B] &() // #[B] &()
// } // }
if let Some(cm) = cm { match cm {
cm.undo_completion(p).abandon(p); Some(cm) => cm.extend_to(p, m),
m.complete(p, kind); None => m.abandon(p),
} else {
m.abandon(p);
} }
} else { } else {
// test no_semi_after_block // test no_semi_after_block

View file

@ -285,9 +285,8 @@ impl Marker {
} }
_ => unreachable!(), _ => unreachable!(),
} }
let finish_pos = p.events.len() as u32;
p.push_event(Event::Finish); 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 /// Abandons the syntax tree node. All its children
@ -305,14 +304,13 @@ impl Marker {
} }
pub(crate) struct CompletedMarker { pub(crate) struct CompletedMarker {
start_pos: u32, pos: u32,
finish_pos: u32,
kind: SyntaxKind, kind: SyntaxKind,
} }
impl CompletedMarker { impl CompletedMarker {
fn new(start_pos: u32, finish_pos: u32, kind: SyntaxKind) -> Self { fn new(pos: u32, kind: SyntaxKind) -> Self {
CompletedMarker { start_pos, finish_pos, kind } CompletedMarker { pos, kind }
} }
/// This method allows to create a new node which starts /// 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); /// distance to `NEWSTART` into forward_parent(=2 in this case);
pub(crate) fn precede(self, p: &mut Parser) -> Marker { pub(crate) fn precede(self, p: &mut Parser) -> Marker {
let new_pos = p.start(); let new_pos = p.start();
let idx = self.start_pos as usize; let idx = self.pos as usize;
match &mut p.events[idx] { match &mut p.events[idx] {
Event::Start { forward_parent, .. } => { Event::Start { forward_parent, .. } => {
*forward_parent = Some(new_pos.pos - self.start_pos); *forward_parent = Some(new_pos.pos - self.pos);
} }
_ => unreachable!(), _ => unreachable!(),
} }
new_pos new_pos
} }
/// Undo this completion and turns into a `Marker` /// Extends this completed marker *to the left* up to `m`.
pub(crate) fn undo_completion(self, p: &mut Parser) -> Marker { pub(crate) fn extend_to(self, p: &mut Parser, mut m: Marker) {
let start_idx = self.start_pos as usize; assert!(m.pos <= self.pos);
let finish_idx = self.finish_pos as usize; m.bomb.defuse();
match &mut p.events[start_idx] {
Event::Start { kind, forward_parent: None } => *kind = TOMBSTONE, p.events.swap(self.pos as usize, m.pos as usize);
_ => unreachable!(),
}
match &mut p.events[finish_idx] {
slot @ Event::Finish => *slot = Event::tombstone(),
_ => unreachable!(),
}
Marker::new(self.start_pos)
} }
pub(crate) fn kind(&self) -> SyntaxKind { pub(crate) fn kind(&self) -> SyntaxKind {