diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-08-12 17:31:42 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-08-12 17:31:42 +0100 |
commit | d583f2c46d22cf8d643ebf98be9cb7059a304431 (patch) | |
tree | 9d898eb9600b0c36a74e4f95238f679c683fa566 /crates/syntax/src/parsing/text_tree_sink.rs | |
parent | 3d6889cba72a9d02199f7adaa2ecc69bc30af834 (diff) | |
parent | a1c187eef3ba08076aedb5154929f7eda8d1b424 (diff) |
Merge #5729
5729: Rename ra_syntax -> syntax
r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/syntax/src/parsing/text_tree_sink.rs')
-rw-r--r-- | crates/syntax/src/parsing/text_tree_sink.rs | 183 |
1 files changed, 183 insertions, 0 deletions
diff --git a/crates/syntax/src/parsing/text_tree_sink.rs b/crates/syntax/src/parsing/text_tree_sink.rs new file mode 100644 index 000000000..c1b5f246d --- /dev/null +++ b/crates/syntax/src/parsing/text_tree_sink.rs | |||
@@ -0,0 +1,183 @@ | |||
1 | //! FIXME: write short doc here | ||
2 | |||
3 | use std::mem; | ||
4 | |||
5 | use parser::{ParseError, TreeSink}; | ||
6 | |||
7 | use crate::{ | ||
8 | parsing::Token, | ||
9 | syntax_node::GreenNode, | ||
10 | SmolStr, SyntaxError, | ||
11 | SyntaxKind::{self, *}, | ||
12 | SyntaxTreeBuilder, TextRange, TextSize, | ||
13 | }; | ||
14 | |||
15 | /// Bridges the parser with our specific syntax tree representation. | ||
16 | /// | ||
17 | /// `TextTreeSink` also handles attachment of trivia (whitespace) to nodes. | ||
18 | pub(crate) struct TextTreeSink<'a> { | ||
19 | text: &'a str, | ||
20 | tokens: &'a [Token], | ||
21 | text_pos: TextSize, | ||
22 | token_pos: usize, | ||
23 | state: State, | ||
24 | inner: SyntaxTreeBuilder, | ||
25 | } | ||
26 | |||
27 | enum State { | ||
28 | PendingStart, | ||
29 | Normal, | ||
30 | PendingFinish, | ||
31 | } | ||
32 | |||
33 | impl<'a> TreeSink for TextTreeSink<'a> { | ||
34 | fn token(&mut self, kind: SyntaxKind, n_tokens: u8) { | ||
35 | match mem::replace(&mut self.state, State::Normal) { | ||
36 | State::PendingStart => unreachable!(), | ||
37 | State::PendingFinish => self.inner.finish_node(), | ||
38 | State::Normal => (), | ||
39 | } | ||
40 | self.eat_trivias(); | ||
41 | let n_tokens = n_tokens as usize; | ||
42 | let len = self.tokens[self.token_pos..self.token_pos + n_tokens] | ||
43 | .iter() | ||
44 | .map(|it| it.len) | ||
45 | .sum::<TextSize>(); | ||
46 | self.do_token(kind, len, n_tokens); | ||
47 | } | ||
48 | |||
49 | fn start_node(&mut self, kind: SyntaxKind) { | ||
50 | match mem::replace(&mut self.state, State::Normal) { | ||
51 | State::PendingStart => { | ||
52 | self.inner.start_node(kind); | ||
53 | // No need to attach trivias to previous node: there is no | ||
54 | // previous node. | ||
55 | return; | ||
56 | } | ||
57 | State::PendingFinish => self.inner.finish_node(), | ||
58 | State::Normal => (), | ||
59 | } | ||
60 | |||
61 | let n_trivias = | ||
62 | self.tokens[self.token_pos..].iter().take_while(|it| it.kind.is_trivia()).count(); | ||
63 | let leading_trivias = &self.tokens[self.token_pos..self.token_pos + n_trivias]; | ||
64 | let mut trivia_end = | ||
65 | self.text_pos + leading_trivias.iter().map(|it| it.len).sum::<TextSize>(); | ||
66 | |||
67 | let n_attached_trivias = { | ||
68 | let leading_trivias = leading_trivias.iter().rev().map(|it| { | ||
69 | let next_end = trivia_end - it.len; | ||
70 | let range = TextRange::new(next_end, trivia_end); | ||
71 | trivia_end = next_end; | ||
72 | (it.kind, &self.text[range]) | ||
73 | }); | ||
74 | n_attached_trivias(kind, leading_trivias) | ||
75 | }; | ||
76 | self.eat_n_trivias(n_trivias - n_attached_trivias); | ||
77 | self.inner.start_node(kind); | ||
78 | self.eat_n_trivias(n_attached_trivias); | ||
79 | } | ||
80 | |||
81 | fn finish_node(&mut self) { | ||
82 | match mem::replace(&mut self.state, State::PendingFinish) { | ||
83 | State::PendingStart => unreachable!(), | ||
84 | State::PendingFinish => self.inner.finish_node(), | ||
85 | State::Normal => (), | ||
86 | } | ||
87 | } | ||
88 | |||
89 | fn error(&mut self, error: ParseError) { | ||
90 | self.inner.error(error, self.text_pos) | ||
91 | } | ||
92 | } | ||
93 | |||
94 | impl<'a> TextTreeSink<'a> { | ||
95 | pub(super) fn new(text: &'a str, tokens: &'a [Token]) -> Self { | ||
96 | Self { | ||
97 | text, | ||
98 | tokens, | ||
99 | text_pos: 0.into(), | ||
100 | token_pos: 0, | ||
101 | state: State::PendingStart, | ||
102 | inner: SyntaxTreeBuilder::default(), | ||
103 | } | ||
104 | } | ||
105 | |||
106 | pub(super) fn finish(mut self) -> (GreenNode, Vec<SyntaxError>) { | ||
107 | match mem::replace(&mut self.state, State::Normal) { | ||
108 | State::PendingFinish => { | ||
109 | self.eat_trivias(); | ||
110 | self.inner.finish_node() | ||
111 | } | ||
112 | State::PendingStart | State::Normal => unreachable!(), | ||
113 | } | ||
114 | |||
115 | self.inner.finish_raw() | ||
116 | } | ||
117 | |||
118 | fn eat_trivias(&mut self) { | ||
119 | while let Some(&token) = self.tokens.get(self.token_pos) { | ||
120 | if !token.kind.is_trivia() { | ||
121 | break; | ||
122 | } | ||
123 | self.do_token(token.kind, token.len, 1); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | fn eat_n_trivias(&mut self, n: usize) { | ||
128 | for _ in 0..n { | ||
129 | let token = self.tokens[self.token_pos]; | ||
130 | assert!(token.kind.is_trivia()); | ||
131 | self.do_token(token.kind, token.len, 1); | ||
132 | } | ||
133 | } | ||
134 | |||
135 | fn do_token(&mut self, kind: SyntaxKind, len: TextSize, n_tokens: usize) { | ||
136 | let range = TextRange::at(self.text_pos, len); | ||
137 | let text: SmolStr = self.text[range].into(); | ||
138 | self.text_pos += len; | ||
139 | self.token_pos += n_tokens; | ||
140 | self.inner.token(kind, text); | ||
141 | } | ||
142 | } | ||
143 | |||
144 | fn n_attached_trivias<'a>( | ||
145 | kind: SyntaxKind, | ||
146 | trivias: impl Iterator<Item = (SyntaxKind, &'a str)>, | ||
147 | ) -> usize { | ||
148 | match kind { | ||
149 | MACRO_CALL | CONST | TYPE_ALIAS | STRUCT | ENUM | VARIANT | FN | TRAIT | MODULE | ||
150 | | RECORD_FIELD | STATIC => { | ||
151 | let mut res = 0; | ||
152 | let mut trivias = trivias.enumerate().peekable(); | ||
153 | |||
154 | while let Some((i, (kind, text))) = trivias.next() { | ||
155 | match kind { | ||
156 | WHITESPACE => { | ||
157 | if text.contains("\n\n") { | ||
158 | // we check whether the next token is a doc-comment | ||
159 | // and skip the whitespace in this case | ||
160 | if let Some((peek_kind, peek_text)) = | ||
161 | trivias.peek().map(|(_, pair)| pair) | ||
162 | { | ||
163 | if *peek_kind == COMMENT | ||
164 | && peek_text.starts_with("///") | ||
165 | && !peek_text.starts_with("////") | ||
166 | { | ||
167 | continue; | ||
168 | } | ||
169 | } | ||
170 | break; | ||
171 | } | ||
172 | } | ||
173 | COMMENT => { | ||
174 | res = i + 1; | ||
175 | } | ||
176 | _ => (), | ||
177 | } | ||
178 | } | ||
179 | res | ||
180 | } | ||
181 | _ => 0, | ||
182 | } | ||
183 | } | ||