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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
use super::ToTokens;
use std::fmt::{self, Display};
use proc_macro;
use proc_macro2::{TokenStream, TokenTree, TokenNode, Term, Span};
use proc_macro2::Delimiter;
#[derive(Clone)]
pub struct Tokens {
tts: Vec<TokenTree>,
}
impl Tokens {
pub fn new() -> Self {
Tokens { tts: Vec::new() }
}
pub fn append<U>(&mut self, token: U)
where U: Into<TokenTree>,
{
self.tts.push(token.into());
}
pub fn append_tokens<T: ToTokens>(&mut self, tokens: T) {
tokens.to_tokens(self)
}
pub fn append_sym(&mut self, sym: &str, span: Span) {
self.append(TokenTree {
span: span,
kind: TokenNode::Term(Term::intern(sym)),
});
}
pub fn append_delimited<F, R>(&mut self,
delim: &str,
span: Span,
f: F) -> R
where F: FnOnce(&mut Tokens) -> R,
{
let delim = match delim {
"(" => Delimiter::Parenthesis,
"[" => Delimiter::Bracket,
"{" => Delimiter::Brace,
" " => Delimiter::None,
_ => panic!("unknown delimiter: {}", delim),
};
let mut child = Tokens::new();
let ret = f(&mut child);
self.append(TokenTree {
span: span,
kind: TokenNode::Group(delim, child.into()),
});
return ret
}
pub fn append_all<T, I>(&mut self, iter: I)
where T: ToTokens,
I: IntoIterator<Item = T>
{
for token in iter {
token.to_tokens(self);
}
}
pub fn append_separated<T, I, U>(&mut self, iter: I, op: U)
where T: ToTokens,
I: IntoIterator<Item = T>,
U: ToTokens,
{
for (i, token) in iter.into_iter().enumerate() {
if i > 0 {
op.to_tokens(self);
}
token.to_tokens(self);
}
}
pub fn append_terminated<T, I, U>(&mut self, iter: I, term: U)
where T: ToTokens,
I: IntoIterator<Item = T>,
U: ToTokens,
{
for token in iter {
token.to_tokens(self);
term.to_tokens(self);
}
}
}
impl From<Tokens> for TokenStream {
fn from(tokens: Tokens) -> TokenStream {
tokens.tts.into_iter().collect()
}
}
impl From<Tokens> for proc_macro::TokenStream {
fn from(tokens: Tokens) -> proc_macro::TokenStream {
TokenStream::from(tokens).into()
}
}
impl Default for Tokens {
fn default() -> Self {
Tokens::new()
}
}
impl ToTokens for Tokens {
fn to_tokens(&self, dst: &mut Tokens) {
dst.tts.extend(self.tts.iter().cloned());
}
fn into_tokens(self) -> Tokens {
self
}
}
impl ToTokens for TokenStream {
fn to_tokens(&self, dst: &mut Tokens) {
dst.append_all(self.clone().into_iter());
}
}
impl ToTokens for TokenTree {
fn to_tokens(&self, dst: &mut Tokens) {
dst.append(self.clone());
}
}
impl Display for Tokens {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
TokenStream::from(self.clone()).fmt(formatter)
}
}