-
-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathunstable.rs
406 lines (342 loc) · 10.4 KB
/
unstable.rs
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
use std::ascii;
use std::fmt;
use std::iter;
use std::str::FromStr;
use proc_macro;
use {TokenTree, TokenNode, Delimiter, Spacing};
#[derive(Clone)]
pub struct TokenStream(proc_macro::TokenStream);
pub struct LexError(proc_macro::LexError);
impl TokenStream {
pub fn empty() -> TokenStream {
TokenStream(proc_macro::TokenStream::empty())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl FromStr for TokenStream {
type Err = LexError;
fn from_str(src: &str) -> Result<TokenStream, LexError> {
Ok(TokenStream(src.parse().map_err(LexError)?))
}
}
impl fmt::Display for TokenStream {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl From<proc_macro::TokenStream> for TokenStream {
fn from(inner: proc_macro::TokenStream) -> TokenStream {
TokenStream(inner)
}
}
impl From<TokenStream> for proc_macro::TokenStream {
fn from(inner: TokenStream) -> proc_macro::TokenStream {
inner.0
}
}
impl From<TokenTree> for TokenStream {
fn from(tree: TokenTree) -> TokenStream {
TokenStream(proc_macro::TokenTree {
span: (tree.span.0).0,
kind: match tree.kind {
TokenNode::Group(delim, s) => {
let delim = match delim {
Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
Delimiter::Bracket => proc_macro::Delimiter::Bracket,
Delimiter::Brace => proc_macro::Delimiter::Brace,
Delimiter::None => proc_macro::Delimiter::None,
};
proc_macro::TokenNode::Group(delim, (s.0).0)
}
TokenNode::Op(ch, kind) => {
let kind = match kind {
Spacing::Joint => proc_macro::Spacing::Joint,
Spacing::Alone => proc_macro::Spacing::Alone,
};
proc_macro::TokenNode::Op(ch, kind)
}
TokenNode::Term(s) => {
proc_macro::TokenNode::Term((s.0).0)
}
TokenNode::Literal(l) => {
proc_macro::TokenNode::Literal((l.0).0)
}
},
}.into())
}
}
impl iter::FromIterator<TokenStream> for TokenStream {
fn from_iter<I: IntoIterator<Item=TokenStream>>(streams: I) -> Self {
let streams = streams.into_iter().map(|s| s.0);
TokenStream(streams.collect::<proc_macro::TokenStream>())
}
}
impl fmt::Debug for TokenStream {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for LexError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
pub struct TokenTreeIter(proc_macro::TokenTreeIter);
impl IntoIterator for TokenStream {
type Item = TokenTree;
type IntoIter = TokenTreeIter;
fn into_iter(self) -> TokenTreeIter {
TokenTreeIter(self.0.into_iter())
}
}
impl Iterator for TokenTreeIter {
type Item = TokenTree;
fn next(&mut self) -> Option<TokenTree> {
let token = match self.0.next() {
Some(n) => n,
None => return None,
};
Some(TokenTree {
span: ::Span(Span(token.span)),
kind: match token.kind {
proc_macro::TokenNode::Group(delim, s) => {
let delim = match delim {
proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
proc_macro::Delimiter::Bracket => Delimiter::Bracket,
proc_macro::Delimiter::Brace => Delimiter::Brace,
proc_macro::Delimiter::None => Delimiter::None,
};
TokenNode::Group(delim, ::TokenStream(TokenStream(s)))
}
proc_macro::TokenNode::Op(ch, kind) => {
let kind = match kind {
proc_macro::Spacing::Joint => Spacing::Joint,
proc_macro::Spacing::Alone => Spacing::Alone,
};
TokenNode::Op(ch, kind)
}
proc_macro::TokenNode::Term(s) => {
TokenNode::Term(::Term(Term(s)))
}
proc_macro::TokenNode::Literal(l) => {
TokenNode::Literal(::Literal(Literal(l)))
}
},
})
}
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
}
impl fmt::Debug for TokenTreeIter {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("TokenTreeIter").finish()
}
}
#[cfg(procmacro2_semver_exempt)]
#[derive(Clone, PartialEq, Eq)]
pub struct FileName(String);
#[cfg(procmacro2_semver_exempt)]
impl fmt::Display for FileName {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
// NOTE: We have to generate our own filename object here because we can't wrap
// the one provided by proc_macro.
#[cfg(procmacro2_semver_exempt)]
#[derive(Clone, PartialEq, Eq)]
pub struct SourceFile(proc_macro::SourceFile, FileName);
#[cfg(procmacro2_semver_exempt)]
impl SourceFile {
fn new(sf: proc_macro::SourceFile) -> Self {
let filename = FileName(sf.path().to_string());
SourceFile(sf, filename)
}
/// Get the path to this source file as a string.
pub fn path(&self) -> &FileName {
&self.1
}
pub fn is_real(&self) -> bool {
self.0.is_real()
}
}
#[cfg(procmacro2_semver_exempt)]
impl AsRef<FileName> for SourceFile {
fn as_ref(&self) -> &FileName {
self.path()
}
}
#[cfg(procmacro2_semver_exempt)]
impl fmt::Debug for SourceFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[cfg(procmacro2_semver_exempt)]
pub struct LineColumn {
pub line: usize,
pub column: usize,
}
#[derive(Copy, Clone)]
pub struct Span(proc_macro::Span);
impl From<proc_macro::Span> for ::Span {
fn from(proc_span: proc_macro::Span) -> ::Span {
::Span(Span(proc_span))
}
}
impl Span {
pub fn call_site() -> Span {
Span(proc_macro::Span::call_site())
}
pub fn def_site() -> Span {
Span(proc_macro::Span::def_site())
}
pub fn resolved_at(&self, other: Span) -> Span {
Span(self.0.resolved_at(other.0))
}
pub fn located_at(&self, other: Span) -> Span {
Span(self.0.located_at(other.0))
}
pub fn unstable(self) -> proc_macro::Span {
self.0
}
#[cfg(procmacro2_semver_exempt)]
pub fn source_file(&self) -> SourceFile {
SourceFile::new(self.0.source_file())
}
#[cfg(procmacro2_semver_exempt)]
pub fn start(&self) -> LineColumn {
let proc_macro::LineColumn{ line, column } = self.0.start();
LineColumn { line, column }
}
#[cfg(procmacro2_semver_exempt)]
pub fn end(&self) -> LineColumn {
let proc_macro::LineColumn{ line, column } = self.0.end();
LineColumn { line, column }
}
#[cfg(procmacro2_semver_exempt)]
pub fn join(&self, other: Span) -> Option<Span> {
self.0.join(other.0).map(Span)
}
}
impl fmt::Debug for Span {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Copy, Clone)]
pub struct Term(proc_macro::Term);
impl Term {
pub fn intern(string: &str) -> Term {
Term(proc_macro::Term::intern(string))
}
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl fmt::Debug for Term {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
#[derive(Clone)]
pub struct Literal(proc_macro::Literal);
impl Literal {
pub fn byte_char(byte: u8) -> Literal {
match byte {
0 => Literal(to_literal("b'\\0'")),
b'\"' => Literal(to_literal("b'\"'")),
n => {
let mut escaped = "b'".to_string();
escaped.extend(ascii::escape_default(n).map(|c| c as char));
escaped.push('\'');
Literal(to_literal(&escaped))
}
}
}
pub fn byte_string(bytes: &[u8]) -> Literal {
Literal(proc_macro::Literal::byte_string(bytes))
}
pub fn doccomment(s: &str) -> Literal {
Literal(to_literal(s))
}
pub fn float(s: f64) -> Literal {
Literal(proc_macro::Literal::float(s))
}
pub fn integer(s: i64) -> Literal {
Literal(proc_macro::Literal::integer(s.into()))
}
pub fn raw_string(s: &str, pounds: usize) -> Literal {
let mut ret = format!("r");
ret.extend((0..pounds).map(|_| "#"));
ret.push('"');
ret.push_str(s);
ret.push('"');
ret.extend((0..pounds).map(|_| "#"));
Literal(to_literal(&ret))
}
pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
let mut ret = format!("br");
ret.extend((0..pounds).map(|_| "#"));
ret.push('"');
ret.push_str(s);
ret.push('"');
ret.extend((0..pounds).map(|_| "#"));
Literal(to_literal(&ret))
}
}
impl fmt::Display for Literal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
impl fmt::Debug for Literal {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.0.fmt(f)
}
}
fn to_literal(s: &str) -> proc_macro::Literal {
let stream = s.parse::<proc_macro::TokenStream>().unwrap();
match stream.into_iter().next().unwrap().kind {
proc_macro::TokenNode::Literal(l) => l,
_ => unreachable!(),
}
}
macro_rules! ints {
($($t:ident,)*) => {$(
impl From<$t> for Literal {
fn from(t: $t) -> Literal {
Literal(proc_macro::Literal::$t(t))
}
}
)*}
}
ints! {
u8, u16, u32, u64, usize,
i8, i16, i32, i64, isize,
}
macro_rules! floats {
($($t:ident,)*) => {$(
impl From<$t> for Literal {
fn from(t: $t) -> Literal {
Literal(proc_macro::Literal::$t(t))
}
}
)*}
}
floats! {
f32, f64,
}
impl<'a> From<&'a str> for Literal {
fn from(t: &'a str) -> Literal {
Literal(proc_macro::Literal::string(t))
}
}
impl From<char> for Literal {
fn from(t: char) -> Literal {
Literal(proc_macro::Literal::character(t))
}
}