blob: 92d53399fa5d8b974ef549574d33ba205ce8a401 [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::*;
57 use escape::escaped_string;
58 use nom::{IResult, multispace};
59
60 named!(pub lit -> Lit, alt!(
61 // TODO other literals
62 quoted => { |q| Lit::Str(q, StrStyle::Cooked) }
63 |
64 int => { |(value, ty)| Lit::Int(value, ty) }
65 ));
66
67 named!(quoted -> String, delimited!(
68 punct!("\""),
69 escaped_string,
70 tag!("\"")
71 ));
72
73 named!(pub int -> (u64, IntTy), preceded!(
74 option!(multispace),
75 tuple!(
76 digits,
77 alt!(
78 tag!("isize") => { |_| IntTy::Isize }
79 |
80 tag!("i8") => { |_| IntTy::I8 }
81 |
82 tag!("i16") => { |_| IntTy::I16 }
83 |
84 tag!("i32") => { |_| IntTy::I32 }
85 |
86 tag!("i64") => { |_| IntTy::I64 }
87 |
88 tag!("usize") => { |_| IntTy::Usize }
89 |
90 tag!("u8") => { |_| IntTy::U8 }
91 |
92 tag!("u16") => { |_| IntTy::U16 }
93 |
94 tag!("u32") => { |_| IntTy::U32 }
95 |
96 tag!("u64") => { |_| IntTy::U64 }
97 |
98 epsilon!() => { |_| IntTy::Unsuffixed }
99 )
100 )
101 ));
102
103 fn digits(input: &str) -> IResult<&str, u64> {
104 let mut value = 0u64;
105 let mut len = 0;
106 let mut bytes = input.bytes().peekable();
107 while let Some(&b) = bytes.peek() {
108 match b {
109 b'0' ... b'9' => {
110 value = match value.checked_mul(10) {
111 Some(value) => value,
112 None => return IResult::Error,
113 };
114 value = match value.checked_add((b - b'0') as u64) {
115 Some(value) => value,
116 None => return IResult::Error,
117 };
118 bytes.next();
119 len += 1;
120 }
121 _ => {
122 return if len > 0 {
123 IResult::Done(&input[len..], value)
124 } else {
125 IResult::Error
126 };
127 },
128 }
129 }
130 IResult::Error
131 }
132}
133
134#[cfg(feature = "printing")]
135mod printing {
136 use super::*;
137 use quote::{Tokens, ToTokens};
138 use std::fmt::{self, Display};
139
140 impl ToTokens for Lit {
141 fn to_tokens(&self, tokens: &mut Tokens) {
142 match *self {
143 Lit::Str(ref s, StrStyle::Cooked) => s.to_tokens(tokens),
144 Lit::Int(value, ty) => tokens.append(&format!("{}{}", value, ty)),
145 _ => unimplemented!(),
146 }
147 }
148 }
149
150 impl Display for IntTy {
151 fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
152 match *self {
153 IntTy::Isize => formatter.write_str("isize"),
154 IntTy::I8 => formatter.write_str("i8"),
155 IntTy::I16 => formatter.write_str("i16"),
156 IntTy::I32 => formatter.write_str("i32"),
157 IntTy::I64 => formatter.write_str("i64"),
158 IntTy::Usize => formatter.write_str("usize"),
159 IntTy::U8 => formatter.write_str("u8"),
160 IntTy::U16 => formatter.write_str("u16"),
161 IntTy::U32 => formatter.write_str("u32"),
162 IntTy::U64 => formatter.write_str("u64"),
163 IntTy::Unsuffixed => Ok(()),
164 }
165 }
166 }
167}