Support tree rebalancing

This commit is contained in:
Aleksey Kladov 2018-01-12 20:32:37 +03:00
parent 15ae9026cf
commit ca0c5ea38b
5 changed files with 86 additions and 29 deletions

View file

@ -32,6 +32,7 @@ fn mod_contents_item(p: &mut Parser) {
} }
fn item(p: &mut Parser) -> bool { fn item(p: &mut Parser) -> bool {
let attrs_start = p.mark();
attributes::outer_attributes(p); attributes::outer_attributes(p);
visibility(p); visibility(p);
// node_if(p, USE_KW, USE_ITEM, use_item) // node_if(p, USE_KW, USE_ITEM, use_item)
@ -41,11 +42,17 @@ fn item(p: &mut Parser) -> bool {
// || unsafe trait, impl // || unsafe trait, impl
// || node_if(p, FN_KW, FN_ITEM, fn_item) // || node_if(p, FN_KW, FN_ITEM, fn_item)
// || node_if(p, TYPE_KW, TYPE_ITEM, type_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, MOD_KW, MOD_ITEM, mod_item)
|| node_if(p, USE_KW, USE_ITEM, use_item) || node_if(p, USE_KW, USE_ITEM, use_item)
|| node_if(p, STRUCT_KW, STRUCT_ITEM, struct_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) { fn struct_item(p: &mut Parser) {

View file

@ -5,7 +5,10 @@ mod parser;
#[derive(Debug)] #[derive(Debug)]
pub(crate) enum Event { pub(crate) enum Event {
Start { kind: SyntaxKind }, Start {
kind: SyntaxKind,
forward_parent: Option<u32>,
},
Finish, Finish,
Token { Token {
kind: SyntaxKind, kind: SyntaxKind,

View file

@ -22,6 +22,9 @@ pub(crate) struct Parser<'t> {
#[derive(Debug, Clone, Copy,PartialEq, Eq)] #[derive(Debug, Clone, Copy,PartialEq, Eq)]
pub(crate) struct Pos(u32); pub(crate) struct Pos(u32);
#[derive(Debug, Clone, Copy,PartialEq, Eq)]
pub(crate) struct Mark(u32);
impl<'t> Parser<'t> { impl<'t> Parser<'t> {
pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> {
let mut tokens = Vec::new(); 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 { pub(crate) fn pos(&self) -> Pos {
Pos(self.pos as u32) Pos(self.pos as u32)
} }
@ -71,7 +89,7 @@ impl<'t> Parser<'t> {
} }
pub(crate) fn start(&mut self, kind: SyntaxKind) { 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) { pub(crate) fn finish(&mut self) {

View file

@ -18,10 +18,39 @@ fn from_events_to_file(
) -> File { ) -> File {
let mut builder = FileBuilder::new(text); let mut builder = FileBuilder::new(text);
let mut idx = 0; let mut idx = 0;
for event in events {
let mut holes = Vec::new();
let mut forward_parents = Vec::new();
for (i, event) in events.iter().enumerate() {
if holes.last() == Some(&i) {
holes.pop();
continue
}
match event { match event {
Event::Start { kind } => builder.start_internal(kind), &Event::Start { kind, forward_parent } => {
Event::Finish => { forward_parents.clear();
let mut idx = i;
loop {
let (kind, fwd) = match events[idx] {
Event::Start { kind, forward_parent } => (kind, forward_parent),
_ => unreachable!(),
};
forward_parents.push((idx, kind));
if let Some(fwd) = fwd {
idx += fwd as usize;
} else {
break;
}
}
for &(idx, kind) in forward_parents.iter().into_iter().rev() {
builder.start_internal(kind);
holes.push(idx);
}
holes.pop();
}
&Event::Finish => {
while idx < tokens.len() { while idx < tokens.len() {
let token = tokens[idx]; let token = tokens[idx];
if is_insignificant(token.kind) { if is_insignificant(token.kind) {
@ -33,7 +62,7 @@ fn from_events_to_file(
} }
builder.finish_internal() builder.finish_internal()
}, },
Event::Token { kind, mut n_raw_tokens } => loop { &Event::Token { kind, mut n_raw_tokens } => loop {
let token = tokens[idx]; let token = tokens[idx];
if !is_insignificant(token.kind) { if !is_insignificant(token.kind) {
n_raw_tokens -= 1; n_raw_tokens -= 1;
@ -44,8 +73,8 @@ fn from_events_to_file(
break; break;
} }
}, },
Event::Error { message } => builder.error().message(message).emit(), &Event::Error { ref message } =>
builder.error().message(message.clone()).emit(),
} }
} }
builder.finish() builder.finish()

View file

@ -1,23 +1,23 @@
FILE@[0; 35) FILE@[0; 35)
ATTR@[0; 13) FN_ITEM@[0; 35)
POUND@[0; 1) ATTR@[0; 13)
L_BRACK@[1; 2) POUND@[0; 1)
META_ITEM@[2; 11) L_BRACK@[1; 2)
IDENT@[2; 5) META_ITEM@[2; 11)
L_PAREN@[5; 6) IDENT@[2; 5)
META_ITEM@[6; 10) L_PAREN@[5; 6)
IDENT@[6; 10) META_ITEM@[6; 10)
R_PAREN@[10; 11) IDENT@[6; 10)
R_BRACK@[11; 12) R_PAREN@[10; 11)
WHITESPACE@[12; 13) R_BRACK@[11; 12)
ATTR@[13; 23) WHITESPACE@[12; 13)
POUND@[13; 14) ATTR@[13; 23)
L_BRACK@[14; 15) POUND@[13; 14)
META_ITEM@[15; 21) L_BRACK@[14; 15)
IDENT@[15; 21) META_ITEM@[15; 21)
R_BRACK@[21; 22) IDENT@[15; 21)
WHITESPACE@[22; 23) R_BRACK@[21; 22)
FN_ITEM@[23; 35) WHITESPACE@[22; 23)
FN_KW@[23; 25) FN_KW@[23; 25)
WHITESPACE@[25; 26) WHITESPACE@[25; 26)
IDENT@[26; 29) IDENT@[26; 29)