-
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathstring.rs
More file actions
369 lines (335 loc) · 12.4 KB
/
string.rs
File metadata and controls
369 lines (335 loc) · 12.4 KB
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
//! Boa's lexing for ECMAScript string literals.
use crate::lexer::{Cursor, Error, Token, TokenKind, Tokenizer, token::EscapeSequence};
use crate::source::ReadChar;
use boa_ast::{LinearSpan, Position, PositionGroup, Span};
use boa_interner::Interner;
use std::io::{self, ErrorKind};
/// String literal lexing.
///
/// Note: expects for the initializer `'` or `"` to already be consumed from the cursor.
///
/// More information:
/// - [ECMAScript reference][spec]
/// - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#sec-literals-string-literals
/// [mdn]: https://developer.cdn.mozilla.net/en-US/docs/Web/JavaScript/Reference/Global_Objects/String
#[derive(Debug, Clone, Copy)]
pub(super) struct StringLiteral {
terminator: StringTerminator,
}
impl StringLiteral {
/// Creates a new string literal lexer.
pub(super) fn new(init: char) -> Self {
let terminator = match init {
'\'' => StringTerminator::SingleQuote,
'"' => StringTerminator::DoubleQuote,
_ => unreachable!(),
};
Self { terminator }
}
}
/// Terminator for the string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum StringTerminator {
SingleQuote,
DoubleQuote,
}
/// Extends a buffer type to store UTF-16 code units and convert to string.
pub(crate) trait UTF16CodeUnitsBuffer {
/// Encodes the code point to UTF-16 code units and push to the buffer.
fn push_code_point(&mut self, code_point: u32);
}
impl UTF16CodeUnitsBuffer for Vec<u16> {
fn push_code_point(&mut self, mut code_point: u32) {
if let Ok(cp) = code_point.try_into() {
self.push(cp);
return;
}
code_point -= 0x10000;
let cu1 = (code_point / 1024 + 0xD800)
.try_into()
.expect("decoded an u32 into two u16.");
let cu2 = (code_point % 1024 + 0xDC00)
.try_into()
.expect("decoded an u32 into two u16.");
self.push(cu1);
self.push(cu2);
}
}
impl<R> Tokenizer<R> for StringLiteral {
fn lex(
&mut self,
cursor: &mut Cursor<R>,
start_pos: PositionGroup,
interner: &mut Interner,
) -> Result<Token, Error>
where
R: ReadChar,
{
let (lit, span, escape_sequence) = Self::take_string_characters(
cursor,
start_pos.position(),
self.terminator,
cursor.strict(),
)?;
Ok(Token::new(
TokenKind::string_literal(interner.get_or_intern(&lit[..]), escape_sequence),
span,
LinearSpan::new(start_pos.linear_position(), cursor.linear_pos()),
))
}
}
impl StringLiteral {
/// Checks if a character is `LineTerminator` as per ECMAScript standards.
///
/// More information:
/// - [ECMAScript reference][spec]
///
/// [spec]: https://tc39.es/ecma262/#prod-LineTerminator
pub(super) const fn is_line_terminator(ch: u32) -> bool {
matches!(
ch,
0x000A /* <LF> */ | 0x000D /* <CR> */ | 0x2028 /* <LS> */ | 0x2029 /* <PS> */
)
}
fn take_string_characters<R>(
cursor: &mut Cursor<R>,
start_pos: Position,
terminator: StringTerminator,
strict: bool,
) -> Result<(Vec<u16>, Span, EscapeSequence), Error>
where
R: ReadChar,
{
let mut buf = Vec::new();
let mut escape_sequence = EscapeSequence::empty();
loop {
let ch_start_pos = cursor.pos();
let ch = cursor.next_char()?;
match ch {
Some(0x0027 /* ' */) if terminator == StringTerminator::SingleQuote => break,
Some(0x0022 /* " */) if terminator == StringTerminator::DoubleQuote => break,
Some(0x005C /* \ */) => {
let (escape_value, escape) = Self::take_escape_sequence_or_line_continuation(
cursor,
ch_start_pos,
strict,
false,
)?;
escape_sequence |= escape;
if let Some(escape_value) = escape_value {
buf.push_code_point(escape_value);
}
}
Some(0x2028) => buf.push(0x2028 /* <LS> */),
Some(0x2029) => buf.push(0x2029 /* <PS> */),
Some(ch) if !Self::is_line_terminator(ch) => {
buf.push_code_point(ch);
}
_ => {
return Err(Error::from(io::Error::new(
ErrorKind::UnexpectedEof,
"unterminated string literal",
)));
}
}
}
Ok((buf, Span::new(start_pos, cursor.pos()), escape_sequence))
}
pub(super) fn take_escape_sequence_or_line_continuation<R>(
cursor: &mut Cursor<R>,
start_pos: Position,
strict: bool,
is_template_literal: bool,
) -> Result<(Option<u32>, EscapeSequence), Error>
where
R: ReadChar,
{
let escape_ch = cursor.next_char()?.ok_or_else(|| {
Error::from(io::Error::new(
ErrorKind::UnexpectedEof,
"unterminated escape sequence in literal",
))
})?;
let escape_value = match escape_ch {
0x0062 /* b */ => (Some(0x0008 /* <BS> */), EscapeSequence::OTHER),
0x0074 /* t */ => (Some(0x0009 /* <HT> */), EscapeSequence::OTHER),
0x006E /* n */ => (Some(0x000A /* <LF> */), EscapeSequence::OTHER),
0x0076 /* v */ => (Some(0x000B /* <VT> */), EscapeSequence::OTHER),
0x0066 /* f */ => (Some(0x000C /* <FF> */), EscapeSequence::OTHER),
0x0072 /* r */ => (Some(0x000D /* <CR> */), EscapeSequence::OTHER),
0x0022 /* " */ => (Some(0x0022 /* " */), EscapeSequence::OTHER),
0x0027 /* ' */ => (Some(0x0027 /* ' */), EscapeSequence::OTHER),
0x005C /* \ */ => (Some(0x005C /* \ */), EscapeSequence::OTHER),
0x0030 /* 0 */ if cursor
.peek_char()?
.filter(|c| (0x30..=0x39 /* 0..=9 */).contains(c))
.is_none() =>
(Some(0x0000 /* NULL */), EscapeSequence::OTHER),
0x0078 /* x */ => {
(Some(Self::take_hex_escape_sequence(cursor, start_pos)?), EscapeSequence::OTHER)
}
0x0075 /* u */ => {
(Some(Self::take_unicode_escape_sequence(cursor, start_pos)?), EscapeSequence::OTHER)
}
0x0038 /* 8 */ | 0x0039 /* 9 */ => {
// Grammar: NonOctalDecimalEscapeSequence
if is_template_literal {
return Err(Error::syntax(
"\\8 and \\9 are not allowed in template literal",
start_pos,
));
} else if strict {
return Err(Error::syntax(
"\\8 and \\9 are not allowed in strict mode",
start_pos,
));
}
(Some(escape_ch), EscapeSequence::NON_OCTAL_DECIMAL)
}
_ if (0x0030..=0x0037 /* '0'..='7' */).contains(&escape_ch) => {
if is_template_literal {
return Err(Error::syntax(
"octal escape sequences are not allowed in template literal",
start_pos,
));
}
if strict {
return Err(Error::syntax(
"octal escape sequences are not allowed in strict mode",
start_pos,
));
}
(Some(Self::take_legacy_octal_escape_sequence(
cursor,
escape_ch.try_into().expect("an ascii char must not fail to convert"),
)?), EscapeSequence::LEGACY_OCTAL)
}
_ if Self::is_line_terminator(escape_ch) => {
// Grammar: LineContinuation
// Grammar: \ LineTerminatorSequence
// LineContinuation is the empty String.
(None, EscapeSequence::OTHER)
}
_ => {
(Some(escape_ch), EscapeSequence::OTHER)
}
};
Ok(escape_value)
}
pub(super) fn take_unicode_escape_sequence<R>(
cursor: &mut Cursor<R>,
start_pos: Position,
) -> Result<u32, Error>
where
R: ReadChar,
{
// Support \u{X..X} (Unicode CodePoint)
if cursor.next_if(0x7B /* { */)? {
let mut code_point = 0u32;
let mut first_digit = true;
loop {
let pos = cursor.pos();
let Some(c) = cursor.next_char()? else {
return Err(Error::syntax(
"Unexpected end of file when looking for character }",
pos,
));
};
if c == 0x7D
/* } */
{
if first_digit {
return Err(Error::syntax(
"malformed Unicode character escape sequence",
start_pos,
));
}
break;
}
let Some(digit) = char::from_u32(c).and_then(|c| c.to_digit(16)) else {
return Err(Error::syntax(
"malformed Unicode character escape sequence",
start_pos,
));
};
code_point = (code_point << 4) | digit;
if code_point > 0x10_FFFF {
return Err(Error::syntax(
"Unicode codepoint must not be greater than 0x10FFFF in escape sequence",
start_pos,
));
}
first_digit = false;
}
Ok(code_point)
} else {
// Grammar: Hex4Digits
// Collect each character after \u e.g \uD83D will give "D83D"
let mut code_point = 0u32;
for _ in 0..4 {
let pos = cursor.pos();
let c = cursor
.next_char()?
.ok_or_else(|| Error::syntax("invalid Unicode escape sequence", pos))?;
let Some(digit) = char::from_u32(c).and_then(|c| c.to_digit(16)) else {
return Err(Error::syntax("invalid Unicode escape sequence", start_pos));
};
code_point = (code_point << 4) | digit;
}
Ok(code_point)
}
}
fn take_hex_escape_sequence<R>(
cursor: &mut Cursor<R>,
start_pos: Position,
) -> Result<u32, Error>
where
R: ReadChar,
{
let mut code_point = 0u32;
for _ in 0..2 {
let pos = cursor.pos();
let c = cursor
.next_char()?
.ok_or_else(|| Error::syntax("invalid Hexadecimal escape sequence", pos))?;
let Some(digit) = char::from_u32(c).and_then(|c| c.to_digit(16)) else {
return Err(Error::syntax(
"invalid Hexadecimal escape sequence",
start_pos,
));
};
code_point = (code_point << 4) | digit;
}
Ok(code_point)
}
fn take_legacy_octal_escape_sequence<R>(
cursor: &mut Cursor<R>,
init_byte: u8,
) -> Result<u32, Error>
where
R: ReadChar,
{
// Grammar: OctalDigit
let mut code_point = u32::from(init_byte - b'0');
// Grammar: ZeroToThree OctalDigit
// Grammar: FourToSeven OctalDigit
if let Some(c) = cursor.peek_char()?
&& (0x30..=0x37/* 0..=7 */).contains(&c)
{
cursor.next_char()?;
code_point = (code_point * 8) + c - 0x30 /* 0 */;
if (0x30..=0x33/* 0..=3 */).contains(&init_byte) {
// Grammar: ZeroToThree OctalDigit OctalDigit
if let Some(c) = cursor.peek_char()?
&& (0x30..=0x37/* 0..=7 */).contains(&c)
{
cursor.next_char()?;
code_point = (code_point * 8) + c - 0x30 /* 0 */;
}
}
}
Ok(code_point)
}
}