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