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();
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

View file

@ -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 {