diff options
Diffstat (limited to 'crates/ra_syntax/src/parsing/builder.rs')
-rw-r--r-- | crates/ra_syntax/src/parsing/builder.rs | 165 |
1 files changed, 143 insertions, 22 deletions
diff --git a/crates/ra_syntax/src/parsing/builder.rs b/crates/ra_syntax/src/parsing/builder.rs index ee0e2cce7..cfe3139b8 100644 --- a/crates/ra_syntax/src/parsing/builder.rs +++ b/crates/ra_syntax/src/parsing/builder.rs | |||
@@ -1,49 +1,170 @@ | |||
1 | use std::mem; | ||
2 | |||
3 | use ra_parser::{TreeSink, ParseError}; | ||
4 | use rowan::GreenNodeBuilder; | ||
5 | |||
1 | use crate::{ | 6 | use crate::{ |
2 | SmolStr, SyntaxKind, SyntaxError, SyntaxErrorKind, TextUnit, | 7 | SmolStr, SyntaxError, SyntaxErrorKind, TextUnit, TextRange, |
3 | parsing::{TreeSink, ParseError}, | 8 | SyntaxKind::{self, *}, |
9 | parsing::Token, | ||
4 | syntax_node::{GreenNode, RaTypes}, | 10 | syntax_node::{GreenNode, RaTypes}, |
5 | }; | 11 | }; |
6 | 12 | ||
7 | use rowan::GreenNodeBuilder; | 13 | /// Bridges the parser with our specific syntax tree representation. |
8 | 14 | /// | |
9 | pub(crate) struct GreenBuilder { | 15 | /// `TreeBuilder` also handles attachment of trivia (whitespace) to nodes. |
16 | pub(crate) struct TreeBuilder<'a> { | ||
17 | text: &'a str, | ||
18 | tokens: &'a [Token], | ||
10 | text_pos: TextUnit, | 19 | text_pos: TextUnit, |
20 | token_pos: usize, | ||
21 | state: State, | ||
11 | errors: Vec<SyntaxError>, | 22 | errors: Vec<SyntaxError>, |
12 | inner: GreenNodeBuilder<RaTypes>, | 23 | inner: GreenNodeBuilder<RaTypes>, |
13 | } | 24 | } |
14 | 25 | ||
15 | impl Default for GreenBuilder { | 26 | enum State { |
16 | fn default() -> GreenBuilder { | 27 | PendingStart, |
17 | GreenBuilder { | 28 | Normal, |
18 | text_pos: TextUnit::default(), | 29 | PendingFinish, |
19 | errors: Vec::new(), | ||
20 | inner: GreenNodeBuilder::new(), | ||
21 | } | ||
22 | } | ||
23 | } | 30 | } |
24 | 31 | ||
25 | impl TreeSink for GreenBuilder { | 32 | impl<'a> TreeSink for TreeBuilder<'a> { |
26 | type Tree = (GreenNode, Vec<SyntaxError>); | 33 | fn leaf(&mut self, kind: SyntaxKind, n_tokens: u8) { |
27 | 34 | match mem::replace(&mut self.state, State::Normal) { | |
28 | fn leaf(&mut self, kind: SyntaxKind, text: SmolStr) { | 35 | State::PendingStart => unreachable!(), |
29 | self.text_pos += TextUnit::of_str(text.as_str()); | 36 | State::PendingFinish => self.inner.finish_internal(), |
30 | self.inner.leaf(kind, text); | 37 | State::Normal => (), |
38 | } | ||
39 | self.eat_trivias(); | ||
40 | let n_tokens = n_tokens as usize; | ||
41 | let len = self.tokens[self.token_pos..self.token_pos + n_tokens] | ||
42 | .iter() | ||
43 | .map(|it| it.len) | ||
44 | .sum::<TextUnit>(); | ||
45 | self.do_leaf(kind, len, n_tokens); | ||
31 | } | 46 | } |
32 | 47 | ||
33 | fn start_branch(&mut self, kind: SyntaxKind) { | 48 | fn start_branch(&mut self, kind: SyntaxKind) { |
34 | self.inner.start_internal(kind) | 49 | match mem::replace(&mut self.state, State::Normal) { |
50 | State::PendingStart => { | ||
51 | self.inner.start_internal(kind); | ||
52 | // No need to attach trivias to previous node: there is no | ||
53 | // previous node. | ||
54 | return; | ||
55 | } | ||
56 | State::PendingFinish => self.inner.finish_internal(), | ||
57 | State::Normal => (), | ||
58 | } | ||
59 | |||
60 | let n_trivias = | ||
61 | self.tokens[self.token_pos..].iter().take_while(|it| it.kind.is_trivia()).count(); | ||
62 | let leading_trivias = &self.tokens[self.token_pos..self.token_pos + n_trivias]; | ||
63 | let mut trivia_end = | ||
64 | self.text_pos + leading_trivias.iter().map(|it| it.len).sum::<TextUnit>(); | ||
65 | |||
66 | let n_attached_trivias = { | ||
67 | let leading_trivias = leading_trivias.iter().rev().map(|it| { | ||
68 | let next_end = trivia_end - it.len; | ||
69 | let range = TextRange::from_to(next_end, trivia_end); | ||
70 | trivia_end = next_end; | ||
71 | (it.kind, &self.text[range]) | ||
72 | }); | ||
73 | n_attached_trivias(kind, leading_trivias) | ||
74 | }; | ||
75 | self.eat_n_trivias(n_trivias - n_attached_trivias); | ||
76 | self.inner.start_internal(kind); | ||
77 | self.eat_n_trivias(n_attached_trivias); | ||
35 | } | 78 | } |
36 | 79 | ||
37 | fn finish_branch(&mut self) { | 80 | fn finish_branch(&mut self) { |
38 | self.inner.finish_internal(); | 81 | match mem::replace(&mut self.state, State::PendingFinish) { |
82 | State::PendingStart => unreachable!(), | ||
83 | State::PendingFinish => self.inner.finish_internal(), | ||
84 | State::Normal => (), | ||
85 | } | ||
39 | } | 86 | } |
40 | 87 | ||
41 | fn error(&mut self, error: ParseError) { | 88 | fn error(&mut self, error: ParseError) { |
42 | let error = SyntaxError::new(SyntaxErrorKind::ParseError(error), self.text_pos); | 89 | let error = SyntaxError::new(SyntaxErrorKind::ParseError(error), self.text_pos); |
43 | self.errors.push(error) | 90 | self.errors.push(error) |
44 | } | 91 | } |
92 | } | ||
93 | |||
94 | impl<'a> TreeBuilder<'a> { | ||
95 | pub(super) fn new(text: &'a str, tokens: &'a [Token]) -> TreeBuilder<'a> { | ||
96 | TreeBuilder { | ||
97 | text, | ||
98 | tokens, | ||
99 | text_pos: 0.into(), | ||
100 | token_pos: 0, | ||
101 | state: State::PendingStart, | ||
102 | errors: Vec::new(), | ||
103 | inner: GreenNodeBuilder::new(), | ||
104 | } | ||
105 | } | ||
106 | |||
107 | pub(super) fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) { | ||
108 | match mem::replace(&mut self.state, State::Normal) { | ||
109 | State::PendingFinish => { | ||
110 | self.eat_trivias(); | ||
111 | self.inner.finish_internal() | ||
112 | } | ||
113 | State::PendingStart | State::Normal => unreachable!(), | ||
114 | } | ||
45 | 115 | ||
46 | fn finish(self) -> (GreenNode, Vec<SyntaxError>) { | ||
47 | (self.inner.finish(), self.errors) | 116 | (self.inner.finish(), self.errors) |
48 | } | 117 | } |
118 | |||
119 | fn eat_trivias(&mut self) { | ||
120 | while let Some(&token) = self.tokens.get(self.token_pos) { | ||
121 | if !token.kind.is_trivia() { | ||
122 | break; | ||
123 | } | ||
124 | self.do_leaf(token.kind, token.len, 1); | ||
125 | } | ||
126 | } | ||
127 | |||
128 | fn eat_n_trivias(&mut self, n: usize) { | ||
129 | for _ in 0..n { | ||
130 | let token = self.tokens[self.token_pos]; | ||
131 | assert!(token.kind.is_trivia()); | ||
132 | self.do_leaf(token.kind, token.len, 1); | ||
133 | } | ||
134 | } | ||
135 | |||
136 | fn do_leaf(&mut self, kind: SyntaxKind, len: TextUnit, n_tokens: usize) { | ||
137 | let range = TextRange::offset_len(self.text_pos, len); | ||
138 | let text: SmolStr = self.text[range].into(); | ||
139 | self.text_pos += len; | ||
140 | self.token_pos += n_tokens; | ||
141 | self.inner.leaf(kind, text); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | fn n_attached_trivias<'a>( | ||
146 | kind: SyntaxKind, | ||
147 | trivias: impl Iterator<Item = (SyntaxKind, &'a str)>, | ||
148 | ) -> usize { | ||
149 | match kind { | ||
150 | CONST_DEF | TYPE_DEF | STRUCT_DEF | ENUM_DEF | ENUM_VARIANT | FN_DEF | TRAIT_DEF | ||
151 | | MODULE | NAMED_FIELD_DEF => { | ||
152 | let mut res = 0; | ||
153 | for (i, (kind, text)) in trivias.enumerate() { | ||
154 | match kind { | ||
155 | WHITESPACE => { | ||
156 | if text.contains("\n\n") { | ||
157 | break; | ||
158 | } | ||
159 | } | ||
160 | COMMENT => { | ||
161 | res = i + 1; | ||
162 | } | ||
163 | _ => (), | ||
164 | } | ||
165 | } | ||
166 | res | ||
167 | } | ||
168 | _ => 0, | ||
169 | } | ||
49 | } | 170 | } |