diff options
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 176 |
1 files changed, 131 insertions, 45 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index b8e2cfc1d..2c60430d1 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -5,17 +5,37 @@ use ra_syntax::{ | |||
5 | ast, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, | 5 | ast, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, |
6 | SyntaxTreeBuilder, TextRange, TextUnit, T, | 6 | SyntaxTreeBuilder, TextRange, TextUnit, T, |
7 | }; | 7 | }; |
8 | use rustc_hash::FxHashMap; | ||
8 | use std::iter::successors; | 9 | use std::iter::successors; |
9 | use tt::buffer::{Cursor, TokenBuffer}; | 10 | use tt::buffer::{Cursor, TokenBuffer}; |
10 | 11 | ||
11 | use crate::subtree_source::SubtreeTokenSource; | 12 | use crate::subtree_source::SubtreeTokenSource; |
12 | use crate::ExpandError; | 13 | use crate::ExpandError; |
13 | 14 | ||
15 | #[derive(Debug, PartialEq, Eq, Clone, Copy)] | ||
16 | pub enum TokenTextRange { | ||
17 | Token(TextRange), | ||
18 | Delimiter(TextRange, TextRange), | ||
19 | } | ||
20 | |||
21 | impl TokenTextRange { | ||
22 | pub fn by_kind(self, kind: SyntaxKind) -> Option<TextRange> { | ||
23 | match self { | ||
24 | TokenTextRange::Token(it) => Some(it), | ||
25 | TokenTextRange::Delimiter(open, close) => match kind { | ||
26 | T!['{'] | T!['('] | T!['['] => Some(open), | ||
27 | T!['}'] | T![')'] | T![']'] => Some(close), | ||
28 | _ => None, | ||
29 | }, | ||
30 | } | ||
31 | } | ||
32 | } | ||
33 | |||
14 | /// Maps `tt::TokenId` to the relative range of the original token. | 34 | /// Maps `tt::TokenId` to the relative range of the original token. |
15 | #[derive(Debug, PartialEq, Eq, Default)] | 35 | #[derive(Debug, PartialEq, Eq, Default)] |
16 | pub struct TokenMap { | 36 | pub struct TokenMap { |
17 | /// Maps `tt::TokenId` to the *relative* source range. | 37 | /// Maps `tt::TokenId` to the *relative* source range. |
18 | entries: Vec<(tt::TokenId, TextRange)>, | 38 | entries: Vec<(tt::TokenId, TokenTextRange)>, |
19 | } | 39 | } |
20 | 40 | ||
21 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro | 41 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro |
@@ -71,17 +91,32 @@ pub fn token_tree_to_syntax_node( | |||
71 | 91 | ||
72 | impl TokenMap { | 92 | impl TokenMap { |
73 | pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> { | 93 | pub fn token_by_range(&self, relative_range: TextRange) -> Option<tt::TokenId> { |
74 | let &(token_id, _) = self.entries.iter().find(|(_, range)| *range == relative_range)?; | 94 | let &(token_id, _) = self.entries.iter().find(|(_, range)| match range { |
95 | TokenTextRange::Token(it) => *it == relative_range, | ||
96 | TokenTextRange::Delimiter(open, close) => { | ||
97 | *open == relative_range || *close == relative_range | ||
98 | } | ||
99 | })?; | ||
75 | Some(token_id) | 100 | Some(token_id) |
76 | } | 101 | } |
77 | 102 | ||
78 | pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TextRange> { | 103 | pub fn range_by_token(&self, token_id: tt::TokenId) -> Option<TokenTextRange> { |
79 | let &(_, range) = self.entries.iter().find(|(tid, _)| *tid == token_id)?; | 104 | let &(_, range) = self.entries.iter().find(|(tid, _)| *tid == token_id)?; |
80 | Some(range) | 105 | Some(range) |
81 | } | 106 | } |
82 | 107 | ||
83 | fn insert(&mut self, token_id: tt::TokenId, relative_range: TextRange) { | 108 | fn insert(&mut self, token_id: tt::TokenId, relative_range: TextRange) { |
84 | self.entries.push((token_id, relative_range)); | 109 | self.entries.push((token_id, TokenTextRange::Token(relative_range))); |
110 | } | ||
111 | |||
112 | fn insert_delim( | ||
113 | &mut self, | ||
114 | token_id: tt::TokenId, | ||
115 | open_relative_range: TextRange, | ||
116 | close_relative_range: TextRange, | ||
117 | ) { | ||
118 | self.entries | ||
119 | .push((token_id, TokenTextRange::Delimiter(open_relative_range, close_relative_range))); | ||
85 | } | 120 | } |
86 | } | 121 | } |
87 | 122 | ||
@@ -121,7 +156,10 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option<Vec<tt::TokenTr | |||
121 | token_trees.push(mk_punct('!')); | 156 | token_trees.push(mk_punct('!')); |
122 | } | 157 | } |
123 | token_trees.push(tt::TokenTree::from(tt::Subtree { | 158 | token_trees.push(tt::TokenTree::from(tt::Subtree { |
124 | delimiter: Some(tt::Delimiter::Bracket), | 159 | delimiter: Some(tt::Delimiter { |
160 | kind: tt::DelimiterKind::Bracket, | ||
161 | id: tt::TokenId::unspecified(), | ||
162 | }), | ||
125 | token_trees: meta_tkns, | 163 | token_trees: meta_tkns, |
126 | })); | 164 | })); |
127 | 165 | ||
@@ -136,11 +174,15 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option<Vec<tt::TokenTr | |||
136 | } | 174 | } |
137 | 175 | ||
138 | fn mk_punct(c: char) -> tt::TokenTree { | 176 | fn mk_punct(c: char) -> tt::TokenTree { |
139 | tt::TokenTree::from(tt::Leaf::from(tt::Punct { char: c, spacing: tt::Spacing::Alone })) | 177 | tt::TokenTree::from(tt::Leaf::from(tt::Punct { |
178 | char: c, | ||
179 | spacing: tt::Spacing::Alone, | ||
180 | id: tt::TokenId::unspecified(), | ||
181 | })) | ||
140 | } | 182 | } |
141 | 183 | ||
142 | fn mk_doc_literal(comment: &ast::Comment) -> tt::TokenTree { | 184 | fn mk_doc_literal(comment: &ast::Comment) -> tt::TokenTree { |
143 | let lit = tt::Literal { text: doc_comment_text(comment) }; | 185 | let lit = tt::Literal { text: doc_comment_text(comment), id: tt::TokenId::unspecified() }; |
144 | 186 | ||
145 | tt::TokenTree::from(tt::Leaf::from(lit)) | 187 | tt::TokenTree::from(tt::Leaf::from(lit)) |
146 | } | 188 | } |
@@ -186,12 +228,16 @@ impl Convertor { | |||
186 | .last() | 228 | .last() |
187 | .unwrap(); | 229 | .unwrap(); |
188 | 230 | ||
189 | let (delimiter, skip_first) = match (first_child.kind(), last_child.kind()) { | 231 | let (delimiter_kind, skip_first) = match (first_child.kind(), last_child.kind()) { |
190 | (T!['('], T![')']) => (Some(tt::Delimiter::Parenthesis), true), | 232 | (T!['('], T![')']) => (Some(tt::DelimiterKind::Parenthesis), true), |
191 | (T!['{'], T!['}']) => (Some(tt::Delimiter::Brace), true), | 233 | (T!['{'], T!['}']) => (Some(tt::DelimiterKind::Brace), true), |
192 | (T!['['], T![']']) => (Some(tt::Delimiter::Bracket), true), | 234 | (T!['['], T![']']) => (Some(tt::DelimiterKind::Bracket), true), |
193 | _ => (None, false), | 235 | _ => (None, false), |
194 | }; | 236 | }; |
237 | let delimiter = delimiter_kind.map(|kind| tt::Delimiter { | ||
238 | kind, | ||
239 | id: self.alloc_delim(first_child.text_range(), last_child.text_range()), | ||
240 | }); | ||
195 | 241 | ||
196 | let mut token_trees = Vec::new(); | 242 | let mut token_trees = Vec::new(); |
197 | let mut child_iter = tt.children_with_tokens().skip(skip_first as usize).peekable(); | 243 | let mut child_iter = tt.children_with_tokens().skip(skip_first as usize).peekable(); |
@@ -223,25 +269,34 @@ impl Convertor { | |||
223 | .take(token.text().len() - 1) | 269 | .take(token.text().len() - 1) |
224 | .chain(std::iter::once(last_spacing)); | 270 | .chain(std::iter::once(last_spacing)); |
225 | for (char, spacing) in token.text().chars().zip(spacing_iter) { | 271 | for (char, spacing) in token.text().chars().zip(spacing_iter) { |
226 | token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); | 272 | token_trees.push( |
273 | tt::Leaf::from(tt::Punct { | ||
274 | char, | ||
275 | spacing, | ||
276 | id: self.alloc(token.text_range()), | ||
277 | }) | ||
278 | .into(), | ||
279 | ); | ||
227 | } | 280 | } |
228 | } else { | 281 | } else { |
229 | let child: tt::TokenTree = | 282 | macro_rules! make_leaf { |
230 | if token.kind() == T![true] || token.kind() == T![false] { | 283 | ($i:ident) => { |
231 | tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() | 284 | tt::$i { |
232 | } else if token.kind().is_keyword() | 285 | id: self.alloc(token.text_range()), |
233 | || token.kind() == IDENT | 286 | text: token.text().clone(), |
234 | || token.kind() == LIFETIME | 287 | } |
235 | { | 288 | .into() |
236 | let id = self.alloc(token.text_range()); | ||
237 | let text = token.text().clone(); | ||
238 | tt::Leaf::from(tt::Ident { text, id }).into() | ||
239 | } else if token.kind().is_literal() { | ||
240 | tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() | ||
241 | } else { | ||
242 | return None; | ||
243 | }; | 289 | }; |
244 | token_trees.push(child); | 290 | } |
291 | |||
292 | let child: tt::Leaf = match token.kind() { | ||
293 | T![true] | T![false] => make_leaf!(Literal), | ||
294 | IDENT | LIFETIME => make_leaf!(Ident), | ||
295 | k if k.is_keyword() => make_leaf!(Ident), | ||
296 | k if k.is_literal() => make_leaf!(Literal), | ||
297 | _ => return None, | ||
298 | }; | ||
299 | token_trees.push(child.into()); | ||
245 | } | 300 | } |
246 | } | 301 | } |
247 | NodeOrToken::Node(node) => { | 302 | NodeOrToken::Node(node) => { |
@@ -266,11 +321,26 @@ impl Convertor { | |||
266 | self.map.insert(token_id, relative_range); | 321 | self.map.insert(token_id, relative_range); |
267 | token_id | 322 | token_id |
268 | } | 323 | } |
324 | |||
325 | fn alloc_delim( | ||
326 | &mut self, | ||
327 | open_abs_range: TextRange, | ||
328 | close_abs_range: TextRange, | ||
329 | ) -> tt::TokenId { | ||
330 | let open_relative_range = open_abs_range - self.global_offset; | ||
331 | let close_relative_range = close_abs_range - self.global_offset; | ||
332 | let token_id = tt::TokenId(self.next_id); | ||
333 | self.next_id += 1; | ||
334 | |||
335 | self.map.insert_delim(token_id, open_relative_range, close_relative_range); | ||
336 | token_id | ||
337 | } | ||
269 | } | 338 | } |
270 | 339 | ||
271 | struct TtTreeSink<'a> { | 340 | struct TtTreeSink<'a> { |
272 | buf: String, | 341 | buf: String, |
273 | cursor: Cursor<'a>, | 342 | cursor: Cursor<'a>, |
343 | open_delims: FxHashMap<tt::TokenId, TextUnit>, | ||
274 | text_pos: TextUnit, | 344 | text_pos: TextUnit, |
275 | inner: SyntaxTreeBuilder, | 345 | inner: SyntaxTreeBuilder, |
276 | token_map: TokenMap, | 346 | token_map: TokenMap, |
@@ -285,6 +355,7 @@ impl<'a> TtTreeSink<'a> { | |||
285 | TtTreeSink { | 355 | TtTreeSink { |
286 | buf: String::new(), | 356 | buf: String::new(), |
287 | cursor, | 357 | cursor, |
358 | open_delims: FxHashMap::default(), | ||
288 | text_pos: 0.into(), | 359 | text_pos: 0.into(), |
289 | inner: SyntaxTreeBuilder::default(), | 360 | inner: SyntaxTreeBuilder::default(), |
290 | roots: smallvec::SmallVec::new(), | 361 | roots: smallvec::SmallVec::new(), |
@@ -297,11 +368,11 @@ impl<'a> TtTreeSink<'a> { | |||
297 | } | 368 | } |
298 | } | 369 | } |
299 | 370 | ||
300 | fn delim_to_str(d: Option<tt::Delimiter>, closing: bool) -> SmolStr { | 371 | fn delim_to_str(d: Option<tt::DelimiterKind>, closing: bool) -> SmolStr { |
301 | let texts = match d { | 372 | let texts = match d { |
302 | Some(tt::Delimiter::Parenthesis) => "()", | 373 | Some(tt::DelimiterKind::Parenthesis) => "()", |
303 | Some(tt::Delimiter::Brace) => "{}", | 374 | Some(tt::DelimiterKind::Brace) => "{}", |
304 | Some(tt::Delimiter::Bracket) => "[]", | 375 | Some(tt::DelimiterKind::Bracket) => "[]", |
305 | None => return "".into(), | 376 | None => return "".into(), |
306 | }; | 377 | }; |
307 | 378 | ||
@@ -322,34 +393,49 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
322 | break; | 393 | break; |
323 | } | 394 | } |
324 | 395 | ||
325 | match self.cursor.token_tree() { | 396 | let text: SmolStr = match self.cursor.token_tree() { |
326 | Some(tt::TokenTree::Leaf(leaf)) => { | 397 | Some(tt::TokenTree::Leaf(leaf)) => { |
327 | // Mark the range if needed | 398 | // Mark the range if needed |
328 | if let tt::Leaf::Ident(ident) = leaf { | 399 | let id = match leaf { |
329 | if kind == IDENT { | 400 | tt::Leaf::Ident(ident) => ident.id, |
330 | let range = | 401 | tt::Leaf::Punct(punct) => punct.id, |
331 | TextRange::offset_len(self.text_pos, TextUnit::of_str(&ident.text)); | 402 | tt::Leaf::Literal(lit) => lit.id, |
332 | self.token_map.insert(ident.id, range); | 403 | }; |
333 | } | 404 | let text = SmolStr::new(format!("{}", leaf)); |
334 | } | 405 | let range = TextRange::offset_len(self.text_pos, TextUnit::of_str(&text)); |
335 | 406 | self.token_map.insert(id, range); | |
336 | self.cursor = self.cursor.bump(); | 407 | self.cursor = self.cursor.bump(); |
337 | self.buf += &format!("{}", leaf); | 408 | text |
338 | } | 409 | } |
339 | Some(tt::TokenTree::Subtree(subtree)) => { | 410 | Some(tt::TokenTree::Subtree(subtree)) => { |
340 | self.cursor = self.cursor.subtree().unwrap(); | 411 | self.cursor = self.cursor.subtree().unwrap(); |
341 | self.buf += &delim_to_str(subtree.delimiter, false); | 412 | if let Some(id) = subtree.delimiter.map(|it| it.id) { |
413 | self.open_delims.insert(id, self.text_pos); | ||
414 | } | ||
415 | delim_to_str(subtree.delimiter_kind(), false) | ||
342 | } | 416 | } |
343 | None => { | 417 | None => { |
344 | if let Some(parent) = self.cursor.end() { | 418 | if let Some(parent) = self.cursor.end() { |
345 | self.cursor = self.cursor.bump(); | 419 | self.cursor = self.cursor.bump(); |
346 | self.buf += &delim_to_str(parent.delimiter, true); | 420 | if let Some(id) = parent.delimiter.map(|it| it.id) { |
421 | if let Some(open_delim) = self.open_delims.get(&id) { | ||
422 | let open_range = | ||
423 | TextRange::offset_len(*open_delim, TextUnit::from_usize(1)); | ||
424 | let close_range = | ||
425 | TextRange::offset_len(self.text_pos, TextUnit::from_usize(1)); | ||
426 | self.token_map.insert_delim(id, open_range, close_range); | ||
427 | } | ||
428 | } | ||
429 | delim_to_str(parent.delimiter_kind(), true) | ||
430 | } else { | ||
431 | continue; | ||
347 | } | 432 | } |
348 | } | 433 | } |
349 | }; | 434 | }; |
435 | self.buf += &text; | ||
436 | self.text_pos += TextUnit::of_str(&text); | ||
350 | } | 437 | } |
351 | 438 | ||
352 | self.text_pos += TextUnit::of_str(&self.buf); | ||
353 | let text = SmolStr::new(self.buf.as_str()); | 439 | let text = SmolStr::new(self.buf.as_str()); |
354 | self.buf.clear(); | 440 | self.buf.clear(); |
355 | self.inner.token(kind, text); | 441 | self.inner.token(kind, text); |
@@ -495,7 +581,7 @@ mod tests { | |||
495 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); | 581 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); |
496 | let tt = ast_to_token_tree(&token_tree).unwrap().0; | 582 | let tt = ast_to_token_tree(&token_tree).unwrap().0; |
497 | 583 | ||
498 | assert_eq!(tt.delimiter, Some(tt::Delimiter::Brace)); | 584 | assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); |
499 | } | 585 | } |
500 | 586 | ||
501 | #[test] | 587 | #[test] |