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"`) |
| 9 | ByteStr(Vec<u8>), |
| 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 |
| 29 | Raw(usize) |
| 30 | } |
| 31 | |
| 32 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 33 | pub enum IntTy { |
| 34 | Isize, |
| 35 | I8, |
| 36 | I16, |
| 37 | I32, |
| 38 | I64, |
| 39 | Usize, |
| 40 | U8, |
| 41 | U16, |
| 42 | U32, |
| 43 | U64, |
| 44 | Unsuffixed |
| 45 | } |
| 46 | |
| 47 | #[derive(Debug, Copy, Clone, Eq, PartialEq)] |
| 48 | pub enum FloatTy { |
| 49 | F32, |
| 50 | F64, |
| 51 | Unsuffixed, |
| 52 | } |
| 53 | |
| 54 | #[cfg(feature = "parsing")] |
| 55 | pub mod parsing { |
| 56 | use super::*; |
David Tolnay | 210884d | 2016-10-01 08:18:42 -0700 | [diff] [blame] | 57 | use escape::{cooked_string, raw_string}; |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 58 | use helper::eat_spaces; |
| 59 | use nom::IResult; |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 60 | |
| 61 | named!(pub lit -> Lit, alt!( |
David Tolnay | 210884d | 2016-10-01 08:18:42 -0700 | [diff] [blame] | 62 | string |
David Tolnay | 7de67d2 | 2016-09-24 07:23:32 -0700 | [diff] [blame] | 63 | // TODO: ByteStr |
| 64 | // TODO: Byte |
| 65 | // TODO: Char |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 66 | | |
| 67 | int => { |(value, ty)| Lit::Int(value, ty) } |
David Tolnay | 7de67d2 | 2016-09-24 07:23:32 -0700 | [diff] [blame] | 68 | // TODO: Float |
| 69 | // TODO: Bool |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 70 | )); |
| 71 | |
David Tolnay | 210884d | 2016-10-01 08:18:42 -0700 | [diff] [blame] | 72 | named!(string -> Lit, alt!( |
| 73 | delimited!( |
| 74 | punct!("\""), |
| 75 | cooked_string, |
| 76 | tag!("\"") |
| 77 | ) => { |s| Lit::Str(s, StrStyle::Cooked) } |
| 78 | | |
| 79 | preceded!( |
| 80 | punct!("r"), |
| 81 | raw_string |
| 82 | ) => { |(s, n)| Lit::Str(s, StrStyle::Raw(n)) } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 83 | )); |
| 84 | |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 85 | named!(pub int -> (u64, IntTy), tuple!( |
| 86 | digits, |
| 87 | alt!( |
| 88 | tag!("isize") => { |_| IntTy::Isize } |
| 89 | | |
| 90 | tag!("i8") => { |_| IntTy::I8 } |
| 91 | | |
| 92 | tag!("i16") => { |_| IntTy::I16 } |
| 93 | | |
| 94 | tag!("i32") => { |_| IntTy::I32 } |
| 95 | | |
| 96 | tag!("i64") => { |_| IntTy::I64 } |
| 97 | | |
| 98 | tag!("usize") => { |_| IntTy::Usize } |
| 99 | | |
| 100 | tag!("u8") => { |_| IntTy::U8 } |
| 101 | | |
| 102 | tag!("u16") => { |_| IntTy::U16 } |
| 103 | | |
| 104 | tag!("u32") => { |_| IntTy::U32 } |
| 105 | | |
| 106 | tag!("u64") => { |_| IntTy::U64 } |
| 107 | | |
| 108 | epsilon!() => { |_| IntTy::Unsuffixed } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 109 | ) |
| 110 | )); |
| 111 | |
David Tolnay | de20622 | 2016-09-30 11:47:01 -0700 | [diff] [blame] | 112 | pub fn digits(input: &str) -> IResult<&str, u64> { |
| 113 | let input = eat_spaces(input); |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 114 | let mut value = 0u64; |
| 115 | let mut len = 0; |
| 116 | let mut bytes = input.bytes().peekable(); |
| 117 | while let Some(&b) = bytes.peek() { |
| 118 | match b { |
| 119 | b'0' ... b'9' => { |
| 120 | value = match value.checked_mul(10) { |
| 121 | Some(value) => value, |
| 122 | None => return IResult::Error, |
| 123 | }; |
| 124 | value = match value.checked_add((b - b'0') as u64) { |
| 125 | Some(value) => value, |
| 126 | None => return IResult::Error, |
| 127 | }; |
| 128 | bytes.next(); |
| 129 | len += 1; |
| 130 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 131 | _ => break, |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 132 | } |
| 133 | } |
David Tolnay | fa0edf2 | 2016-09-23 22:58:24 -0700 | [diff] [blame] | 134 | if len > 0 { |
| 135 | IResult::Done(&input[len..], value) |
| 136 | } else { |
| 137 | IResult::Error |
| 138 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | |
| 142 | #[cfg(feature = "printing")] |
| 143 | mod printing { |
| 144 | use super::*; |
| 145 | use quote::{Tokens, ToTokens}; |
| 146 | use std::fmt::{self, Display}; |
| 147 | |
| 148 | impl ToTokens for Lit { |
| 149 | fn to_tokens(&self, tokens: &mut Tokens) { |
| 150 | match *self { |
| 151 | Lit::Str(ref s, StrStyle::Cooked) => s.to_tokens(tokens), |
David Tolnay | 627e3d5 | 2016-10-01 08:27:31 -0700 | [diff] [blame^] | 152 | Lit::Str(ref s, StrStyle::Raw(n)) => { |
| 153 | let mut tok = "r".to_string(); |
| 154 | for _ in 0..n { |
| 155 | tok.push('#'); |
| 156 | } |
| 157 | tok.push('"'); |
| 158 | tok.push_str(s); |
| 159 | tok.push('"'); |
| 160 | for _ in 0..n { |
| 161 | tok.push('#'); |
| 162 | } |
| 163 | tokens.append(&tok); |
| 164 | } |
David Tolnay | f4bbbd9 | 2016-09-23 14:41:55 -0700 | [diff] [blame] | 165 | Lit::Int(value, ty) => tokens.append(&format!("{}{}", value, ty)), |
| 166 | _ => unimplemented!(), |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | impl Display for IntTy { |
| 172 | fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> { |
| 173 | match *self { |
| 174 | IntTy::Isize => formatter.write_str("isize"), |
| 175 | IntTy::I8 => formatter.write_str("i8"), |
| 176 | IntTy::I16 => formatter.write_str("i16"), |
| 177 | IntTy::I32 => formatter.write_str("i32"), |
| 178 | IntTy::I64 => formatter.write_str("i64"), |
| 179 | IntTy::Usize => formatter.write_str("usize"), |
| 180 | IntTy::U8 => formatter.write_str("u8"), |
| 181 | IntTy::U16 => formatter.write_str("u16"), |
| 182 | IntTy::U32 => formatter.write_str("u32"), |
| 183 | IntTy::U64 => formatter.write_str("u64"), |
| 184 | IntTy::Unsuffixed => Ok(()), |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |