1use crate::lex::{Cursor, LexError, Scan};
2use crate::{Span, Spanner, ToTokens, TokenStream, TokenTree};
3
4pub mod float;
6pub mod int;
8
9mod r#bool;
10mod byte;
11mod byte_str;
12mod c_str;
13mod char;
14mod str;
15mod verbatim;
16
17pub use r#bool::*;
18pub use byte::*;
19pub use byte_str::*;
20pub use c_str::*;
21pub use char::*;
22pub use float::*;
23pub use int::*;
24pub use str::*;
25pub use verbatim::*;
26
27#[derive(Debug, Clone, PartialEq, Eq, Hash)]
29#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(into = "String"))]
30pub enum Lit {
31 Int(LitInt),
32 Float(LitFloat),
33 Str(LitStr),
34 ByteStr(LitByteStr),
35 CStr(LitCStr),
36 Char(LitChar),
37 Byte(LitByte),
38 Bool(LitBool),
39 Verbatim(LitVerbatim),
40}
41
42impl Lit {
43 pub fn string(value: &str) -> Self {
44 Self::Str(LitStr::new(value, Span::default()))
45 }
46
47 pub fn char(value: char) -> Self {
48 Self::Char(LitChar::new(value, Span::default()))
49 }
50
51 pub fn from_repr(repr: &str, span: Span) -> Self {
55 use std::str::FromStr;
56
57 match TokenStream::from_str(repr).ok().and_then(|ts| ts.into_iter().next()) {
58 Some(TokenTree::Literal(mut lit)) if lit.repr() == repr => {
59 lit.set_span(span);
60 lit
61 }
62 _ => Self::Verbatim(LitVerbatim::new(repr, span)),
63 }
64 }
65
66 #[inline]
67 pub fn is_int(&self) -> bool {
68 matches!(self, Self::Int(_))
69 }
70
71 #[inline]
72 pub fn is_float(&self) -> bool {
73 matches!(self, Self::Float(_))
74 }
75
76 #[inline]
77 pub fn is_str(&self) -> bool {
78 matches!(self, Self::Str(_))
79 }
80
81 #[inline]
82 pub fn is_c_str(&self) -> bool {
83 matches!(self, Self::CStr(_))
84 }
85
86 #[inline]
87 pub fn is_bool(&self) -> bool {
88 matches!(self, Self::Bool(_))
89 }
90
91 #[inline]
92 pub fn is_char(&self) -> bool {
93 matches!(self, Self::Char(_))
94 }
95
96 #[inline]
97 pub fn is_byte(&self) -> bool {
98 matches!(self, Self::Byte(_))
99 }
100
101 #[inline]
102 pub fn is_byte_str(&self) -> bool {
103 matches!(self, Self::ByteStr(_))
104 }
105
106 #[inline]
107 pub fn is_verbatim(&self) -> bool {
108 matches!(self, Self::Verbatim(_))
109 }
110
111 #[inline]
112 pub fn as_str(&self) -> Option<&LitStr> {
113 if let Self::Str(v) = self { Some(v) } else { None }
114 }
115
116 #[inline]
117 pub fn as_c_str(&self) -> Option<&LitCStr> {
118 if let Self::CStr(v) = self { Some(v) } else { None }
119 }
120
121 #[inline]
122 pub fn as_bool(&self) -> Option<&LitBool> {
123 if let Self::Bool(v) = self { Some(v) } else { None }
124 }
125
126 #[inline]
127 pub fn as_int(&self) -> Option<&LitInt> {
128 if let Self::Int(v) = self { Some(v) } else { None }
129 }
130
131 #[inline]
132 pub fn as_float(&self) -> Option<&LitFloat> {
133 if let Self::Float(v) = self { Some(v) } else { None }
134 }
135
136 #[inline]
137 pub fn as_char(&self) -> Option<&LitChar> {
138 if let Self::Char(v) = self { Some(v) } else { None }
139 }
140
141 #[inline]
142 pub fn as_byte(&self) -> Option<&LitByte> {
143 if let Self::Byte(v) = self { Some(v) } else { None }
144 }
145
146 #[inline]
147 pub fn as_byte_str(&self) -> Option<&LitByteStr> {
148 if let Self::ByteStr(v) = self { Some(v) } else { None }
149 }
150
151 #[inline]
152 pub fn to_token_tree(&self) -> TokenTree {
153 TokenTree::Literal(self.clone())
154 }
155
156 #[inline]
157 pub fn into_token_tree(self) -> TokenTree {
158 TokenTree::Literal(self)
159 }
160
161 pub fn repr(&self) -> &str {
162 match self {
163 Self::Int(v) => v.repr(),
164 Self::Float(v) => v.repr(),
165 Self::Str(v) => v.repr(),
166 Self::ByteStr(v) => v.repr(),
167 Self::CStr(v) => v.repr(),
168 Self::Char(v) => v.repr(),
169 Self::Byte(v) => v.repr(),
170 Self::Bool(v) => v.repr(),
171 Self::Verbatim(v) => v.repr(),
172 }
173 }
174
175 pub fn span(&self) -> Span {
176 match self {
177 Self::Int(v) => v.span(),
178 Self::Float(v) => v.span(),
179 Self::Str(v) => v.span(),
180 Self::ByteStr(v) => v.span(),
181 Self::CStr(v) => v.span(),
182 Self::Char(v) => v.span(),
183 Self::Byte(v) => v.span(),
184 Self::Bool(v) => v.span(),
185 Self::Verbatim(v) => v.span(),
186 }
187 }
188
189 pub fn set_span(&mut self, span: Span) {
190 match self {
191 Self::Int(v) => v.set_span(span),
192 Self::Float(v) => v.set_span(span),
193 Self::Str(v) => v.set_span(span),
194 Self::ByteStr(v) => v.set_span(span),
195 Self::CStr(v) => v.set_span(span),
196 Self::Char(v) => v.set_span(span),
197 Self::Byte(v) => v.set_span(span),
198 Self::Bool(v) => v.set_span(span),
199 Self::Verbatim(v) => v.set_span(span),
200 }
201 }
202}
203
204impl Spanner for Lit {
205 fn span(&self) -> Span {
206 self.span()
207 }
208}
209
210impl std::fmt::Display for Lit {
211 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
212 f.write_str(self.repr())
213 }
214}
215
216#[cfg(feature = "serde")]
217impl From<Lit> for String {
218 fn from(value: Lit) -> Self {
219 value.repr().to_string()
220 }
221}
222
223impl Scan for Lit {
224 fn scan(cursor: Cursor<'_>) -> Result<(Cursor<'_>, Self), LexError> {
225 if let Ok((end, v)) = LitBool::scan(cursor) {
226 return Ok((end, Self::Bool(v)));
227 }
228
229 if let Ok((end, v)) = LitByte::scan(cursor) {
230 return Ok((end, Self::Byte(v)));
231 }
232
233 if let Ok((end, v)) = LitByteStr::scan(cursor) {
234 return Ok((end, Self::ByteStr(v)));
235 }
236
237 if let Ok((end, v)) = LitCStr::scan(cursor) {
238 return Ok((end, Self::CStr(v)));
239 }
240
241 if let Ok((end, v)) = LitStr::scan(cursor) {
242 return Ok((end, Self::Str(v)));
243 }
244
245 if let Ok((end, v)) = LitChar::scan(cursor) {
246 return Ok((end, Self::Char(v)));
247 }
248
249 if let Ok((end, v)) = LitFloat::scan(cursor) {
250 return Ok((end, Self::Float(v)));
251 }
252
253 if let Ok((end, v)) = LitInt::scan(cursor) {
254 return Ok((end, Self::Int(v)));
255 }
256
257 cursor.error().into()
258 }
259}
260
261impl ToTokens for Lit {
262 fn to_tokens(&self, tokens: &mut TokenStream) {
263 tokens.extend_one(TokenTree::Literal(self.clone()));
264 }
265}