diff options
author | Edwin Cheng <[email protected]> | 2021-03-05 19:17:36 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2021-03-05 19:17:36 +0000 |
commit | 0405d758a5d9db06994c4bc4ad6683d3afffd44a (patch) | |
tree | b6fff536dab0dfd9d4895f1d1fc4f82f299e7194 /crates/proc_macro_srv | |
parent | 16a76aa158d0898d6a46d7bba7310150555a69b9 (diff) |
Simplify TokenStream FromStr
Diffstat (limited to 'crates/proc_macro_srv')
-rw-r--r-- | crates/proc_macro_srv/src/rustc_server.rs | 80 |
1 files changed, 42 insertions, 38 deletions
diff --git a/crates/proc_macro_srv/src/rustc_server.rs b/crates/proc_macro_srv/src/rustc_server.rs index 952b4a97f..14c853c77 100644 --- a/crates/proc_macro_srv/src/rustc_server.rs +++ b/crates/proc_macro_srv/src/rustc_server.rs | |||
@@ -184,6 +184,7 @@ pub mod token_stream { | |||
184 | let (subtree, _token_map) = | 184 | let (subtree, _token_map) = |
185 | mbe::parse_to_token_tree(src).ok_or("Failed to parse from mbe")?; | 185 | mbe::parse_to_token_tree(src).ok_or("Failed to parse from mbe")?; |
186 | 186 | ||
187 | let subtree = subtree_replace_token_ids_with_unspecified(subtree); | ||
187 | Ok(TokenStream { subtree }) | 188 | Ok(TokenStream { subtree }) |
188 | } | 189 | } |
189 | } | 190 | } |
@@ -226,6 +227,44 @@ pub mod token_stream { | |||
226 | } | 227 | } |
227 | } | 228 | } |
228 | } | 229 | } |
230 | |||
231 | fn subtree_replace_token_ids_with_unspecified(subtree: tt::Subtree) -> tt::Subtree { | ||
232 | tt::Subtree { | ||
233 | delimiter: subtree | ||
234 | .delimiter | ||
235 | .map(|d| tt::Delimiter { id: tt::TokenId::unspecified(), ..d }), | ||
236 | token_trees: subtree | ||
237 | .token_trees | ||
238 | .into_iter() | ||
239 | .map(|t| token_tree_replace_token_ids_with_unspecified(t)) | ||
240 | .collect(), | ||
241 | } | ||
242 | } | ||
243 | |||
244 | fn token_tree_replace_token_ids_with_unspecified(tt: tt::TokenTree) -> tt::TokenTree { | ||
245 | match tt { | ||
246 | tt::TokenTree::Leaf(leaf) => { | ||
247 | tt::TokenTree::Leaf(leaf_replace_token_ids_with_unspecified(leaf)) | ||
248 | } | ||
249 | tt::TokenTree::Subtree(subtree) => { | ||
250 | tt::TokenTree::Subtree(subtree_replace_token_ids_with_unspecified(subtree)) | ||
251 | } | ||
252 | } | ||
253 | } | ||
254 | |||
255 | fn leaf_replace_token_ids_with_unspecified(leaf: tt::Leaf) -> tt::Leaf { | ||
256 | match leaf { | ||
257 | tt::Leaf::Literal(lit) => { | ||
258 | tt::Leaf::Literal(tt::Literal { id: tt::TokenId::unspecified(), ..lit }) | ||
259 | } | ||
260 | tt::Leaf::Punct(punct) => { | ||
261 | tt::Leaf::Punct(tt::Punct { id: tt::TokenId::unspecified(), ..punct }) | ||
262 | } | ||
263 | tt::Leaf::Ident(ident) => { | ||
264 | tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), ..ident }) | ||
265 | } | ||
266 | } | ||
267 | } | ||
229 | } | 268 | } |
230 | 269 | ||
231 | impl TokenStreamBuilder { | 270 | impl TokenStreamBuilder { |
@@ -277,42 +316,6 @@ impl server::FreeFunctions for Rustc { | |||
277 | } | 316 | } |
278 | } | 317 | } |
279 | 318 | ||
280 | fn subtree_replace_token_ids_with_unspecified(subtree: tt::Subtree) -> tt::Subtree { | ||
281 | tt::Subtree { | ||
282 | delimiter: subtree.delimiter.map(|d| tt::Delimiter { id: tt::TokenId::unspecified(), ..d }), | ||
283 | token_trees: subtree | ||
284 | .token_trees | ||
285 | .into_iter() | ||
286 | .map(|t| token_tree_replace_token_ids_with_unspecified(t)) | ||
287 | .collect(), | ||
288 | } | ||
289 | } | ||
290 | |||
291 | fn token_tree_replace_token_ids_with_unspecified(tt: tt::TokenTree) -> tt::TokenTree { | ||
292 | match tt { | ||
293 | tt::TokenTree::Leaf(leaf) => { | ||
294 | tt::TokenTree::Leaf(leaf_replace_token_ids_with_unspecified(leaf)) | ||
295 | } | ||
296 | tt::TokenTree::Subtree(subtree) => { | ||
297 | tt::TokenTree::Subtree(subtree_replace_token_ids_with_unspecified(subtree)) | ||
298 | } | ||
299 | } | ||
300 | } | ||
301 | |||
302 | fn leaf_replace_token_ids_with_unspecified(leaf: tt::Leaf) -> tt::Leaf { | ||
303 | match leaf { | ||
304 | tt::Leaf::Literal(lit) => { | ||
305 | tt::Leaf::Literal(tt::Literal { id: tt::TokenId::unspecified(), ..lit }) | ||
306 | } | ||
307 | tt::Leaf::Punct(punct) => { | ||
308 | tt::Leaf::Punct(tt::Punct { id: tt::TokenId::unspecified(), ..punct }) | ||
309 | } | ||
310 | tt::Leaf::Ident(ident) => { | ||
311 | tt::Leaf::Ident(tt::Ident { id: tt::TokenId::unspecified(), ..ident }) | ||
312 | } | ||
313 | } | ||
314 | } | ||
315 | |||
316 | impl server::TokenStream for Rustc { | 319 | impl server::TokenStream for Rustc { |
317 | fn new(&mut self) -> Self::TokenStream { | 320 | fn new(&mut self) -> Self::TokenStream { |
318 | Self::TokenStream::new() | 321 | Self::TokenStream::new() |
@@ -322,8 +325,9 @@ impl server::TokenStream for Rustc { | |||
322 | stream.is_empty() | 325 | stream.is_empty() |
323 | } | 326 | } |
324 | fn from_str(&mut self, src: &str) -> Self::TokenStream { | 327 | fn from_str(&mut self, src: &str) -> Self::TokenStream { |
325 | let (subtree, _) = mbe::parse_to_token_tree(src).expect("cannot parse string"); | 328 | use std::str::FromStr; |
326 | TokenStream::with_subtree(subtree_replace_token_ids_with_unspecified(subtree)) | 329 | |
330 | Self::TokenStream::from_str(src).expect("cannot parse string") | ||
327 | } | 331 | } |
328 | fn to_string(&mut self, stream: &Self::TokenStream) -> String { | 332 | fn to_string(&mut self, stream: &Self::TokenStream) -> String { |
329 | stream.to_string() | 333 | stream.to_string() |