Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 1 | #![allow(dead_code)] |
| 2 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 3 | use std::ascii; |
| 4 | use std::fmt; |
| 5 | use std::iter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 6 | use std::str::FromStr; |
| 7 | |
| 8 | use proc_macro; |
| 9 | |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 10 | use {Delimiter, Group, Op, Spacing, TokenTree}; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 11 | |
| 12 | #[derive(Clone)] |
| 13 | pub struct TokenStream(proc_macro::TokenStream); |
| 14 | |
| 15 | pub struct LexError(proc_macro::LexError); |
| 16 | |
| 17 | impl TokenStream { |
| 18 | pub fn empty() -> TokenStream { |
| 19 | TokenStream(proc_macro::TokenStream::empty()) |
| 20 | } |
| 21 | |
| 22 | pub fn is_empty(&self) -> bool { |
| 23 | self.0.is_empty() |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | impl FromStr for TokenStream { |
| 28 | type Err = LexError; |
| 29 | |
| 30 | fn from_str(src: &str) -> Result<TokenStream, LexError> { |
| 31 | Ok(TokenStream(src.parse().map_err(LexError)?)) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl fmt::Display for TokenStream { |
| 36 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 37 | self.0.fmt(f) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl From<proc_macro::TokenStream> for TokenStream { |
| 42 | fn from(inner: proc_macro::TokenStream) -> TokenStream { |
| 43 | TokenStream(inner) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | impl From<TokenStream> for proc_macro::TokenStream { |
| 48 | fn from(inner: TokenStream) -> proc_macro::TokenStream { |
| 49 | inner.0 |
| 50 | } |
| 51 | } |
| 52 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 53 | impl From<TokenTree> for TokenStream { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 54 | fn from(token: TokenTree) -> TokenStream { |
| 55 | let (span, kind) = match token { |
| 56 | TokenTree::Group(tt) => { |
| 57 | let delim = match tt.delimiter() { |
| 58 | Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis, |
| 59 | Delimiter::Bracket => proc_macro::Delimiter::Bracket, |
| 60 | Delimiter::Brace => proc_macro::Delimiter::Brace, |
| 61 | Delimiter::None => proc_macro::Delimiter::None, |
| 62 | }; |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 63 | let span = tt.span().inner; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 64 | let group = proc_macro::TokenNode::Group(delim, tt.stream.inner.0); |
| 65 | (span, group) |
| 66 | } |
| 67 | TokenTree::Op(tt) => { |
| 68 | let kind = match tt.spacing() { |
| 69 | Spacing::Joint => proc_macro::Spacing::Joint, |
| 70 | Spacing::Alone => proc_macro::Spacing::Alone, |
| 71 | }; |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 72 | (tt.span().inner, proc_macro::TokenNode::Op(tt.op(), kind)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 73 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 74 | TokenTree::Term(tt) => (tt.inner.span, proc_macro::TokenNode::Term(tt.inner.term)), |
| 75 | TokenTree::Literal(tt) => (tt.inner.span, proc_macro::TokenNode::Literal(tt.inner.lit)), |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 76 | }; |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 77 | TokenStream( |
| 78 | proc_macro::TokenTree { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 79 | span: span.0, |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 80 | kind, |
| 81 | }.into(), |
| 82 | ) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 86 | impl iter::FromIterator<TokenTree> for TokenStream { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 87 | fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 88 | let streams = streams.into_iter().map(TokenStream::from); |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 89 | TokenStream(streams.collect::<proc_macro::TokenStream>()) |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl fmt::Debug for TokenStream { |
| 94 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 040be63 | 2017-07-05 17:56:48 -0700 | [diff] [blame] | 95 | self.0.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | impl fmt::Debug for LexError { |
| 100 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 040be63 | 2017-07-05 17:56:48 -0700 | [diff] [blame] | 101 | self.0.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 105 | pub struct TokenTreeIter(proc_macro::TokenTreeIter); |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 106 | |
| 107 | impl IntoIterator for TokenStream { |
| 108 | type Item = TokenTree; |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 109 | type IntoIter = TokenTreeIter; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 110 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 111 | fn into_iter(self) -> TokenTreeIter { |
| 112 | TokenTreeIter(self.0.into_iter()) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 116 | impl Iterator for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 117 | type Item = TokenTree; |
| 118 | |
| 119 | fn next(&mut self) -> Option<TokenTree> { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 120 | let token = self.0.next()?; |
| 121 | let span = ::Span::_new(Span(token.span)); |
| 122 | Some(match token.kind { |
| 123 | proc_macro::TokenNode::Group(delim, s) => { |
| 124 | let delim = match delim { |
| 125 | proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, |
| 126 | proc_macro::Delimiter::Bracket => Delimiter::Bracket, |
| 127 | proc_macro::Delimiter::Brace => Delimiter::Brace, |
| 128 | proc_macro::Delimiter::None => Delimiter::None, |
| 129 | }; |
| 130 | let stream = ::TokenStream::_new(TokenStream(s)); |
| 131 | let mut g = Group::new(delim, stream); |
| 132 | g.set_span(span); |
| 133 | g.into() |
| 134 | } |
| 135 | proc_macro::TokenNode::Op(ch, kind) => { |
| 136 | let kind = match kind { |
| 137 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 138 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 139 | }; |
| 140 | let mut o = Op::new(ch, kind); |
| 141 | o.span = span; |
| 142 | o.into() |
| 143 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 144 | proc_macro::TokenNode::Term(s) => { |
| 145 | ::Term::_new(Term { |
| 146 | term: s, |
| 147 | span: span.inner, |
| 148 | }).into() |
| 149 | } |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 150 | proc_macro::TokenNode::Literal(l) => { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 151 | ::Literal::_new(Literal { |
| 152 | lit: l, |
| 153 | span: span.inner, |
| 154 | }).into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 155 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 156 | }) |
| 157 | } |
| 158 | |
| 159 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 160 | self.0.size_hint() |
| 161 | } |
| 162 | } |
| 163 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 164 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 165 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 166 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 170 | #[derive(Clone, PartialEq, Eq)] |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 171 | pub struct FileName(String); |
| 172 | |
| 173 | impl fmt::Display for FileName { |
| 174 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 175 | self.0.fmt(f) |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | // NOTE: We have to generate our own filename object here because we can't wrap |
| 180 | // the one provided by proc_macro. |
| 181 | #[derive(Clone, PartialEq, Eq)] |
| 182 | pub struct SourceFile(proc_macro::SourceFile, FileName); |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 183 | |
| 184 | impl SourceFile { |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 185 | fn new(sf: proc_macro::SourceFile) -> Self { |
| 186 | let filename = FileName(sf.path().to_string()); |
| 187 | SourceFile(sf, filename) |
| 188 | } |
| 189 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 190 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 191 | pub fn path(&self) -> &FileName { |
| 192 | &self.1 |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | pub fn is_real(&self) -> bool { |
| 196 | self.0.is_real() |
| 197 | } |
| 198 | } |
| 199 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 200 | impl AsRef<FileName> for SourceFile { |
| 201 | fn as_ref(&self) -> &FileName { |
| 202 | self.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | impl fmt::Debug for SourceFile { |
| 207 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 208 | self.0.fmt(f) |
| 209 | } |
| 210 | } |
| 211 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 212 | pub struct LineColumn { |
| 213 | pub line: usize, |
| 214 | pub column: usize, |
| 215 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 216 | |
Nika Layzell | 9973798 | 2018-03-11 18:51:27 -0400 | [diff] [blame] | 217 | #[derive(Copy, Clone, PartialEq, Eq)] |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 218 | pub struct Span(proc_macro::Span); |
| 219 | |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 220 | impl From<proc_macro::Span> for ::Span { |
| 221 | fn from(proc_span: proc_macro::Span) -> ::Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 222 | ::Span::_new(Span(proc_span)) |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 226 | impl Span { |
| 227 | pub fn call_site() -> Span { |
| 228 | Span(proc_macro::Span::call_site()) |
| 229 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 230 | |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 231 | pub fn def_site() -> Span { |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 232 | Span(proc_macro::Span::def_site()) |
| 233 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 234 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 235 | pub fn resolved_at(&self, other: Span) -> Span { |
| 236 | Span(self.0.resolved_at(other.0)) |
| 237 | } |
| 238 | |
| 239 | pub fn located_at(&self, other: Span) -> Span { |
| 240 | Span(self.0.located_at(other.0)) |
| 241 | } |
| 242 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 243 | pub fn unstable(self) -> proc_macro::Span { |
| 244 | self.0 |
| 245 | } |
| 246 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 247 | pub fn source_file(&self) -> SourceFile { |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 248 | SourceFile::new(self.0.source_file()) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | pub fn start(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 252 | let proc_macro::LineColumn { line, column } = self.0.start(); |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 253 | LineColumn { line, column } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | pub fn end(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 257 | let proc_macro::LineColumn { line, column } = self.0.end(); |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 258 | LineColumn { line, column } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | pub fn join(&self, other: Span) -> Option<Span> { |
| 262 | self.0.join(other.0).map(Span) |
| 263 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 266 | impl fmt::Debug for Span { |
| 267 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 040be63 | 2017-07-05 17:56:48 -0700 | [diff] [blame] | 268 | self.0.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | #[derive(Copy, Clone)] |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 273 | pub struct Term { |
| 274 | term: proc_macro::Term, |
| 275 | span: Span, |
| 276 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 277 | |
David Tolnay | 10effeb | 2018-01-06 11:07:49 -0800 | [diff] [blame] | 278 | impl Term { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 279 | pub fn new(string: &str, span: Span) -> Term { |
| 280 | Term { |
| 281 | term: proc_macro::Term::intern(string), |
| 282 | span, |
| 283 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 284 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 285 | |
David Tolnay | 10effeb | 2018-01-06 11:07:49 -0800 | [diff] [blame] | 286 | pub fn as_str(&self) -> &str { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 287 | self.term.as_str() |
| 288 | } |
| 289 | |
| 290 | pub fn span(&self) -> Span { |
| 291 | self.span |
| 292 | } |
| 293 | |
| 294 | pub fn set_span(&mut self, span: Span) { |
| 295 | self.span = span; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 299 | impl fmt::Debug for Term { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 300 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 301 | self.term.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 302 | } |
| 303 | } |
| 304 | |
| 305 | #[derive(Clone)] |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 306 | pub struct Literal { |
| 307 | lit: proc_macro::Literal, |
| 308 | span: Span, |
| 309 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 310 | |
| 311 | impl Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 312 | fn _new(lit: proc_macro::Literal) -> Literal { |
| 313 | Literal { |
| 314 | lit, |
| 315 | span: Span::call_site(), |
| 316 | } |
| 317 | } |
| 318 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 319 | pub fn byte_char(byte: u8) -> Literal { |
| 320 | match byte { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 321 | 0 => Literal::_new(to_literal("b'\\0'")), |
| 322 | b'\"' => Literal::_new(to_literal("b'\"'")), |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 323 | n => { |
| 324 | let mut escaped = "b'".to_string(); |
| 325 | escaped.extend(ascii::escape_default(n).map(|c| c as char)); |
| 326 | escaped.push('\''); |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 327 | Literal::_new(to_literal(&escaped)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 333 | Literal::_new(proc_macro::Literal::byte_string(bytes)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 334 | } |
| 335 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 336 | pub fn float(s: f64) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 337 | Literal::_new(proc_macro::Literal::float(s)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 340 | pub fn integer(s: i64) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 341 | Literal::_new(proc_macro::Literal::integer(s.into())) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 344 | pub fn span(&self) -> Span { |
| 345 | self.span |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 348 | pub fn set_span(&mut self, span: Span) { |
| 349 | self.span = span; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | impl fmt::Display for Literal { |
| 354 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 355 | self.lit.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | impl fmt::Debug for Literal { |
| 360 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 361 | self.lit.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
| 365 | fn to_literal(s: &str) -> proc_macro::Literal { |
| 366 | let stream = s.parse::<proc_macro::TokenStream>().unwrap(); |
| 367 | match stream.into_iter().next().unwrap().kind { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 368 | proc_macro::TokenNode::Literal(l) => l, |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 369 | _ => unreachable!(), |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | macro_rules! ints { |
Alex Crichton | 040be63 | 2017-07-05 17:56:48 -0700 | [diff] [blame] | 374 | ($($t:ident,)*) => {$( |
| 375 | impl From<$t> for Literal { |
| 376 | fn from(t: $t) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 377 | Literal::_new(proc_macro::Literal::$t(t)) |
Alex Crichton | 040be63 | 2017-07-05 17:56:48 -0700 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | )*} |
| 381 | } |
| 382 | |
| 383 | ints! { |
Alex Crichton | 6e81d76 | 2017-07-14 06:09:31 -0700 | [diff] [blame] | 384 | u8, u16, u32, u64, usize, |
| 385 | i8, i16, i32, i64, isize, |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | macro_rules! floats { |
| 389 | ($($t:ident,)*) => {$( |
| 390 | impl From<$t> for Literal { |
| 391 | fn from(t: $t) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 392 | Literal::_new(proc_macro::Literal::$t(t)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | )*} |
| 396 | } |
| 397 | |
| 398 | floats! { |
| 399 | f32, f64, |
| 400 | } |
| 401 | |
| 402 | impl<'a> From<&'a str> for Literal { |
| 403 | fn from(t: &'a str) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 404 | Literal::_new(proc_macro::Literal::string(t)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
| 408 | impl From<char> for Literal { |
| 409 | fn from(t: char) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame^] | 410 | Literal::_new(proc_macro::Literal::character(t)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 411 | } |
| 412 | } |