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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#[cfg(all(
not(all(target_arch = "wasm32", target_os = "unknown")),
feature = "proc-macro"
))]
use proc_macro as pm;
use proc_macro2::{Delimiter, Group, Ident, Literal, Punct, Spacing, Span, TokenStream, TokenTree};
use std::marker::PhantomData;
use std::ptr;
use private;
use Lifetime;
enum Entry {
Group(Group, TokenBuffer),
Ident(Ident),
Punct(Punct),
Literal(Literal),
End(*const Entry),
}
pub struct TokenBuffer {
data: Box<[Entry]>,
}
impl TokenBuffer {
fn inner_new(stream: TokenStream, up: *const Entry) -> TokenBuffer {
let mut entries = Vec::new();
let mut seqs = Vec::new();
for tt in stream {
match tt {
TokenTree::Ident(sym) => {
entries.push(Entry::Ident(sym));
}
TokenTree::Punct(op) => {
entries.push(Entry::Punct(op));
}
TokenTree::Literal(l) => {
entries.push(Entry::Literal(l));
}
TokenTree::Group(g) => {
seqs.push((entries.len(), g));
entries.push(Entry::End(ptr::null()));
}
}
}
entries.push(Entry::End(up));
let mut entries = entries.into_boxed_slice();
for (idx, group) in seqs {
let seq_up = &entries[idx + 1] as *const Entry;
let inner = Self::inner_new(group.stream(), seq_up);
entries[idx] = Entry::Group(group, inner);
}
TokenBuffer { data: entries }
}
#[cfg(all(
not(all(target_arch = "wasm32", target_os = "unknown")),
feature = "proc-macro"
))]
pub fn new(stream: pm::TokenStream) -> TokenBuffer {
Self::new2(stream.into())
}
pub fn new2(stream: TokenStream) -> TokenBuffer {
Self::inner_new(stream, ptr::null())
}
pub fn begin(&self) -> Cursor {
unsafe { Cursor::create(&self.data[0], &self.data[self.data.len() - 1]) }
}
}
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct Cursor<'a> {
ptr: *const Entry,
scope: *const Entry,
marker: PhantomData<&'a Entry>,
}
impl<'a> Cursor<'a> {
pub fn empty() -> Self {
struct UnsafeSyncEntry(Entry);
unsafe impl Sync for UnsafeSyncEntry {}
static EMPTY_ENTRY: UnsafeSyncEntry = UnsafeSyncEntry(Entry::End(0 as *const Entry));
Cursor {
ptr: &EMPTY_ENTRY.0,
scope: &EMPTY_ENTRY.0,
marker: PhantomData,
}
}
unsafe fn create(mut ptr: *const Entry, scope: *const Entry) -> Self {
while let Entry::End(exit) = *ptr {
if ptr == scope {
break;
}
ptr = exit;
}
Cursor {
ptr: ptr,
scope: scope,
marker: PhantomData,
}
}
fn entry(self) -> &'a Entry {
unsafe { &*self.ptr }
}
unsafe fn bump(self) -> Cursor<'a> {
Cursor::create(self.ptr.offset(1), self.scope)
}
fn ignore_none(&mut self) {
if let Entry::Group(ref group, ref buf) = *self.entry() {
if group.delimiter() == Delimiter::None {
unsafe {
*self = Cursor::create(&buf.data[0], self.scope);
}
}
}
}
#[inline]
pub fn eof(self) -> bool {
self.ptr == self.scope
}
pub fn group(mut self, delim: Delimiter) -> Option<(Cursor<'a>, Span, Cursor<'a>)> {
if delim != Delimiter::None {
self.ignore_none();
}
if let Entry::Group(ref group, ref buf) = *self.entry() {
if group.delimiter() == delim {
return Some((buf.begin(), group.span(), unsafe { self.bump() }));
}
}
None
}
pub fn ident(mut self) -> Option<(Ident, Cursor<'a>)> {
self.ignore_none();
match *self.entry() {
Entry::Ident(ref ident) => Some((ident.clone(), unsafe { self.bump() })),
_ => None,
}
}
pub fn punct(mut self) -> Option<(Punct, Cursor<'a>)> {
self.ignore_none();
match *self.entry() {
Entry::Punct(ref op) if op.as_char() != '\'' => {
Some((op.clone(), unsafe { self.bump() }))
}
_ => None,
}
}
pub fn literal(mut self) -> Option<(Literal, Cursor<'a>)> {
self.ignore_none();
match *self.entry() {
Entry::Literal(ref lit) => Some((lit.clone(), unsafe { self.bump() })),
_ => None,
}
}
pub fn lifetime(mut self) -> Option<(Lifetime, Cursor<'a>)> {
self.ignore_none();
match *self.entry() {
Entry::Punct(ref op) if op.as_char() == '\'' && op.spacing() == Spacing::Joint => {
let next = unsafe { self.bump() };
match next.ident() {
Some((ident, rest)) => {
let lifetime = Lifetime {
apostrophe: op.span(),
ident: ident,
};
Some((lifetime, rest))
}
None => None,
}
}
_ => None,
}
}
pub fn token_stream(self) -> TokenStream {
let mut tts = Vec::new();
let mut cursor = self;
while let Some((tt, rest)) = cursor.token_tree() {
tts.push(tt);
cursor = rest;
}
tts.into_iter().collect()
}
pub fn token_tree(self) -> Option<(TokenTree, Cursor<'a>)> {
let tree = match *self.entry() {
Entry::Group(ref group, _) => group.clone().into(),
Entry::Literal(ref lit) => lit.clone().into(),
Entry::Ident(ref ident) => ident.clone().into(),
Entry::Punct(ref op) => op.clone().into(),
Entry::End(..) => {
return None;
}
};
Some((tree, unsafe { self.bump() }))
}
pub fn span(self) -> Span {
match *self.entry() {
Entry::Group(ref group, _) => group.span(),
Entry::Literal(ref l) => l.span(),
Entry::Ident(ref t) => t.span(),
Entry::Punct(ref o) => o.span(),
Entry::End(..) => Span::call_site(),
}
}
}
impl private {
pub fn same_scope(a: Cursor, b: Cursor) -> bool {
a.scope == b.scope
}
#[cfg(procmacro2_semver_exempt)]
pub fn open_span_of_group(cursor: Cursor) -> Span {
match *cursor.entry() {
Entry::Group(ref group, _) => group.span_open(),
_ => cursor.span(),
}
}
#[cfg(procmacro2_semver_exempt)]
pub fn close_span_of_group(cursor: Cursor) -> Span {
match *cursor.entry() {
Entry::Group(ref group, _) => group.span_close(),
_ => cursor.span(),
}
}
}