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
#![cfg_attr(feature = "unstable", feature(proc_macro))]
extern crate proc_macro;
#[cfg(not(feature = "unstable"))]
extern crate unicode_xid;
use std::fmt;
use std::str::FromStr;
use std::iter::FromIterator;
#[macro_use]
#[cfg(not(feature = "unstable"))]
mod strnom;
#[path = "stable.rs"]
#[cfg(not(feature = "unstable"))]
mod imp;
#[path = "unstable.rs"]
#[cfg(feature = "unstable")]
mod imp;
#[macro_use]
mod macros;
#[derive(Clone)]
pub struct TokenStream(imp::TokenStream);
pub struct LexError(imp::LexError);
impl FromStr for TokenStream {
type Err = LexError;
fn from_str(src: &str) -> Result<TokenStream, LexError> {
match src.parse() {
Ok(e) => Ok(TokenStream(e)),
Err(e) => Err(LexError(e)),
}
}
}
impl From<proc_macro::TokenStream> for TokenStream {
fn from(inner: proc_macro::TokenStream) -> TokenStream {
TokenStream(inner.into())
}
}
impl From<TokenStream> for proc_macro::TokenStream {
fn from(inner: TokenStream) -> proc_macro::TokenStream {
inner.0.into()
}
}
impl From<TokenTree> for TokenStream {
fn from(tree: TokenTree) -> TokenStream {
TokenStream(tree.into())
}
}
impl<T: Into<TokenStream>> FromIterator<T> for TokenStream {
fn from_iter<I: IntoIterator<Item = T>>(streams: I) -> Self {
TokenStream(streams.into_iter().map(|t| t.into().0).collect())
}
}
impl IntoIterator for TokenStream {
type Item = TokenTree;
type IntoIter = TokenTreeIter;
fn into_iter(self) -> TokenTreeIter {
TokenTreeIter(self.0.into_iter())
}
}
impl TokenStream {
pub fn empty() -> TokenStream {
TokenStream(imp::TokenStream::empty())
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
#[derive(Copy, Clone)]
pub struct Span(imp::Span);
impl Default for Span {
fn default() -> Span {
Span(imp::Span::default())
}
}
impl Span {
pub fn call_site() -> Span {
Span(imp::Span::call_site())
}
}
#[derive(Clone, Debug)]
pub struct TokenTree {
pub span: Span,
pub kind: TokenNode,
}
impl From<TokenNode> for TokenTree {
fn from(kind: TokenNode) -> TokenTree {
TokenTree { span: Span::default(), kind: kind }
}
}
impl fmt::Display for TokenTree {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
TokenStream::from(self.clone()).fmt(f)
}
}
#[derive(Clone, Debug)]
pub enum TokenNode {
Group(Delimiter, TokenStream),
Term(Term),
Op(char, Spacing),
Literal(Literal),
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Delimiter {
Parenthesis,
Brace,
Bracket,
None,
}
#[derive(Copy, Clone)]
pub struct Term(imp::Term);
impl Term {
pub fn intern(string: &str) -> Term {
Term(string.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Spacing {
Alone,
Joint,
}
#[derive(Clone)]
pub struct Literal(imp::Literal);
macro_rules! int_literals {
($($kind:ident,)*) => ($(
pub fn $kind(n: $kind) -> Literal {
Literal(n.into())
}
)*)
}
impl Literal {
pub fn integer(s: i64) -> Literal {
Literal(imp::Literal::integer(s))
}
int_literals! {
u8, u16, u32, u64, usize,
i8, i16, i32, i64, isize,
}
pub fn float(f: f64) -> Literal {
Literal(imp::Literal::float(f))
}
pub fn f64(f: f64) -> Literal {
Literal(f.into())
}
pub fn f32(f: f32) -> Literal {
Literal(f.into())
}
pub fn string(string: &str) -> Literal {
Literal(string.into())
}
pub fn character(ch: char) -> Literal {
Literal(ch.into())
}
pub fn byte_string(s: &[u8]) -> Literal {
Literal(imp::Literal::byte_string(s))
}
pub fn byte_char(b: u8) -> Literal {
Literal(imp::Literal::byte_char(b))
}
pub fn doccomment(s: &str) -> Literal {
Literal(imp::Literal::doccomment(s))
}
pub fn raw_string(s: &str, pounds: usize) -> Literal {
Literal(imp::Literal::raw_string(s, pounds))
}
pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
Literal(imp::Literal::raw_byte_string(s, pounds))
}
}
pub struct TokenTreeIter(imp::TokenTreeIter);
impl Iterator for TokenTreeIter {
type Item = TokenTree;
fn next(&mut self) -> Option<TokenTree> {
self.0.next()
}
}
forward_fmt!(Debug for LexError);
forward_fmt!(Debug for Literal);
forward_fmt!(Debug for Span);
forward_fmt!(Debug for Term);
forward_fmt!(Debug for TokenTreeIter);
forward_fmt!(Debug for TokenStream);
forward_fmt!(Display for Literal);
forward_fmt!(Display for TokenStream);