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