blob: 456b3c6a6d64456b6a15af7e8698e9ea845b4917 [file] [log] [blame]
David Tolnayf4bbbd92016-09-23 14:41:55 -07001/// Literal kind.
2///
3/// E.g. `"foo"`, `42`, `12.34` or `bool`
4#[derive(Debug, Clone, Eq, PartialEq)]
5pub 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)]
23pub 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)]
33pub 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)]
48pub enum FloatTy {
49 F32,
50 F64,
51 Unsuffixed,
52}
53
54#[cfg(feature = "parsing")]
55pub mod parsing {
56 use super::*;
David Tolnay210884d2016-10-01 08:18:42 -070057 use escape::{cooked_string, raw_string};
David Tolnayde206222016-09-30 11:47:01 -070058 use helper::eat_spaces;
59 use nom::IResult;
David Tolnayf4bbbd92016-09-23 14:41:55 -070060
61 named!(pub lit -> Lit, alt!(
David Tolnay210884d2016-10-01 08:18:42 -070062 string
David Tolnay7de67d22016-09-24 07:23:32 -070063 // TODO: ByteStr
64 // TODO: Byte
65 // TODO: Char
David Tolnayf4bbbd92016-09-23 14:41:55 -070066 |
67 int => { |(value, ty)| Lit::Int(value, ty) }
David Tolnay7de67d22016-09-24 07:23:32 -070068 // TODO: Float
69 // TODO: Bool
David Tolnayf4bbbd92016-09-23 14:41:55 -070070 ));
71
David Tolnay210884d2016-10-01 08:18:42 -070072 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 Tolnayf4bbbd92016-09-23 14:41:55 -070083 ));
84
David Tolnayde206222016-09-30 11:47:01 -070085 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 Tolnayf4bbbd92016-09-23 14:41:55 -0700109 )
110 ));
111
David Tolnayde206222016-09-30 11:47:01 -0700112 pub fn digits(input: &str) -> IResult<&str, u64> {
113 let input = eat_spaces(input);
David Tolnayf4bbbd92016-09-23 14:41:55 -0700114 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 Tolnayfa0edf22016-09-23 22:58:24 -0700131 _ => break,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700132 }
133 }
David Tolnayfa0edf22016-09-23 22:58:24 -0700134 if len > 0 {
135 IResult::Done(&input[len..], value)
136 } else {
137 IResult::Error
138 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700139 }
140}
141
142#[cfg(feature = "printing")]
143mod 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),
152 Lit::Int(value, ty) => tokens.append(&format!("{}{}", value, ty)),
153 _ => unimplemented!(),
154 }
155 }
156 }
157
158 impl Display for IntTy {
159 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
160 match *self {
161 IntTy::Isize => formatter.write_str("isize"),
162 IntTy::I8 => formatter.write_str("i8"),
163 IntTy::I16 => formatter.write_str("i16"),
164 IntTy::I32 => formatter.write_str("i32"),
165 IntTy::I64 => formatter.write_str("i64"),
166 IntTy::Usize => formatter.write_str("usize"),
167 IntTy::U8 => formatter.write_str("u8"),
168 IntTy::U16 => formatter.write_str("u16"),
169 IntTy::U32 => formatter.write_str("u32"),
170 IntTy::U64 => formatter.write_str("u64"),
171 IntTy::Unsuffixed => Ok(()),
172 }
173 }
174 }
175}