David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 1 | /// Literal kind. |
| 2 | /// |
| 3 | /// E.g. `"foo"`, `42`, `12.34` or `bool` |
| 4 | #[derive(Debug, Clone, Eq, PartialEq)] |
| 5 | pub enum Lit { |
| 6 | /// A string literal (`"foo"`) |
| 7 | Str(String, StrStyle), |
| 8 | /// A byte string (`b"foo"`) |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 9 | ByteStr(Vec<u8>, StrStyle), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 10 | /// A byte char (`b'f'`) |
| 11 | Byte(u8), |
| 12 | /// A character literal (`'a'`) |
| 13 | Char(char), |
| 14 | /// An integer literal (`1`) |
| 15 | Int(u64, IntTy), |
| 16 | /// A float literal (`1f64` or `1E10f64` or `1.0E10`) |
| 17 | Float(String, FloatTy), |
| 18 | /// A boolean literal |
| 19 | Bool(bool), |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 23 | pub enum StrStyle { |
| 24 | /// A regular string, like `"foo"` |
| 25 | Cooked, |
| 26 | /// A raw string, like `r##"foo"##` |
| 27 | /// |
| 28 | /// The uint is the number of `#` symbols used |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 29 | Raw(usize), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 30 | } |
| 31 | |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 32 | impl From<String> for Lit { |
| 33 | fn from(input: String) -> Lit { |
| 34 | Lit::Str(input, StrStyle::Cooked) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | impl<'a> From<&'a str> for Lit { |
| 39 | fn from(input: &str) -> Lit { |
| 40 | Lit::Str(input.into(), StrStyle::Cooked) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl From<Vec<u8>> for Lit { |
| 45 | fn from(input: Vec<u8>) -> Lit { |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 46 | Lit::ByteStr(input, StrStyle::Cooked) |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
| 50 | impl<'a> From<&'a [u8]> for Lit { |
| 51 | fn from(input: &[u8]) -> Lit { |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 52 | Lit::ByteStr(input.into(), StrStyle::Cooked) |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
| 56 | impl From<char> for Lit { |
| 57 | fn from(input: char) -> Lit { |
| 58 | Lit::Char(input) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | impl From<bool> for Lit { |
| 63 | fn from(input: bool) -> Lit { |
| 64 | Lit::Bool(input) |
| 65 | } |
| 66 | } |
| 67 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 68 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 69 | pub enum IntTy { |
| 70 | Isize, |
| 71 | I8, |
| 72 | I16, |
| 73 | I32, |
| 74 | I64, |
| 75 | Usize, |
| 76 | U8, |
| 77 | U16, |
| 78 | U32, |
| 79 | U64, |
David Tolnay | daaf774 | 2016-10-03 11:11:43 -0700 | [diff] [blame] | 80 | Unsuffixed, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 84 | pub enum FloatTy { |
| 85 | F32, |
| 86 | F64, |
| 87 | Unsuffixed, |
| 88 | } |
| 89 | |
Pascal Hertleif | 36342c5 | 2016-10-19 10:31:42 +0200 | [diff] [blame] | 90 | macro_rules! impl_from_for_lit { |
| 91 | (Int, [$($rust_type:ty => $syn_type:expr),+]) => { |
| 92 | $( |
| 93 | impl From<$rust_type> for Lit { |
| 94 | fn from(input: $rust_type) -> Lit { |
| 95 | Lit::Int(input as u64, $syn_type) |
| 96 | } |
| 97 | } |
| 98 | )+ |
| 99 | }; |
| 100 | (Float, [$($rust_type:ty => $syn_type:expr),+]) => { |
| 101 | $( |
| 102 | impl From<$rust_type> for Lit { |
| 103 | fn from(input: $rust_type) -> Lit { |
| 104 | Lit::Float(format!("{}", input), $syn_type) |
| 105 | } |
| 106 | } |
| 107 | )+ |
| 108 | }; |
| 109 | } |
| 110 | |
| 111 | impl_from_for_lit! {Int, [ |
| 112 | isize => IntTy::Isize, |
| 113 | i8 => IntTy::I8, |
| 114 | i16 => IntTy::I16, |
| 115 | i32 => IntTy::I32, |
| 116 | i64 => IntTy::I64, |
| 117 | usize => IntTy::Usize, |
| 118 | u8 => IntTy::U8, |
| 119 | u16 => IntTy::U16, |
| 120 | u32 => IntTy::U32, |
| 121 | u64 => IntTy::U64 |
| 122 | ]} |
| 123 | |
| 124 | impl_from_for_lit! {Float, [ |
| 125 | f32 => FloatTy::F32, |
| 126 | f64 => FloatTy::F64 |
| 127 | ]} |
| 128 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 129 | #[cfg(feature = "parsing")] |
| 130 | pub mod parsing { |
| 131 | use super::*; |
David Tolnay | 615cf6a | 2016-10-08 23:07:02 -0700 | [diff] [blame] | 132 | use escape::{cooked_char, cooked_string, raw_string}; |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 133 | use space::skip_whitespace; |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 134 | use nom::IResult; |
David Tolnay | 4f5f60f | 2016-10-24 00:52:58 -0700 | [diff] [blame] | 135 | use unicode_xid::UnicodeXID; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 136 | |
| 137 | named!(pub lit -> Lit, alt!( |
David Tolnay | 210884d | 2016-10-01 08:18:42 -0700 | [diff] [blame] | 138 | string |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 139 | | |
| 140 | byte_string |
David Tolnay | 615cf6a | 2016-10-08 23:07:02 -0700 | [diff] [blame] | 141 | | |
| 142 | byte |
| 143 | | |
| 144 | character |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 145 | | |
David Tolnay | 4f5f60f | 2016-10-24 00:52:58 -0700 | [diff] [blame] | 146 | float // must be before int |
| 147 | | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 148 | int => { |(value, ty)| Lit::Int(value, ty) } |
David Tolnay | 759d2ff | 2016-10-01 16:18:15 -0700 | [diff] [blame] | 149 | | |
David Tolnay | 3ce49d0 | 2016-10-23 22:29:19 -0700 | [diff] [blame] | 150 | boolean |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 151 | )); |
| 152 | |
David Tolnay | 210884d | 2016-10-01 08:18:42 -0700 | [diff] [blame] | 153 | named!(string -> Lit, alt!( |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 154 | quoted_string => { |s| Lit::Str(s, StrStyle::Cooked) } |
David Tolnay | 210884d | 2016-10-01 08:18:42 -0700 | [diff] [blame] | 155 | | |
| 156 | preceded!( |
| 157 | punct!("r"), |
| 158 | raw_string |
| 159 | ) => { |(s, n)| Lit::Str(s, StrStyle::Raw(n)) } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 160 | )); |
| 161 | |
David Tolnay | 4260229 | 2016-10-01 22:25:45 -0700 | [diff] [blame] | 162 | named!(pub quoted_string -> String, delimited!( |
| 163 | punct!("\""), |
| 164 | cooked_string, |
| 165 | tag!("\"") |
| 166 | )); |
| 167 | |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 168 | named!(byte_string -> Lit, alt!( |
| 169 | delimited!( |
| 170 | punct!("b\""), |
| 171 | cooked_string, |
| 172 | tag!("\"") |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 173 | ) => { |s: String| Lit::ByteStr(s.into_bytes(), StrStyle::Cooked) } |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 174 | | |
| 175 | preceded!( |
| 176 | punct!("br"), |
| 177 | raw_string |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 178 | ) => { |(s, n): (String, _)| Lit::ByteStr(s.into_bytes(), StrStyle::Raw(n)) } |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 179 | )); |
| 180 | |
David Tolnay | 615cf6a | 2016-10-08 23:07:02 -0700 | [diff] [blame] | 181 | named!(byte -> Lit, do_parse!( |
| 182 | punct!("b") >> |
| 183 | tag!("'") >> |
| 184 | ch: cooked_char >> |
| 185 | tag!("'") >> |
| 186 | (Lit::Byte(ch as u8)) |
| 187 | )); |
| 188 | |
| 189 | named!(character -> Lit, do_parse!( |
| 190 | punct!("'") >> |
| 191 | ch: cooked_char >> |
| 192 | tag!("'") >> |
| 193 | (Lit::Char(ch)) |
| 194 | )); |
| 195 | |
David Tolnay | 4f5f60f | 2016-10-24 00:52:58 -0700 | [diff] [blame] | 196 | named!(float -> Lit, do_parse!( |
David Tolnay | 4f5f60f | 2016-10-24 00:52:58 -0700 | [diff] [blame] | 197 | value: float_string >> |
| 198 | suffix: alt!( |
| 199 | tag!("f32") => { |_| FloatTy::F32 } |
| 200 | | |
| 201 | tag!("f64") => { |_| FloatTy::F64 } |
| 202 | | |
| 203 | epsilon!() => { |_| FloatTy::Unsuffixed } |
| 204 | ) >> |
| 205 | (Lit::Float(value, suffix)) |
| 206 | )); |
| 207 | |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 208 | named!(pub int -> (u64, IntTy), tuple!( |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 209 | digits, |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 210 | alt!( |
| 211 | tag!("isize") => { |_| IntTy::Isize } |
| 212 | | |
| 213 | tag!("i8") => { |_| IntTy::I8 } |
| 214 | | |
| 215 | tag!("i16") => { |_| IntTy::I16 } |
| 216 | | |
| 217 | tag!("i32") => { |_| IntTy::I32 } |
| 218 | | |
| 219 | tag!("i64") => { |_| IntTy::I64 } |
| 220 | | |
| 221 | tag!("usize") => { |_| IntTy::Usize } |
| 222 | | |
| 223 | tag!("u8") => { |_| IntTy::U8 } |
| 224 | | |
| 225 | tag!("u16") => { |_| IntTy::U16 } |
| 226 | | |
| 227 | tag!("u32") => { |_| IntTy::U32 } |
| 228 | | |
| 229 | tag!("u64") => { |_| IntTy::U64 } |
| 230 | | |
| 231 | epsilon!() => { |_| IntTy::Unsuffixed } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 232 | ) |
| 233 | )); |
| 234 | |
David Tolnay | 3ce49d0 | 2016-10-23 22:29:19 -0700 | [diff] [blame] | 235 | named!(boolean -> Lit, alt!( |
| 236 | keyword!("true") => { |_| Lit::Bool(true) } |
| 237 | | |
| 238 | keyword!("false") => { |_| Lit::Bool(false) } |
| 239 | )); |
| 240 | |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 241 | fn float_string(mut input: &str) -> IResult<&str, String> { |
| 242 | input = skip_whitespace(input); |
| 243 | |
David Tolnay | 4f5f60f | 2016-10-24 00:52:58 -0700 | [diff] [blame] | 244 | let mut chars = input.chars().peekable(); |
| 245 | match chars.next() { |
| 246 | Some(ch) if ch >= '0' && ch <= '9' => {} |
| 247 | _ => return IResult::Error, |
| 248 | } |
| 249 | |
| 250 | let mut len = 1; |
| 251 | let mut has_dot = false; |
| 252 | let mut has_exp = false; |
| 253 | while let Some(&ch) = chars.peek() { |
| 254 | match ch { |
| 255 | '0'...'9' | '_' => { |
| 256 | chars.next(); |
| 257 | len += 1; |
| 258 | } |
| 259 | '.' => { |
| 260 | if has_dot { |
| 261 | break; |
| 262 | } |
| 263 | chars.next(); |
| 264 | if chars.peek() |
| 265 | .map(|&ch| ch == '.' || UnicodeXID::is_xid_start(ch)) |
| 266 | .unwrap_or(false) { |
| 267 | return IResult::Error; |
| 268 | } |
| 269 | len += 1; |
| 270 | has_dot = true; |
| 271 | } |
| 272 | 'e' | 'E' => { |
| 273 | chars.next(); |
| 274 | len += 1; |
| 275 | has_exp = true; |
| 276 | break; |
| 277 | } |
| 278 | _ => break, |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | let rest = &input[len..]; |
| 283 | if !(has_dot || has_exp || rest.starts_with("f32") || rest.starts_with("f64")) { |
| 284 | return IResult::Error; |
| 285 | } |
| 286 | |
| 287 | if has_exp { |
| 288 | let mut has_exp_value = false; |
| 289 | while let Some(&ch) = chars.peek() { |
| 290 | match ch { |
| 291 | '+' | '-' => { |
| 292 | if has_exp_value { |
| 293 | break; |
| 294 | } |
| 295 | chars.next(); |
| 296 | len += 1; |
| 297 | } |
| 298 | '0'...'9' => { |
| 299 | chars.next(); |
| 300 | len += 1; |
| 301 | has_exp_value = true; |
| 302 | } |
| 303 | '_' => { |
| 304 | chars.next(); |
| 305 | len += 1; |
| 306 | } |
| 307 | _ => break, |
| 308 | } |
| 309 | } |
| 310 | if !has_exp_value { |
| 311 | return IResult::Error; |
| 312 | } |
| 313 | } |
| 314 | |
David Tolnay | c7b636a | 2016-10-24 13:01:08 -0700 | [diff] [blame] | 315 | IResult::Done(&input[len..], input[..len].replace("_", "")) |
David Tolnay | 4f5f60f | 2016-10-24 00:52:58 -0700 | [diff] [blame] | 316 | } |
| 317 | |
David Tolnay | 8a19e6d | 2016-10-24 01:23:40 -0700 | [diff] [blame] | 318 | pub fn digits(mut input: &str) -> IResult<&str, u64> { |
David Tolnay | def6637 | 2016-10-24 21:51:32 -0700 | [diff] [blame] | 319 | input = skip_whitespace(input); |
| 320 | |
David Tolnay | 8a19e6d | 2016-10-24 01:23:40 -0700 | [diff] [blame] | 321 | let base = if input.starts_with("0x") { |
| 322 | input = &input[2..]; |
| 323 | 16 |
| 324 | } else if input.starts_with("0o") { |
| 325 | input = &input[2..]; |
| 326 | 8 |
| 327 | } else if input.starts_with("0b") { |
| 328 | input = &input[2..]; |
| 329 | 2 |
| 330 | } else { |
| 331 | 10 |
| 332 | }; |
| 333 | |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 334 | let mut value = 0u64; |
| 335 | let mut len = 0; |
David Tolnay | 24b5ff7 | 2016-10-24 01:05:05 -0700 | [diff] [blame] | 336 | let mut empty = true; |
| 337 | for b in input.bytes() { |
David Tolnay | 8a19e6d | 2016-10-24 01:23:40 -0700 | [diff] [blame] | 338 | let digit = match b { |
| 339 | b'0'...b'9' => (b - b'0') as u64, |
| 340 | b'a'...b'f' => 10 + (b - b'a') as u64, |
| 341 | b'A'...b'F' => 10 + (b - b'A') as u64, |
David Tolnay | 24b5ff7 | 2016-10-24 01:05:05 -0700 | [diff] [blame] | 342 | b'_' => { |
| 343 | if empty { |
| 344 | return IResult::Error; |
| 345 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 346 | len += 1; |
David Tolnay | 8a19e6d | 2016-10-24 01:23:40 -0700 | [diff] [blame] | 347 | continue; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 348 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 349 | _ => break, |
David Tolnay | 8a19e6d | 2016-10-24 01:23:40 -0700 | [diff] [blame] | 350 | }; |
| 351 | if digit >= base { |
| 352 | return IResult::Error; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 353 | } |
David Tolnay | 8a19e6d | 2016-10-24 01:23:40 -0700 | [diff] [blame] | 354 | value = match value.checked_mul(base) { |
| 355 | Some(value) => value, |
| 356 | None => return IResult::Error, |
| 357 | }; |
| 358 | value = match value.checked_add(digit) { |
| 359 | Some(value) => value, |
| 360 | None => return IResult::Error, |
| 361 | }; |
| 362 | len += 1; |
| 363 | empty = false; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 364 | } |
David Tolnay | 24b5ff7 | 2016-10-24 01:05:05 -0700 | [diff] [blame] | 365 | if empty { |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 366 | IResult::Error |
David Tolnay | 24b5ff7 | 2016-10-24 01:05:05 -0700 | [diff] [blame] | 367 | } else { |
| 368 | IResult::Done(&input[len..], value) |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 369 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | #[cfg(feature = "printing")] |
| 374 | mod printing { |
| 375 | use super::*; |
| 376 | use quote::{Tokens, ToTokens}; |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 377 | use std::{ascii, iter}; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 378 | use std::fmt::{self, Display}; |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 379 | use std::str; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 380 | |
| 381 | impl ToTokens for Lit { |
| 382 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 383 | match *self { |
| 384 | Lit::Str(ref s, StrStyle::Cooked) => s.to_tokens(tokens), |
David Tolnay | 627e3d5 | 2016-10-01 08:27:31 -0700 | [diff] [blame] | 385 | Lit::Str(ref s, StrStyle::Raw(n)) => { |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 386 | tokens.append(&format!("r{delim}\"{string}\"{delim}", |
| 387 | delim = iter::repeat("#").take(n).collect::<String>(), |
| 388 | string = s)); |
| 389 | } |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 390 | Lit::ByteStr(ref v, StrStyle::Cooked) => { |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 391 | let mut escaped = "b\"".to_string(); |
| 392 | for &ch in v.iter() { |
David Tolnay | 289f4c7 | 2016-10-25 00:00:09 -0700 | [diff] [blame^] | 393 | match ch { |
| 394 | 0 => escaped.push_str(r"\0"), |
| 395 | b'\'' => escaped.push('\''), |
| 396 | _ => escaped.extend(ascii::escape_default(ch).map(|c| c as char)), |
| 397 | } |
David Tolnay | 627e3d5 | 2016-10-01 08:27:31 -0700 | [diff] [blame] | 398 | } |
David Tolnay | 56d6213 | 2016-10-01 16:14:54 -0700 | [diff] [blame] | 399 | escaped.push('"'); |
| 400 | tokens.append(&escaped); |
David Tolnay | 627e3d5 | 2016-10-01 08:27:31 -0700 | [diff] [blame] | 401 | } |
David Tolnay | 4a65840 | 2016-10-24 00:21:41 -0700 | [diff] [blame] | 402 | Lit::ByteStr(ref vec, StrStyle::Raw(n)) => { |
| 403 | tokens.append(&format!("br{delim}\"{string}\"{delim}", |
| 404 | delim = iter::repeat("#").take(n).collect::<String>(), |
| 405 | string = str::from_utf8(vec).unwrap())); |
| 406 | } |
David Tolnay | 289f4c7 | 2016-10-25 00:00:09 -0700 | [diff] [blame^] | 407 | Lit::Byte(b) => { |
| 408 | match b { |
| 409 | 0 => tokens.append(r"b'\0'"), |
| 410 | b'\"' => tokens.append("b'\"'"), |
| 411 | _ => { |
| 412 | let mut escaped = "b'".to_string(); |
| 413 | escaped.extend(ascii::escape_default(b).map(|c| c as char)); |
| 414 | escaped.push('\''); |
| 415 | tokens.append(&escaped); |
| 416 | } |
| 417 | } |
| 418 | } |
David Tolnay | f17fd2f | 2016-10-07 23:38:08 -0700 | [diff] [blame] | 419 | Lit::Char(ch) => ch.to_tokens(tokens), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 420 | Lit::Int(value, ty) => tokens.append(&format!("{}{}", value, ty)), |
David Tolnay | f17fd2f | 2016-10-07 23:38:08 -0700 | [diff] [blame] | 421 | Lit::Float(ref value, ty) => tokens.append(&format!("{}{}", value, ty)), |
David Tolnay | 759d2ff | 2016-10-01 16:18:15 -0700 | [diff] [blame] | 422 | Lit::Bool(true) => tokens.append("true"), |
| 423 | Lit::Bool(false) => tokens.append("false"), |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | impl Display for IntTy { |
| 429 | fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 430 | match *self { |
| 431 | IntTy::Isize => formatter.write_str("isize"), |
| 432 | IntTy::I8 => formatter.write_str("i8"), |
| 433 | IntTy::I16 => formatter.write_str("i16"), |
| 434 | IntTy::I32 => formatter.write_str("i32"), |
| 435 | IntTy::I64 => formatter.write_str("i64"), |
| 436 | IntTy::Usize => formatter.write_str("usize"), |
| 437 | IntTy::U8 => formatter.write_str("u8"), |
| 438 | IntTy::U16 => formatter.write_str("u16"), |
| 439 | IntTy::U32 => formatter.write_str("u32"), |
| 440 | IntTy::U64 => formatter.write_str("u64"), |
| 441 | IntTy::Unsuffixed => Ok(()), |
| 442 | } |
| 443 | } |
| 444 | } |
David Tolnay | f17fd2f | 2016-10-07 23:38:08 -0700 | [diff] [blame] | 445 | |
| 446 | impl Display for FloatTy { |
| 447 | fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 448 | match *self { |
| 449 | FloatTy::F32 => formatter.write_str("f32"), |
| 450 | FloatTy::F64 => formatter.write_str("f64"), |
| 451 | FloatTy::Unsuffixed => Ok(()), |
| 452 | } |
| 453 | } |
| 454 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 455 | } |