Skip to main content

moxy_token/lit/
char.rs

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