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 { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 85 | let streams = streams.into_iter().map(TokenStream::from) |
| 86 | .flat_map(|t| t.0); |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 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 | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 103 | pub struct TokenTreeIter(proc_macro::token_stream::IntoIter); |
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> { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 118 | let token = self.0.next()?; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 119 | Some(match token { |
| 120 | proc_macro::TokenTree::Group(tt) => { |
| 121 | let delim = match tt.delimiter() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 122 | proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis, |
| 123 | proc_macro::Delimiter::Bracket => Delimiter::Bracket, |
| 124 | proc_macro::Delimiter::Brace => Delimiter::Brace, |
| 125 | proc_macro::Delimiter::None => Delimiter::None, |
| 126 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 127 | let stream = ::TokenStream::_new(TokenStream(tt.stream())); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 128 | let mut g = Group::new(delim, stream); |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 129 | g.set_span(::Span::_new(Span(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 130 | g.into() |
| 131 | } |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 132 | proc_macro::TokenTree::Op(tt) => { |
| 133 | let spacing = match tt.spacing() { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 134 | proc_macro::Spacing::Joint => Spacing::Joint, |
| 135 | proc_macro::Spacing::Alone => Spacing::Alone, |
| 136 | }; |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 137 | let mut o = Op::new(tt.op(), spacing); |
| 138 | o.set_span(::Span::_new(Span(tt.span()))); |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 139 | o.into() |
| 140 | } |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 141 | proc_macro::TokenTree::Term(s) => { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 142 | ::Term::_new(Term { |
| 143 | term: s, |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 144 | }).into() |
| 145 | } |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 146 | proc_macro::TokenTree::Literal(l) => { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 147 | ::Literal::_new(Literal { |
| 148 | lit: l, |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 149 | }).into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 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 | |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 212 | #[derive(Copy, Clone)] |
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 { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 247 | let proc_macro::LineColumn { line, column } = self.0.start(); |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 248 | LineColumn { line, column } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | pub fn end(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 252 | let proc_macro::LineColumn { line, column } = self.0.end(); |
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 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 | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 268 | pub struct Term { |
| 269 | term: proc_macro::Term, |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 270 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 271 | |
David Tolnay | 10effeb | 2018-01-06 11:07:49 -0800 | [diff] [blame] | 272 | impl Term { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 273 | pub fn new(string: &str, span: Span) -> Term { |
| 274 | Term { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 275 | term: proc_macro::Term::new(string, span.0), |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 276 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 277 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 278 | |
David Tolnay | 10effeb | 2018-01-06 11:07:49 -0800 | [diff] [blame] | 279 | pub fn as_str(&self) -> &str { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 280 | self.term.as_str() |
| 281 | } |
| 282 | |
| 283 | pub fn span(&self) -> Span { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 284 | Span(self.term.span()) |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 288 | self.term.set_span(span.0); |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 292 | impl fmt::Debug for Term { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 293 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 294 | self.term.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | #[derive(Clone)] |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 299 | pub struct Literal { |
| 300 | lit: proc_macro::Literal, |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 301 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 302 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 303 | macro_rules! suffixed_numbers { |
| 304 | ($($name:ident => $kind:ident,)*) => ($( |
| 305 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 306 | Literal::_new(proc_macro::Literal::$name(n)) |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 307 | } |
| 308 | )*) |
| 309 | } |
| 310 | |
| 311 | macro_rules! unsuffixed_integers { |
| 312 | ($($name:ident => $kind:ident,)*) => ($( |
| 313 | pub fn $name(n: $kind) -> Literal { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 314 | Literal::_new(proc_macro::Literal::$name(n)) |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 315 | } |
| 316 | )*) |
| 317 | } |
| 318 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 319 | impl Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 320 | fn _new(lit: proc_macro::Literal) -> Literal { |
| 321 | Literal { |
| 322 | lit, |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 326 | suffixed_numbers! { |
| 327 | u8_suffixed => u8, |
| 328 | u16_suffixed => u16, |
| 329 | u32_suffixed => u32, |
| 330 | u64_suffixed => u64, |
| 331 | usize_suffixed => usize, |
| 332 | i8_suffixed => i8, |
| 333 | i16_suffixed => i16, |
| 334 | i32_suffixed => i32, |
| 335 | i64_suffixed => i64, |
| 336 | isize_suffixed => isize, |
| 337 | |
| 338 | f32_suffixed => f32, |
| 339 | f64_suffixed => f64, |
| 340 | } |
| 341 | |
| 342 | unsuffixed_integers! { |
| 343 | u8_unsuffixed => u8, |
| 344 | u16_unsuffixed => u16, |
| 345 | u32_unsuffixed => u32, |
| 346 | u64_unsuffixed => u64, |
| 347 | usize_unsuffixed => usize, |
| 348 | i8_unsuffixed => i8, |
| 349 | i16_unsuffixed => i16, |
| 350 | i32_unsuffixed => i32, |
| 351 | i64_unsuffixed => i64, |
| 352 | isize_unsuffixed => isize, |
| 353 | } |
| 354 | |
| 355 | pub fn f32_unsuffixed(f: f32) -> Literal { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 356 | Literal::_new(proc_macro::Literal::f32_unsuffixed(f)) |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | pub fn f64_unsuffixed(f: f64) -> Literal { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 360 | Literal::_new(proc_macro::Literal::f64_unsuffixed(f)) |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | |
| 364 | pub fn string(t: &str) -> Literal { |
| 365 | Literal::_new(proc_macro::Literal::string(t)) |
| 366 | } |
| 367 | |
| 368 | pub fn character(t: char) -> Literal { |
| 369 | Literal::_new(proc_macro::Literal::character(t)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 373 | Literal::_new(proc_macro::Literal::byte_string(bytes)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 374 | } |
| 375 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 376 | pub fn span(&self) -> Span { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 377 | Span(self.lit.span()) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 380 | pub fn set_span(&mut self, span: Span) { |
Alex Crichton | 9cd80a6 | 2018-04-05 17:46:58 -0700 | [diff] [blame^] | 381 | self.lit.set_span(span.0); |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | impl fmt::Display for Literal { |
| 386 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 387 | self.lit.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
| 390 | |
| 391 | impl fmt::Debug for Literal { |
| 392 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 393 | self.lit.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 394 | } |
| 395 | } |