diff options
author | Kevin Mehall <[email protected]> | 2021-03-06 19:30:43 +0000 |
---|---|---|
committer | Kevin Mehall <[email protected]> | 2021-03-06 19:30:43 +0000 |
commit | aea974939064b0f7b83de371a93ee4190c80e544 (patch) | |
tree | 3e70459ed77a91b24bc894c1ce0d1ef16f447a11 /crates | |
parent | 93c9b346359a1ca70126242f740e6fc0911c535b (diff) |
Move TokenStream::to_string helpers inside the method
Diffstat (limited to 'crates')
-rw-r--r-- | crates/proc_macro_srv/src/rustc_server.rs | 64 |
1 files changed, 34 insertions, 30 deletions
diff --git a/crates/proc_macro_srv/src/rustc_server.rs b/crates/proc_macro_srv/src/rustc_server.rs index 2798dbf0d..ceefd187d 100644 --- a/crates/proc_macro_srv/src/rustc_server.rs +++ b/crates/proc_macro_srv/src/rustc_server.rs | |||
@@ -199,39 +199,43 @@ pub mod token_stream { | |||
199 | 199 | ||
200 | impl ToString for TokenStream { | 200 | impl ToString for TokenStream { |
201 | fn to_string(&self) -> String { | 201 | fn to_string(&self) -> String { |
202 | tokentrees_to_text(&self.token_trees[..]) | 202 | return tokentrees_to_text(&self.token_trees[..]); |
203 | } | 203 | |
204 | } | 204 | fn tokentrees_to_text(tkns: &[tt::TokenTree]) -> String { |
205 | tkns.iter() | ||
206 | .fold((String::new(), true), |(last, last_to_joint), tkn| { | ||
207 | let s = [last, tokentree_to_text(tkn)].join(if last_to_joint { | ||
208 | "" | ||
209 | } else { | ||
210 | " " | ||
211 | }); | ||
212 | let mut is_joint = false; | ||
213 | if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn { | ||
214 | if punct.spacing == tt::Spacing::Joint { | ||
215 | is_joint = true; | ||
216 | } | ||
217 | } | ||
218 | (s, is_joint) | ||
219 | }) | ||
220 | .0 | ||
221 | } | ||
205 | 222 | ||
206 | fn tokentrees_to_text(tkns: &[tt::TokenTree]) -> String { | 223 | fn tokentree_to_text(tkn: &tt::TokenTree) -> String { |
207 | tkns.iter() | 224 | match tkn { |
208 | .fold((String::new(), true), |(last, last_to_joint), tkn| { | 225 | tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident.text.clone().into(), |
209 | let s = [last, tokentree_to_text(tkn)].join(if last_to_joint { "" } else { " " }); | 226 | tt::TokenTree::Leaf(tt::Leaf::Literal(literal)) => literal.text.clone().into(), |
210 | let mut is_joint = false; | 227 | tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => format!("{}", punct.char), |
211 | if let tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) = tkn { | 228 | tt::TokenTree::Subtree(subtree) => { |
212 | if punct.spacing == tt::Spacing::Joint { | 229 | let content = tokentrees_to_text(&subtree.token_trees); |
213 | is_joint = true; | 230 | let (open, close) = match subtree.delimiter.map(|it| it.kind) { |
231 | None => ("", ""), | ||
232 | Some(tt::DelimiterKind::Brace) => ("{", "}"), | ||
233 | Some(tt::DelimiterKind::Parenthesis) => ("(", ")"), | ||
234 | Some(tt::DelimiterKind::Bracket) => ("[", "]"), | ||
235 | }; | ||
236 | format!("{}{}{}", open, content, close) | ||
214 | } | 237 | } |
215 | } | 238 | } |
216 | (s, is_joint) | ||
217 | }) | ||
218 | .0 | ||
219 | } | ||
220 | |||
221 | fn tokentree_to_text(tkn: &tt::TokenTree) -> String { | ||
222 | match tkn { | ||
223 | tt::TokenTree::Leaf(tt::Leaf::Ident(ident)) => ident.text.clone().into(), | ||
224 | tt::TokenTree::Leaf(tt::Leaf::Literal(literal)) => literal.text.clone().into(), | ||
225 | tt::TokenTree::Leaf(tt::Leaf::Punct(punct)) => format!("{}", punct.char), | ||
226 | tt::TokenTree::Subtree(subtree) => { | ||
227 | let content = tokentrees_to_text(&subtree.token_trees); | ||
228 | let (open, close) = match subtree.delimiter.map(|it| it.kind) { | ||
229 | None => ("", ""), | ||
230 | Some(tt::DelimiterKind::Brace) => ("{", "}"), | ||
231 | Some(tt::DelimiterKind::Parenthesis) => ("(", ")"), | ||
232 | Some(tt::DelimiterKind::Bracket) => ("[", "]"), | ||
233 | }; | ||
234 | format!("{}{}{}", open, content, close) | ||
235 | } | 239 | } |
236 | } | 240 | } |
237 | } | 241 | } |