1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
use std::ops::{Deref, DerefMut};
use proc_macro::{Span, TokenStream};
use crate::compile_error;
pub struct Literal {
data: LiteralData,
span: Span,
}
impl Literal {
pub fn data(&self) -> &LiteralData {
&self.data
}
pub fn span(&self) -> Span {
self.span
}
}
#[non_exhaustive]
pub enum LiteralData {
String(String),
Unknown,
}
impl Literal {
pub fn parse(lit: &proc_macro::Literal) -> Result<Output<Self>, Vec<TokenStream>> {
let mut errors = Vec::new();
let lit_str = lit.to_string();
let lit = if lit_str.starts_with("\"") {
let s = parse_str_literal(&lit_str, lit.span())?;
errors.extend(s.errors);
Self {
data: LiteralData::String(s.x),
span: lit.span(),
}
} else if lit_str.starts_with("r") {
Self {
data: LiteralData::String(parse_raw_str_literal(&lit_str)),
span: lit.span(),
}
} else {
Self {
data: LiteralData::Unknown,
span: lit.span(),
}
};
Ok(Output::new(lit, errors))
}
}
pub struct Output<T> {
pub x: T,
pub errors: Vec<TokenStream>,
}
impl<T> Output<T> {
fn new(x: T, errors: Vec<TokenStream>) -> Self {
Self { x, errors }
}
}
impl<T> Deref for Output<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.x
}
}
impl<T> DerefMut for Output<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.x
}
}
/// See [the Reference](https://doc.rust-lang.org/1.91.1/reference/tokens.html#string-literals)
/// for information about the syntax being parsed here.
fn parse_str_literal(input: &str, span: Span) -> Result<Output<String>, Vec<TokenStream>> {
let mut errors = Vec::new();
enum State {
Normal,
InEscape,
}
assert!(input.starts_with("\""));
let input = &input[1..];
let mut state = State::Normal;
let mut text = String::new();
let mut iter = input.char_indices().peekable();
while let Some((i, c)) = iter.next() {
match state {
State::Normal => match c {
'\\' => state = State::InEscape,
'\"' => break,
'\r' => unreachable!(),
_ => text.push(c),
},
State::InEscape => {
match c {
// - QUOTE_ESCAPE
'"' => {
text.push('"');
state = State::Normal
}
'\'' => {
text.push('"');
state = State::Normal
}
// - ASCII_ESCAPE
'x' => {
// consume 2 more chars,
// - a hex digit from 0 through 7
// - a hex digit from 0 through F
let (_, x) = iter.next().unwrap();
let (_, y) = iter.next().unwrap();
let x: u8 = x as u8 - b'0';
let sum: u8 = match y as u8 {
y @ b'0'..=b'9' => (x * 16) + (y - b'0'),
y @ b'A'..=b'F' => (x * 16) + (y - b'A' + 10),
y @ b'a'..=b'f' => (x * 16) + (y - b'a' + 10),
_ => unreachable!(),
};
text.push(sum as char);
state = State::Normal
}
'n' => {
text.push('\n');
state = State::Normal
}
'r' => {
text.push('\r');
state = State::Normal
}
't' => {
text.push('\t');
state = State::Normal
}
'\\' => {
text.push('\\');
state = State::Normal
}
'0' => {
text.push('\0');
state = State::Normal
}
// - UNICODE_ESCAPE
'u' => {
// consume an opening curly brace, a closing curly brace,
// and hex digits in between (of which there can be between 1 and 6).
let (_, '{') = iter.next().unwrap() else {
unreachable!()
};
let mut sum: u32 = 0;
for n in 0..7 {
let (_, maybe_digit) = iter.next().unwrap();
if n == 6 {
assert_eq!(maybe_digit, '}');
}
match maybe_digit as u8 {
d @ b'0'..=b'9' => {
sum = (sum * 16) + ((d - b'0') as u32);
}
d @ b'a'..=b'f' => {
sum = (sum * 16) + ((d - b'a' + 10) as u32);
}
d @ b'A'..=b'F' => {
sum = (sum * 16) + ((d - b'A' + 10) as u32);
}
b'}' => break,
_ => unreachable!(),
}
}
// This *should* be infallible, since it passed rustc's lexer already,
// but we're testing anyway. If this panics, please open an issue.
// (We'll convert it to do proper spanned error reporting.)
let new_c = char::from_u32(sum).unwrap();
text.push(new_c);
state = State::Normal
}
// - STRING_CONTINUE
'\n' => {
// https://doc.rust-lang.org/nightly/reference/expressions/literal-expr.html#string-continuation-escapes
// > The escape sequence consists of `\`
// > followed immediately by `U+000A` (LF),
// > and all following whitespace characters before the next non-whitespace character.
// > For this purpose, the whitespace characters are
// > `U+0009` (HT), `U+000A` (LF), `U+000D` (CR), and `U+0020` (SPACE).
// Loop:
// 1. Peek, check if the next thing is whitespace
// 2. If it is, consume it. Otherwise, bail.
loop {
let &(_, next_c) = iter.peek().unwrap();
match next_c {
'\u{0009}' | '\u{000A}' | '\u{000D}' | '\u{0020}' => {
iter.next();
}
_ => break,
}
}
state = State::Normal
}
_ => {
// TODO: if/when we get subspans, we should use one here.
errors.push(compile_error(
&format!("invalid string escape at byte offset: {i}"),
span,
))
}
}
}
}
}
if iter.next().is_some() {
errors.push(compile_error(
"string literal suffixes are not supported",
span,
))
}
Ok(Output::new(text, errors))
}
fn parse_raw_str_literal(input: &str) -> String {
assert!(input.starts_with("r"));
let input = &input[1..];
let mut text = String::new();
let mut cursor = input;
let mut hash_prefix_count = 0usize;
loop {
match cursor.as_bytes() {
[b'#', ..] => {
cursor = &cursor[1..];
hash_prefix_count += 1;
}
[b'"', ..] => {
cursor = &cursor[1..];
break;
}
_ => unreachable!(),
}
}
let hash_prefix_count = hash_prefix_count;
'string_body: loop {
match cursor.as_bytes() {
[b'"', ..] => {
let mut hash_suffix_count = 0;
'attempt_termination: loop {
let offset: usize = (hash_suffix_count + 1).into();
if hash_suffix_count == hash_prefix_count {
break 'string_body;
}
match &cursor.as_bytes()[offset..] {
[b'#', ..] => {
hash_suffix_count += 1;
}
[_, ..] => {
text.push('"');
for _ in 0..hash_suffix_count {
text.push('#');
}
cursor = &cursor[offset..];
break 'attempt_termination;
}
[] => break 'string_body,
}
}
}
[_, ..] => {
// we want the first char and the offset of the second
let mut iter = cursor.char_indices();
let (_, c) = iter.next().unwrap();
let (offset, _) = iter.next().unwrap();
text.push(c);
cursor = &cursor[offset..];
}
[] => unreachable!(),
}
}
text
}