blob: a3ce1fd8f0db0fe8441d3002cceb66e364cfe4e5 [file] [log] [blame]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -07001use std::ascii;
2use std::fmt;
3use std::iter;
4use std::ops;
5use std::str::FromStr;
6
7use proc_macro;
8
Alex Crichton1a7f7622017-07-05 17:47:15 -07009use {TokenTree, TokenNode, Delimiter, Spacing};
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070010
11#[derive(Clone)]
12pub struct TokenStream(proc_macro::TokenStream);
13
14pub struct LexError(proc_macro::LexError);
15
16impl TokenStream {
17 pub fn empty() -> TokenStream {
18 TokenStream(proc_macro::TokenStream::empty())
19 }
20
21 pub fn is_empty(&self) -> bool {
22 self.0.is_empty()
23 }
24}
25
26impl FromStr for TokenStream {
27 type Err = LexError;
28
29 fn from_str(src: &str) -> Result<TokenStream, LexError> {
30 Ok(TokenStream(src.parse().map_err(LexError)?))
31 }
32}
33
34impl fmt::Display for TokenStream {
35 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36 self.0.fmt(f)
37 }
38}
39
40impl From<proc_macro::TokenStream> for TokenStream {
41 fn from(inner: proc_macro::TokenStream) -> TokenStream {
42 TokenStream(inner)
43 }
44}
45
46impl From<TokenStream> for proc_macro::TokenStream {
47 fn from(inner: TokenStream) -> proc_macro::TokenStream {
48 inner.0
49 }
50}
51
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070052impl From<TokenTree> for TokenStream {
53 fn from(tree: TokenTree) -> TokenStream {
54 TokenStream(proc_macro::TokenTree {
55 span: (tree.span.0).0,
56 kind: match tree.kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -070057 TokenNode::Group(delim, s) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070058 let delim = match delim {
59 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
60 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
61 Delimiter::Brace => proc_macro::Delimiter::Brace,
62 Delimiter::None => proc_macro::Delimiter::None,
63 };
Alex Crichton1a7f7622017-07-05 17:47:15 -070064 proc_macro::TokenNode::Group(delim, (s.0).0)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070065 }
Alex Crichton1a7f7622017-07-05 17:47:15 -070066 TokenNode::Op(ch, kind) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070067 let kind = match kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -070068 Spacing::Joint => proc_macro::Spacing::Joint,
69 Spacing::Alone => proc_macro::Spacing::Alone,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070070 };
Alex Crichton1a7f7622017-07-05 17:47:15 -070071 proc_macro::TokenNode::Op(ch, kind)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070072 }
Alex Crichton1a7f7622017-07-05 17:47:15 -070073 TokenNode::Term(s) => {
74 proc_macro::TokenNode::Term((s.0).0)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070075 }
Alex Crichton1a7f7622017-07-05 17:47:15 -070076 TokenNode::Literal(l) => {
77 proc_macro::TokenNode::Literal((l.0).0)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070078 }
79 },
80 }.into())
81 }
82}
83
84impl iter::FromIterator<TokenStream> for TokenStream {
85 fn from_iter<I: IntoIterator<Item=TokenStream>>(streams: I) -> Self {
86 let streams = streams.into_iter().map(|s| s.0);
87 TokenStream(streams.collect::<proc_macro::TokenStream>())
88 }
89}
90
91impl fmt::Debug for TokenStream {
92 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070093 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -070094 }
95}
96
97impl fmt::Debug for LexError {
98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -070099 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700100 }
101}
102
Alex Crichton1a7f7622017-07-05 17:47:15 -0700103pub struct TokenTreeIter(proc_macro::TokenTreeIter);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700104
105impl IntoIterator for TokenStream {
106 type Item = TokenTree;
Alex Crichton1a7f7622017-07-05 17:47:15 -0700107 type IntoIter = TokenTreeIter;
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700108
Alex Crichton1a7f7622017-07-05 17:47:15 -0700109 fn into_iter(self) -> TokenTreeIter {
110 TokenTreeIter(self.0.into_iter())
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700111 }
112}
113
Alex Crichton1a7f7622017-07-05 17:47:15 -0700114impl Iterator for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700115 type Item = TokenTree;
116
117 fn next(&mut self) -> Option<TokenTree> {
118 let token = match self.0.next() {
119 Some(n) => n,
120 None => return None,
121 };
122 Some(TokenTree {
123 span: ::Span(Span(token.span)),
124 kind: match token.kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700125 proc_macro::TokenNode::Group(delim, s) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700126 let delim = match delim {
127 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
128 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
129 proc_macro::Delimiter::Brace => Delimiter::Brace,
130 proc_macro::Delimiter::None => Delimiter::None,
131 };
Alex Crichton1a7f7622017-07-05 17:47:15 -0700132 TokenNode::Group(delim, ::TokenStream(TokenStream(s)))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700133 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700134 proc_macro::TokenNode::Op(ch, kind) => {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700135 let kind = match kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700136 proc_macro::Spacing::Joint => Spacing::Joint,
137 proc_macro::Spacing::Alone => Spacing::Alone,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700138 };
Alex Crichton1a7f7622017-07-05 17:47:15 -0700139 TokenNode::Op(ch, kind)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700140 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700141 proc_macro::TokenNode::Term(s) => {
142 TokenNode::Term(::Term(Term(s)))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700143 }
Alex Crichton1a7f7622017-07-05 17:47:15 -0700144 proc_macro::TokenNode::Literal(l) => {
145 TokenNode::Literal(::Literal(Literal(l)))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700146 }
147 },
148 })
149 }
150
151 fn size_hint(&self) -> (usize, Option<usize>) {
152 self.0.size_hint()
153 }
154}
155
Alex Crichton1a7f7622017-07-05 17:47:15 -0700156impl fmt::Debug for TokenTreeIter {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700157 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700158 f.debug_struct("TokenTreeIter").finish()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700159 }
160}
161
Alex Crichton998f6422017-11-19 08:06:27 -0800162#[derive(Copy, Clone)]
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700163pub struct Span(proc_macro::Span);
164
165impl Span {
166 pub fn call_site() -> Span {
167 Span(proc_macro::Span::call_site())
168 }
169}
170
Alex Crichton998f6422017-11-19 08:06:27 -0800171impl Default for Span {
172 fn default() -> Span {
173 Span(proc_macro::Span::def_site())
174 }
175}
176
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700177impl fmt::Debug for Span {
178 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700179 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700180 }
181}
182
183#[derive(Copy, Clone)]
Alex Crichton1a7f7622017-07-05 17:47:15 -0700184pub struct Term(proc_macro::Term);
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700185
Alex Crichton1a7f7622017-07-05 17:47:15 -0700186impl<'a> From<&'a str> for Term {
187 fn from(string: &'a str) -> Term {
188 Term(proc_macro::Term::intern(string))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700189 }
190}
191
Alex Crichton1a7f7622017-07-05 17:47:15 -0700192impl ops::Deref for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700193 type Target = str;
194
195 fn deref(&self) -> &str {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700196 self.0.as_str()
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700197 }
198}
199
Alex Crichton1a7f7622017-07-05 17:47:15 -0700200impl fmt::Debug for Term {
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700201 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700202 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700203 }
204}
205
206#[derive(Clone)]
207pub struct Literal(proc_macro::Literal);
208
209impl Literal {
210 pub fn byte_char(byte: u8) -> Literal {
211 match byte {
212 0 => Literal(to_literal("b'\\0'")),
213 b'\"' => Literal(to_literal("b'\"'")),
214 n => {
215 let mut escaped = "b'".to_string();
216 escaped.extend(ascii::escape_default(n).map(|c| c as char));
217 escaped.push('\'');
218 Literal(to_literal(&escaped))
219 }
220 }
221 }
222
223 pub fn byte_string(bytes: &[u8]) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700224 Literal(proc_macro::Literal::byte_string(bytes))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700225 }
226
227 pub fn doccomment(s: &str) -> Literal {
228 Literal(to_literal(s))
229 }
230
Alex Crichton1a7f7622017-07-05 17:47:15 -0700231 pub fn float(s: f64) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700232 Literal(proc_macro::Literal::float(s))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700233 }
234
Alex Crichton1a7f7622017-07-05 17:47:15 -0700235 pub fn integer(s: i64) -> Literal {
Alex Crichton040be632017-07-05 17:56:48 -0700236 Literal(proc_macro::Literal::integer(s.into()))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700237 }
238
239 pub fn raw_string(s: &str, pounds: usize) -> Literal {
240 let mut ret = format!("r");
241 ret.extend((0..pounds).map(|_| "#"));
242 ret.push('"');
243 ret.push_str(s);
244 ret.push('"');
245 ret.extend((0..pounds).map(|_| "#"));
246 Literal(to_literal(&ret))
247 }
248
249 pub fn raw_byte_string(s: &str, pounds: usize) -> Literal {
250 let mut ret = format!("br");
251 ret.extend((0..pounds).map(|_| "#"));
252 ret.push('"');
253 ret.push_str(s);
254 ret.push('"');
255 ret.extend((0..pounds).map(|_| "#"));
256 Literal(to_literal(&ret))
257 }
258}
259
260impl fmt::Display for Literal {
261 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
262 self.0.fmt(f)
263 }
264}
265
266impl fmt::Debug for Literal {
267 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
Alex Crichton040be632017-07-05 17:56:48 -0700268 self.0.fmt(f)
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700269 }
270}
271
272fn to_literal(s: &str) -> proc_macro::Literal {
273 let stream = s.parse::<proc_macro::TokenStream>().unwrap();
274 match stream.into_iter().next().unwrap().kind {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700275 proc_macro::TokenNode::Literal(l) => l,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700276 _ => unreachable!(),
277 }
278}
279
280macro_rules! ints {
Alex Crichton040be632017-07-05 17:56:48 -0700281 ($($t:ident,)*) => {$(
282 impl From<$t> for Literal {
283 fn from(t: $t) -> Literal {
284 Literal(proc_macro::Literal::$t(t))
285 }
286 }
287 )*}
288}
289
290ints! {
Alex Crichton6e81d762017-07-14 06:09:31 -0700291 u8, u16, u32, u64, usize,
292 i8, i16, i32, i64, isize,
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700293}
294
295macro_rules! floats {
296 ($($t:ident,)*) => {$(
297 impl From<$t> for Literal {
298 fn from(t: $t) -> Literal {
Alex Crichton1a7f7622017-07-05 17:47:15 -0700299 Literal(proc_macro::Literal::$t(t))
Alex Crichtoncbec8ec2017-06-02 13:19:33 -0700300 }
301 }
302 )*}
303}
304
305floats! {
306 f32, f64,
307}
308
309impl<'a> From<&'a str> for Literal {
310 fn from(t: &'a str) -> Literal {
311 Literal(proc_macro::Literal::string(t))
312 }
313}
314
315impl From<char> for Literal {
316 fn from(t: char) -> Literal {
317 Literal(proc_macro::Literal::character(t))
318 }
319}