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
//! String literal scanner for the JS lexer.
//!
//! These `impl<'a> JSLexer<'a>` methods live in a child module of `lexer`, so
//! they can access the private fields of `JSLexer` declared in `lexer/mod.rs`.
use crate::utf8::{
append_unicode_to_storage, is_utf8_start,
match_unicode_line_terminator_offset1, UTF8_LINE_TERMINATOR_CHAR0,
};
use super::{GrammarContext, JSLexer};
impl<'a> JSLexer<'a> {
/// Scan a string literal under the grammar context `grammar_context`. Port
/// of `JSLexer::scanStringInContext` (JSLexer.h:1029-1035): JSX-context
/// strings (`AllowJSXIdentifier`) decode `&`-HTML-entities, allow raw
/// newlines, and treat `\` as a literal character.
pub(crate) fn scan_string_in_context(&mut self, grammar_context: GrammarContext) {
if grammar_context == GrammarContext::AllowJSXIdentifier {
self.scan_string::<true>();
} else {
self.scan_string::<false>();
}
}
/// Scan a string literal (the cursor is on the opening quote). Port of
/// `JSLexer::scanString<JSX>` (JSLexer.cpp:1977-2126). When `JSX`, a raw
/// `\n`/`\r` is pushed to storage (not a non-terminated error), a `&` is
/// decoded as an HTML entity, and `\` is a literal character (no escape).
///
/// The C++ `template <bool JSX>` is preserved as the const generic `JSX`, so
/// each specialization folds the `JSX` checks away at compile time.
pub(crate) fn scan_string<const JSX: bool>(&mut self) {
debug_assert!(self.cursor.peek() == b'\'' || self.cursor.peek() == b'"');
let quote_ch = self.cursor.peek();
self.cursor.advance(1);
// Track whether we encounter any escapes or new line continuations. We
// need that information in order to detect directives.
let mut escapes = false;
self.tmp_storage.clear();
loop {
let c = self.cursor.peek();
if c == quote_ch {
self.cursor.advance(1);
break;
} else if !JSX && c == b'\\' {
escapes = true;
self.cursor.advance(1);
let e = self.cursor.peek();
match e {
b'\'' | b'"' | b'\\' => {
self.tmp_storage.push(e);
self.cursor.advance(1);
}
b'b' => {
self.cursor.advance(1);
self.tmp_storage.push(8);
}
b'f' => {
self.cursor.advance(1);
self.tmp_storage.push(12);
}
b'n' => {
self.cursor.advance(1);
self.tmp_storage.push(10);
}
b'r' => {
self.cursor.advance(1);
self.tmp_storage.push(13);
}
b't' => {
self.cursor.advance(1);
self.tmp_storage.push(9);
}
b'v' => {
self.cursor.advance(1);
self.tmp_storage.push(11);
}
0 => {
// EOF?
if self.cursor.at_end() {
// eof?
let loc = self.cur_loc();
self.error(loc, "non-terminated string");
let start = self.token.start_loc();
self.sm.note(start, "string started here");
break;
} else {
self.tmp_storage.push(e);
self.cursor.advance(1);
}
}
b'0' => {
// '\0' is not an octal so handle it separately.
if !(self.cursor.peek_at(1) >= b'0' && self.cursor.peek_at(1) <= b'7') {
self.cursor.advance(1);
append_unicode_to_storage(&mut self.tmp_storage, 0);
} else {
let v = self.consume_octal(3) as u32;
append_unicode_to_storage(&mut self.tmp_storage, v);
}
}
b'1' | b'2' | b'3' => {
let v = self.consume_octal(3) as u32;
append_unicode_to_storage(&mut self.tmp_storage, v);
}
b'4' | b'5' | b'6' | b'7' => {
let v = self.consume_octal(2) as u32;
append_unicode_to_storage(&mut self.tmp_storage, v);
}
b'x' => {
self.cursor.advance(1);
let v = self.consume_hex(2, true);
append_unicode_to_storage(&mut self.tmp_storage, v.unwrap_or(0));
}
b'u' => {
// Back up one so the cursor is on the '\\'.
self.cursor.seek(self.cursor.offset() - 1);
let cp = self.consume_unicode_escape();
append_unicode_to_storage(&mut self.tmp_storage, cp);
}
// Escaped line terminator. We just need to skip it.
b'\n' => {
self.cursor.advance(1);
}
b'\r' => {
self.cursor.advance(1);
if self.cursor.peek() == b'\n' {
// skip CR LF
self.cursor.advance(1);
}
}
UTF8_LINE_TERMINATOR_CHAR0 => {
if match_unicode_line_terminator_offset1(
&self.cursor.raw()[self.cursor.offset() as usize..],
) {
self.cursor.advance(3);
} else {
let cp = self.decode_utf8_advance();
append_unicode_to_storage(&mut self.tmp_storage, cp);
}
}
_ => {
if is_utf8_start(e) {
let cp = self.decode_utf8_advance();
append_unicode_to_storage(&mut self.tmp_storage, cp);
} else {
self.tmp_storage.push(e);
self.cursor.advance(1);
}
}
}
} else if c == b'\n' || c == b'\r' {
if JSX {
// A raw new line is allowed in a JSX string.
self.tmp_storage.push(c);
self.cursor.advance(1);
} else {
// A raw new line in a (non-JSX) string is not allowed.
let loc = self.cur_loc();
self.error(loc, "non-terminated string");
let start = self.token.start_loc();
self.sm.note(start, "string started here");
break;
}
} else if JSX && c == b'&' {
if let Some(code_point) = self.consume_html_entity_optional() {
append_unicode_to_storage(&mut self.tmp_storage, code_point);
} else {
self.tmp_storage.push(c);
self.cursor.advance(1);
}
} else if c == 0 && self.cursor.at_end() {
let loc = self.cur_loc();
self.error(loc, "non-terminated string");
let start = self.token.start_loc();
self.sm.note(start, "string started here");
break;
} else if is_utf8_start(c) {
// Decode and re-encode the character and append it to the string
// storage.
let cp = self.decode_utf8_advance();
append_unicode_to_storage(&mut self.tmp_storage, cp);
} else {
self.tmp_storage.push(c);
self.cursor.advance(1);
}
}
let atom = self.get_string_literal(self.tmp_storage.as_slice());
self.token.set_string_literal(atom, escapes);
}
}
#[cfg(test)]
mod tests {
use hermes_atom_table::AtomTable;
use hermes_support::manager::SourceErrorManager;
use super::super::{GrammarContext, JSLexer};
use crate::token_kinds::TokenKind;
/// Lex the first token of `src` (a string literal) in JSX context and return
/// its cooked value bytes.
fn jsx_str_cooked(src: &str) -> Vec<u8> {
let mut sm = SourceErrorManager::new();
let id = sm.add_buffer("t", src);
let tab = AtomTable::new();
let mut lex = JSLexer::new(id, &mut sm, &tab, GrammarContext::AllowJSXIdentifier);
let tok = lex.advance(GrammarContext::AllowJSXIdentifier);
assert_eq!(tok.kind(), TokenKind::string_literal);
tab.bytes(tok.get_string_literal()).to_vec()
}
#[test]
fn jsx_string() {
// In JSX context, '&' entities are decoded and raw newlines are allowed.
assert_eq!(jsx_str_cooked("\"a&b\""), b"a&b".to_vec());
assert_eq!(jsx_str_cooked("'x\ny'"), b"x\ny".to_vec());
assert_eq!(jsx_str_cooked("\"A\""), b"A".to_vec());
}
}