1use crate::lex::{Cursor, LexError, Scan};
2use crate::lit::Lit;
3use crate::{Span, Spanner};
4
5#[derive(Debug, Clone)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize), serde(into = "String"))]
8pub struct LitByte {
9 value: u8,
10 repr: Box<str>,
11 span: Span,
12}
13
14impl LitByte {
15 #[inline]
16 pub(crate) fn from_parts(value: u8, repr: &str, span: Span) -> Self {
17 Self {
18 value,
19 repr: repr.into(),
20 span,
21 }
22 }
23
24 #[inline]
25 pub fn value(&self) -> u8 {
26 self.value
27 }
28
29 #[inline]
30 pub fn repr(&self) -> &str {
31 &self.repr
32 }
33
34 #[inline]
35 pub fn span(&self) -> Span {
36 self.span
37 }
38
39 #[inline]
40 pub fn set_span(&mut self, span: Span) {
41 self.span = span;
42 }
43}
44
45impl PartialEq for LitByte {
46 fn eq(&self, other: &Self) -> bool {
47 self.value == other.value
48 }
49}
50
51impl Eq for LitByte {}
52
53impl std::hash::Hash for LitByte {
54 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
55 self.value.hash(state);
56 }
57}
58
59impl std::fmt::Display for LitByte {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 f.write_str(&self.repr)
62 }
63}
64
65impl Spanner for LitByte {
66 fn span(&self) -> Span {
67 self.span
68 }
69}
70
71impl Scan for LitByte {
72 fn scan(cursor: Cursor<'_>) -> Result<(Cursor<'_>, Self), LexError> {
73 let end = scan(cursor, "b'")?;
74 let len = end.offset() as usize - cursor.offset() as usize;
75 let repr = &cursor.rest()[..len];
76 let span = cursor.span_to(&end);
77 let inner = repr.strip_prefix("b'").and_then(|r| r.strip_suffix('\''));
78
79 match inner.and_then(decode_one_char) {
80 Some(value) => Ok((end, Self::from_parts(value as u8, repr, span))),
81 _ => cursor.error().into(),
82 }
83 }
84}
85
86impl From<LitByte> for Lit {
87 fn from(value: LitByte) -> Self {
88 Self::Byte(value)
89 }
90}
91
92#[cfg(feature = "serde")]
93impl From<LitByte> for String {
94 fn from(value: LitByte) -> Self {
95 value.repr.into_string()
96 }
97}
98
99fn scan<'a>(c: Cursor<'a>, open: &str) -> Result<Cursor<'a>, LexError> {
100 if !c.starts_with(open) {
101 return c.error().into();
102 }
103
104 let start = c;
105 let c = c.advance_by(open.len());
106 let c = match c.first() {
107 None | Some('\'') => return start.error().into(),
108 Some('\\') => escape(c.advance())?,
109 Some(ch) => c.advance_by(ch.len_utf8()),
110 };
111
112 if !c.starts_with("'") {
113 return start.error().into();
114 }
115
116 Ok(c.advance())
117}
118
119fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, LexError> {
120 match c.first() {
121 None => c.error().into(),
122 Some('n' | 'r' | 't' | '\\' | '\'' | '"' | '0') => Ok(c.advance()),
123 Some('x') => {
124 let c = c.advance();
125 let c = hex_digit(c)?;
126 hex_digit(c)
127 }
128 Some('u') => {
129 let c = c.advance();
130
131 if !c.starts_with("{") {
132 return c.error().into();
133 }
134
135 let mut c = c.advance();
136 let mut count = 0;
137
138 loop {
139 match c.first() {
140 Some('}') if count > 0 => return Ok(c.advance()),
141 Some(ch) if ch.is_ascii_hexdigit() && count < 6 => {
142 count += 1;
143 c = c.advance();
144 }
145 _ => return c.error().into(),
146 }
147 }
148 }
149 _ => c.error().into(),
150 }
151}
152
153fn hex_digit(c: Cursor<'_>) -> Result<Cursor<'_>, LexError> {
154 match c.first() {
155 Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()),
156 _ => c.error().into(),
157 }
158}
159
160fn decode_one_char(inner: &str) -> Option<char> {
162 let mut chars = inner.chars();
163 let value = match chars.next()? {
164 '\\' => decode_escape(&mut chars)?,
165 c => c,
166 };
167
168 if chars.next().is_some() {
169 return None;
170 }
171
172 Some(value)
173}
174
175fn decode_escape(chars: &mut std::str::Chars<'_>) -> Option<char> {
176 match chars.next()? {
177 'n' => Some('\n'),
178 'r' => Some('\r'),
179 't' => Some('\t'),
180 '\\' => Some('\\'),
181 '\'' => Some('\''),
182 '"' => Some('"'),
183 '0' => Some('\0'),
184 'x' => {
185 let hi = chars.next()?.to_digit(16)?;
186 let lo = chars.next()?.to_digit(16)?;
187 char::from_u32(hi * 16 + lo)
188 }
189 'u' => {
190 if chars.next()? != '{' {
191 return None;
192 }
193
194 let mut value = 0u32;
195 let mut count = 0;
196
197 for c in chars.by_ref() {
198 if c == '}' {
199 break;
200 }
201
202 value = value.checked_mul(16)?.checked_add(c.to_digit(16)?)?;
203 count += 1;
204
205 if count > 6 {
206 return None;
207 }
208 }
209
210 char::from_u32(value)
211 }
212 _ => None,
213 }
214}