blob: 6f1b90349e65e8db8d42f1a8c14dac7cb310687b [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 Tolnay14cbdeb2016-10-01 12:13:59 -070058 use space::whitespace;
David Tolnayde206222016-09-30 11:47:01 -070059 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!(
David Tolnay14cbdeb2016-10-01 12:13:59 -070086 preceded!(
87 option!(whitespace),
88 digits
89 ),
David Tolnayde206222016-09-30 11:47:01 -070090 alt!(
91 tag!("isize") => { |_| IntTy::Isize }
92 |
93 tag!("i8") => { |_| IntTy::I8 }
94 |
95 tag!("i16") => { |_| IntTy::I16 }
96 |
97 tag!("i32") => { |_| IntTy::I32 }
98 |
99 tag!("i64") => { |_| IntTy::I64 }
100 |
101 tag!("usize") => { |_| IntTy::Usize }
102 |
103 tag!("u8") => { |_| IntTy::U8 }
104 |
105 tag!("u16") => { |_| IntTy::U16 }
106 |
107 tag!("u32") => { |_| IntTy::U32 }
108 |
109 tag!("u64") => { |_| IntTy::U64 }
110 |
111 epsilon!() => { |_| IntTy::Unsuffixed }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700112 )
113 ));
114
David Tolnayde206222016-09-30 11:47:01 -0700115 pub fn digits(input: &str) -> IResult<&str, u64> {
David Tolnayf4bbbd92016-09-23 14:41:55 -0700116 let mut value = 0u64;
117 let mut len = 0;
118 let mut bytes = input.bytes().peekable();
119 while let Some(&b) = bytes.peek() {
120 match b {
121 b'0' ... b'9' => {
122 value = match value.checked_mul(10) {
123 Some(value) => value,
124 None => return IResult::Error,
125 };
126 value = match value.checked_add((b - b'0') as u64) {
127 Some(value) => value,
128 None => return IResult::Error,
129 };
130 bytes.next();
131 len += 1;
132 }
David Tolnayfa0edf22016-09-23 22:58:24 -0700133 _ => break,
David Tolnayf4bbbd92016-09-23 14:41:55 -0700134 }
135 }
David Tolnayfa0edf22016-09-23 22:58:24 -0700136 if len > 0 {
137 IResult::Done(&input[len..], value)
138 } else {
139 IResult::Error
140 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700141 }
142}
143
144#[cfg(feature = "printing")]
145mod printing {
146 use super::*;
147 use quote::{Tokens, ToTokens};
148 use std::fmt::{self, Display};
149
150 impl ToTokens for Lit {
151 fn to_tokens(&self, tokens: &mut Tokens) {
152 match *self {
153 Lit::Str(ref s, StrStyle::Cooked) => s.to_tokens(tokens),
David Tolnay627e3d52016-10-01 08:27:31 -0700154 Lit::Str(ref s, StrStyle::Raw(n)) => {
155 let mut tok = "r".to_string();
156 for _ in 0..n {
157 tok.push('#');
158 }
159 tok.push('"');
160 tok.push_str(s);
161 tok.push('"');
162 for _ in 0..n {
163 tok.push('#');
164 }
165 tokens.append(&tok);
166 }
David Tolnayf4bbbd92016-09-23 14:41:55 -0700167 Lit::Int(value, ty) => tokens.append(&format!("{}{}", value, ty)),
168 _ => unimplemented!(),
169 }
170 }
171 }
172
173 impl Display for IntTy {
174 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
175 match *self {
176 IntTy::Isize => formatter.write_str("isize"),
177 IntTy::I8 => formatter.write_str("i8"),
178 IntTy::I16 => formatter.write_str("i16"),
179 IntTy::I32 => formatter.write_str("i32"),
180 IntTy::I64 => formatter.write_str("i64"),
181 IntTy::Usize => formatter.write_str("usize"),
182 IntTy::U8 => formatter.write_str("u8"),
183 IntTy::U16 => formatter.write_str("u16"),
184 IntTy::U32 => formatter.write_str("u32"),
185 IntTy::U64 => formatter.write_str("u64"),
186 IntTy::Unsuffixed => Ok(()),
187 }
188 }
189 }
190}