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 { |
| 54 | let (span, kind) = match token { |
| 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 | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 62 | let span = tt.span().inner; |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 63 | let group = proc_macro::TokenNode::Group(delim, tt.stream.inner.0); |
| 64 | (span, group) |
| 65 | } |
| 66 | TokenTree::Op(tt) => { |
| 67 | let kind = match tt.spacing() { |
| 68 | Spacing::Joint => proc_macro::Spacing::Joint, |
| 69 | Spacing::Alone => proc_macro::Spacing::Alone, |
| 70 | }; |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 71 | (tt.span().inner, proc_macro::TokenNode::Op(tt.op(), kind)) |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 72 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 73 | TokenTree::Term(tt) => (tt.inner.span, proc_macro::TokenNode::Term(tt.inner.term)), |
| 74 | 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] | 75 | }; |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 76 | TokenStream( |
| 77 | proc_macro::TokenTree { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 78 | span: span.0, |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 79 | kind, |
| 80 | }.into(), |
| 81 | ) |
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 { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 86 | fn from_iter<I: IntoIterator<Item = TokenTree>>(streams: I) -> Self { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 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 | } |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 143 | proc_macro::TokenNode::Term(s) => { |
| 144 | ::Term::_new(Term { |
| 145 | term: s, |
| 146 | span: span.inner, |
| 147 | }).into() |
| 148 | } |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 149 | proc_macro::TokenNode::Literal(l) => { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 150 | ::Literal::_new(Literal { |
| 151 | lit: l, |
| 152 | span: span.inner, |
| 153 | }).into() |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 154 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 155 | }) |
| 156 | } |
| 157 | |
| 158 | fn size_hint(&self) -> (usize, Option<usize>) { |
| 159 | self.0.size_hint() |
| 160 | } |
| 161 | } |
| 162 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 163 | impl fmt::Debug for TokenTreeIter { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 164 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 165 | f.debug_struct("TokenTreeIter").finish() |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 166 | } |
| 167 | } |
| 168 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 169 | #[derive(Clone, PartialEq, Eq)] |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 170 | pub struct FileName(String); |
| 171 | |
| 172 | impl fmt::Display for FileName { |
| 173 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 174 | self.0.fmt(f) |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | // NOTE: We have to generate our own filename object here because we can't wrap |
| 179 | // the one provided by proc_macro. |
| 180 | #[derive(Clone, PartialEq, Eq)] |
| 181 | pub struct SourceFile(proc_macro::SourceFile, FileName); |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 182 | |
| 183 | impl SourceFile { |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 184 | fn new(sf: proc_macro::SourceFile) -> Self { |
| 185 | let filename = FileName(sf.path().to_string()); |
| 186 | SourceFile(sf, filename) |
| 187 | } |
| 188 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 189 | /// Get the path to this source file as a string. |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 190 | pub fn path(&self) -> &FileName { |
| 191 | &self.1 |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | pub fn is_real(&self) -> bool { |
| 195 | self.0.is_real() |
| 196 | } |
| 197 | } |
| 198 | |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 199 | impl AsRef<FileName> for SourceFile { |
| 200 | fn as_ref(&self) -> &FileName { |
| 201 | self.path() |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | impl fmt::Debug for SourceFile { |
| 206 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 207 | self.0.fmt(f) |
| 208 | } |
| 209 | } |
| 210 | |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 211 | pub struct LineColumn { |
| 212 | pub line: usize, |
| 213 | pub column: usize, |
| 214 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 215 | |
Nika Layzell | 9973798 | 2018-03-11 18:51:27 -0400 | [diff] [blame] | 216 | #[derive(Copy, Clone, PartialEq, Eq)] |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 217 | pub struct Span(proc_macro::Span); |
| 218 | |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 219 | impl From<proc_macro::Span> for ::Span { |
| 220 | fn from(proc_span: proc_macro::Span) -> ::Span { |
Alex Crichton | af5bad4 | 2018-03-27 14:45:10 -0700 | [diff] [blame] | 221 | ::Span::_new(Span(proc_span)) |
Sergio Benitez | 1380508 | 2018-01-04 01:25:45 -0800 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 225 | impl Span { |
| 226 | pub fn call_site() -> Span { |
| 227 | Span(proc_macro::Span::call_site()) |
| 228 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 229 | |
Alex Crichton | e6085b7 | 2017-11-21 07:24:25 -0800 | [diff] [blame] | 230 | pub fn def_site() -> Span { |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 231 | Span(proc_macro::Span::def_site()) |
| 232 | } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 233 | |
David Tolnay | 4e8e397 | 2018-01-05 18:10:22 -0800 | [diff] [blame] | 234 | pub fn resolved_at(&self, other: Span) -> Span { |
| 235 | Span(self.0.resolved_at(other.0)) |
| 236 | } |
| 237 | |
| 238 | pub fn located_at(&self, other: Span) -> Span { |
| 239 | Span(self.0.located_at(other.0)) |
| 240 | } |
| 241 | |
David Tolnay | 16a1720 | 2017-12-31 10:47:24 -0500 | [diff] [blame] | 242 | pub fn unstable(self) -> proc_macro::Span { |
| 243 | self.0 |
| 244 | } |
| 245 | |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 246 | pub fn source_file(&self) -> SourceFile { |
Nika Layzell | b35a9a3 | 2017-12-30 14:34:35 -0500 | [diff] [blame] | 247 | SourceFile::new(self.0.source_file()) |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | pub fn start(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 251 | let proc_macro::LineColumn { line, column } = self.0.start(); |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 252 | LineColumn { line, column } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | pub fn end(&self) -> LineColumn { |
David Tolnay | b28f38a | 2018-03-31 22:02:29 +0200 | [diff] [blame] | 256 | let proc_macro::LineColumn { line, column } = self.0.end(); |
Nika Layzell | 1ecb6ce | 2017-12-30 14:34:05 -0500 | [diff] [blame] | 257 | LineColumn { line, column } |
Nika Layzell | f8d5f21 | 2017-12-11 14:07:02 -0500 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | pub fn join(&self, other: Span) -> Option<Span> { |
| 261 | self.0.join(other.0).map(Span) |
| 262 | } |
Alex Crichton | 998f642 | 2017-11-19 08:06:27 -0800 | [diff] [blame] | 263 | } |
| 264 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 265 | impl fmt::Debug for Span { |
| 266 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | 040be63 | 2017-07-05 17:56:48 -0700 | [diff] [blame] | 267 | self.0.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | |
| 271 | #[derive(Copy, Clone)] |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 272 | pub struct Term { |
| 273 | term: proc_macro::Term, |
| 274 | span: Span, |
| 275 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 276 | |
David Tolnay | 10effeb | 2018-01-06 11:07:49 -0800 | [diff] [blame] | 277 | impl Term { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 278 | pub fn new(string: &str, span: Span) -> Term { |
| 279 | Term { |
| 280 | term: proc_macro::Term::intern(string), |
| 281 | span, |
| 282 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 283 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 284 | |
David Tolnay | 10effeb | 2018-01-06 11:07:49 -0800 | [diff] [blame] | 285 | pub fn as_str(&self) -> &str { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 286 | self.term.as_str() |
| 287 | } |
| 288 | |
| 289 | pub fn span(&self) -> Span { |
| 290 | self.span |
| 291 | } |
| 292 | |
| 293 | pub fn set_span(&mut self, span: Span) { |
| 294 | self.span = span; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
Alex Crichton | 1a7f762 | 2017-07-05 17:47:15 -0700 | [diff] [blame] | 298 | impl fmt::Debug for Term { |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 299 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 300 | self.term.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
| 304 | #[derive(Clone)] |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 305 | pub struct Literal { |
| 306 | lit: proc_macro::Literal, |
| 307 | span: Span, |
| 308 | } |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 309 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame^] | 310 | macro_rules! suffixed_numbers { |
| 311 | ($($name:ident => $kind:ident,)*) => ($( |
| 312 | pub fn $name(n: $kind) -> Literal { |
| 313 | Literal::_new(proc_macro::Literal::$kind(n)) |
| 314 | } |
| 315 | )*) |
| 316 | } |
| 317 | |
| 318 | macro_rules! unsuffixed_integers { |
| 319 | ($($name:ident => $kind:ident,)*) => ($( |
| 320 | pub fn $name(n: $kind) -> Literal { |
| 321 | Literal::_new(proc_macro::Literal::integer(n as i128)) |
| 322 | } |
| 323 | )*) |
| 324 | } |
| 325 | |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 326 | impl Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 327 | fn _new(lit: proc_macro::Literal) -> Literal { |
| 328 | Literal { |
| 329 | lit, |
| 330 | span: Span::call_site(), |
| 331 | } |
| 332 | } |
| 333 | |
Alex Crichton | a914a61 | 2018-04-04 07:48:44 -0700 | [diff] [blame^] | 334 | suffixed_numbers! { |
| 335 | u8_suffixed => u8, |
| 336 | u16_suffixed => u16, |
| 337 | u32_suffixed => u32, |
| 338 | u64_suffixed => u64, |
| 339 | usize_suffixed => usize, |
| 340 | i8_suffixed => i8, |
| 341 | i16_suffixed => i16, |
| 342 | i32_suffixed => i32, |
| 343 | i64_suffixed => i64, |
| 344 | isize_suffixed => isize, |
| 345 | |
| 346 | f32_suffixed => f32, |
| 347 | f64_suffixed => f64, |
| 348 | } |
| 349 | |
| 350 | unsuffixed_integers! { |
| 351 | u8_unsuffixed => u8, |
| 352 | u16_unsuffixed => u16, |
| 353 | u32_unsuffixed => u32, |
| 354 | u64_unsuffixed => u64, |
| 355 | usize_unsuffixed => usize, |
| 356 | i8_unsuffixed => i8, |
| 357 | i16_unsuffixed => i16, |
| 358 | i32_unsuffixed => i32, |
| 359 | i64_unsuffixed => i64, |
| 360 | isize_unsuffixed => isize, |
| 361 | } |
| 362 | |
| 363 | pub fn f32_unsuffixed(f: f32) -> Literal { |
| 364 | Literal::f64_unsuffixed(f.into()) |
| 365 | } |
| 366 | |
| 367 | pub fn f64_unsuffixed(f: f64) -> Literal { |
| 368 | Literal::_new(proc_macro::Literal::float(f)) |
| 369 | } |
| 370 | |
| 371 | |
| 372 | pub fn string(t: &str) -> Literal { |
| 373 | Literal::_new(proc_macro::Literal::string(t)) |
| 374 | } |
| 375 | |
| 376 | pub fn character(t: char) -> Literal { |
| 377 | Literal::_new(proc_macro::Literal::character(t)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | pub fn byte_string(bytes: &[u8]) -> Literal { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 381 | Literal::_new(proc_macro::Literal::byte_string(bytes)) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 384 | pub fn span(&self) -> Span { |
| 385 | self.span |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 386 | } |
| 387 | |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 388 | pub fn set_span(&mut self, span: Span) { |
| 389 | self.span = span; |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | impl fmt::Display for Literal { |
| 394 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 395 | self.lit.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | |
| 399 | impl fmt::Debug for Literal { |
| 400 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
Alex Crichton | b2c9462 | 2018-04-04 07:36:41 -0700 | [diff] [blame] | 401 | self.lit.fmt(f) |
Alex Crichton | cbec8ec | 2017-06-02 13:19:33 -0700 | [diff] [blame] | 402 | } |
| 403 | } |