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