aboutsummaryrefslogtreecommitdiff
path: root/crates/tt
diff options
context:
space:
mode:
Diffstat (limited to 'crates/tt')
-rw-r--r--crates/tt/src/lib.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/crates/tt/src/lib.rs b/crates/tt/src/lib.rs
index 8301dc28a..9d9a01e30 100644
--- a/crates/tt/src/lib.rs
+++ b/crates/tt/src/lib.rs
@@ -227,6 +227,53 @@ impl Subtree {
227 } 227 }
228} 228}
229 229
230impl Subtree {
231 /// A simple line string used for debugging
232 pub fn as_debug_string(&self) -> String {
233 let delim = match self.delimiter_kind() {
234 Some(DelimiterKind::Brace) => ("{", "}"),
235 Some(DelimiterKind::Bracket) => ("[", "]"),
236 Some(DelimiterKind::Parenthesis) => ("(", ")"),
237 None => (" ", " "),
238 };
239
240 let mut res = String::new();
241 res.push_str(delim.0);
242 let mut iter = self.token_trees.iter();
243 let mut last = None;
244 while let Some(child) = iter.next() {
245 let s = match child {
246 TokenTree::Leaf(it) => {
247 let s = match it {
248 Leaf::Literal(it) => it.text.to_string(),
249 Leaf::Punct(it) => it.char.to_string(),
250 Leaf::Ident(it) => it.text.to_string(),
251 };
252 match (it, last) {
253 (Leaf::Ident(_), Some(&TokenTree::Leaf(Leaf::Ident(_)))) => {
254 " ".to_string() + &s
255 }
256 (Leaf::Punct(_), Some(&TokenTree::Leaf(Leaf::Punct(punct)))) => {
257 if punct.spacing == Spacing::Alone {
258 " ".to_string() + &s
259 } else {
260 s
261 }
262 }
263 _ => s,
264 }
265 }
266 TokenTree::Subtree(it) => it.as_debug_string(),
267 };
268 res.push_str(&s);
269 last = Some(child);
270 }
271
272 res.push_str(delim.1);
273 res
274 }
275}
276
230pub mod buffer; 277pub mod buffer;
231 278
232#[derive(Debug, PartialEq, Eq, Clone)] 279#[derive(Debug, PartialEq, Eq, Clone)]