From ea7b569e1b133b6c19ef60c9cb2b2fd6b79847da Mon Sep 17 00:00:00 2001 From: csmoe Date: Mon, 31 Dec 2018 20:53:43 +0800 Subject: docing parser methods --- crates/ra_syntax/src/parser_impl/event.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'crates/ra_syntax/src/parser_impl') diff --git a/crates/ra_syntax/src/parser_impl/event.rs b/crates/ra_syntax/src/parser_impl/event.rs index 3d8b062d5..d6299b5e3 100644 --- a/crates/ra_syntax/src/parser_impl/event.rs +++ b/crates/ra_syntax/src/parser_impl/event.rs @@ -154,7 +154,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> { self.finish(last); } Event::Token { kind, n_raw_tokens } => { - self.eat_ws(); + self.eat_trivias(); let n_raw_tokens = n_raw_tokens as usize; let len = self.tokens[self.token_pos..self.token_pos + n_raw_tokens] .iter() @@ -173,7 +173,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> { fn start(&mut self, kind: SyntaxKind) { if kind == SOURCE_FILE { - self.sink.start_internal(kind); + self.sink.start_branch(kind); return; } let n_trivias = self.tokens[self.token_pos..] @@ -194,18 +194,18 @@ impl<'a, S: Sink> EventProcessor<'a, S> { n_attached_trivias(kind, leading_trivias) }; self.eat_n_trivias(n_trivias - n_attached_trivias); - self.sink.start_internal(kind); + self.sink.start_branch(kind); self.eat_n_trivias(n_attached_trivias); } fn finish(&mut self, last: bool) { if last { - self.eat_ws() + self.eat_trivias() } - self.sink.finish_internal(); + self.sink.finish_branch(); } - fn eat_ws(&mut self) { + fn eat_trivias(&mut self) { while let Some(&token) = self.tokens.get(self.token_pos) { if !token.kind.is_trivia() { break; -- cgit v1.2.3 From b01e707dba7810c3d28c82a84dec9064cc01d3c8 Mon Sep 17 00:00:00 2001 From: csmoe Date: Mon, 31 Dec 2018 21:30:37 +0800 Subject: doc parser input --- crates/ra_syntax/src/parser_impl/input.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'crates/ra_syntax/src/parser_impl') diff --git a/crates/ra_syntax/src/parser_impl/input.rs b/crates/ra_syntax/src/parser_impl/input.rs index ac6d900d8..083a7aa15 100644 --- a/crates/ra_syntax/src/parser_impl/input.rs +++ b/crates/ra_syntax/src/parser_impl/input.rs @@ -4,11 +4,26 @@ use std::ops::{Add, AddAssign}; pub(crate) struct ParserInput<'t> { text: &'t str, + /// start position of each token(expect whitespace and comment) + /// ```non-rust + /// struct Foo; + /// ^------^--- + /// | | ^- + /// 0 7 10 + /// ``` + /// (token, start_offset): `[(struct, 0), (Foo, 7), (;, 10)]` start_offsets: Vec, - tokens: Vec, // non-whitespace tokens + /// non-whitespace/comment tokens + /// ```non-rust + /// struct Foo {} + /// ^^^^^^ ^^^ ^^ + /// ``` + /// tokens: `[struct, Foo, {, }]` + tokens: Vec, } impl<'t> ParserInput<'t> { + /// Generate input from tokens(expect comment and whitespace). pub fn new(text: &'t str, raw_tokens: &'t [Token]) -> ParserInput<'t> { let mut tokens = Vec::new(); let mut start_offsets = Vec::new(); @@ -28,6 +43,7 @@ impl<'t> ParserInput<'t> { } } + /// Get the syntax kind of token at given input position. pub fn kind(&self, pos: InputPosition) -> SyntaxKind { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { @@ -36,7 +52,8 @@ impl<'t> ParserInput<'t> { self.tokens[idx].kind } - pub fn len(&self, pos: InputPosition) -> TextUnit { + /// Get the length of a token at given input position. + pub fn token_len(&self, pos: InputPosition) -> TextUnit { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return 0.into(); @@ -44,7 +61,8 @@ impl<'t> ParserInput<'t> { self.tokens[idx].len } - pub fn start(&self, pos: InputPosition) -> TextUnit { + /// Get the start position of a taken at given input position. + pub fn token_start_at(&self, pos: InputPosition) -> TextUnit { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return 0.into(); @@ -52,7 +70,8 @@ impl<'t> ParserInput<'t> { self.start_offsets[idx] } - pub fn text(&self, pos: InputPosition) -> &'t str { + /// Get the raw text of a toen at given input position. + pub fn token_text(&self, pos: InputPosition) -> &'t str { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { return ""; -- cgit v1.2.3 From df591a1e48a876653f1f48ed595d1754470d116f Mon Sep 17 00:00:00 2001 From: csmoe Date: Tue, 1 Jan 2019 16:09:51 +0800 Subject: doc parsing events --- crates/ra_syntax/src/parser_impl/event.rs | 40 +++++++++++++++++++++---------- crates/ra_syntax/src/parser_impl/input.rs | 2 +- 2 files changed, 28 insertions(+), 14 deletions(-) (limited to 'crates/ra_syntax/src/parser_impl') diff --git a/crates/ra_syntax/src/parser_impl/event.rs b/crates/ra_syntax/src/parser_impl/event.rs index d6299b5e3..835b2c78d 100644 --- a/crates/ra_syntax/src/parser_impl/event.rs +++ b/crates/ra_syntax/src/parser_impl/event.rs @@ -36,7 +36,7 @@ pub(crate) enum Event { /// /// For left-recursive syntactic constructs, the parser produces /// a child node before it sees a parent. `forward_parent` - /// exists to allow to tweak parent-child relationships. + /// saves the position of current event's parent. /// /// Consider this path /// @@ -84,6 +84,15 @@ pub(crate) enum Event { }, } +impl Event { + pub(crate) fn tombstone() -> Self { + Event::Start { + kind: TOMBSTONE, + forward_parent: None, + } + } +} + pub(super) struct EventProcessor<'a, S: Sink> { sink: S, text_pos: TextUnit, @@ -110,17 +119,12 @@ impl<'a, S: Sink> EventProcessor<'a, S> { } } + /// Generate the syntax tree with the control of events. pub(super) fn process(mut self) -> S { - fn tombstone() -> Event { - Event::Start { - kind: TOMBSTONE, - forward_parent: None, - } - } let mut forward_parents = Vec::new(); for i in 0..self.events.len() { - match mem::replace(&mut self.events[i], tombstone()) { + match mem::replace(&mut self.events[i], Event::tombstone()) { Event::Start { kind: TOMBSTONE, .. } => (), @@ -129,12 +133,18 @@ impl<'a, S: Sink> EventProcessor<'a, S> { kind, forward_parent, } => { + // For events[A, B, C], B is A's forward_parent, C is B's forward_parent, + // in the normal control flow, the parent-child relation: `A -> B -> C`, + // while with the magic forward_parent, it writes: `C <- B <- A`. + + // append `A` into parents. forward_parents.push(kind); let mut idx = i; let mut fp = forward_parent; while let Some(fwd) = fp { idx += fwd as usize; - fp = match mem::replace(&mut self.events[idx], tombstone()) { + // append `A`'s forward_parent `B` + fp = match mem::replace(&mut self.events[idx], Event::tombstone()) { Event::Start { kind, forward_parent, @@ -144,14 +154,16 @@ impl<'a, S: Sink> EventProcessor<'a, S> { } _ => unreachable!(), }; + // append `B`'s forward_parent `C` in the next stage. } + for kind in forward_parents.drain(..).rev() { self.start(kind); } } Event::Finish => { - let last = i == self.events.len() - 1; - self.finish(last); + let is_last = i == self.events.len() - 1; + self.finish(is_last); } Event::Token { kind, n_raw_tokens } => { self.eat_trivias(); @@ -171,6 +183,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> { self.sink } + /// Add the node into syntax tree but discard the comments/whitespaces. fn start(&mut self, kind: SyntaxKind) { if kind == SOURCE_FILE { self.sink.start_branch(kind); @@ -198,8 +211,8 @@ impl<'a, S: Sink> EventProcessor<'a, S> { self.eat_n_trivias(n_attached_trivias); } - fn finish(&mut self, last: bool) { - if last { + fn finish(&mut self, is_last: bool) { + if is_last { self.eat_trivias() } self.sink.finish_branch(); @@ -235,6 +248,7 @@ fn n_attached_trivias<'a>( kind: SyntaxKind, trivias: impl Iterator, ) -> usize { + // FIXME: parse attached trivias of CONST_DEF/TYPE_DEF match kind { STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | MODULE => { let mut res = 0; diff --git a/crates/ra_syntax/src/parser_impl/input.rs b/crates/ra_syntax/src/parser_impl/input.rs index 083a7aa15..7fde5b3ab 100644 --- a/crates/ra_syntax/src/parser_impl/input.rs +++ b/crates/ra_syntax/src/parser_impl/input.rs @@ -70,7 +70,7 @@ impl<'t> ParserInput<'t> { self.start_offsets[idx] } - /// Get the raw text of a toen at given input position. + /// Get the raw text of a token at given input position. pub fn token_text(&self, pos: InputPosition) -> &'t str { let idx = pos.0 as usize; if !(idx < self.tokens.len()) { -- cgit v1.2.3 From 58139c558aa085588264ba659b8483a036c1da0e Mon Sep 17 00:00:00 2001 From: csmoe Date: Fri, 4 Jan 2019 12:22:57 +0800 Subject: consume trivias for type/const def --- crates/ra_syntax/src/parser_impl/event.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'crates/ra_syntax/src/parser_impl') diff --git a/crates/ra_syntax/src/parser_impl/event.rs b/crates/ra_syntax/src/parser_impl/event.rs index 835b2c78d..73dd6e02b 100644 --- a/crates/ra_syntax/src/parser_impl/event.rs +++ b/crates/ra_syntax/src/parser_impl/event.rs @@ -248,9 +248,8 @@ fn n_attached_trivias<'a>( kind: SyntaxKind, trivias: impl Iterator, ) -> usize { - // FIXME: parse attached trivias of CONST_DEF/TYPE_DEF match kind { - STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | MODULE => { + CONST_DEF | TYPE_DEF | STRUCT_DEF | ENUM_DEF | FN_DEF | TRAIT_DEF | MODULE => { let mut res = 0; for (i, (kind, text)) in trivias.enumerate() { match kind { -- cgit v1.2.3