diff options
Diffstat (limited to 'crates/ra_syntax/src/parsing/event.rs')
-rw-r--r-- | crates/ra_syntax/src/parsing/event.rs | 247 |
1 files changed, 0 insertions, 247 deletions
diff --git a/crates/ra_syntax/src/parsing/event.rs b/crates/ra_syntax/src/parsing/event.rs deleted file mode 100644 index f6f020eab..000000000 --- a/crates/ra_syntax/src/parsing/event.rs +++ /dev/null | |||
@@ -1,247 +0,0 @@ | |||
1 | //! This module provides a way to construct a `File`. | ||
2 | //! It is intended to be completely decoupled from the | ||
3 | //! parser, so as to allow to evolve the tree representation | ||
4 | //! and the parser algorithm independently. | ||
5 | //! | ||
6 | //! The `TreeSink` trait is the bridge between the parser and the | ||
7 | //! tree builder: the parser produces a stream of events like | ||
8 | //! `start node`, `finish node`, and `FileBuilder` converts | ||
9 | //! this stream to a real tree. | ||
10 | use std::mem; | ||
11 | |||
12 | use crate::{ | ||
13 | SmolStr, | ||
14 | SyntaxKind::{self, *}, | ||
15 | TextRange, TextUnit, | ||
16 | parsing::{ | ||
17 | ParseError, TreeSink, | ||
18 | lexer::Token, | ||
19 | }, | ||
20 | }; | ||
21 | |||
22 | /// `Parser` produces a flat list of `Event`s. | ||
23 | /// They are converted to a tree-structure in | ||
24 | /// a separate pass, via `TreeBuilder`. | ||
25 | #[derive(Debug)] | ||
26 | pub(crate) enum Event { | ||
27 | /// This event signifies the start of the node. | ||
28 | /// It should be either abandoned (in which case the | ||
29 | /// `kind` is `TOMBSTONE`, and the event is ignored), | ||
30 | /// or completed via a `Finish` event. | ||
31 | /// | ||
32 | /// All tokens between a `Start` and a `Finish` would | ||
33 | /// become the children of the respective node. | ||
34 | /// | ||
35 | /// For left-recursive syntactic constructs, the parser produces | ||
36 | /// a child node before it sees a parent. `forward_parent` | ||
37 | /// saves the position of current event's parent. | ||
38 | /// | ||
39 | /// Consider this path | ||
40 | /// | ||
41 | /// foo::bar | ||
42 | /// | ||
43 | /// The events for it would look like this: | ||
44 | /// | ||
45 | /// | ||
46 | /// START(PATH) IDENT('foo') FINISH START(PATH) COLONCOLON IDENT('bar') FINISH | ||
47 | /// | /\ | ||
48 | /// | | | ||
49 | /// +------forward-parent------+ | ||
50 | /// | ||
51 | /// And the tree would look like this | ||
52 | /// | ||
53 | /// +--PATH---------+ | ||
54 | /// | | | | ||
55 | /// | | | | ||
56 | /// | '::' 'bar' | ||
57 | /// | | ||
58 | /// PATH | ||
59 | /// | | ||
60 | /// 'foo' | ||
61 | /// | ||
62 | /// See also `CompletedMarker::precede`. | ||
63 | Start { | ||
64 | kind: SyntaxKind, | ||
65 | forward_parent: Option<u32>, | ||
66 | }, | ||
67 | |||
68 | /// Complete the previous `Start` event | ||
69 | Finish, | ||
70 | |||
71 | /// Produce a single leaf-element. | ||
72 | /// `n_raw_tokens` is used to glue complex contextual tokens. | ||
73 | /// For example, lexer tokenizes `>>` as `>`, `>`, and | ||
74 | /// `n_raw_tokens = 2` is used to produced a single `>>`. | ||
75 | Token { | ||
76 | kind: SyntaxKind, | ||
77 | n_raw_tokens: u8, | ||
78 | }, | ||
79 | |||
80 | Error { | ||
81 | msg: ParseError, | ||
82 | }, | ||
83 | } | ||
84 | |||
85 | impl Event { | ||
86 | pub(crate) fn tombstone() -> Self { | ||
87 | Event::Start { kind: TOMBSTONE, forward_parent: None } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | pub(super) struct EventProcessor<'a, S: TreeSink> { | ||
92 | sink: S, | ||
93 | text_pos: TextUnit, | ||
94 | text: &'a str, | ||
95 | token_pos: usize, | ||
96 | tokens: &'a [Token], | ||
97 | events: &'a mut [Event], | ||
98 | } | ||
99 | |||
100 | impl<'a, S: TreeSink> EventProcessor<'a, S> { | ||
101 | pub(super) fn new( | ||
102 | sink: S, | ||
103 | text: &'a str, | ||
104 | tokens: &'a [Token], | ||
105 | events: &'a mut [Event], | ||
106 | ) -> EventProcessor<'a, S> { | ||
107 | EventProcessor { sink, text_pos: 0.into(), text, token_pos: 0, tokens, events } | ||
108 | } | ||
109 | |||
110 | /// Generate the syntax tree with the control of events. | ||
111 | pub(crate) fn process(mut self) -> S { | ||
112 | let mut forward_parents = Vec::new(); | ||
113 | |||
114 | for i in 0..self.events.len() { | ||
115 | match mem::replace(&mut self.events[i], Event::tombstone()) { | ||
116 | Event::Start { kind: TOMBSTONE, .. } => (), | ||
117 | |||
118 | Event::Start { kind, forward_parent } => { | ||
119 | // For events[A, B, C], B is A's forward_parent, C is B's forward_parent, | ||
120 | // in the normal control flow, the parent-child relation: `A -> B -> C`, | ||
121 | // while with the magic forward_parent, it writes: `C <- B <- A`. | ||
122 | |||
123 | // append `A` into parents. | ||
124 | forward_parents.push(kind); | ||
125 | let mut idx = i; | ||
126 | let mut fp = forward_parent; | ||
127 | while let Some(fwd) = fp { | ||
128 | idx += fwd as usize; | ||
129 | // append `A`'s forward_parent `B` | ||
130 | fp = match mem::replace(&mut self.events[idx], Event::tombstone()) { | ||
131 | Event::Start { kind, forward_parent } => { | ||
132 | forward_parents.push(kind); | ||
133 | forward_parent | ||
134 | } | ||
135 | _ => unreachable!(), | ||
136 | }; | ||
137 | // append `B`'s forward_parent `C` in the next stage. | ||
138 | } | ||
139 | |||
140 | for kind in forward_parents.drain(..).rev() { | ||
141 | self.start(kind); | ||
142 | } | ||
143 | } | ||
144 | Event::Finish => { | ||
145 | let is_last = i == self.events.len() - 1; | ||
146 | self.finish(is_last); | ||
147 | } | ||
148 | Event::Token { kind, n_raw_tokens } => { | ||
149 | self.eat_trivias(); | ||
150 | let n_raw_tokens = n_raw_tokens as usize; | ||
151 | let len = self.tokens[self.token_pos..self.token_pos + n_raw_tokens] | ||
152 | .iter() | ||
153 | .map(|it| it.len) | ||
154 | .sum::<TextUnit>(); | ||
155 | self.leaf(kind, len, n_raw_tokens); | ||
156 | } | ||
157 | Event::Error { msg } => self.sink.error(msg), | ||
158 | } | ||
159 | } | ||
160 | self.sink | ||
161 | } | ||
162 | |||
163 | /// Add the node into syntax tree but discard the comments/whitespaces. | ||
164 | fn start(&mut self, kind: SyntaxKind) { | ||
165 | if kind == SOURCE_FILE { | ||
166 | self.sink.start_branch(kind); | ||
167 | return; | ||
168 | } | ||
169 | let n_trivias = | ||
170 | self.tokens[self.token_pos..].iter().take_while(|it| it.kind.is_trivia()).count(); | ||
171 | let leading_trivias = &self.tokens[self.token_pos..self.token_pos + n_trivias]; | ||
172 | let mut trivia_end = | ||
173 | self.text_pos + leading_trivias.iter().map(|it| it.len).sum::<TextUnit>(); | ||
174 | |||
175 | let n_attached_trivias = { | ||
176 | let leading_trivias = leading_trivias.iter().rev().map(|it| { | ||
177 | let next_end = trivia_end - it.len; | ||
178 | let range = TextRange::from_to(next_end, trivia_end); | ||
179 | trivia_end = next_end; | ||
180 | (it.kind, &self.text[range]) | ||
181 | }); | ||
182 | n_attached_trivias(kind, leading_trivias) | ||
183 | }; | ||
184 | self.eat_n_trivias(n_trivias - n_attached_trivias); | ||
185 | self.sink.start_branch(kind); | ||
186 | self.eat_n_trivias(n_attached_trivias); | ||
187 | } | ||
188 | |||
189 | fn finish(&mut self, is_last: bool) { | ||
190 | if is_last { | ||
191 | self.eat_trivias() | ||
192 | } | ||
193 | self.sink.finish_branch(); | ||
194 | } | ||
195 | |||
196 | fn eat_trivias(&mut self) { | ||
197 | while let Some(&token) = self.tokens.get(self.token_pos) { | ||
198 | if !token.kind.is_trivia() { | ||
199 | break; | ||
200 | } | ||
201 | self.leaf(token.kind, token.len, 1); | ||
202 | } | ||
203 | } | ||
204 | |||
205 | fn eat_n_trivias(&mut self, n: usize) { | ||
206 | for _ in 0..n { | ||
207 | let token = self.tokens[self.token_pos]; | ||
208 | assert!(token.kind.is_trivia()); | ||
209 | self.leaf(token.kind, token.len, 1); | ||
210 | } | ||
211 | } | ||
212 | |||
213 | fn leaf(&mut self, kind: SyntaxKind, len: TextUnit, n_tokens: usize) { | ||
214 | let range = TextRange::offset_len(self.text_pos, len); | ||
215 | let text: SmolStr = self.text[range].into(); | ||
216 | self.text_pos += len; | ||
217 | self.token_pos += n_tokens; | ||
218 | self.sink.leaf(kind, text); | ||
219 | } | ||
220 | } | ||
221 | |||
222 | fn n_attached_trivias<'a>( | ||
223 | kind: SyntaxKind, | ||
224 | trivias: impl Iterator<Item = (SyntaxKind, &'a str)>, | ||
225 | ) -> usize { | ||
226 | match kind { | ||
227 | CONST_DEF | TYPE_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF | TRAIT_DEF | ||
228 | | MODULE | NAMED_FIELD_DEF => { | ||
229 | let mut res = 0; | ||
230 | for (i, (kind, text)) in trivias.enumerate() { | ||
231 | match kind { | ||
232 | WHITESPACE => { | ||
233 | if text.contains("\n\n") { | ||
234 | break; | ||
235 | } | ||
236 | } | ||
237 | COMMENT => { | ||
238 | res = i + 1; | ||
239 | } | ||
240 | _ => (), | ||
241 | } | ||
242 | } | ||
243 | res | ||
244 | } | ||
245 | _ => 0, | ||
246 | } | ||
247 | } | ||