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
#![allow(clippy::needless_lifetimes)]
use super::lex::{skip_ellipsis, skip_word_markers, skip_ws_and_comments};
use super::number::parse_number_token;
use super::strings::parse_string_literal_concat_fast;
use crate::emit::{Emitter, JRResult};
use crate::options::Options;
use crate::parser::parse_regex_literal;
use crate::parser::parse_symbol_or_unquoted_string;
use memchr::memchr2;
pub fn parse_array<'i, E: Emitter>(
input: &mut &'i str,
opts: &Options,
out: &mut E,
logger: &mut crate::parser::Logger,
) -> JRResult<()> {
if !input.starts_with('[') {
return Ok(());
}
*input = &input[1..];
out.emit_char('[')?;
// Enter-array fast path: if only ASCII whitespace before a closing ']', close immediately.
if let Some(']') = fast_ws_to_only_rbracket(input) {
out.emit_char(']')?;
return Ok(());
}
skip_ws_and_comments(input, opts);
let mut first = true;
let mut idx = 0usize;
'outer: loop {
skip_ws_and_comments(input, opts);
if input.is_empty() {
// best-effort close
out.emit_char(']')?;
break;
}
// If we see a closing '}' here, best-effort close the array and let outer object handle it
if input.starts_with('}') {
out.emit_char(']')?;
break;
}
if input.starts_with(']') {
*input = &input[1..];
out.emit_char(']')?;
break;
}
if input.is_empty() {
break;
}
skip_word_markers(input, &opts.word_comment_markers);
while skip_ellipsis(input) {
skip_ws_and_comments(input, opts);
}
// optional comma between elements (fast path: ASCII ws -> ',' or ']')
if let Some(delim) = fast_ws_to_comma_or_rbracket(input) {
match delim {
',' => { /* consumed comma, proceed to parse next element */ }
']' => {
out.emit_char(']')?;
break;
}
_ => unreachable!(),
}
} else {
// After top-of-loop skipper, simply consume a stray comma or close if present
if input.starts_with(',') {
*input = &input[1..];
}
if input.starts_with(']') {
*input = &input[1..];
out.emit_char(']')?;
break;
}
}
// Look-ahead: if we see an immediately truncated object like "{]",
// drop the partial element and close the array (Python parity).
if input.starts_with('{') {
let mut look = &input[1..];
skip_ws_and_comments(&mut look, opts);
if let Some(stripped) = look.strip_prefix(']') {
*input = stripped;
out.emit_char(']')?;
break 'outer;
}
}
// emit comma only when we are going to output an element
if !first {
out.emit_char(',')?;
}
first = false;
// Pre-trim whitespace and ellipsis placeholders before parsing element
skip_ws_and_comments(input, opts);
while skip_ellipsis(input) {
skip_ws_and_comments(input, opts);
}
// Track array index for value path
logger.push_index(idx);
if input.is_empty() {
out.emit_char(']')?;
break;
}
let c = input.chars().next().unwrap();
match c {
'{' => super::object::parse_object(input, opts, out, logger)?,
'[' => parse_array(input, opts, out, logger)?,
'"' | '\'' => parse_string_literal_concat_fast(input, opts, out)?,
'/' => parse_regex_literal(input, opts, out)?,
c if c == '-' || c == '.' || c.is_ascii_digit() => {
parse_number_token(input, opts, out)?
}
_ => parse_symbol_or_unquoted_string(input, opts, out, logger)?,
}
logger.pop_index();
idx += 1;
// Fast path after element: ASCII ws -> next delimiter ',' or ']'
if let Some(delim) = fast_ws_to_comma_or_rbracket(input) {
match delim {
',' => {
continue 'outer;
}
']' => {
out.emit_char(']')?;
break 'outer;
}
_ => unreachable!(),
}
} else {
// Fallback: generic skipping and optional comma consumption
skip_ws_and_comments(input, opts);
if input.starts_with('}') {
out.emit_char(']')?;
break;
}
if input.starts_with(',') {
*input = &input[1..];
}
}
}
Ok(())
}
#[inline]
fn fast_ws_to_only_rbracket(input: &mut &str) -> Option<char> {
let s = *input;
if s.is_empty() {
return None;
}
let bytes = s.as_bytes();
if let Some(pos) = memchr2(b',', b']', bytes) {
// If we found a comma before ']', this isn't an immediate close; ignore.
if bytes[pos] == b',' {
return None;
}
// Ensure all bytes before ']' are ASCII whitespace only.
for &b in &bytes[..pos] {
match b {
b' ' | b'\t' | b'\n' | b'\r' => {}
_ => return None,
}
}
*input = &s[pos + 1..];
Some(']')
} else {
None
}
}
#[inline]
fn fast_ws_to_comma_or_rbracket(input: &mut &str) -> Option<char> {
let s = *input;
if s.is_empty() {
return None;
}
let bytes = s.as_bytes();
// Find next ',' or ']' quickly
if let Some(pos) = memchr2(b',', b']', bytes) {
// Ensure the prefix is only ASCII whitespace
for &b in &bytes[..pos] {
match b {
b' ' | b'\t' | b'\n' | b'\r' => {}
_ => return None,
}
}
let delim = bytes[pos] as char;
*input = &s[pos + 1..];
Some(delim)
} else {
// No delimiter ahead; nothing to do
None
}
}