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