diff options
author | Edwin Cheng <edwin0cheng@gmail.com> | 2019-12-12 13:47:54 +0000 |
---|---|---|
committer | Edwin Cheng <edwin0cheng@gmail.com> | 2019-12-18 03:20:22 +0000 |
commit | aceb9d7fb0809ccf364514d9177342edea144c59 (patch) | |
tree | 138ee608477eb951072eeac6ace239647e3d01a3 /crates/ra_mbe | |
parent | 46ca40ccfced6945e05a25979a2703ad967d2fe0 (diff) |
Add token ids for all tt::Leaf
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r-- | crates/ra_mbe/src/mbe_expander/transcriber.rs | 7 | ||||
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 45 | ||||
-rw-r--r-- | crates/ra_mbe/src/tests.rs | 10 |
3 files changed, 38 insertions, 24 deletions
diff --git a/crates/ra_mbe/src/mbe_expander/transcriber.rs b/crates/ra_mbe/src/mbe_expander/transcriber.rs index f7636db11..eda66cd50 100644 --- a/crates/ra_mbe/src/mbe_expander/transcriber.rs +++ b/crates/ra_mbe/src/mbe_expander/transcriber.rs | |||
@@ -108,7 +108,12 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> Result<Fragment, ExpandError> | |||
108 | let tt = tt::Subtree { | 108 | let tt = tt::Subtree { |
109 | delimiter: None, | 109 | delimiter: None, |
110 | token_trees: vec![ | 110 | token_trees: vec![ |
111 | tt::Leaf::from(tt::Punct { char: '$', spacing: tt::Spacing::Alone }).into(), | 111 | tt::Leaf::from(tt::Punct { |
112 | char: '$', | ||
113 | spacing: tt::Spacing::Alone, | ||
114 | id: tt::TokenId::unspecified(), | ||
115 | }) | ||
116 | .into(), | ||
112 | tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) | 117 | tt::Leaf::from(tt::Ident { text: v.clone(), id: tt::TokenId::unspecified() }) |
113 | .into(), | 118 | .into(), |
114 | ], | 119 | ], |
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index b8e2cfc1d..8f65ff125 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -136,11 +136,15 @@ fn convert_doc_comment(token: &ra_syntax::SyntaxToken) -> Option<Vec<tt::TokenTr | |||
136 | } | 136 | } |
137 | 137 | ||
138 | fn mk_punct(c: char) -> tt::TokenTree { | 138 | fn mk_punct(c: char) -> tt::TokenTree { |
139 | tt::TokenTree::from(tt::Leaf::from(tt::Punct { char: c, spacing: tt::Spacing::Alone })) | 139 | tt::TokenTree::from(tt::Leaf::from(tt::Punct { |
140 | char: c, | ||
141 | spacing: tt::Spacing::Alone, | ||
142 | id: tt::TokenId::unspecified(), | ||
143 | })) | ||
140 | } | 144 | } |
141 | 145 | ||
142 | fn mk_doc_literal(comment: &ast::Comment) -> tt::TokenTree { | 146 | fn mk_doc_literal(comment: &ast::Comment) -> tt::TokenTree { |
143 | let lit = tt::Literal { text: doc_comment_text(comment) }; | 147 | let lit = tt::Literal { text: doc_comment_text(comment), id: tt::TokenId::unspecified() }; |
144 | 148 | ||
145 | tt::TokenTree::from(tt::Leaf::from(lit)) | 149 | tt::TokenTree::from(tt::Leaf::from(lit)) |
146 | } | 150 | } |
@@ -223,24 +227,29 @@ impl Convertor { | |||
223 | .take(token.text().len() - 1) | 227 | .take(token.text().len() - 1) |
224 | .chain(std::iter::once(last_spacing)); | 228 | .chain(std::iter::once(last_spacing)); |
225 | for (char, spacing) in token.text().chars().zip(spacing_iter) { | 229 | for (char, spacing) in token.text().chars().zip(spacing_iter) { |
226 | token_trees.push(tt::Leaf::from(tt::Punct { char, spacing }).into()); | 230 | let id = self.alloc(token.text_range()); |
231 | token_trees | ||
232 | .push(tt::Leaf::from(tt::Punct { char, spacing, id }).into()); | ||
227 | } | 233 | } |
228 | } else { | 234 | } else { |
229 | let child: tt::TokenTree = | 235 | let child: tt::TokenTree = if token.kind() == T![true] |
230 | if token.kind() == T![true] || token.kind() == T![false] { | 236 | || token.kind() == T![false] |
231 | tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() | 237 | { |
232 | } else if token.kind().is_keyword() | 238 | let id = self.alloc(token.text_range()); |
233 | || token.kind() == IDENT | 239 | tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() |
234 | || token.kind() == LIFETIME | 240 | } else if token.kind().is_keyword() |
235 | { | 241 | || token.kind() == IDENT |
236 | let id = self.alloc(token.text_range()); | 242 | || token.kind() == LIFETIME |
237 | let text = token.text().clone(); | 243 | { |
238 | tt::Leaf::from(tt::Ident { text, id }).into() | 244 | let id = self.alloc(token.text_range()); |
239 | } else if token.kind().is_literal() { | 245 | let text = token.text().clone(); |
240 | tt::Leaf::from(tt::Literal { text: token.text().clone() }).into() | 246 | tt::Leaf::from(tt::Ident { text, id }).into() |
241 | } else { | 247 | } else if token.kind().is_literal() { |
242 | return None; | 248 | let id = self.alloc(token.text_range()); |
243 | }; | 249 | tt::Leaf::from(tt::Literal { text: token.text().clone(), id }).into() |
250 | } else { | ||
251 | return None; | ||
252 | }; | ||
244 | token_trees.push(child); | 253 | token_trees.push(child); |
245 | } | 254 | } |
246 | } | 255 | } |
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs index 148cc2625..70e65bc74 100644 --- a/crates/ra_mbe/src/tests.rs +++ b/crates/ra_mbe/src/tests.rs | |||
@@ -78,12 +78,12 @@ macro_rules! foobar { | |||
78 | 78 | ||
79 | assert_eq!(expansion.token_trees.len(), 3); | 79 | assert_eq!(expansion.token_trees.len(), 3); |
80 | // ($e:ident) => { foo bar $e } | 80 | // ($e:ident) => { foo bar $e } |
81 | // 0 1 2 3 4 | 81 | // 0123 45 6 7 89 |
82 | assert_eq!(get_id(&expansion.token_trees[0]), Some(2)); | 82 | assert_eq!(get_id(&expansion.token_trees[0]), Some(6)); |
83 | assert_eq!(get_id(&expansion.token_trees[1]), Some(3)); | 83 | assert_eq!(get_id(&expansion.token_trees[1]), Some(7)); |
84 | 84 | ||
85 | // So baz should be 5 | 85 | // So baz should be 10 |
86 | assert_eq!(get_id(&expansion.token_trees[2]), Some(5)); | 86 | assert_eq!(get_id(&expansion.token_trees[2]), Some(10)); |
87 | } | 87 | } |
88 | 88 | ||
89 | #[test] | 89 | #[test] |