From ca0c5ea38b623422e1a25388817e224c2c4f4ae2 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 12 Jan 2018 20:32:37 +0300 Subject: Support tree rebalancing --- src/parser/event_parser/grammar/items.rs | 11 +++++++++-- src/parser/event_parser/mod.rs | 5 ++++- src/parser/event_parser/parser.rs | 20 +++++++++++++++++++- 3 files changed, 32 insertions(+), 4 deletions(-) (limited to 'src/parser/event_parser') diff --git a/src/parser/event_parser/grammar/items.rs b/src/parser/event_parser/grammar/items.rs index 522986ed0..725f04d1e 100644 --- a/src/parser/event_parser/grammar/items.rs +++ b/src/parser/event_parser/grammar/items.rs @@ -32,6 +32,7 @@ fn mod_contents_item(p: &mut Parser) { } fn item(p: &mut Parser) -> bool { + let attrs_start = p.mark(); attributes::outer_attributes(p); visibility(p); // node_if(p, USE_KW, USE_ITEM, use_item) @@ -41,11 +42,17 @@ fn item(p: &mut Parser) -> bool { // || unsafe trait, impl // || node_if(p, FN_KW, FN_ITEM, fn_item) // || node_if(p, TYPE_KW, TYPE_ITEM, type_item) - node_if(p, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item) + let item_start = p.mark(); + let item_parsed = node_if(p, [EXTERN_KW, CRATE_KW], EXTERN_CRATE_ITEM, extern_crate_item) || node_if(p, MOD_KW, MOD_ITEM, mod_item) || node_if(p, USE_KW, USE_ITEM, use_item) || node_if(p, STRUCT_KW, STRUCT_ITEM, struct_item) - || node_if(p, FN_KW, FN_ITEM, fn_item) + || node_if(p, FN_KW, FN_ITEM, fn_item); + + if item_parsed && attrs_start != item_start { + p.forward_parent(attrs_start, item_start); + } + item_parsed } fn struct_item(p: &mut Parser) { diff --git a/src/parser/event_parser/mod.rs b/src/parser/event_parser/mod.rs index 87b317c84..c89a3ebe7 100644 --- a/src/parser/event_parser/mod.rs +++ b/src/parser/event_parser/mod.rs @@ -5,7 +5,10 @@ mod parser; #[derive(Debug)] pub(crate) enum Event { - Start { kind: SyntaxKind }, + Start { + kind: SyntaxKind, + forward_parent: Option, + }, Finish, Token { kind: SyntaxKind, diff --git a/src/parser/event_parser/parser.rs b/src/parser/event_parser/parser.rs index 8bc382b12..14c0efe2d 100644 --- a/src/parser/event_parser/parser.rs +++ b/src/parser/event_parser/parser.rs @@ -22,6 +22,9 @@ pub(crate) struct Parser<'t> { #[derive(Debug, Clone, Copy,PartialEq, Eq)] pub(crate) struct Pos(u32); +#[derive(Debug, Clone, Copy,PartialEq, Eq)] +pub(crate) struct Mark(u32); + impl<'t> Parser<'t> { pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { let mut tokens = Vec::new(); @@ -47,6 +50,21 @@ impl<'t> Parser<'t> { } } + pub(crate) fn mark(&self) -> Mark { + Mark(self.events.len() as u32) + } + + pub(crate) fn forward_parent(&mut self, child: Mark, parent: Mark) { + assert!(child.0 < parent.0); + let diff = parent.0 - child.0; + match self.events[child.0 as usize] { + Event::Start { ref mut forward_parent, .. } => { + *forward_parent = Some(diff); + } + _ => unreachable!() + } + } + pub(crate) fn pos(&self) -> Pos { Pos(self.pos as u32) } @@ -71,7 +89,7 @@ impl<'t> Parser<'t> { } pub(crate) fn start(&mut self, kind: SyntaxKind) { - self.event(Event::Start { kind }); + self.event(Event::Start { kind, forward_parent: None }); } pub(crate) fn finish(&mut self) { -- cgit v1.2.3