Skip to main content

moxy_token/lit/
char.rs

1use crate::lex::{Cursor, LexError, Scan};
2use crate::lit::Lit;
3use crate::{Span, Spanner, ToTokens, TokenStream, TokenTree};
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 ToTokens for LitChar {
81    fn to_tokens(&self, tokens: &mut TokenStream) {
82        tokens.extend_one(TokenTree::Literal(self.clone().into()));
83    }
84}
85
86impl Scan for LitChar {
87    fn scan(cursor: Cursor<'_>) -> Result<(Cursor<'_>, Self), LexError> {
88        let end = scan_quoted(cursor, "'")?;
89        let len = end.offset() as usize - cursor.offset() as usize;
90        let repr = &cursor.rest()[..len];
91        let span = cursor.span_to(&end);
92
93        let inner = repr.strip_prefix('\'').and_then(|r| r.strip_suffix('\''));
94        match inner.and_then(decode_one_char) {
95            Some(value) => Ok((end, Self::from_parts(value, repr, span))),
96            None => cursor.error().into(),
97        }
98    }
99}
100
101impl From<LitChar> for Lit {
102    fn from(value: LitChar) -> Self {
103        Self::Char(value)
104    }
105}
106
107#[cfg(feature = "serde")]
108impl From<LitChar> for String {
109    fn from(value: LitChar) -> Self {
110        value.repr.into_string()
111    }
112}
113
114fn scan_quoted<'a>(c: Cursor<'a>, open: &str) -> Result<Cursor<'a>, LexError> {
115    if !c.starts_with(open) {
116        return c.error().into();
117    }
118
119    let start = c;
120    let c = c.advance_by(open.len());
121    let c = match c.first() {
122        None | Some('\'') => return start.error().into(),
123        Some('\\') => escape(c.advance())?,
124        Some(ch) => c.advance_by(ch.len_utf8()),
125    };
126
127    if !c.starts_with("'") {
128        return start.error().into();
129    }
130
131    Ok(c.advance())
132}
133
134fn escape(c: Cursor<'_>) -> Result<Cursor<'_>, LexError> {
135    match c.first() {
136        None => c.error().into(),
137        Some('n' | 'r' | 't' | '\\' | '\'' | '"' | '0') => Ok(c.advance()),
138        Some('x') => {
139            let c = c.advance();
140            let c = hex_digit(c)?;
141            hex_digit(c)
142        }
143        Some('u') => {
144            let c = c.advance();
145
146            if !c.starts_with("{") {
147                return c.error().into();
148            }
149
150            let mut c = c.advance();
151            let mut count = 0;
152
153            loop {
154                match c.first() {
155                    Some('}') if count > 0 => return Ok(c.advance()),
156                    Some(ch) if ch.is_ascii_hexdigit() && count < 6 => {
157                        count += 1;
158                        c = c.advance();
159                    }
160                    _ => return c.error().into(),
161                }
162            }
163        }
164        _ => c.error().into(),
165    }
166}
167
168fn hex_digit(c: Cursor<'_>) -> Result<Cursor<'_>, LexError> {
169    match c.first() {
170        Some(ch) if ch.is_ascii_hexdigit() => Ok(c.advance()),
171        _ => c.error().into(),
172    }
173}
174
175/// Decode a single (possibly escaped) char from a char-literal inner body.
176fn decode_one_char(inner: &str) -> Option<char> {
177    let mut chars = inner.chars();
178    let value = match chars.next()? {
179        '\\' => decode_escape(&mut chars)?,
180        c => c,
181    };
182
183    if chars.next().is_some() {
184        return None;
185    }
186
187    Some(value)
188}
189
190fn decode_escape(chars: &mut std::str::Chars<'_>) -> Option<char> {
191    match chars.next()? {
192        'n' => Some('\n'),
193        'r' => Some('\r'),
194        't' => Some('\t'),
195        '\\' => Some('\\'),
196        '\'' => Some('\''),
197        '"' => Some('"'),
198        '0' => Some('\0'),
199        'x' => {
200            let hi = chars.next()?.to_digit(16)?;
201            let lo = chars.next()?.to_digit(16)?;
202            char::from_u32(hi * 16 + lo)
203        }
204        'u' => {
205            if chars.next()? != '{' {
206                return None;
207            }
208            let mut value = 0u32;
209            let mut count = 0;
210            for c in chars.by_ref() {
211                if c == '}' {
212                    break;
213                }
214                value = value.checked_mul(16)?.checked_add(c.to_digit(16)?)?;
215                count += 1;
216                if count > 6 {
217                    return None;
218                }
219            }
220            char::from_u32(value)
221        }
222        _ => None,
223    }
224}