1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
use std::fmt;
use smol_str::SmolStr;
use join_to_string::join;
#[derive(Debug, Clone)]
pub enum TokenTree {
Leaf(Leaf),
Subtree(Subtree),
}
impl_froms!(TokenTree: Leaf, Subtree);
#[derive(Debug, Clone)]
pub enum Leaf {
Literal(Literal),
Punct(Punct),
Ident(Ident),
}
impl_froms!(Leaf: Literal, Punct, Ident);
#[derive(Debug, Clone)]
pub struct Subtree {
pub delimiter: Delimiter,
pub token_trees: Vec<TokenTree>,
}
#[derive(Clone, Copy, Debug)]
pub enum Delimiter {
Parenthesis,
Brace,
Bracket,
None,
}
#[derive(Debug, Clone)]
pub struct Literal {
pub text: SmolStr,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Punct {
pub char: char,
}
#[derive(Debug, Clone)]
pub struct Ident {
pub text: SmolStr,
}
impl fmt::Display for TokenTree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TokenTree::Leaf(it) => fmt::Display::fmt(it, f),
TokenTree::Subtree(it) => fmt::Display::fmt(it, f),
}
}
}
impl fmt::Display for Subtree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let (l, r) = match self.delimiter {
Delimiter::Parenthesis => ("(", ")"),
Delimiter::Brace => ("{", "}"),
Delimiter::Bracket => ("[", "]"),
Delimiter::None => ("", ""),
};
join(self.token_trees.iter())
.separator(" ")
.surround_with(l, r)
.to_fmt(f)
}
}
impl fmt::Display for Leaf {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Leaf::Ident(it) => fmt::Display::fmt(it, f),
Leaf::Literal(it) => fmt::Display::fmt(it, f),
Leaf::Punct(it) => fmt::Display::fmt(it, f),
}
}
}
impl fmt::Display for Ident {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.text, f)
}
}
impl fmt::Display for Literal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.text, f)
}
}
impl fmt::Display for Punct {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(&self.char, f)
}
}
|