Coverage Report

Created: 2024-06-24 06:31

/src/njs/src/njs_parser.c
Line
Count
Source (jump to first uncovered line)
1
2
/*
3
 * Copyright (C) Igor Sysoev
4
 * Copyright (C) Alexander Borisov
5
 * Copyright (C) NGINX, Inc.
6
 */
7
8
9
#include <njs_main.h>
10
11
12
static njs_int_t njs_parser_scope_begin(njs_parser_t *parser, njs_scope_t type,
13
    njs_bool_t init_this);
14
static void njs_parser_scope_end(njs_parser_t *parser);
15
16
static njs_int_t njs_parser_check_error_state(njs_parser_t *parser,
17
    njs_lexer_token_t *token, njs_queue_link_t *current);
18
19
static njs_int_t njs_parser_primary_expression_test(njs_parser_t *parser,
20
    njs_lexer_token_t *token, njs_queue_link_t *current);
21
static njs_int_t njs_parser_regexp_literal(njs_parser_t *parser,
22
    njs_lexer_token_t *token, njs_queue_link_t *current);
23
static njs_int_t njs_parser_template_literal(njs_parser_t *parser,
24
    njs_lexer_token_t *token, njs_queue_link_t *current);
25
static njs_int_t njs_parser_template_literal_string(njs_parser_t *parser,
26
    njs_lexer_token_t *token, njs_queue_link_t *current);
27
static njs_int_t njs_parser_template_literal_expression(
28
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
29
static njs_int_t njs_parser_cover_parenthesized_expression(
30
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
31
static njs_int_t njs_parser_binding_identifier_pattern(njs_parser_t *parser,
32
    njs_lexer_token_t *token, njs_queue_link_t *current);
33
static njs_int_t njs_parser_cover_parenthesized_expression_after(
34
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
35
static njs_int_t njs_parser_cover_parenthesized_expression_end(
36
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
37
38
static njs_int_t njs_parser_array_literal(njs_parser_t *parser,
39
    njs_lexer_token_t *token, njs_queue_link_t *current);
40
static njs_int_t njs_parser_array_element_list(njs_parser_t *parser,
41
    njs_lexer_token_t *token, njs_queue_link_t *current);
42
static njs_int_t njs_parser_array_after(njs_parser_t *parser,
43
    njs_lexer_token_t *token, njs_queue_link_t *current);
44
static njs_int_t njs_parser_array_spread_element(njs_parser_t *parser,
45
    njs_lexer_token_t *token, njs_queue_link_t *current);
46
47
static njs_int_t njs_parser_object_literal(njs_parser_t *parser,
48
    njs_lexer_token_t *token, njs_queue_link_t *current);
49
static njs_int_t njs_parser_object_literal_after(njs_parser_t *parser,
50
    njs_lexer_token_t *token, njs_queue_link_t *current);
51
static njs_int_t njs_parser_property_definition_list(njs_parser_t *parser,
52
    njs_lexer_token_t *token, njs_queue_link_t *current);
53
static njs_int_t njs_parser_property_definition_list_after(
54
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
55
static njs_int_t njs_parser_property_definition(njs_parser_t *parser,
56
    njs_lexer_token_t *token, njs_queue_link_t *current);
57
static njs_int_t njs_parser_property_definition_after(njs_parser_t *parser,
58
    njs_lexer_token_t *token, njs_queue_link_t *current);
59
static njs_int_t njs_parser_computed_property_name_after(njs_parser_t *parser,
60
    njs_lexer_token_t *token, njs_queue_link_t *current);
61
static njs_int_t njs_parser_computed_property_async_after(njs_parser_t *parser,
62
    njs_lexer_token_t *token, njs_queue_link_t *current);
63
static njs_int_t njs_parser_computed_property_name_handler(njs_parser_t *parser,
64
    njs_lexer_token_t *token, njs_queue_link_t *current, njs_bool_t async);
65
66
static njs_int_t njs_parser_initializer(njs_parser_t *parser,
67
    njs_lexer_token_t *token, njs_queue_link_t *current);
68
static njs_int_t njs_parser_initializer_after(njs_parser_t *parser,
69
    njs_lexer_token_t *token, njs_queue_link_t *current);
70
static njs_int_t njs_parser_initializer_assign(njs_parser_t *parser,
71
    njs_token_type_t type);
72
73
static njs_int_t njs_parser_member_expression(njs_parser_t *parser,
74
    njs_lexer_token_t *token, njs_queue_link_t *current);
75
static njs_int_t njs_parser_member_expression_next(njs_parser_t *parser,
76
    njs_lexer_token_t *token, njs_queue_link_t *current);
77
static njs_int_t njs_parser_member_expression_bracket(njs_parser_t *parser,
78
    njs_lexer_token_t *token, njs_queue_link_t *current);
79
static njs_int_t njs_parser_member_expression_new_next(njs_parser_t *parser,
80
    njs_lexer_token_t *token, njs_queue_link_t *current);
81
static njs_int_t njs_parser_super_property(njs_parser_t *parser,
82
    njs_lexer_token_t *token, njs_queue_link_t *current);
83
static njs_int_t njs_parser_member_expression_import(njs_parser_t *parser,
84
    njs_lexer_token_t *token, njs_queue_link_t *current);
85
static njs_int_t njs_parser_member_expression_new(njs_parser_t *parser,
86
    njs_lexer_token_t *token, njs_queue_link_t *current);
87
static njs_int_t njs_parser_member_expression_new_after(njs_parser_t *parser,
88
    njs_lexer_token_t *token, njs_queue_link_t *current);
89
static njs_int_t njs_parser_member_expression_new_args(njs_parser_t *parser,
90
    njs_lexer_token_t *token, njs_queue_link_t *current);
91
92
static njs_parser_node_t *njs_parser_create_call(njs_parser_t *parser,
93
    njs_parser_node_t *node, uint8_t ctor);
94
static njs_int_t njs_parser_call_expression(njs_parser_t *parser,
95
    njs_lexer_token_t *token, njs_queue_link_t *current);
96
static njs_int_t njs_parser_call_expression_args(njs_parser_t *parser,
97
    njs_lexer_token_t *token, njs_queue_link_t *current);
98
static njs_int_t njs_parser_call_expression_after(njs_parser_t *parser,
99
    njs_lexer_token_t *token, njs_queue_link_t *current);
100
static njs_int_t njs_parser_arguments(njs_parser_t *parser,
101
    njs_lexer_token_t *token, njs_queue_link_t *current);
102
static njs_int_t njs_parser_parenthesis_or_comma(njs_parser_t *parser,
103
    njs_lexer_token_t *token, njs_queue_link_t *current);
104
static njs_int_t njs_parser_argument_list(njs_parser_t *parser,
105
    njs_lexer_token_t *token, njs_queue_link_t *current);
106
static njs_int_t njs_parser_argument_list_after(njs_parser_t *parser,
107
    njs_lexer_token_t *token, njs_queue_link_t *current);
108
static njs_int_t njs_parser_optional_expression_after(njs_parser_t *parser,
109
    njs_lexer_token_t *token, njs_queue_link_t *current);
110
static njs_int_t njs_parser_optional_chain(njs_parser_t *parser,
111
    njs_lexer_token_t *token, njs_queue_link_t *current);
112
static njs_int_t njs_parser_optional_chain_after(njs_parser_t *parser,
113
    njs_lexer_token_t *token, njs_queue_link_t *current);
114
static njs_int_t njs_parser_new_expression(njs_parser_t *parser,
115
    njs_lexer_token_t *token, njs_queue_link_t *current);
116
static njs_int_t njs_parser_new_expression_after(njs_parser_t *parser,
117
    njs_lexer_token_t *token, njs_queue_link_t *current);
118
static njs_int_t njs_parser_left_hand_side_expression(njs_parser_t *parser,
119
    njs_lexer_token_t *token, njs_queue_link_t *current);
120
static njs_int_t njs_parser_left_hand_side_expression_after(
121
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
122
static njs_int_t njs_parser_left_hand_side_expression_node(
123
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
124
static njs_int_t njs_parser_left_hand_side_expression_optional(
125
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
126
static njs_int_t njs_parser_update_expression(njs_parser_t *parser,
127
    njs_lexer_token_t *token, njs_queue_link_t *current);
128
static njs_int_t njs_parser_update_expression_post(njs_parser_t *parser,
129
    njs_lexer_token_t *token, njs_queue_link_t *current);
130
static njs_int_t njs_parser_update_expression_unary(njs_parser_t *parser,
131
    njs_lexer_token_t *token, njs_queue_link_t *current);
132
133
static njs_int_t njs_parser_unary_expression(njs_parser_t *parser,
134
    njs_lexer_token_t *token, njs_queue_link_t *current);
135
static njs_int_t njs_parser_unary_expression_after(njs_parser_t *parser,
136
    njs_lexer_token_t *token, njs_queue_link_t *current);
137
static njs_int_t njs_parser_unary_expression_next(njs_parser_t *parser,
138
    njs_lexer_token_t *token, njs_queue_link_t *current);
139
static njs_int_t njs_parser_await(njs_parser_t *parser,
140
    njs_lexer_token_t *token, njs_queue_link_t *current);
141
static njs_int_t njs_parser_await_after(njs_parser_t *parser,
142
    njs_lexer_token_t *token, njs_queue_link_t *current);
143
144
static njs_int_t njs_parser_exponentiation_expression(njs_parser_t *parser,
145
    njs_lexer_token_t *token, njs_queue_link_t *current);
146
static njs_int_t njs_parser_exponentiation_expression_match(
147
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
148
149
static njs_int_t njs_parser_multiplicative_expression(njs_parser_t *parser,
150
    njs_lexer_token_t *token, njs_queue_link_t *current);
151
static njs_int_t njs_parser_multiplicative_expression_match(
152
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
153
154
static njs_int_t njs_parser_additive_expression(njs_parser_t *parser,
155
    njs_lexer_token_t *token, njs_queue_link_t *current);
156
static njs_int_t njs_parser_additive_expression_match(njs_parser_t *parser,
157
    njs_lexer_token_t *token, njs_queue_link_t *current);
158
159
static njs_int_t njs_parser_shift_expression(njs_parser_t *parser,
160
    njs_lexer_token_t *token, njs_queue_link_t *current);
161
static njs_int_t njs_parser_shift_expression_match(njs_parser_t *parser,
162
    njs_lexer_token_t *token, njs_queue_link_t *current);
163
164
static njs_int_t njs_parser_relational_expression(njs_parser_t *parser,
165
    njs_lexer_token_t *token, njs_queue_link_t *current);
166
static njs_int_t njs_parser_relational_expression_match(njs_parser_t *parser,
167
    njs_lexer_token_t *token, njs_queue_link_t *current);
168
169
static njs_int_t njs_parser_equality_expression(njs_parser_t *parser,
170
    njs_lexer_token_t *token, njs_queue_link_t *current);
171
static njs_int_t njs_parser_equality_expression_match(njs_parser_t *parser,
172
    njs_lexer_token_t *token, njs_queue_link_t *current);
173
174
static njs_int_t njs_parser_bitwise_AND_expression(njs_parser_t *parser,
175
    njs_lexer_token_t *token, njs_queue_link_t *current);
176
static njs_int_t njs_parser_bitwise_AND_expression_and(njs_parser_t *parser,
177
    njs_lexer_token_t *token, njs_queue_link_t *current);
178
static njs_int_t njs_parser_bitwise_XOR_expression(njs_parser_t *parser,
179
    njs_lexer_token_t *token, njs_queue_link_t *current);
180
static njs_int_t njs_parser_bitwise_XOR_expression_xor(njs_parser_t *parser,
181
    njs_lexer_token_t *token, njs_queue_link_t *current);
182
static njs_int_t njs_parser_bitwise_OR_expression(njs_parser_t *parser,
183
    njs_lexer_token_t *token, njs_queue_link_t *current);
184
static njs_int_t njs_parser_bitwise_OR_expression_or(njs_parser_t *parser,
185
    njs_lexer_token_t *token, njs_queue_link_t *current);
186
187
static njs_int_t njs_parser_logical_AND_expression(njs_parser_t *parser,
188
    njs_lexer_token_t *token, njs_queue_link_t *current);
189
static njs_int_t njs_parser_logical_AND_expression_and(njs_parser_t *parser,
190
    njs_lexer_token_t *token, njs_queue_link_t *current);
191
static njs_int_t njs_parser_logical_OR_expression(njs_parser_t *parser,
192
    njs_lexer_token_t *token, njs_queue_link_t *current);
193
static njs_int_t njs_parser_logical_OR_expression_or(njs_parser_t *parser,
194
    njs_lexer_token_t *token, njs_queue_link_t *current);
195
static njs_int_t njs_parser_coalesce_expression(njs_parser_t *parser,
196
    njs_lexer_token_t *token, njs_queue_link_t *current);
197
static njs_int_t njs_parser_short_circuit_expression(njs_parser_t *parser,
198
    njs_lexer_token_t *token, njs_queue_link_t *current);
199
200
static njs_int_t njs_parser_conditional_expression(njs_parser_t *parser,
201
    njs_lexer_token_t *token, njs_queue_link_t *current);
202
static njs_int_t njs_parser_conditional_question_mark(njs_parser_t *parser,
203
    njs_lexer_token_t *token, njs_queue_link_t *current);
204
static njs_int_t njs_parser_conditional_colon(njs_parser_t *parser,
205
    njs_lexer_token_t *token, njs_queue_link_t *current);
206
static njs_int_t njs_parser_conditional_colon_after(njs_parser_t *parser,
207
    njs_lexer_token_t *token, njs_queue_link_t *current);
208
209
static njs_int_t njs_parser_assignment_expression(njs_parser_t *parser,
210
    njs_lexer_token_t *token, njs_queue_link_t *current);
211
static njs_int_t njs_parser_match_arrow_expression(njs_parser_t *parser,
212
    njs_lexer_token_t *token);
213
static njs_int_t njs_parser_assignment_expression_after(njs_parser_t *parser,
214
    njs_lexer_token_t *token, njs_queue_link_t *current);
215
static njs_int_t njs_parser_assignment_operator(njs_parser_t *parser,
216
    njs_lexer_token_t *token, njs_queue_link_t *current);
217
static njs_int_t njs_parser_assignment_operator_after(njs_parser_t *parser,
218
    njs_lexer_token_t *token, njs_queue_link_t *current);
219
220
static njs_int_t njs_parser_expression(njs_parser_t *parser,
221
    njs_lexer_token_t *token, njs_queue_link_t *current);
222
static njs_int_t njs_parser_expression_comma(njs_parser_t *parser,
223
    njs_lexer_token_t *token, njs_queue_link_t *current);
224
225
static njs_int_t njs_parser_statement(njs_parser_t *parser,
226
    njs_lexer_token_t *token, njs_queue_link_t *current);
227
static njs_int_t njs_parser_statement_wo_node(njs_parser_t *parser,
228
    njs_lexer_token_t *token, njs_queue_link_t *current);
229
static njs_int_t njs_parser_statement_after(njs_parser_t *parser,
230
    njs_lexer_token_t *token, njs_queue_link_t *current);
231
static njs_int_t njs_parser_declaration(njs_parser_t *parser,
232
    njs_lexer_token_t *token, njs_queue_link_t *current);
233
static njs_int_t njs_parser_hoistable_declaration(njs_parser_t *parser,
234
    njs_lexer_token_t *token, njs_queue_link_t *current);
235
236
static njs_int_t njs_parser_block_statement(njs_parser_t *parser,
237
    njs_lexer_token_t *token, njs_queue_link_t *current);
238
static njs_int_t njs_parser_block_statement_open_brace(njs_parser_t *parser,
239
    njs_lexer_token_t *token, njs_queue_link_t *current);
240
static njs_int_t njs_parser_block_statement_close_brace(njs_parser_t *parser,
241
    njs_lexer_token_t *token, njs_queue_link_t *current);
242
static njs_int_t njs_parser_statement_list(njs_parser_t *parser,
243
    njs_lexer_token_t *token, njs_queue_link_t *current);
244
static njs_int_t njs_parser_statement_list_next(njs_parser_t *parser,
245
    njs_lexer_token_t *token, njs_queue_link_t *current);
246
static njs_int_t njs_parser_statement_list_item(njs_parser_t *parser,
247
    njs_lexer_token_t *token, njs_queue_link_t *current);
248
249
static njs_int_t njs_parser_lexical_declaration(njs_parser_t *parser,
250
    njs_lexer_token_t *token, njs_queue_link_t *current);
251
static njs_int_t njs_parser_variable_statement(njs_parser_t *parser,
252
    njs_lexer_token_t *token, njs_queue_link_t *current);
253
static njs_int_t njs_parser_variable_declaration_list(njs_parser_t *parser,
254
    njs_lexer_token_t *token, njs_queue_link_t *current);
255
static njs_int_t njs_parser_variable_declaration_list_next(
256
    njs_parser_t *parser, njs_lexer_token_t *token, njs_queue_link_t *current);
257
static njs_int_t njs_parser_variable_declaration(njs_parser_t *parser,
258
    njs_lexer_token_t *token, njs_queue_link_t *current);
259
260
static njs_int_t njs_parser_binding_pattern(njs_parser_t *parser,
261
    njs_lexer_token_t *token, njs_queue_link_t *current);
262
static njs_int_t njs_parser_object_binding_pattern(njs_parser_t *parser,
263
    njs_lexer_token_t *token, njs_queue_link_t *current);
264
static njs_int_t njs_parser_array_binding_pattern(njs_parser_t *parser,
265
    njs_lexer_token_t *token, njs_queue_link_t *current);
266
267
static njs_int_t njs_parser_expression_statement(njs_parser_t *parser,
268
    njs_lexer_token_t *token, njs_queue_link_t *current);
269
static njs_int_t njs_parser_expression_statement_after(njs_parser_t *parser,
270
    njs_lexer_token_t *token, njs_queue_link_t *current);
271
272
static njs_int_t njs_parser_if_statement(njs_parser_t *parser,
273
    njs_lexer_token_t *token, njs_queue_link_t *current);
274
static njs_int_t njs_parser_if_close_parenthesis(njs_parser_t *parser,
275
    njs_lexer_token_t *token, njs_queue_link_t *current);
276
static njs_int_t njs_parser_else_statement(njs_parser_t *parser,
277
    njs_lexer_token_t *token, njs_queue_link_t *current);
278
static njs_int_t njs_parser_else_statement_after(njs_parser_t *parser,
279
    njs_lexer_token_t *token, njs_queue_link_t *current);
280
281
static njs_int_t njs_parser_iteration_statement_do(njs_parser_t *parser,
282
    njs_lexer_token_t *token, njs_queue_link_t *current);
283
static njs_int_t njs_parser_iteration_statement_do_while(njs_parser_t *parser,
284
    njs_lexer_token_t *token, njs_queue_link_t *current);
285
static njs_int_t njs_parser_do_while_semicolon(njs_parser_t *parser,
286
    njs_lexer_token_t *token, njs_queue_link_t *current);
287
288
static njs_int_t njs_parser_iteration_statement_while(njs_parser_t *parser,
289
    njs_lexer_token_t *token, njs_queue_link_t *current);
290
static njs_int_t njs_parser_while_statement(njs_parser_t *parser,
291
    njs_lexer_token_t *token, njs_queue_link_t *current);
292
static njs_int_t njs_parser_while_after(njs_parser_t *parser,
293
    njs_lexer_token_t *token, njs_queue_link_t *current);
294
295
static njs_int_t njs_parser_iteration_statement_for(njs_parser_t *parser,
296
    njs_lexer_token_t *token, njs_queue_link_t *current);
297
static njs_int_t njs_parser_for_expression_map_continue(
298
    njs_parser_t *parser, njs_lexer_token_t *token,
299
    njs_queue_link_t *current);
300
static njs_int_t njs_parser_for_expression_map_reparse(
301
    njs_parser_t *parser, njs_lexer_token_t *token,
302
    njs_queue_link_t *current);
303
static njs_int_t njs_parser_expression_continue_op(njs_parser_t *parser,
304
    njs_lexer_token_t *token, njs_queue_link_t *current);
305
static njs_int_t njs_parser_expression_continue_assign_comma(
306
    njs_parser_t *parser, njs_lexer_token_t *token,
307
    njs_queue_link_t *current);
308
static njs_int_t njs_parser_for_in_statement_statement(njs_parser_t *parser,
309
    njs_lexer_token_t *token, njs_queue_link_t *current);
310
static njs_int_t njs_parser_iteration_statement_for_map(njs_parser_t *parser,
311
    njs_lexer_token_t *token, njs_queue_link_t *current);
312
static njs_int_t njs_parser_for_var_binding_or_var_list(njs_parser_t *parser,
313
    njs_lexer_token_t *token, njs_queue_link_t *current,
314
    njs_token_type_t token_type);
315
static njs_int_t njs_parser_for_var_in_statement(njs_parser_t *parser,
316
    njs_lexer_token_t *token, njs_queue_link_t *current);
317
static njs_int_t njs_parser_for_var_in_statement_after(njs_parser_t *parser,
318
    njs_lexer_token_t *token, njs_queue_link_t *current);
319
static njs_int_t njs_parser_for_var_in_of_expression(njs_parser_t *parser,
320
    njs_lexer_token_t *token, njs_queue_link_t *current);
321
static njs_int_t njs_parser_for_in_statement(njs_parser_t *parser,
322
    njs_lexer_token_t *token, njs_queue_link_t *current);
323
static njs_int_t njs_parser_for_in_statement_after(njs_parser_t *parser,
324
    njs_lexer_token_t *token, njs_queue_link_t *current);
325
static njs_int_t njs_parser_for_expression(njs_parser_t *parser,
326
    njs_lexer_token_t *token, njs_queue_link_t *current);
327
static njs_int_t njs_parser_for_expression_end(njs_parser_t *parser,
328
    njs_lexer_token_t *token, njs_queue_link_t *current);
329
static njs_int_t njs_parser_for_end(njs_parser_t *parser,
330
    njs_lexer_token_t *token, njs_queue_link_t *current);
331
332
static njs_int_t njs_parser_switch_statement(njs_parser_t *parser,
333
    njs_lexer_token_t *token, njs_queue_link_t *current);
334
static njs_int_t njs_parser_switch_statement_after(njs_parser_t *parser,
335
    njs_lexer_token_t *token, njs_queue_link_t *current);
336
static njs_int_t njs_parser_switch_block(njs_parser_t *parser,
337
    njs_lexer_token_t *token, njs_queue_link_t *current);
338
static njs_int_t njs_parser_switch_block_after(njs_parser_t *parser,
339
    njs_lexer_token_t *token, njs_queue_link_t *current);
340
static njs_int_t njs_parser_switch_case(njs_parser_t *parser,
341
    njs_lexer_token_t *token, njs_queue_link_t *current);
342
static njs_int_t njs_parser_switch_case_wo_def(njs_parser_t *parser,
343
    njs_lexer_token_t *token, njs_queue_link_t *current);
344
static njs_int_t njs_parser_switch_case_def(njs_parser_t *parser,
345
    njs_lexer_token_t *token, njs_queue_link_t *current,
346
    njs_bool_t with_default);
347
static njs_int_t njs_parser_switch_case_after(njs_parser_t *parser,
348
    njs_lexer_token_t *token, njs_queue_link_t *current);
349
static njs_int_t njs_parser_switch_case_block(njs_parser_t *parser,
350
    njs_lexer_token_t *token, njs_queue_link_t *current);
351
static njs_int_t njs_parser_switch_case_after_wo_def(njs_parser_t *parser,
352
    njs_lexer_token_t *token, njs_queue_link_t *current);
353
static njs_int_t njs_parser_switch_case_block_wo_def(njs_parser_t *parser,
354
    njs_lexer_token_t *token, njs_queue_link_t *current);
355
356
static njs_int_t njs_parser_continue_statement(njs_parser_t *parser,
357
    njs_lexer_token_t *token, njs_queue_link_t *current);
358
359
static njs_int_t njs_parser_break_statement(njs_parser_t *parser,
360
    njs_lexer_token_t *token, njs_queue_link_t *current);
361
362
static njs_int_t njs_parser_break_continue(njs_parser_t *parser,
363
    njs_lexer_token_t *token, njs_token_type_t type);
364
365
static njs_int_t njs_parser_return_statement(njs_parser_t *parser,
366
    njs_lexer_token_t *token, njs_queue_link_t *current);
367
static njs_int_t njs_parser_return_statement_after(njs_parser_t *parser,
368
    njs_lexer_token_t *token, njs_queue_link_t *current);
369
370
static njs_int_t njs_parser_with_statement(njs_parser_t *parser,
371
    njs_lexer_token_t *token, njs_queue_link_t *current);
372
373
static njs_int_t njs_parser_labelled_statement(njs_parser_t *parser,
374
    njs_lexer_token_t *token, njs_queue_link_t *current);
375
static njs_int_t njs_parser_labelled_statement_after(njs_parser_t *parser,
376
    njs_lexer_token_t *token, njs_queue_link_t *current);
377
378
static njs_int_t njs_parser_throw_statement(njs_parser_t *parser,
379
    njs_lexer_token_t *token, njs_queue_link_t *current);
380
static njs_int_t njs_parser_throw_statement_after(njs_parser_t *parser,
381
    njs_lexer_token_t *token, njs_queue_link_t *current);
382
383
static njs_int_t njs_parser_try_statement(njs_parser_t *parser,
384
    njs_lexer_token_t *token, njs_queue_link_t *current);
385
static njs_int_t njs_parser_catch_or_finally(njs_parser_t *parser,
386
    njs_lexer_token_t *token, njs_queue_link_t *current);
387
static njs_int_t njs_parser_catch_after(njs_parser_t *parser,
388
    njs_lexer_token_t *token, njs_queue_link_t *current);
389
static njs_int_t njs_parser_catch_parenthesis(njs_parser_t *parser,
390
    njs_lexer_token_t *token, njs_queue_link_t *current);
391
static njs_int_t njs_parser_catch_statement_open_brace(njs_parser_t *parser,
392
    njs_lexer_token_t *token, njs_queue_link_t *current);
393
static njs_int_t njs_parser_catch_finally(njs_parser_t *parser,
394
    njs_lexer_token_t *token, njs_queue_link_t *current);
395
396
static njs_int_t njs_parser_debugger_statement(njs_parser_t *parser,
397
    njs_lexer_token_t *token, njs_queue_link_t *current);
398
399
static njs_int_t njs_parser_function_declaration(njs_parser_t *parser,
400
    njs_lexer_token_t *token, njs_queue_link_t *current);
401
static njs_int_t njs_parser_function_declaration_after(njs_parser_t *parser,
402
    njs_lexer_token_t *token, njs_queue_link_t *current);
403
static njs_int_t njs_parser_function_parse(njs_parser_t *parser,
404
    njs_lexer_token_t *token, njs_queue_link_t *current);
405
static njs_int_t njs_parser_function_expression(njs_parser_t *parser,
406
    njs_lexer_token_t *token, njs_queue_link_t *current);
407
static njs_int_t njs_parser_function_expression_after(njs_parser_t *parser,
408
    njs_lexer_token_t *token, njs_queue_link_t *current);
409
static njs_int_t njs_parser_unique_formal_parameters(njs_parser_t *parser,
410
    njs_lexer_token_t *token, njs_queue_link_t *current);
411
static njs_int_t njs_parser_formal_parameters(njs_parser_t *parser,
412
    njs_lexer_token_t *token, njs_queue_link_t *current);
413
static njs_int_t njs_parser_formal_parameters_after(njs_parser_t *parser,
414
    njs_lexer_token_t *token, njs_queue_link_t *current);
415
416
static njs_int_t njs_parser_arrow_function(njs_parser_t *parser,
417
    njs_lexer_token_t *token, njs_queue_link_t *current);
418
static njs_int_t njs_parser_arrow_function_args_after(njs_parser_t *parser,
419
    njs_lexer_token_t *token, njs_queue_link_t *current);
420
static njs_int_t njs_parser_arrow_function_arrow(njs_parser_t *parser,
421
    njs_lexer_token_t *token, njs_queue_link_t *current);
422
static njs_int_t njs_parser_arrow_function_body_after(njs_parser_t *parser,
423
    njs_lexer_token_t *token, njs_queue_link_t *current);
424
425
static njs_int_t njs_parser_method_definition(njs_parser_t *parser,
426
    njs_lexer_token_t *token, njs_queue_link_t *current);
427
static njs_int_t njs_parser_get_set(njs_parser_t *parser,
428
    njs_lexer_token_t *token, njs_queue_link_t *current);
429
static njs_int_t njs_parser_get_set_after(njs_parser_t *parser,
430
    njs_lexer_token_t *token, njs_queue_link_t *current);
431
static njs_int_t njs_parser_get_after(njs_parser_t *parser,
432
    njs_lexer_token_t *token, njs_queue_link_t *current);
433
static njs_int_t njs_parser_set_after(njs_parser_t *parser,
434
    njs_lexer_token_t *token, njs_queue_link_t *current);
435
static njs_int_t njs_parser_function_lambda(njs_parser_t *parser,
436
    njs_lexer_token_t *token, njs_queue_link_t *current);
437
static njs_int_t njs_parser_function_lambda_args_after(njs_parser_t *parser,
438
    njs_lexer_token_t *token, njs_queue_link_t *current);
439
static njs_int_t njs_parser_function_lambda_body_after(njs_parser_t *parser,
440
    njs_lexer_token_t *token, njs_queue_link_t *current);
441
442
static njs_int_t njs_parser_export(njs_parser_t *parser,
443
    njs_lexer_token_t *token, njs_queue_link_t *current);
444
static njs_int_t njs_parser_export_after(njs_parser_t *parser,
445
    njs_lexer_token_t *token, njs_queue_link_t *current);
446
447
static njs_int_t njs_parser_import(njs_parser_t *parser,
448
    njs_lexer_token_t *token, njs_queue_link_t *current);
449
static njs_int_t njs_parser_export_sink(njs_parser_t *parser);
450
451
static njs_parser_node_t *njs_parser_return_set(njs_parser_t *parser,
452
    njs_parser_node_t *expr);
453
static njs_parser_node_t *njs_parser_variable_node(njs_parser_t *parser,
454
    uintptr_t unique_id, njs_variable_type_t type, njs_variable_t  **retvar);
455
456
static njs_parser_node_t *njs_parser_reference(njs_parser_t *parser,
457
    njs_lexer_token_t *token);
458
459
static njs_parser_node_t *njs_parser_argument(njs_parser_t *parser,
460
    njs_parser_node_t *expr, njs_index_t index);
461
462
static njs_int_t njs_parser_object_property(njs_parser_t *parser,
463
    njs_parser_node_t *parent, njs_parser_node_t *property,
464
    njs_parser_node_t *value, njs_bool_t proto_init);
465
static njs_int_t njs_parser_property_accessor(njs_parser_t *parser,
466
    njs_parser_node_t *parent, njs_parser_node_t *property,
467
    njs_parser_node_t *value, njs_token_type_t accessor);
468
static njs_int_t njs_parser_array_item(njs_parser_t *parser,
469
    njs_parser_node_t *array, njs_parser_node_t *value);
470
static njs_int_t njs_parser_template_string(njs_parser_t *parser,
471
    njs_lexer_token_t *token);
472
static njs_token_type_t njs_parser_escape_string_create(njs_parser_t *parser,
473
    njs_lexer_token_t *token, njs_value_t *value);
474
static njs_int_t njs_parser_escape_string_calc_length(njs_parser_t *parser,
475
    njs_lexer_token_t *token, size_t *out_size, size_t *out_length);
476
477
static void njs_parser_serialize_tree(njs_chb_t *chain,
478
    njs_parser_node_t *node, njs_int_t *ret, size_t indent);
479
static njs_int_t njs_parser_serialize_node(njs_chb_t *chain,
480
    njs_parser_node_t *node);
481
482
483
#define njs_parser_chain_top(parser)                                          \
484
63.4k
    ((parser)->scope->top)
485
486
487
#define njs_parser_chain_top_set(parser, node)                                \
488
410k
    (parser)->scope->top = node
489
490
491
njs_inline njs_int_t
492
njs_parser_not_supported(njs_parser_t *parser, njs_lexer_token_t *token)
493
0
{
494
0
    if (token->type != NJS_TOKEN_END) {
495
0
        njs_parser_syntax_error(parser, "Token \"%V\" not supported "
496
0
                                "in this version", &token->text);
497
0
    } else {
498
0
        njs_parser_syntax_error(parser, "Not supported in this version");
499
0
    }
500
501
0
    return NJS_DONE;
502
0
}
503
504
505
njs_inline njs_int_t
506
njs_parser_reject(njs_parser_t *parser)
507
12
{
508
12
    njs_queue_link_t          *link;
509
12
    njs_parser_stack_entry_t  *entry;
510
511
514
    while (!njs_queue_is_empty(&parser->stack)) {
512
514
        entry = njs_queue_link_data(njs_queue_first(&parser->stack),
513
514
                                    njs_parser_stack_entry_t, link);
514
515
514
        link = njs_queue_first(&parser->stack);
516
514
        njs_queue_remove(link);
517
518
514
        if (!entry->optional) {
519
12
            njs_parser_next(parser, entry->state);
520
12
            parser->target = entry->node;
521
522
12
            return NJS_DECLINED;
523
12
        }
524
514
    }
525
526
0
    return njs_parser_failed(parser);
527
12
}
528
529
530
njs_int_t
531
njs_parser_init(njs_vm_t *vm, njs_parser_t *parser, njs_parser_scope_t *scope,
532
    njs_str_t *file, u_char *start, u_char *end, njs_uint_t runtime)
533
16.7k
{
534
16.7k
    njs_lexer_t  *lexer;
535
536
16.7k
    njs_memzero(parser, sizeof(njs_parser_t));
537
538
16.7k
    parser->scope = scope;
539
540
16.7k
    lexer = &parser->lexer0;
541
16.7k
    parser->lexer = lexer;
542
543
16.7k
    parser->use_lhs = 0;
544
545
16.7k
    return njs_lexer_init(vm, lexer, file, start, end, runtime, 0);
546
16.7k
}
547
548
549
njs_int_t
550
njs_parser(njs_vm_t *vm, njs_parser_t *parser)
551
16.7k
{
552
16.7k
    njs_int_t                        ret;
553
16.7k
    njs_str_t                        str;
554
16.7k
    njs_lexer_token_t                *token;
555
16.7k
    const njs_lexer_keyword_entry_t  *keyword;
556
557
16.7k
    parser->vm = vm;
558
559
16.7k
    njs_set_invalid(&vm->exception);
560
561
16.7k
    if (parser->scope == NULL) {
562
16.7k
        ret = njs_parser_scope_begin(parser,
563
16.7k
                                     parser->module ? NJS_SCOPE_FUNCTION
564
16.7k
                                                    : NJS_SCOPE_GLOBAL, 1);
565
16.7k
        if (njs_slow_path(ret != NJS_OK)) {
566
0
            return NJS_ERROR;
567
0
        }
568
569
16.7k
    } else {
570
0
        parser->scope->top = NULL;
571
0
        parser->node = NULL;
572
0
        parser->ret = NJS_OK;
573
0
    }
574
575
    /* Add this as first variable. */
576
16.7k
    njs_string_get(&njs_string_undefined, &str);
577
578
16.7k
    keyword = njs_lexer_keyword(str.start, str.length);
579
16.7k
    if (njs_slow_path(keyword == NULL)) {
580
0
        return NJS_ERROR;
581
0
    }
582
583
16.7k
    parser->undefined_id = (uintptr_t) keyword->value;
584
585
16.7k
    njs_queue_init(&parser->stack);
586
587
16.7k
    parser->target = NULL;
588
16.7k
    njs_parser_next(parser, njs_parser_statement_list);
589
590
16.7k
    ret = njs_parser_after(parser, njs_queue_first(&parser->stack),
591
16.7k
                           NULL, 0, njs_parser_check_error_state);
592
16.7k
    if (ret != NJS_OK) {
593
0
        return ret;
594
0
    }
595
596
123M
    do {
597
123M
        token = njs_lexer_token(parser->lexer, 0);
598
123M
        if (njs_slow_path(token == NULL)) {
599
0
            return NJS_ERROR;
600
0
        }
601
602
123M
        parser->ret = parser->state(parser, token,
603
123M
                                    njs_queue_first(&parser->stack));
604
605
123M
    } while (parser->ret != NJS_DONE && parser->ret != NJS_ERROR);
606
607
16.7k
    if (parser->ret != NJS_DONE) {
608
0
        return NJS_ERROR;
609
0
    }
610
611
16.7k
    if (njs_is_error(&vm->exception)) {
612
10.4k
        return NJS_ERROR;
613
10.4k
    }
614
615
6.24k
    if (parser->node == NULL) {
616
        /* Empty string, just semicolons or variables declarations. */
617
618
0
        parser->node = njs_parser_node_new(parser, 0);
619
0
        if (njs_slow_path(parser->node == NULL)) {
620
0
            return NJS_ERROR;
621
0
        }
622
0
    }
623
624
6.24k
    if (parser->module) {
625
0
        ret = njs_parser_export_sink(parser);
626
0
        if (ret != NJS_OK) {
627
0
            return NJS_ERROR;
628
0
        }
629
630
6.24k
    } else {
631
6.24k
        parser->node->token_type = NJS_TOKEN_END;
632
6.24k
        parser->node->token_line = parser->lexer->line;
633
634
6.24k
        njs_parser_chain_top_set(parser, parser->node);
635
6.24k
    }
636
637
6.24k
    return NJS_OK;
638
6.24k
}
639
640
641
static njs_int_t
642
njs_parser_check_error_state(njs_parser_t *parser, njs_lexer_token_t *token,
643
    njs_queue_link_t *current)
644
3
{
645
3
    return njs_parser_failed(parser);
646
3
}
647
648
649
njs_int_t
650
njs_parser_failed_state(njs_parser_t *parser, njs_lexer_token_t *token,
651
    njs_queue_link_t *current)
652
7.33k
{
653
7.33k
    if (token->type != NJS_TOKEN_END) {
654
7.33k
        njs_parser_syntax_error(parser, "Unexpected token \"%V\"",
655
7.33k
                                &token->text);
656
7.33k
    } else {
657
7
        njs_parser_syntax_error(parser, "Unexpected end of input");
658
7
    }
659
660
7.33k
    return NJS_DONE;
661
7.33k
}
662
663
664
static njs_int_t
665
njs_parser_scope_begin(njs_parser_t *parser, njs_scope_t type,
666
    njs_bool_t init_this)
667
108k
{
668
108k
    njs_variable_t                   *var;
669
108k
    njs_parser_scope_t               *scope, *parent;
670
108k
    const njs_lexer_keyword_entry_t  *keyword;
671
672
108k
    static const njs_str_t  njs_this_str = njs_str("this");
673
674
108k
    scope = njs_mp_zalloc(parser->vm->mem_pool, sizeof(njs_parser_scope_t));
675
108k
    if (njs_slow_path(scope == NULL)) {
676
0
        return NJS_ERROR;
677
0
    }
678
679
108k
    scope->type = type;
680
681
108k
    njs_rbtree_init(&scope->variables, njs_parser_scope_rbtree_compare);
682
108k
    njs_rbtree_init(&scope->labels, njs_parser_scope_rbtree_compare);
683
108k
    njs_rbtree_init(&scope->references, njs_parser_scope_rbtree_compare);
684
685
108k
    parent = parser->scope;
686
108k
    scope->parent = parent;
687
108k
    parser->scope = scope;
688
689
108k
    if (type == NJS_SCOPE_FUNCTION || type == NJS_SCOPE_GLOBAL) {
690
80.0k
        if (init_this) {
691
            /* Add this as first variable. */
692
45.8k
            keyword = njs_lexer_keyword(njs_this_str.start,
693
45.8k
                                        njs_this_str.length);
694
45.8k
            if (njs_slow_path(keyword == NULL)) {
695
0
                return NJS_ERROR;
696
0
            }
697
698
45.8k
            var = njs_variable_add(parser, scope, (uintptr_t) keyword->value,
699
45.8k
                                   NJS_VARIABLE_VAR);
700
45.8k
            if (njs_slow_path(var == NULL)) {
701
0
                return NJS_ERROR;
702
0
            }
703
704
45.8k
            var->index = njs_scope_index(type, 0, NJS_LEVEL_LOCAL,
705
45.8k
                                         NJS_VARIABLE_VAR);
706
45.8k
        }
707
80.0k
    }
708
709
108k
    scope->items = 1;
710
711
108k
    return NJS_OK;
712
108k
}
713
714
715
static void
716
njs_parser_scope_end(njs_parser_t *parser)
717
73.0k
{
718
73.0k
    njs_parser_scope_t  *scope, *parent;
719
720
73.0k
    scope = parser->scope;
721
722
73.0k
    parent = scope->parent;
723
73.0k
    parser->scope = parent;
724
73.0k
}
725
726
727
intptr_t
728
njs_parser_scope_rbtree_compare(njs_rbtree_node_t *node1,
729
    njs_rbtree_node_t *node2)
730
5.48M
{
731
5.48M
    njs_variable_node_t  *lex_node1, *lex_node2;
732
733
5.48M
    lex_node1 = (njs_variable_node_t *) node1;
734
5.48M
    lex_node2 = (njs_variable_node_t *) node2;
735
736
5.48M
    if (lex_node1->key < lex_node2->key) {
737
1.70M
        return -1;
738
1.70M
    }
739
740
3.77M
    if (lex_node1->key > lex_node2->key) {
741
3.03M
        return 1;
742
3.03M
    }
743
744
742k
    return 0;
745
3.77M
}
746
747
748
static njs_int_t
749
njs_parser_generator_expression(njs_parser_t *parser, njs_lexer_token_t *token,
750
    njs_queue_link_t *current)
751
0
{
752
0
    return njs_parser_not_supported(parser, token);
753
0
}
754
755
756
static njs_int_t
757
njs_parser_class_expression(njs_parser_t *parser, njs_lexer_token_t *token,
758
    njs_queue_link_t *current)
759
0
{
760
0
    return njs_parser_not_supported(parser, token);
761
0
}
762
763
764
static njs_int_t
765
njs_parser_async_generator_expression(njs_parser_t *parser,
766
    njs_lexer_token_t *token, njs_queue_link_t *current)
767
0
{
768
0
    return njs_parser_not_supported(parser, token);
769
0
}
770
771
772
static njs_int_t
773
njs_parser_generator_declaration(njs_parser_t *parser, njs_lexer_token_t *token,
774
    njs_queue_link_t *current)
775
0
{
776
0
    return njs_parser_not_supported(parser, token);
777
0
}
778
779
780
static njs_int_t
781
njs_parser_class_declaration(njs_parser_t *parser, njs_lexer_token_t *token,
782
    njs_queue_link_t *current)
783
0
{
784
0
    return njs_parser_not_supported(parser, token);
785
0
}
786
787
788
static njs_int_t
789
njs_parser_function_or_generator_handler(njs_parser_t *parser,
790
    njs_lexer_token_t *token, njs_queue_link_t *current, njs_bool_t is_async)
791
234
{
792
234
    njs_parser_node_t  *node, *cur;
793
794
234
    cur = parser->node;
795
796
234
    if (token->type == NJS_TOKEN_MULTIPLICATION) {
797
0
        njs_lexer_consume_token(parser->lexer, 1);
798
0
        njs_parser_next(parser, njs_parser_generator_declaration);
799
800
234
    } else {
801
234
        if (is_async) {
802
0
            node = njs_parser_node_new(parser,
803
0
                                       NJS_TOKEN_ASYNC_FUNCTION_DECLARATION);
804
234
        } else {
805
234
            node = njs_parser_node_new(parser, NJS_TOKEN_FUNCTION_DECLARATION);
806
234
        }
807
808
234
        if (node == NULL) {
809
0
            return NJS_ERROR;
810
0
        }
811
812
234
        node->token_line = token->line;
813
234
        parser->node = node;
814
815
234
        njs_lexer_consume_token(parser->lexer, 1);
816
234
        njs_parser_next(parser, njs_parser_function_declaration);
817
234
    }
818
819
234
    return njs_parser_after(parser, current, cur, 1,
820
234
                            njs_parser_statement_after);
821
234
}
822
823
824
static njs_int_t
825
njs_parser_function_or_generator(njs_parser_t *parser,
826
    njs_lexer_token_t *token, njs_queue_link_t *current)
827
391k
{
828
391k
    if (token->type != NJS_TOKEN_FUNCTION) {
829
391k
        return NJS_DECLINED;
830
391k
    }
831
832
234
    return njs_parser_function_or_generator_handler(parser, token, current, 0);
833
391k
}
834
835
836
static njs_int_t
837
njs_parser_async_function_or_generator(njs_parser_t *parser,
838
    njs_lexer_token_t *token, njs_queue_link_t *current)
839
391k
{
840
391k
    if (token->type != NJS_TOKEN_ASYNC) {
841
391k
        return NJS_DECLINED;
842
391k
    }
843
844
0
    token = njs_lexer_peek_token(parser->lexer, token, 1);
845
0
    if (token == NULL) {
846
0
        return NJS_ERROR;
847
0
    }
848
849
0
    if (token->type != NJS_TOKEN_FUNCTION) {
850
0
        return NJS_DECLINED;
851
0
    }
852
853
0
    njs_lexer_consume_token(parser->lexer, 1);
854
855
0
    return njs_parser_function_or_generator_handler(parser, token, current, 1);
856
0
}
857
858
859
njs_inline njs_int_t
860
njs_parser_expect_semicolon(njs_parser_t *parser, njs_lexer_token_t *token)
861
339k
{
862
339k
    if (token->type != NJS_TOKEN_SEMICOLON) {
863
311k
        if (parser->strict_semicolon
864
311k
            || (token->type != NJS_TOKEN_END
865
311k
                && token->type != NJS_TOKEN_CLOSE_BRACE
866
311k
                && parser->lexer->prev_type != NJS_TOKEN_LINE_END))
867
9
        {
868
9
            return NJS_DECLINED;
869
9
        }
870
871
311k
        return NJS_OK;
872
311k
    }
873
874
27.8k
    njs_lexer_consume_token(parser->lexer, 1);
875
876
27.8k
    return NJS_OK;
877
339k
}
878
879
880
static njs_int_t
881
njs_parser_semicolon(njs_parser_t *parser, njs_lexer_token_t *token,
882
    njs_queue_link_t *current)
883
6.52k
{
884
6.52k
    if (njs_parser_expect_semicolon(parser, token) != NJS_OK) {
885
0
        return njs_parser_failed(parser);
886
0
    }
887
888
6.52k
    return njs_parser_stack_pop(parser);
889
6.52k
}
890
891
892
static njs_int_t
893
njs_parser_close_bracked(njs_parser_t *parser, njs_lexer_token_t *token,
894
    njs_queue_link_t *current)
895
0
{
896
0
    if (token->type != NJS_TOKEN_CLOSE_BRACKET) {
897
0
        return njs_parser_failed(parser);
898
0
    }
899
900
0
    njs_lexer_consume_token(parser->lexer, 1);
901
902
0
    return njs_parser_stack_pop(parser);
903
0
}
904
905
906
static njs_int_t
907
njs_parser_close_parenthesis(njs_parser_t *parser, njs_lexer_token_t *token,
908
    njs_queue_link_t *current)
909
10.7k
{
910
10.7k
    if (parser->ret != NJS_OK) {
911
5
        return njs_parser_failed(parser);
912
5
    }
913
914
10.7k
    if (token->type != NJS_TOKEN_CLOSE_PARENTHESIS) {
915
0
        return njs_parser_failed(parser);
916
0
    }
917
918
10.7k
    njs_lexer_consume_token(parser->lexer, 1);
919
920
10.7k
    return njs_parser_stack_pop(parser);
921
10.7k
}
922
923
924
static njs_int_t
925
njs_parser_expression_parenthesis(njs_parser_t *parser,
926
    njs_lexer_token_t *token, njs_queue_link_t *current)
927
2.14k
{
928
2.14k
    if (token->type != NJS_TOKEN_OPEN_PARENTHESIS) {
929
0
        return njs_parser_failed(parser);
930
0
    }
931
932
2.14k
    njs_lexer_consume_token(parser->lexer, 1);
933
934
2.14k
    parser->node = NULL;
935
936
2.14k
    njs_parser_next(parser, njs_parser_expression);
937
938
2.14k
    return njs_parser_after(parser, current, NULL, 0,
939
2.14k
                            njs_parser_close_parenthesis);
940
2.14k
}
941
942
943
static njs_int_t
944
njs_parser_iteration_statement_for_end(njs_parser_t *parser,
945
    njs_lexer_token_t *token, njs_queue_link_t *current)
946
10.4k
{
947
10.4k
    parser->node->token_line = (uint32_t) (uintptr_t) parser->target;
948
10.4k
    parser->target = NULL;
949
950
10.4k
    njs_parser_scope_end(parser);
951
952
10.4k
    return njs_parser_stack_pop(parser);
953
10.4k
}
954
955
956
/*
957
 * 12.2 Primary Expression.
958
 */
959
static njs_int_t
960
njs_parser_primary_expression_test(njs_parser_t *parser,
961
    njs_lexer_token_t *token, njs_queue_link_t *current)
962
3.30M
{
963
3.30M
    njs_int_t          ret;
964
3.30M
    njs_lexer_token_t  *next;
965
3.30M
    njs_parser_node_t  *node;
966
967
3.30M
    switch (token->type) {
968
    /* IdentifierReference */
969
3.26k
    case NJS_TOKEN_THIS:
970
3.36k
    case NJS_TOKEN_NULL:
971
448k
    case NJS_TOKEN_NAME:
972
448k
    case NJS_TOKEN_YIELD:
973
448k
    case NJS_TOKEN_AWAIT:
974
448k
        goto reference;
975
976
173
    case NJS_TOKEN_TRUE:
977
173
        node = njs_parser_node_new(parser, token->type);
978
173
        if (node == NULL) {
979
0
            return NJS_ERROR;
980
0
        }
981
982
173
        node->u.value = njs_value_true;
983
173
        node->token_line = token->line;
984
985
173
        parser->node = node;
986
173
        goto done;
987
988
1.53k
    case NJS_TOKEN_FALSE:
989
1.53k
        node = njs_parser_node_new(parser, token->type);
990
1.53k
        if (node == NULL) {
991
0
            return NJS_ERROR;
992
0
        }
993
994
1.53k
        node->u.value = njs_value_false;
995
1.53k
        node->token_line = token->line;
996
997
1.53k
        parser->node = node;
998
1.53k
        goto done;
999
1000
2.75M
    case NJS_TOKEN_NUMBER:
1001
2.75M
        node = njs_parser_node_new(parser, NJS_TOKEN_NUMBER);
1002
2.75M
        if (node == NULL) {
1003
0
            return NJS_ERROR;
1004
0
        }
1005
1006
2.75M
        njs_set_number(&node->u.value, token->number);
1007
2.75M
        node->token_line = token->line;
1008
1009
2.75M
        parser->node = node;
1010
2.75M
        goto done;
1011
1012
4.47k
    case NJS_TOKEN_STRING:
1013
4.47k
        node = njs_parser_node_new(parser, NJS_TOKEN_STRING);
1014
4.47k
        if (node == NULL) {
1015
0
            return NJS_ERROR;
1016
0
        }
1017
1018
4.47k
        node->token_line = token->line;
1019
1020
4.47k
        ret = njs_parser_string_create(parser->vm, token, &node->u.value);
1021
4.47k
        if (ret != NJS_OK) {
1022
0
            return NJS_ERROR;
1023
0
        }
1024
1025
4.47k
        parser->node = node;
1026
4.47k
        goto done;
1027
1028
487
    case NJS_TOKEN_ESCAPE_STRING:
1029
        /* Internal optimization. This is not in the specification. */
1030
1031
487
        node = njs_parser_node_new(parser, NJS_TOKEN_STRING);
1032
487
        if (node == NULL) {
1033
0
            return NJS_ERROR;
1034
0
        }
1035
1036
487
        node->token_line = token->line;
1037
1038
487
        ret = njs_parser_escape_string_create(parser, token, &node->u.value);
1039
487
        if (ret != NJS_TOKEN_STRING) {
1040
0
            return NJS_ERROR;
1041
0
        }
1042
1043
487
        parser->node = node;
1044
487
        goto done;
1045
1046
0
    case NJS_TOKEN_UNTERMINATED_STRING:
1047
        /* Internal optimization. This is not in the specification. */
1048
1049
0
        njs_parser_syntax_error(parser, "Unterminated string \"%V\"",
1050
0
                                &token->text);
1051
0
        return NJS_ERROR;
1052
1053
    /* ArrayLiteral */
1054
12.9k
    case NJS_TOKEN_OPEN_BRACKET:
1055
12.9k
        node = njs_parser_node_new(parser, NJS_TOKEN_ARRAY);
1056
12.9k
        if (node == NULL) {
1057
0
            return NJS_ERROR;
1058
0
        }
1059
1060
12.9k
        node->token_line = token->line;
1061
12.9k
        parser->node = node;
1062
1063
12.9k
        njs_parser_next(parser, njs_parser_array_literal);
1064
12.9k
        break;
1065
1066
    /* ObjectLiteral */
1067
6.07k
    case NJS_TOKEN_OPEN_BRACE:
1068
6.07k
        node = njs_parser_node_new(parser, NJS_TOKEN_OBJECT);
1069
6.07k
        if (node == NULL) {
1070
0
            return NJS_ERROR;
1071
0
        }
1072
1073
6.07k
        node->token_line = token->line;
1074
6.07k
        parser->node = node;
1075
1076
6.07k
        njs_parser_next(parser, njs_parser_object_literal);
1077
6.07k
        break;
1078
1079
    /* FunctionExpression */
1080
19.5k
    case NJS_TOKEN_FUNCTION:
1081
19.5k
        token = njs_lexer_peek_token(parser->lexer, token, 0);
1082
19.5k
        if (token == NULL) {
1083
0
            return NJS_ERROR;
1084
0
        }
1085
1086
        /* GeneratorExpression */
1087
19.5k
        if (token->type == NJS_TOKEN_MULTIPLICATION) {
1088
0
            njs_parser_next(parser, njs_parser_generator_expression);
1089
1090
19.5k
        } else {
1091
19.5k
            node = njs_parser_node_new(parser, NJS_TOKEN_FUNCTION_EXPRESSION);
1092
19.5k
            if (node == NULL) {
1093
0
                return NJS_ERROR;
1094
0
            }
1095
1096
19.5k
            node->token_line = token->line;
1097
19.5k
            parser->node = node;
1098
1099
19.5k
            njs_parser_next(parser, njs_parser_function_expression);
1100
19.5k
        }
1101
1102
19.5k
        break;
1103
1104
    /* ClassExpression */
1105
19.5k
    case NJS_TOKEN_CLASS:
1106
0
        njs_parser_next(parser, njs_parser_class_expression);
1107
0
        return NJS_OK;
1108
1109
    /* AsyncFunctionExpression */
1110
0
    case NJS_TOKEN_ASYNC:
1111
0
        next = njs_lexer_peek_token(parser->lexer, token, 1);
1112
0
        if (next == NULL) {
1113
0
            return NJS_ERROR;
1114
0
        }
1115
1116
0
        if (next->type != NJS_TOKEN_FUNCTION) {
1117
0
            goto reference;
1118
0
        }
1119
1120
0
        njs_lexer_consume_token(parser->lexer, 1);
1121
1122
0
        next = njs_lexer_peek_token(parser->lexer, next, 0);
1123
0
        if (njs_slow_path(next == NULL)) {
1124
0
            return NJS_ERROR;
1125
0
        }
1126
1127
        /* GeneratorExpression */
1128
0
        if (next->type == NJS_TOKEN_MULTIPLICATION) {
1129
0
            njs_parser_next(parser, njs_parser_async_generator_expression);
1130
1131
0
        } else {
1132
0
            node = njs_parser_node_new(parser,
1133
0
                                       NJS_TOKEN_ASYNC_FUNCTION_EXPRESSION);
1134
0
            if (node == NULL) {
1135
0
                return NJS_ERROR;
1136
0
            }
1137
1138
0
            node->token_line = next->line;
1139
0
            parser->node = node;
1140
1141
0
            njs_parser_next(parser, njs_parser_function_expression);
1142
0
        }
1143
1144
0
        break;
1145
1146
    /* RegularExpressionLiteral */
1147
30.2k
    case NJS_TOKEN_DIVISION:
1148
30.2k
    case NJS_TOKEN_DIVISION_ASSIGNMENT:
1149
30.2k
        node = njs_parser_node_new(parser, NJS_TOKEN_REGEXP);
1150
30.2k
        if (node == NULL) {
1151
0
            return NJS_ERROR;
1152
0
        }
1153
1154
30.2k
        node->token_line = token->line;
1155
30.2k
        parser->node = node;
1156
1157
30.2k
        ret = njs_parser_regexp_literal(parser, token, current);
1158
30.2k
        if (ret != NJS_OK) {
1159
3.12k
            return NJS_ERROR;
1160
3.12k
        }
1161
1162
27.0k
        goto done;
1163
1164
    /* TemplateLiteral */
1165
27.0k
    case NJS_TOKEN_GRAVE:
1166
6.98k
        node = njs_parser_node_new(parser, NJS_TOKEN_TEMPLATE_LITERAL);
1167
6.98k
        if (node == NULL) {
1168
0
            return NJS_ERROR;
1169
0
        }
1170
1171
6.98k
        node->token_line = token->line;
1172
6.98k
        parser->node = node;
1173
1174
6.98k
        njs_parser_next(parser, njs_parser_template_literal);
1175
6.98k
        return NJS_OK;
1176
1177
    /* CoverParenthesizedExpressionAndArrowParameterList */
1178
19.0k
    case NJS_TOKEN_OPEN_PARENTHESIS:
1179
19.0k
        njs_lexer_consume_token(parser->lexer, 1);
1180
1181
        /* TODO: By specification. */
1182
19.0k
        (void) njs_parser_cover_parenthesized_expression;
1183
1184
19.0k
        parser->node = NULL;
1185
1186
19.0k
        njs_parser_next(parser, njs_parser_expression);
1187
1188
19.0k
        return njs_parser_after(parser, current, NULL, 0,
1189
6.98k
                                njs_parser_close_parenthesis);
1190
1191
2.90k
    default:
1192
2.90k
        if (njs_lexer_token_is_identifier_reference(token)) {
1193
2.90k
            goto reference;
1194
2.90k
        }
1195
1196
6
        return njs_parser_reject(parser);
1197
3.30M
    }
1198
1199
38.6k
    njs_lexer_consume_token(parser->lexer, 1);
1200
1201
38.6k
    return NJS_OK;
1202
1203
451k
reference:
1204
1205
451k
    node = njs_parser_reference(parser, token);
1206
451k
    if (node == NULL) {
1207
0
        return NJS_ERROR;
1208
0
    }
1209
1210
451k
    node->token_line = token->line;
1211
451k
    parser->node = node;
1212
1213
3.23M
done:
1214
1215
3.23M
    njs_lexer_consume_token(parser->lexer, 1);
1216
1217
3.23M
    return NJS_DONE;
1218
451k
}
1219
1220
1221
static njs_int_t
1222
njs_parser_regexp_literal(njs_parser_t *parser, njs_lexer_token_t *token,
1223
    njs_queue_link_t *current)
1224
30.2k
{
1225
30.2k
    u_char                *p;
1226
30.2k
    njs_str_t             text;
1227
30.2k
    njs_int_t             ret;
1228
30.2k
    njs_lexer_t           *lexer;
1229
30.2k
    njs_value_t           *value, retval;
1230
30.2k
    njs_regex_flags_t     flags;
1231
30.2k
    njs_regexp_pattern_t  *pattern;
1232
1233
30.2k
    static const njs_value_t  string_message = njs_string("message");
1234
1235
30.2k
    value = &parser->node->u.value;
1236
30.2k
    lexer = parser->lexer;
1237
1238
30.2k
    if (token->type == NJS_TOKEN_DIVISION_ASSIGNMENT) {
1239
4
        lexer->start--;
1240
4
    }
1241
1242
5.73M
    for (p = lexer->start; p < lexer->end; p++) {
1243
1244
5.73M
        switch (*p) {
1245
0
        case '\n':
1246
0
        case '\r':
1247
0
            goto failed;
1248
1249
3.95k
        case '[':
1250
2.50M
            while (1) {
1251
2.50M
                if (++p >= lexer->end) {
1252
0
                    goto failed;
1253
0
                }
1254
1255
2.50M
                if (*p == ']') {
1256
3.43k
                    break;
1257
3.43k
                }
1258
1259
2.50M
                switch (*p) {
1260
0
                case '\n':
1261
514
                case '\r':
1262
514
                    goto failed;
1263
1264
529
                case '\\':
1265
529
                    if (++p >= lexer->end || *p == '\n' || *p == '\r') {
1266
0
                        goto failed;
1267
0
                    }
1268
1269
529
                    break;
1270
2.50M
                }
1271
2.50M
            }
1272
1273
3.43k
            break;
1274
1275
8.38k
        case '\\':
1276
8.38k
            if (++p >= lexer->end || *p == '\n' || *p == '\r') {
1277
0
                goto failed;
1278
0
            }
1279
1280
8.38k
            break;
1281
1282
29.6k
        case '/':
1283
29.6k
            text.start = lexer->start;
1284
29.6k
            text.length = p - text.start;
1285
29.6k
            p++;
1286
29.6k
            lexer->start = p;
1287
1288
29.6k
            flags = njs_regexp_flags(&p, lexer->end);
1289
1290
29.6k
            if (njs_slow_path(flags < 0)) {
1291
0
                njs_parser_syntax_error(parser, "Invalid RegExp flags \"%*s\"",
1292
0
                                        p - lexer->start, lexer->start);
1293
1294
0
                return NJS_ERROR;
1295
0
            }
1296
1297
29.6k
            lexer->start = p;
1298
1299
29.6k
            pattern = njs_regexp_pattern_create(parser->vm, text.start,
1300
29.6k
                                                text.length, flags);
1301
1302
29.6k
            if (njs_slow_path(pattern == NULL)) {
1303
2.60k
                retval = njs_vm_exception(parser->vm);
1304
2.60k
                ret = njs_value_property(parser->vm, &retval,
1305
2.60k
                                         njs_value_arg(&string_message),
1306
2.60k
                                         &retval);
1307
2.60k
                if (njs_slow_path(ret != NJS_OK)) {
1308
0
                    return NJS_ERROR;
1309
0
                }
1310
1311
2.60k
                njs_string_get(&retval, &text);
1312
1313
2.60k
                njs_parser_syntax_error(parser, "%V", &text);
1314
1315
2.60k
                return NJS_ERROR;
1316
2.60k
            }
1317
1318
27.0k
            value->data.u.data = pattern;
1319
1320
27.0k
            return NJS_OK;
1321
5.73M
        }
1322
5.73M
    }
1323
1324
519
failed:
1325
1326
519
    njs_parser_syntax_error(parser, "Unterminated RegExp \"%*s\"",
1327
519
                            p - (lexer->start - 1), lexer->start - 1);
1328
519
    return NJS_ERROR;
1329
30.2k
}
1330
1331
1332
static njs_int_t
1333
njs_parser_template_literal(njs_parser_t *parser, njs_lexer_token_t *token,
1334
    njs_queue_link_t *current)
1335
247k
{
1336
247k
    njs_index_t        index;
1337
247k
    njs_parser_node_t  *node, *array, *template, *temp;
1338
1339
247k
    temp = njs_parser_node_new(parser, 0);
1340
247k
    if (temp == NULL) {
1341
0
        return NJS_ERROR;
1342
0
    }
1343
1344
247k
    array = njs_parser_node_new(parser, NJS_TOKEN_ARRAY);
1345
247k
    if (array == NULL) {
1346
0
        return NJS_ERROR;
1347
0
    }
1348
1349
247k
    array->token_line = token->line;
1350
1351
247k
    template = parser->node;
1352
1353
247k
    index = njs_scope_temp_index(template->scope);
1354
247k
    if (njs_slow_path(index == NJS_INDEX_ERROR)) {
1355
0
        return NJS_ERROR;
1356
0
    }
1357
1358
247k
    if (template->token_type != NJS_TOKEN_TEMPLATE_LITERAL) {
1359
240k
        node = njs_parser_argument(parser, array, index);
1360
240k
        if (node == NULL) {
1361
0
            return NJS_ERROR;
1362
0
        }
1363
1364
240k
        node->temporary = 1;
1365
1366
240k
        template->right = node;
1367
240k
        temp->right = node;
1368
1369
240k
        index = njs_scope_temp_index(template->scope);
1370
240k
        if (njs_slow_path(index == NJS_INDEX_ERROR)) {
1371
0
            return NJS_ERROR;
1372
0
        }
1373
1374
240k
    } else {
1375
6.98k
        template->left = array;
1376
6.98k
        temp->right = template;
1377
6.98k
    }
1378
1379
247k
    temp->temporary = 1;
1380
247k
    temp->left = template;
1381
247k
    temp->index = index;
1382
1383
247k
    parser->target = temp;
1384
1385
247k
    token->text.start++;
1386
247k
    token->text.length = 0;
1387
1388
247k
    njs_parser_next(parser, njs_parser_template_literal_string);
1389
1390
247k
    return NJS_OK;
1391
247k
}
1392
1393
1394
static njs_int_t
1395
njs_parser_template_literal_string(njs_parser_t *parser,
1396
    njs_lexer_token_t *token, njs_queue_link_t *current)
1397
256k
{
1398
256k
    njs_int_t          ret, ret_item;
1399
256k
    njs_parser_node_t  *template;
1400
1401
256k
    template = parser->target->left;
1402
1403
256k
    ret = njs_parser_template_string(parser, token);
1404
256k
    if (ret == NJS_ERROR) {
1405
2
        njs_parser_syntax_error(parser, "Unterminated template literal");
1406
2
        return NJS_DONE;
1407
2
    }
1408
1409
256k
    if (template->token_type != NJS_TOKEN_TEMPLATE_LITERAL) {
1410
245k
        ret_item = njs_parser_array_item(parser, template->right->left,
1411
245k
                                         parser->node);
1412
1413
245k
    } else {
1414
11.0k
        ret_item = njs_parser_array_item(parser, template->left, parser->node);
1415
11.0k
    }
1416
1417
256k
    if (ret_item != NJS_OK) {
1418
0
        return NJS_ERROR;
1419
0
    }
1420
1421
256k
    if (ret == NJS_DONE) {
1422
247k
        parser->node = template;
1423
1424
247k
        njs_parser_node_free(parser, parser->target);
1425
247k
        njs_lexer_consume_token(parser->lexer, 1);
1426
1427
247k
        return njs_parser_stack_pop(parser);
1428
247k
    }
1429
1430
9.57k
    parser->node = NULL;
1431
1432
9.57k
    njs_parser_next(parser, njs_parser_expression);
1433
1434
9.57k
    njs_lexer_consume_token(parser->lexer, 1);
1435
1436
9.57k
    return njs_parser_after(parser, current, parser->target, 0,
1437
256k
                            njs_parser_template_literal_expression);
1438
256k
}
1439
1440
1441
static njs_int_t
1442
njs_parser_template_literal_expression(njs_parser_t *parser,
1443
    njs_lexer_token_t *token, njs_queue_link_t *current)
1444
9.57k
{
1445
9.57k
    njs_int_t          ret;
1446
9.57k
    njs_parser_node_t  *template, *node, *parent;
1447
1448
9.57k
    if (parser->ret != NJS_OK) {
1449
0
        return njs_parser_failed(parser);
1450
0
    }
1451
1452
9.57k
    if (token->type != NJS_TOKEN_CLOSE_BRACE) {
1453
0
        njs_parser_syntax_error(parser, "Missing \"}\" in template expression");
1454
0
        return NJS_DONE;
1455
0
    }
1456
1457
9.57k
    parent = parser->target->right;
1458
9.57k
    template = parser->target->left;
1459
1460
9.57k
    if (template->token_type != NJS_TOKEN_TEMPLATE_LITERAL) {
1461
5.53k
        node = njs_parser_argument(parser, parser->node, parser->target->index);
1462
5.53k
        if (node == NULL) {
1463
0
            return NJS_ERROR;
1464
0
        }
1465
1466
5.53k
        parent->right = node;
1467
5.53k
        parent = node;
1468
1469
5.53k
        parser->target->index = njs_scope_temp_index(node->scope);
1470
5.53k
        if (njs_slow_path(parser->target->index == NJS_INDEX_ERROR)) {
1471
0
            return NJS_ERROR;
1472
0
        }
1473
1474
5.53k
    } else {
1475
4.04k
        ret = njs_parser_array_item(parser, template->left, parser->node);
1476
4.04k
        if (ret != NJS_OK) {
1477
0
            return NJS_ERROR;
1478
0
        }
1479
4.04k
    }
1480
1481
9.57k
    parser->target->right = parent;
1482
1483
9.57k
    parser->node = NULL;
1484
1485
9.57k
    njs_parser_next(parser, njs_parser_template_literal_string);
1486
1487
9.57k
    token->text.length = 0;
1488
9.57k
    token->text.start += 1;
1489
1490
9.57k
    return NJS_OK;
1491
9.57k
}
1492
1493
1494
static njs_int_t
1495
njs_parser_cover_parenthesized_expression(njs_parser_t *parser,
1496
    njs_lexer_token_t *token, njs_queue_link_t *current)
1497
0
{
1498
0
    switch (token->type) {
1499
0
    case NJS_TOKEN_CLOSE_PARENTHESIS:
1500
0
        (void) njs_parser_stack_pop(parser);
1501
0
        break;
1502
1503
0
    case NJS_TOKEN_ELLIPSIS:
1504
0
        njs_parser_next(parser, njs_parser_binding_identifier_pattern);
1505
0
        break;
1506
1507
0
    default:
1508
0
        parser->node = NULL;
1509
1510
0
        njs_parser_next(parser, njs_parser_expression);
1511
1512
0
        return njs_parser_after(parser, current, NULL, 0,
1513
0
                               njs_parser_cover_parenthesized_expression_after);
1514
0
    }
1515
1516
0
    njs_lexer_consume_token(parser->lexer, 1);
1517
1518
0
    return NJS_OK;
1519
0
}
1520
1521
1522
static njs_int_t
1523
njs_parser_binding_identifier_pattern(njs_parser_t *parser,
1524
    njs_lexer_token_t *token, njs_queue_link_t *current)
1525
0
{
1526
    /*
1527
     * BindingIdentifier )
1528
     * BindingPattern )
1529
     */
1530
1531
0
    switch (token->type) {
1532
1533
    /* BindingIdentifier */
1534
0
    case NJS_TOKEN_NAME:
1535
0
        njs_parser_next(parser, njs_parser_cover_parenthesized_expression_end);
1536
0
        break;
1537
1538
0
    case NJS_TOKEN_YIELD:
1539
0
        njs_parser_next(parser, njs_parser_cover_parenthesized_expression_end);
1540
0
        break;
1541
1542
0
    case NJS_TOKEN_AWAIT:
1543
0
        njs_parser_next(parser, njs_parser_cover_parenthesized_expression_end);
1544
0
        break;
1545
1546
    /* BindingPattern */
1547
0
    case NJS_TOKEN_OPEN_BRACKET:
1548
0
        njs_parser_next(parser, njs_parser_array_binding_pattern);
1549
1550
0
        njs_lexer_consume_token(parser->lexer, 1);
1551
0
        return njs_parser_after(parser, current, NULL, 0,
1552
0
                                njs_parser_cover_parenthesized_expression_end);
1553
1554
0
    case NJS_TOKEN_OPEN_BRACE:
1555
0
        njs_parser_next(parser, njs_parser_object_binding_pattern);
1556
1557
0
        njs_lexer_consume_token(parser->lexer, 1);
1558
0
        return njs_parser_after(parser, current, NULL, 0,
1559
0
                                njs_parser_cover_parenthesized_expression_end);
1560
1561
0
    default:
1562
0
        return NJS_ERROR;
1563
0
    }
1564
1565
0
    njs_lexer_consume_token(parser->lexer, 1);
1566
1567
0
    return NJS_OK;
1568
0
}
1569
1570
1571
static njs_int_t
1572
njs_parser_cover_parenthesized_expression_after(njs_parser_t *parser,
1573
    njs_lexer_token_t *token, njs_queue_link_t *current)
1574
0
{
1575
    /*
1576
     * )
1577
     * ,)
1578
     * , ... BindingIdentifier )
1579
     * , ... BindingPattern )
1580
     */
1581
1582
0
    if (token->type == NJS_TOKEN_CLOSE_PARENTHESIS) {
1583
0
        goto shift_stack;
1584
0
    }
1585
1586
0
    if (token->type != NJS_TOKEN_COMMA) {
1587
0
        return njs_parser_failed(parser);
1588
0
    }
1589
1590
0
    njs_lexer_consume_token(parser->lexer, 1);
1591
1592
0
    token = njs_lexer_token(parser->lexer, 0);
1593
0
    if (njs_slow_path(token == NULL)) {
1594
0
        return NJS_ERROR;
1595
0
    }
1596
1597
0
    if (token->type == NJS_TOKEN_CLOSE_PARENTHESIS) {
1598
0
        goto shift_stack;
1599
0
    }
1600
1601
0
    if(token->type != NJS_TOKEN_ELLIPSIS) {
1602
0
        return njs_parser_failed(parser);
1603
0
    }
1604
1605
0
    njs_lexer_consume_token(parser->lexer, 1);
1606
1607
0
    njs_parser_next(parser, njs_parser_binding_identifier_pattern);
1608
1609
0
    return NJS_OK;
1610
1611
0
shift_stack:
1612
1613
0
    njs_lexer_consume_token(parser->lexer, 1);
1614
1615
0
    return njs_parser_stack_pop(parser);
1616
0
}
1617
1618
1619
static njs_int_t
1620
njs_parser_cover_parenthesized_expression_end(njs_parser_t *parser,
1621
    njs_lexer_token_t *token, njs_queue_link_t *current)
1622
0
{
1623
0
    if (token->type != NJS_TOKEN_CLOSE_PARENTHESIS) {
1624
0
        return njs_parser_failed(parser);
1625
0
    }
1626
1627
0
    njs_lexer_consume_token(parser->lexer, 1);
1628
1629
0
    return njs_parser_stack_pop(parser);
1630
0
}
1631
1632
1633
/*
1634
 * 12.2.5 Array Initializer.
1635
 */
1636
static njs_int_t
1637
njs_parser_array_literal(njs_parser_t *parser, njs_lexer_token_t *token,
1638
    njs_queue_link_t *current)
1639
12.9k
{
1640
12.9k
    parser->target = parser->node;
1641
12.9k
    parser->node = NULL;
1642
1643
12.9k
    njs_parser_next(parser, njs_parser_array_element_list);
1644
1645
12.9k
    return NJS_OK;
1646
12.9k
}
1647
1648
1649
static njs_int_t
1650
njs_parser_array_element_list(njs_parser_t *parser, njs_lexer_token_t *token,
1651
    njs_queue_link_t *current)
1652
2.81M
{
1653
2.81M
    njs_parser_node_t  *array;
1654
1655
2.81M
    array = parser->target;
1656
1657
2.81M
    switch (token->type) {
1658
4.78k
    case NJS_TOKEN_CLOSE_BRACKET:
1659
4.78k
        njs_lexer_consume_token(parser->lexer, 1);
1660
1661
4.78k
        parser->node = array;
1662
1663
4.78k
        return njs_parser_stack_pop(parser);
1664
1665
99.0k
    case NJS_TOKEN_COMMA:
1666
99.0k
        njs_lexer_consume_token(parser->lexer, 1);
1667
1668
99.0k
        array->ctor = 1;
1669
99.0k
        array->u.length++;
1670
1671
99.0k
        return NJS_OK;
1672
1673
0
    case NJS_TOKEN_ELLIPSIS:
1674
#if 0
1675
        njs_lexer_consume_token(parser->lexer, 1);
1676
1677
        njs_parser_next(parser, njs_parser_assignment_expression);
1678
1679
        return njs_parser_after(parser, current, array, 0,
1680
                                njs_parser_array_spread_element);
1681
#else
1682
0
        (void) njs_parser_array_spread_element;
1683
0
        return njs_parser_failed(parser);
1684
0
#endif
1685
1686
2.71M
    default:
1687
2.71M
        break;
1688
2.81M
    }
1689
1690
2.71M
    njs_parser_next(parser, njs_parser_assignment_expression);
1691
1692
2.71M
    return njs_parser_after(parser, current, array, 0, njs_parser_array_after);
1693
2.81M
}
1694
1695
1696
static njs_int_t
1697
njs_parser_array_after(njs_parser_t *parser, njs_lexer_token_t *token,
1698
    njs_queue_link_t *current)
1699
2.70M
{
1700
2.70M
    njs_int_t  ret;
1701
1702
2.70M
    if (parser->ret != NJS_OK) {
1703
0
        return njs_parser_failed(parser);
1704
0
    }
1705
1706
2.70M
    ret = njs_parser_array_item(parser, parser->target, parser->node);
1707
2.70M
    if (ret != NJS_OK) {
1708
0
        return NJS_ERROR;
1709
0
    }
1710
1711
2.70M
    switch (token->type) {
1712
2.70M
    case NJS_TOKEN_COMMA:
1713
2.70M
        njs_lexer_consume_token(parser->lexer, 1);
1714
1715
        /* Fall through. */
1716
1717
2.70M
    case NJS_TOKEN_CLOSE_BRACKET:
1718
2.70M
        njs_parser_next(parser, njs_parser_array_element_list);
1719
2.70M
        break;
1720
1721
3
    default:
1722
3
        return njs_parser_failed(parser);
1723
2.70M
    }
1724
1725
2.70M
    return NJS_OK;
1726
2.70M
}
1727
1728
1729
static njs_int_t
1730
njs_parser_array_spread_element(njs_parser_t *parser, njs_lexer_token_t *token,
1731
    njs_queue_link_t *current)
1732
0
{
1733
0
    if (parser->ret != NJS_OK || token->type != NJS_TOKEN_CLOSE_BRACKET) {
1734
0
        return njs_parser_failed(parser);
1735
0
    }
1736
1737
0
    njs_lexer_consume_token(parser->lexer, 1);
1738
1739
0
    parser->node = parser->target;
1740
1741
0
    return njs_parser_stack_pop(parser);
1742
0
}
1743
1744
1745
/*
1746
 * 12.2.6 Object Initializer.
1747
 */
1748
static njs_int_t
1749
njs_parser_object_literal(njs_parser_t *parser, njs_lexer_token_t *token,
1750
    njs_queue_link_t *current)
1751
6.07k
{
1752
6.07k
    njs_parser_node_t *node;
1753
1754
6.07k
    node = njs_parser_node_new(parser, 0);
1755
6.07k
    if (node == NULL) {
1756
0
        return NJS_ERROR;
1757
0
    }
1758
1759
6.07k
    node->left = parser->node;
1760
1761
6.07k
    parser->node = NULL;
1762
6.07k
    parser->target = node;
1763
1764
6.07k
    njs_parser_next(parser, njs_parser_property_definition_list);
1765
1766
6.07k
    return njs_parser_after(parser, current, node, 1,
1767
6.07k
                            njs_parser_object_literal_after);
1768
6.07k
}
1769
1770
1771
static njs_int_t
1772
njs_parser_object_literal_after(njs_parser_t *parser, njs_lexer_token_t *token,
1773
    njs_queue_link_t *current)
1774
6.07k
{
1775
6.07k
    if (token->type == NJS_TOKEN_COMMA) {
1776
0
        njs_lexer_consume_token(parser->lexer, 1);
1777
1778
0
        token = njs_lexer_token(parser->lexer, 1);
1779
0
        if (token == NULL) {
1780
0
            return NJS_ERROR;
1781
0
        }
1782
0
    }
1783
1784
6.07k
    if (token->type != NJS_TOKEN_CLOSE_BRACE) {
1785
0
        return njs_parser_failed(parser);
1786
0
    }
1787
1788
6.07k
    njs_lexer_consume_token(parser->lexer, 1);
1789
1790
6.07k
    parser->node = parser->target->left;
1791
1792
6.07k
    njs_parser_node_free(parser, parser->target);
1793
6.07k
    parser->target = NULL;
1794
1795
6.07k
    return njs_parser_stack_pop(parser);
1796
6.07k
}
1797
1798
1799
static njs_int_t
1800
njs_parser_property_definition_list(njs_parser_t *parser,
1801
    njs_lexer_token_t *token, njs_queue_link_t *current)
1802
6.07k
{
1803
6.07k
    njs_parser_next(parser, njs_parser_property_definition);
1804
1805
6.07k
    return njs_parser_after(parser, current, parser->target, 1,
1806
6.07k
                            njs_parser_property_definition_list_after);
1807
6.07k
}
1808
1809
1810
static njs_int_t
1811
njs_parser_property_definition_list_after(njs_parser_t *parser,
1812
    njs_lexer_token_t *token, njs_queue_link_t *current)
1813
15.3k
{
1814
15.3k
    if (token->type != NJS_TOKEN_COMMA) {
1815
6.07k
        return njs_parser_stack_pop(parser);
1816
6.07k
    }
1817
1818
9.29k
    njs_lexer_consume_token(parser->lexer, 1);
1819
1820
9.29k
    njs_parser_next(parser, njs_parser_property_definition);
1821
1822
9.29k
    return njs_parser_after(parser, current, parser->target, 1,
1823
15.3k
                            njs_parser_property_definition_list_after);
1824
15.3k
}
1825
1826
1827
njs_inline njs_parser_node_t *
1828
njs_parser_property_name_node(njs_parser_t *parser, njs_lexer_token_t *token)
1829
10.5k
{
1830
10.5k
    njs_int_t          ret;
1831
10.5k
    njs_parser_node_t  *property;
1832
1833
10.5k
    if (token->type == NJS_TOKEN_NUMBER) {
1834
9.55k
        property = njs_parser_node_new(parser, NJS_TOKEN_NUMBER);
1835
9.55k
        if (property != NULL) {
1836
9.55k
             njs_set_number(&property->u.value, token->number);
1837
9.55k
        }
1838
1839
9.55k
    } else if (token->type == NJS_TOKEN_ESCAPE_STRING) {
1840
0
        property = njs_parser_node_new(parser, NJS_TOKEN_STRING);
1841
1842
0
        if (property != NULL) {
1843
0
            ret = njs_parser_escape_string_create(parser, token,
1844
0
                                                  &property->u.value);
1845
0
            if (ret != NJS_TOKEN_STRING) {
1846
0
                return NULL;
1847
0
            }
1848
0
        }
1849
1850
962
    } else {
1851
962
        property = njs_parser_node_string(parser->vm, token, parser);
1852
962
    }
1853
1854
10.5k
    if (property != NULL) {
1855
10.5k
        property->token_line = token->line;
1856
10.5k
    }
1857
1858
10.5k
    return property;
1859
10.5k
}
1860
1861
1862
njs_inline njs_int_t
1863
njs_parser_property_name(njs_parser_t *parser, njs_queue_link_t *current,
1864
    unsigned consume)
1865
1.21k
{
1866
1.21k
    njs_lexer_token_t  *token;
1867
1.21k
    njs_parser_node_t  *property;
1868
1869
1.21k
    if (consume > 1) {
1870
1.21k
        token = njs_lexer_token(parser->lexer, 0);
1871
1.21k
        if (token == NULL) {
1872
0
            return NJS_ERROR;
1873
0
        }
1874
1875
1.21k
        property = njs_parser_property_name_node(parser, token);
1876
1.21k
        if (property == NULL) {
1877
0
            return NJS_ERROR;
1878
0
        }
1879
1880
1.21k
        parser->target->right = property;
1881
1.21k
        parser->target->index = (token->type == NJS_TOKEN_NAME);
1882
1.21k
    }
1883
1884
1.21k
    njs_lexer_consume_token(parser->lexer, consume);
1885
1886
1.21k
    parser->node = NULL;
1887
1888
1.21k
    njs_parser_next(parser, njs_parser_assignment_expression);
1889
1890
1.21k
    return njs_parser_after(parser, current, parser->target, 1,
1891
1.21k
                            njs_parser_property_definition_after);
1892
1.21k
}
1893
1894
1895
static njs_int_t
1896
njs_parser_property_definition_ident(njs_parser_t *parser,
1897
    njs_lexer_token_t *token, njs_parser_node_t *temp)
1898
1
{
1899
1
    temp->right = njs_parser_node_string(parser->vm, token, parser);
1900
1
    if (temp->right == NULL) {
1901
0
        return NJS_ERROR;
1902
0
    }
1903
1904
1
    temp->right->index = NJS_TOKEN_OPEN_BRACKET;
1905
1906
1
    parser->node = njs_parser_reference(parser, token);
1907
1
    if (parser->node == NULL) {
1908
0
        return NJS_ERROR;
1909
0
    }
1910
1911
1
    njs_lexer_consume_token(parser->lexer, 1);
1912
1913
1
    token = njs_lexer_token(parser->lexer, 0);
1914
1
    if (token == NULL) {
1915
0
        return NJS_ERROR;
1916
0
    }
1917
1918
    /* CoverInitializedName */
1919
1
    if (token->type == NJS_TOKEN_ASSIGNMENT) {
1920
0
        return njs_parser_not_supported(parser, token);
1921
0
    }
1922
1923
1
    njs_parser_next(parser, njs_parser_property_definition_after);
1924
1925
1
    return NJS_OK;
1926
1
}
1927
1928
1929
static njs_int_t
1930
njs_parser_property_definition(njs_parser_t *parser, njs_lexer_token_t *token,
1931
    njs_queue_link_t *current)
1932
15.3k
{
1933
15.3k
    njs_str_t          *name;
1934
15.3k
    njs_token_type_t   accessor;
1935
15.3k
    njs_lexer_token_t  *next;
1936
15.3k
    njs_parser_node_t  *temp, *property;
1937
1938
15.3k
    temp = parser->target;
1939
1940
15.3k
    switch (token->type) {
1941
4.85k
    case NJS_TOKEN_CLOSE_BRACE:
1942
4.85k
        return njs_parser_stack_pop(parser);
1943
1944
    /* PropertyName */
1945
0
    case NJS_TOKEN_STRING:
1946
0
    case NJS_TOKEN_ESCAPE_STRING:
1947
8.71k
    case NJS_TOKEN_NUMBER:
1948
8.71k
        next = njs_lexer_peek_token(parser->lexer, token, 0);
1949
8.71k
        if (next == NULL) {
1950
0
            return NJS_ERROR;
1951
0
        }
1952
1953
8.71k
        if (next->type == NJS_TOKEN_COLON) {
1954
266
            return njs_parser_property_name(parser, current, 2);
1955
1956
8.44k
        } else if (next->type == NJS_TOKEN_OPEN_PARENTHESIS) {
1957
8.44k
            goto method_definition;
1958
8.44k
        }
1959
1960
0
        njs_lexer_consume_token(parser->lexer, 1);
1961
1962
0
        return njs_parser_failed(parser);
1963
1964
    /* ComputedPropertyName */
1965
0
    case NJS_TOKEN_OPEN_BRACKET:
1966
0
        njs_lexer_consume_token(parser->lexer, 1);
1967
1968
0
        njs_parser_next(parser, njs_parser_assignment_expression);
1969
1970
0
        return njs_parser_after(parser, current, temp, 1,
1971
8.71k
                                njs_parser_computed_property_name_after);
1972
1973
0
    case NJS_TOKEN_ELLIPSIS:
1974
0
        return njs_parser_not_supported(parser, token);
1975
1976
0
    case NJS_TOKEN_ASYNC:
1977
0
        token = njs_lexer_peek_token(parser->lexer, token, 0);
1978
0
        if (token == NULL) {
1979
0
            return NJS_ERROR;
1980
0
        }
1981
1982
0
        if (token->type == NJS_TOKEN_OPEN_BRACKET) {
1983
0
            njs_lexer_consume_token(parser->lexer, 2);
1984
1985
0
            njs_parser_next(parser, njs_parser_assignment_expression);
1986
1987
0
            return njs_parser_after(parser, current, temp, 1,
1988
0
                                    njs_parser_computed_property_async_after);
1989
0
        }
1990
1991
0
        if (token->type == NJS_TOKEN_COLON) {
1992
0
            return njs_parser_property_name(parser, current, 2);
1993
0
        }
1994
1995
0
        if (!njs_lexer_token_is_identifier_name(token)) {
1996
0
            return njs_parser_failed(parser);
1997
0
        }
1998
1999
0
        next = njs_lexer_peek_token(parser->lexer, token, 0);
2000
0
        if (next == NULL) {
2001
0
            return NJS_ERROR;
2002
0
        }
2003
2004
0
        if (next->type == NJS_TOKEN_OPEN_PARENTHESIS) {
2005
0
            goto method_definition;
2006
0
        }
2007
2008
0
        njs_lexer_consume_token(parser->lexer, 1);
2009
2010
0
        return njs_parser_failed(parser);
2011
2012
1.80k
    default:
2013
1.80k
        if (!njs_lexer_token_is_identifier_name(token)) {
2014
0
            return njs_parser_reject(parser);
2015
0
        }
2016
2017
1.80k
        next = njs_lexer_peek_token(parser->lexer, token, 0);
2018
1.80k
        if (next == NULL) {
2019
0
            return NJS_ERROR;
2020
0
        }
2021
2022
        /* PropertyName */
2023
1.80k
        if (next->type == NJS_TOKEN_COLON) {
2024
949
            return njs_parser_property_name(parser, current, 2);
2025
2026
        /* MethodDefinition */
2027
949
        } else if (next->type == NJS_TOKEN_OPEN_PARENTHESIS) {
2028
13
            goto method_definition;
2029
2030
839
        } else if (njs_lexer_token_is_reserved(token)) {
2031
0
            njs_lexer_consume_token(parser->lexer, 1);
2032
2033
0
            return njs_parser_failed(parser);
2034
0
        }
2035
2036
839
        name = &token->text;
2037
2038
839
        if (name->length == 3 && (memcmp(name->start, "get", 3) == 0
2039
838
                                  || memcmp(name->start, "set", 3) == 0))
2040
838
        {
2041
838
            accessor = (name->start[0] == 'g') ? NJS_TOKEN_PROPERTY_GETTER
2042
838
                                               : NJS_TOKEN_PROPERTY_SETTER;
2043
2044
838
            temp->right = (njs_parser_node_t *) (uintptr_t) accessor;
2045
2046
838
            njs_parser_next(parser, njs_parser_get_set);
2047
2048
838
            return NJS_OK;
2049
838
        }
2050
2051
1
        return njs_parser_property_definition_ident(parser, token, temp);
2052
15.3k
    }
2053
2054
8.46k
method_definition:
2055
2056
8.46k
    property = njs_parser_property_name_node(parser, token);
2057
8.46k
    if (property == NULL) {
2058
0
        return NJS_ERROR;
2059
0
    }
2060
2061
8.46k
    temp->right = property;
2062
8.46k
    temp->right->index = NJS_TOKEN_OPEN_BRACKET;
2063
2064
8.46k
    njs_parser_next(parser, njs_parser_method_definition);
2065
2066
8.46k
    return njs_parser_after(parser, current, temp, 1,
2067
8.46k
                            njs_parser_property_definition_after);
2068
8.46k
}
2069
2070
2071
static njs_int_t
2072
njs_parser_property_definition_after(njs_parser_t *parser,
2073
    njs_lexer_token_t *token, njs_queue_link_t *current)
2074
9.67k
{
2075
9.67k
    njs_int_t          ret;
2076
9.67k
    njs_str_t          name;
2077
9.67k
    njs_bool_t         proto_init;
2078
9.67k
    njs_parser_node_t  *property, *temp;
2079
2080
9.67k
    static const njs_str_t  proto_string = njs_str("__proto__");
2081
2082
9.67k
    temp = parser->target;
2083
9.67k
    property = temp->right;
2084
2085
9.67k
    proto_init = 0;
2086
2087
9.67k
    if (property->index != NJS_TOKEN_OPEN_BRACKET
2088
9.67k
        && njs_is_string(&property->u.value))
2089
949
    {
2090
949
        njs_string_get(&property->u.value, &name);
2091
2092
949
        if (njs_slow_path(njs_strstr_eq(&name, &proto_string))) {
2093
374
            if (temp->token_type == NJS_TOKEN_PROTO_INIT) {
2094
0
                njs_parser_syntax_error(parser,
2095
0
                                   "Duplicate __proto__ fields are not allowed "
2096
0
                                   "in object literals");
2097
0
                return NJS_ERROR;
2098
0
            }
2099
2100
374
            temp->token_type = NJS_TOKEN_PROTO_INIT;
2101
374
            proto_init = 1;
2102
374
        }
2103
949
    }
2104
2105
9.67k
    if (property->index != 0) {
2106
8.46k
        property->index = 0;
2107
8.46k
    }
2108
2109
9.67k
    ret = njs_parser_object_property(parser, temp->left, property,
2110
9.67k
                                     parser->node, proto_init);
2111
9.67k
    if (ret != NJS_OK) {
2112
0
        return NJS_ERROR;
2113
0
    }
2114
2115
9.67k
    temp->right = NULL;
2116
2117
9.67k
    return njs_parser_stack_pop(parser);
2118
9.67k
}
2119
2120
2121
static njs_int_t
2122
njs_parser_computed_property_name_after(njs_parser_t *parser,
2123
    njs_lexer_token_t *token, njs_queue_link_t *current)
2124
0
{
2125
0
    return njs_parser_computed_property_name_handler(parser, token, current, 0);
2126
0
}
2127
2128
2129
static njs_int_t
2130
njs_parser_computed_property_async_after(njs_parser_t *parser,
2131
    njs_lexer_token_t *token, njs_queue_link_t *current)
2132
0
{
2133
0
    return njs_parser_computed_property_name_handler(parser, token, current, 1);
2134
0
}
2135
2136
2137
static njs_int_t
2138
njs_parser_computed_property_name_handler(njs_parser_t *parser,
2139
    njs_lexer_token_t *token, njs_queue_link_t *current, njs_bool_t async)
2140
0
{
2141
0
    njs_token_type_t   type;
2142
0
    njs_parser_node_t  *expr, *target;
2143
2144
0
    if (token->type != NJS_TOKEN_CLOSE_BRACKET) {
2145
0
        return njs_parser_failed(parser);
2146
0
    }
2147
2148
0
    njs_lexer_consume_token(parser->lexer, 1);
2149
2150
0
    token = njs_lexer_token(parser->lexer, 0);
2151
0
    if (token == NULL) {
2152
0
        return NJS_ERROR;
2153
0
    }
2154
2155
0
    target = parser->target;
2156
2157
    /*
2158
     * For further identification.
2159
     * In njs_parser_property_definition_after() index will be reset to zero.
2160
     */
2161
0
    parser->node->index = NJS_TOKEN_OPEN_BRACKET;
2162
2163
0
    target->right = parser->node;
2164
2165
0
    if (!async && token->type == NJS_TOKEN_COLON) {
2166
0
        return njs_parser_property_name(parser, current, 1);
2167
2168
    /* MethodDefinition */
2169
0
    } else if (token->type == NJS_TOKEN_OPEN_PARENTHESIS) {
2170
0
        type = (async) ? NJS_TOKEN_ASYNC_FUNCTION : NJS_TOKEN_FUNCTION;
2171
2172
0
        expr = njs_parser_node_new(parser, type);
2173
0
        if (expr == NULL) {
2174
0
            return NJS_ERROR;
2175
0
        }
2176
2177
0
        expr->token_line = token->line;
2178
2179
0
        parser->node = expr;
2180
2181
0
        njs_lexer_consume_token(parser->lexer, 1);
2182
0
        njs_parser_next(parser, njs_parser_function_lambda);
2183
2184
0
        return njs_parser_after(parser, current, parser->target, 1,
2185
0
                                njs_parser_property_definition_after);
2186
0
    }
2187
2188
0
    return njs_parser_failed(parser);
2189
0
}
2190
2191
2192
static njs_int_t
2193
njs_parser_initializer(njs_parser_t *parser, njs_lexer_token_t *token,
2194
    njs_queue_link_t *current)
2195
4.08k
{
2196
4.08k
    njs_parser_node_t  *node;
2197
2198
4.08k
    if (token->type != NJS_TOKEN_ASSIGNMENT) {
2199
0
        return njs_parser_failed(parser);
2200
0
    }
2201
2202
4.08k
    njs_lexer_consume_token(parser->lexer, 1);
2203
2204
4.08k
    node = parser->node;
2205
2206
4.08k
    parser->node = NULL;
2207
2208
4.08k
    njs_parser_next(parser, njs_parser_assignment_expression);
2209
2210
4.08k
    return njs_parser_after(parser, current, node, 1,
2211
4.08k
                            njs_parser_initializer_after);
2212
4.08k
}
2213
2214
2215
static njs_int_t
2216
njs_parser_initializer_after(njs_parser_t *parser, njs_lexer_token_t *token,
2217
    njs_queue_link_t *current)
2218
6.79k
{
2219
6.79k
    njs_parser_node_t  *stmt;
2220
2221
6.79k
    stmt = njs_parser_node_new(parser, NJS_TOKEN_STATEMENT);
2222
6.79k
    if (stmt == NULL) {
2223
0
        return NJS_ERROR;
2224
0
    }
2225
2226
6.79k
    stmt->left = NULL;
2227
6.79k
    stmt->right = parser->target;
2228
2229
6.79k
    parser->target->right = parser->node;
2230
6.79k
    parser->node = stmt;
2231
2232
6.79k
    return njs_parser_stack_pop(parser);
2233
6.79k
}
2234
2235
2236
static njs_int_t
2237
njs_parser_initializer_assign(njs_parser_t *parser, njs_token_type_t type)
2238
6.79k
{
2239
6.79k
    njs_parser_node_t  *assign;
2240
2241
6.79k
    assign = njs_parser_node_new(parser, type);
2242
6.79k
    if (assign == NULL) {
2243
0
        return NJS_ERROR;
2244
0
    }
2245
2246
6.79k
    assign->u.operation = NJS_VMCODE_MOVE;
2247
6.79k
    assign->left = parser->node;
2248
2249
    /* assign->right in njs_parser_initializer_after. */
2250
2251
6.79k
    parser->node = assign;
2252
2253
6.79k
    return NJS_OK;
2254
6.79k
}
2255
2256
2257
/*
2258
 * 12.3 Left-Hand-Side Expressions.
2259
 */
2260
static njs_int_t
2261
njs_parser_property(njs_parser_t *parser, njs_lexer_token_t *token,
2262
    njs_queue_link_t *current)
2263
3.56M
{
2264
3.56M
    njs_parser_node_t  *node, *prop_node;
2265
2266
    /*
2267
     * [ Expression ]
2268
     * . IdentifierName
2269
     * TemplateLiteral
2270
     */
2271
2272
3.56M
    switch (token->type) {
2273
9.84k
    case NJS_TOKEN_OPEN_BRACKET:
2274
9.84k
        node = njs_parser_node_new(parser, NJS_TOKEN_PROPERTY);
2275
9.84k
        if (node == NULL) {
2276
0
            return NJS_ERROR;
2277
0
        }
2278
2279
9.84k
        node->u.operation = NJS_VMCODE_PROPERTY_GET;
2280
9.84k
        node->left = parser->node;
2281
9.84k
        node->token_line = token->line;
2282
2283
9.84k
        parser->node = NULL;
2284
2285
9.84k
        njs_lexer_consume_token(parser->lexer, 1);
2286
2287
9.84k
        njs_parser_next(parser, njs_parser_expression);
2288
2289
9.84k
        return njs_parser_after(parser, current, node, 1,
2290
9.84k
                                njs_parser_member_expression_bracket);
2291
2292
32.8k
    case NJS_TOKEN_DOT:
2293
32.8k
        token = njs_lexer_peek_token(parser->lexer, token, 0);
2294
32.8k
        if (token == NULL) {
2295
0
            return NJS_ERROR;
2296
0
        }
2297
2298
32.8k
        if (njs_lexer_token_is_identifier_name(token)) {
2299
32.8k
            node = njs_parser_node_new(parser, NJS_TOKEN_PROPERTY);
2300
32.8k
            if (node == NULL) {
2301
0
                return NJS_ERROR;
2302
0
            }
2303
2304
32.8k
            node->u.operation = NJS_VMCODE_PROPERTY_GET;
2305
32.8k
            node->token_line = token->line;
2306
2307
32.8k
            prop_node = njs_parser_node_string(parser->vm, token, parser);
2308
32.8k
            if (prop_node == NULL) {
2309
0
                return NJS_ERROR;
2310
0
            }
2311
2312
32.8k
            prop_node->token_line = token->line;
2313
2314
32.8k
            node->left = parser->node;
2315
32.8k
            node->right = prop_node;
2316
2317
32.8k
            parser->node = node;
2318
2319
32.8k
            njs_lexer_consume_token(parser->lexer, 2);
2320
32.8k
            return NJS_AGAIN;
2321
32.8k
        }
2322
2323
0
        njs_lexer_consume_token(parser->lexer, 1);
2324
2325
0
        return NJS_DECLINED;
2326
2327
240k
    case NJS_TOKEN_GRAVE:
2328
240k
        node = njs_parser_create_call(parser, parser->node, 0);
2329
240k
        if (node == NULL) {
2330
0
            return NJS_ERROR;
2331
0
        }
2332
2333
240k
        node->token_line = token->line;
2334
2335
240k
        parser->node = node;
2336
2337
240k
        njs_parser_next(parser, njs_parser_template_literal);
2338
2339
240k
        break;
2340
2341
3.28M
    default:
2342
3.28M
        return NJS_DONE;
2343
3.56M
    }
2344
2345
240k
    return NJS_OK;
2346
3.56M
}
2347
2348
2349
static njs_int_t
2350
njs_parser_member_expression(njs_parser_t *parser, njs_lexer_token_t *token,
2351
    njs_queue_link_t *current)
2352
3.30M
{
2353
3.30M
    njs_int_t  ret;
2354
2355
    /*
2356
     * PrimaryExpression
2357
     * MemberExpression [ Expression ]
2358
     * MemberExpression . IdentifierName
2359
     * MemberExpression TemplateLiteral
2360
     * SuperProperty
2361
     * MetaProperty
2362
     * new MemberExpression Arguments
2363
     */
2364
2365
3.30M
    switch (token->type) {
2366
    /* SuperProperty */
2367
0
    case NJS_TOKEN_SUPER:
2368
        /* TODO: By specification. */
2369
0
        (void) njs_parser_super_property;
2370
0
        return njs_parser_not_supported(parser, token);
2371
2372
    /* MetaProperty */
2373
0
    case NJS_TOKEN_IMPORT:
2374
        /* TODO: By specification. */
2375
0
        (void) njs_parser_member_expression_import;
2376
0
        return njs_parser_not_supported(parser, token);
2377
2378
223
    case NJS_TOKEN_NEW:
2379
223
        njs_lexer_consume_token(parser->lexer, 1);
2380
2381
223
        njs_parser_next(parser, njs_parser_member_expression_new);
2382
223
        break;
2383
2384
3.30M
    default:
2385
3.30M
        ret = njs_parser_primary_expression_test(parser, token, current);
2386
2387
3.30M
        if (ret != NJS_OK) {
2388
3.24M
            if (ret == NJS_DONE) {
2389
3.23M
                njs_parser_next(parser, njs_parser_member_expression_next);
2390
3.23M
                return NJS_OK;
2391
3.23M
            }
2392
2393
3.12k
            if (njs_is_error(&parser->vm->exception)) {
2394
3.12k
                return NJS_DONE;
2395
3.12k
            }
2396
2397
5
            return ret;
2398
3.12k
        }
2399
2400
64.6k
        break;
2401
3.30M
    }
2402
2403
64.9k
    return njs_parser_after(parser, current, NULL, 1,
2404
3.30M
                            njs_parser_member_expression_next);
2405
3.30M
}
2406
2407
2408
static njs_int_t
2409
njs_parser_member_expression_next(njs_parser_t *parser,
2410
    njs_lexer_token_t *token, njs_queue_link_t *current)
2411
3.55M
{
2412
3.55M
    njs_int_t  ret;
2413
2414
    /*
2415
     * [ Expression ]
2416
     * . IdentifierName
2417
     * TemplateLiteral
2418
     */
2419
2420
3.55M
    ret = njs_parser_property(parser, token, current);
2421
2422
3.55M
    switch (ret) {
2423
32.6k
    case NJS_AGAIN:
2424
32.6k
        return NJS_OK;
2425
2426
3.27M
    case NJS_DONE:
2427
3.27M
        return njs_parser_stack_pop(parser);
2428
2429
0
    case NJS_DECLINED:
2430
0
        return njs_parser_failed(parser);
2431
2432
249k
    default:
2433
249k
        break;
2434
3.55M
    }
2435
2436
249k
    return njs_parser_after(parser, current, NULL, 1,
2437
3.55M
                            njs_parser_member_expression_next);
2438
3.55M
}
2439
2440
2441
static njs_int_t
2442
njs_parser_member_expression_bracket(njs_parser_t *parser,
2443
    njs_lexer_token_t *token, njs_queue_link_t *current)
2444
9.84k
{
2445
9.84k
    if (token->type != NJS_TOKEN_CLOSE_BRACKET) {
2446
0
        return njs_parser_failed(parser);
2447
0
    }
2448
2449
9.84k
    njs_lexer_consume_token(parser->lexer, 1);
2450
2451
9.84k
    parser->target->right = parser->node;
2452
9.84k
    parser->node = parser->target;
2453
2454
9.84k
    return njs_parser_stack_pop(parser);
2455
9.84k
}
2456
2457
2458
static njs_int_t
2459
njs_parser_member_expression_new_next(njs_parser_t *parser,
2460
    njs_lexer_token_t *token, njs_queue_link_t *current)
2461
223
{
2462
223
    njs_int_t  ret;
2463
2464
    /*
2465
     * PrimaryExpression
2466
     * SuperProperty
2467
     * MetaProperty
2468
     * Arguments
2469
     */
2470
2471
223
    switch (token->type) {
2472
    /* SuperProperty */
2473
0
    case NJS_TOKEN_SUPER:
2474
0
        (void) njs_parser_super_property;
2475
0
        return njs_parser_not_supported(parser, token);
2476
2477
    /* MetaProperty */
2478
0
    case NJS_TOKEN_IMPORT:
2479
0
        (void) njs_parser_member_expression_import;
2480
0
        return njs_parser_not_supported(parser, token);
2481
2482
223
    default:
2483
223
        ret = njs_parser_primary_expression_test(parser, token, current);
2484
2485
223
        if (ret != NJS_OK) {
2486
216
            if (ret == NJS_DONE) {
2487
216
                njs_parser_next(parser, njs_parser_member_expression_next);
2488
216
                return NJS_OK;
2489
216
            }
2490
2491
0
            return ret;
2492
216
        }
2493
2494
7
        return njs_parser_after(parser, current, NULL, 1,
2495
223
                                njs_parser_member_expression_next);
2496
223
    }
2497
223
}
2498
2499
2500
static njs_int_t
2501
njs_parser_super_property(njs_parser_t *parser, njs_lexer_token_t *token,
2502
    njs_queue_link_t *current)
2503
0
{
2504
    /*
2505
     * [ Expression ]
2506
     * . IdentifierName
2507
     */
2508
2509
0
    if (token->type == NJS_TOKEN_OPEN_BRACKET) {
2510
0
        parser->node = NULL;
2511
2512
0
        njs_parser_next(parser, njs_parser_expression);
2513
2514
0
        return njs_parser_after(parser, current, NULL, 1,
2515
0
                                njs_parser_close_bracked);
2516
0
    }
2517
2518
0
    if (token->type != NJS_TOKEN_DOT) {
2519
0
        return njs_parser_failed(parser);
2520
0
    }
2521
2522
0
    njs_lexer_consume_token(parser->lexer, 1);
2523
2524
0
    token = njs_lexer_token(parser->lexer, 0);
2525
0
    if (token == NULL) {
2526
0
        return NJS_ERROR;
2527
0
    }
2528
2529
0
    if (!njs_lexer_token_is_identifier_name(token)) {
2530
0
        return njs_parser_failed(parser);
2531
0
    }
2532
2533
0
    return njs_parser_stack_pop(parser);
2534
0
}
2535
2536
2537
static njs_int_t
2538
njs_parser_member_expression_import(njs_parser_t *parser,
2539
    njs_lexer_token_t *token, njs_queue_link_t *current)
2540
0
{
2541
    /*
2542
     * import . meta
2543
     */
2544
2545
0
    if (token->type != NJS_TOKEN_DOT) {
2546
0
        return njs_parser_failed(parser);
2547
0
    }
2548
2549
0
    njs_lexer_consume_token(parser->lexer, 1);
2550
2551
0
    token = njs_lexer_token(parser->lexer, 0);
2552
0
    if (token == NULL) {
2553
0
        return NJS_ERROR;
2554
0
    }
2555
2556
0
    if (token->type != NJS_TOKEN_META) {
2557
0
        return njs_parser_failed(parser);
2558
0
    }
2559
2560
0
    njs_lexer_consume_token(parser->lexer, 1);
2561
2562
0
    return njs_parser_stack_pop(parser);
2563
0
}
2564
2565
2566
static njs_int_t
2567
njs_parser_member_expression_new(njs_parser_t *parser,
2568
    njs_lexer_token_t *token, njs_queue_link_t *current)
2569
223
{
2570
    /*
2571
     * new MemberExpression Arguments
2572
     * new . target
2573
     */
2574
2575
223
    if (token->type != NJS_TOKEN_DOT) {
2576
223
        njs_parser_next(parser, njs_parser_member_expression_new_next);
2577
2578
223
        return njs_parser_after(parser, current, NULL, 1,
2579
223
                                njs_parser_member_expression_new_after);
2580
223
    }
2581
2582
    /* njs_lexer_consume_token(parser->lexer, 1); */
2583
2584
0
    token = njs_lexer_token(parser->lexer, 0);
2585
0
    if (token == NULL) {
2586
0
        return NJS_ERROR;
2587
0
    }
2588
2589
0
    if (token->type != NJS_TOKEN_TARGET) {
2590
0
        return njs_parser_failed(parser);
2591
0
    }
2592
2593
    /* njs_lexer_consume_token(parser->lexer, 1); */
2594
2595
    /* return njs_parser_stack_pop(parser); */
2596
0
    return njs_parser_not_supported(parser, token);
2597
0
}
2598
2599
2600
static njs_int_t
2601
njs_parser_member_expression_new_after(njs_parser_t *parser,
2602
    njs_lexer_token_t *token, njs_queue_link_t *current)
2603
223
{
2604
223
    njs_parser_node_t  *func;
2605
2606
223
    if (token->type != NJS_TOKEN_OPEN_PARENTHESIS) {
2607
5
        parser->node = njs_parser_create_call(parser, parser->node, 1);
2608
5
        if (parser->node == NULL) {
2609
0
            return NJS_ERROR;
2610
0
        }
2611
2612
5
        parser->node->token_line = token->line;
2613
2614
5
        return njs_parser_stack_pop(parser);
2615
5
    }
2616
2617
218
    func = njs_parser_create_call(parser, parser->node, 1);
2618
218
    if (func == NULL) {
2619
0
        return NJS_ERROR;
2620
0
    }
2621
2622
218
    func->token_line = token->line;
2623
218
    parser->node = func;
2624
2625
218
    njs_lexer_consume_token(parser->lexer, 1);
2626
218
    njs_parser_next(parser, njs_parser_arguments);
2627
2628
218
    return njs_parser_after(parser, current, func, 1,
2629
218
                            njs_parser_member_expression_new_args);
2630
218
}
2631
2632
2633
static njs_int_t
2634
njs_parser_member_expression_new_args(njs_parser_t *parser,
2635
    njs_lexer_token_t *token, njs_queue_link_t *current)
2636
218
{
2637
218
    parser->node = parser->target;
2638
2639
218
    return njs_parser_stack_pop(parser);
2640
218
}
2641
2642
2643
static njs_parser_node_t *
2644
njs_parser_create_call(njs_parser_t *parser, njs_parser_node_t *node,
2645
    uint8_t ctor)
2646
254k
{
2647
254k
    njs_parser_node_t  *func;
2648
2649
254k
    switch (node->token_type) {
2650
17.8k
    case NJS_TOKEN_NAME:
2651
17.8k
        func = node;
2652
17.8k
        func->token_type = NJS_TOKEN_FUNCTION_CALL;
2653
2654
17.8k
        break;
2655
2656
6.99k
    case NJS_TOKEN_PROPERTY:
2657
6.99k
        func = njs_parser_node_new(parser, NJS_TOKEN_METHOD_CALL);
2658
6.99k
        if (func == NULL) {
2659
0
            return NULL;
2660
0
        }
2661
2662
6.99k
        func->left = node;
2663
6.99k
        break;
2664
2665
229k
    default:
2666
        /*
2667
         * NJS_TOKEN_METHOD_CALL,
2668
         * NJS_TOKEN_FUNCTION_CALL,
2669
         * NJS_TOKEN_FUNCTION_EXPRESSION,
2670
         * NJS_TOKEN_OPEN_PARENTHESIS,
2671
         * NJS_TOKEN_EVAL.
2672
         */
2673
229k
        func = njs_parser_node_new(parser, NJS_TOKEN_FUNCTION_CALL);
2674
229k
        if (func == NULL) {
2675
0
            return NULL;
2676
0
        }
2677
2678
229k
        func->left = node;
2679
229k
        break;
2680
254k
    }
2681
2682
254k
    func->ctor = ctor;
2683
2684
254k
    return func;
2685
254k
}
2686
2687
2688
static njs_int_t
2689
njs_parser_call_expression(njs_parser_t *parser, njs_lexer_token_t *token,
2690
    njs_queue_link_t *current)
2691
0
{
2692
0
    njs_int_t  ret;
2693
2694
    /*
2695
     * CoverCallExpressionAndAsyncArrowHead
2696
     * SuperCall
2697
     * ImportCall
2698
     * CallExpression Arguments
2699
     * CallExpression [ Expression ]
2700
     * CallExpression . IdentifierName
2701
     * CallExpression TemplateLiteral
2702
     */
2703
2704
0
    switch (token->type) {
2705
    /* MemberExpression or SuperCall */
2706
0
    case NJS_TOKEN_SUPER:
2707
0
        return njs_parser_not_supported(parser, token);
2708
2709
0
    case NJS_TOKEN_IMPORT:
2710
0
        return njs_parser_not_supported(parser, token);
2711
2712
0
    default:
2713
0
        break;
2714
0
    }
2715
2716
0
    njs_parser_next(parser, njs_parser_member_expression);
2717
2718
0
    ret = njs_parser_after(parser, current, NULL, 1,
2719
0
                           njs_parser_call_expression_args);
2720
0
    if (ret != NJS_OK) {
2721
0
        return NJS_ERROR;
2722
0
    }
2723
2724
0
    return njs_parser_after(parser, current, NULL, 1,
2725
0
                            njs_parser_call_expression_after);
2726
0
}
2727
2728
2729
static njs_int_t
2730
njs_parser_call_expression_args(njs_parser_t *parser, njs_lexer_token_t *token,
2731
    njs_queue_link_t *current)
2732
0
{
2733
0
    njs_parser_node_t  *func;
2734
2735
0
    if (token->type != NJS_TOKEN_OPEN_PARENTHESIS) {
2736
0
        return njs_parser_failed(parser);
2737
0
    }
2738
2739
0
    func = njs_parser_create_call(parser, parser->node, 0);
2740
0
    if (func == NULL) {
2741
0
        return NJS_ERROR;
2742
0
    }
2743
2744
0
    func->token_line = token->line;
2745
0
    parser->node = func;
2746
2747
0
    njs_lexer_consume_token(parser->lexer, 1);
2748
0
    njs_parser_next(parser, njs_parser_arguments);
2749
2750
0
    return njs_parser_after(parser, current, func, 1,
2751
0
                            njs_parser_left_hand_side_expression_node);
2752
0
}
2753
2754
2755
static njs_int_t
2756
njs_parser_call_expression_after(njs_parser_t *parser,
2757
    njs_lexer_token_t *token, njs_queue_link_t *current)
2758
0
{
2759
0
    njs_int_t          ret;
2760
0
    njs_parser_node_t  *func;
2761
2762
    /*
2763
     * Arguments
2764
     * [ Expression ]
2765
     * . IdentifierName
2766
     * TemplateLiteral
2767
     */
2768
2769
0
    switch (token->type) {
2770
0
    case NJS_TOKEN_OPEN_PARENTHESIS:
2771
0
        func = njs_parser_create_call(parser, parser->node, 0);
2772
0
        if (func == NULL) {
2773
0
            return NJS_ERROR;
2774
0
        }
2775
2776
0
        func->token_line = token->line;
2777
0
        parser->node = func;
2778
2779
0
        njs_lexer_consume_token(parser->lexer, 1);
2780
0
        njs_parser_next(parser, njs_parser_arguments);
2781
2782
0
        ret = njs_parser_after(parser, current, func, 1,
2783
0
                               njs_parser_left_hand_side_expression_node);
2784
0
        if (ret != NJS_OK) {
2785
0
            return NJS_ERROR;
2786
0
        }
2787
2788
0
        break;
2789
2790
0
    default:
2791
0
        ret = njs_parser_property(parser, token, current);
2792
2793
0
        switch (ret) {
2794
0
        case NJS_AGAIN:
2795
0
            return NJS_OK;
2796
2797
0
        case NJS_DONE:
2798
0
            return njs_parser_stack_pop(parser);
2799
2800
0
        case NJS_DECLINED:
2801
0
            return njs_parser_failed(parser);
2802
2803
0
        default:
2804
0
            break;
2805
0
        }
2806
2807
0
        break;
2808
0
    }
2809
2810
0
    return njs_parser_after(parser, current, NULL, 1,
2811
0
                            njs_parser_call_expression_after);
2812
0
}
2813
2814
2815
static njs_int_t
2816
njs_parser_arguments(njs_parser_t *parser, njs_lexer_token_t *token,
2817
    njs_queue_link_t *current)
2818
13.8k
{
2819
    /*
2820
     * )
2821
     * ArgumentList )
2822
     * ArgumentList , )
2823
     */
2824
2825
13.8k
    if (token->type == NJS_TOKEN_CLOSE_PARENTHESIS) {
2826
1.31k
        njs_lexer_consume_token(parser->lexer, 1);
2827
1.31k
        return njs_parser_stack_pop(parser);
2828
1.31k
    }
2829
2830
12.5k
    parser->scope->in_args++;
2831
2832
12.5k
    njs_parser_next(parser, njs_parser_argument_list);
2833
2834
12.5k
    return njs_parser_after(parser, current, NULL, 1,
2835
13.8k
                            njs_parser_parenthesis_or_comma);
2836
13.8k
}
2837
2838
2839
static njs_int_t
2840
njs_parser_parenthesis_or_comma(njs_parser_t *parser, njs_lexer_token_t *token,
2841
    njs_queue_link_t *current)
2842
6.34k
{
2843
6.34k
    parser->scope->in_args--;
2844
2845
6.34k
    if (token->type == NJS_TOKEN_CLOSE_PARENTHESIS) {
2846
6.34k
        njs_lexer_consume_token(parser->lexer, 1);
2847
6.34k
        return njs_parser_stack_pop(parser);
2848
6.34k
    }
2849
2850
0
    if (token->type != NJS_TOKEN_COMMA) {
2851
0
        return njs_parser_failed(parser);
2852
0
    }
2853
2854
0
    njs_lexer_consume_token(parser->lexer, 1);
2855
2856
0
    token = njs_lexer_token(parser->lexer, 0);
2857
0
    if (njs_slow_path(token == NULL)) {
2858
0
        return NJS_ERROR;
2859
0
    }
2860
2861
0
    if (token->type != NJS_TOKEN_CLOSE_PARENTHESIS) {
2862
0
        return njs_parser_failed(parser);
2863
0
    }
2864
2865
0
    njs_lexer_consume_token(parser->lexer, 1);
2866
2867
0
    return njs_parser_stack_pop(parser);
2868
0
}
2869
2870
2871
static njs_int_t
2872
njs_parser_argument_list(njs_parser_t *parser, njs_lexer_token_t *token,
2873
    njs_queue_link_t *current)
2874
20.9k
{
2875
    /*
2876
     * AssignmentExpression
2877
     * ... AssignmentExpression
2878
     * ArgumentList , AssignmentExpression
2879
     * ArgumentList , ... AssignmentExpression
2880
     */
2881
2882
#if 0 /* TODO. */
2883
    if (token->type == NJS_TOKEN_ELLIPSIS) {
2884
        njs_lexer_consume_token(parser->lexer, 1);
2885
    }
2886
#endif
2887
2888
20.9k
    njs_parser_next(parser, njs_parser_assignment_expression);
2889
2890
20.9k
    return njs_parser_after(parser, current, parser->node, 1,
2891
20.9k
                            njs_parser_argument_list_after);
2892
20.9k
}
2893
2894
2895
static njs_int_t
2896
njs_parser_argument_list_after(njs_parser_t *parser, njs_lexer_token_t *token,
2897
    njs_queue_link_t *current)
2898
14.7k
{
2899
14.7k
    njs_parser_node_t  *node;
2900
2901
14.7k
    node = njs_parser_node_new(parser, NJS_TOKEN_ARGUMENT);
2902
14.7k
    if (node == NULL) {
2903
0
        return NJS_ERROR;
2904
0
    }
2905
2906
14.7k
    node->index = njs_scope_temp_index(node->scope);
2907
14.7k
    if (njs_slow_path(node->index == NJS_INDEX_ERROR)) {
2908
0
        return NJS_ERROR;
2909
0
    }
2910
2911
14.7k
    node->token_line = token->line;
2912
14.7k
    node->left = parser->node;
2913
2914
14.7k
    parser->node->dest = node;
2915
14.7k
    parser->target->right = node;
2916
14.7k
    parser->node = node;
2917
2918
14.7k
    if (token->type == NJS_TOKEN_COMMA) {
2919
8.52k
        njs_lexer_consume_token(parser->lexer, 1);
2920
2921
8.52k
        token = njs_lexer_token(parser->lexer, 0);
2922
8.52k
        if (token == NULL) {
2923
0
            return NJS_ERROR;
2924
0
        }
2925
2926
8.52k
        if (token->type == NJS_TOKEN_CLOSE_PARENTHESIS) {
2927
111
            return njs_parser_stack_pop(parser);
2928
111
        }
2929
2930
8.41k
        return njs_parser_argument_list(parser, token, current);
2931
8.52k
    }
2932
2933
6.23k
    return njs_parser_stack_pop(parser);
2934
14.7k
}
2935
2936
2937
static njs_int_t
2938
njs_parser_optional_expression_after(njs_parser_t *parser,
2939
    njs_lexer_token_t *token, njs_queue_link_t *current)
2940
140
{
2941
140
    if (token->type != NJS_TOKEN_CONDITIONAL) {
2942
0
        return njs_parser_stack_pop(parser);
2943
0
    }
2944
2945
140
    token = njs_lexer_peek_token(parser->lexer, token, 0);
2946
140
    if (token == NULL) {
2947
0
        return NJS_ERROR;
2948
0
    }
2949
2950
140
    if (token->type != NJS_TOKEN_DOT) {
2951
140
        return njs_parser_stack_pop(parser);
2952
140
    }
2953
2954
0
    njs_parser_next(parser, njs_parser_optional_chain);
2955
2956
0
    return njs_parser_after(parser, current, NULL, 1,
2957
140
                            njs_parser_optional_expression_after);
2958
140
}
2959
2960
2961
static njs_int_t
2962
njs_parser_optional_chain(njs_parser_t *parser, njs_lexer_token_t *token,
2963
    njs_queue_link_t *current)
2964
0
{
2965
0
    njs_int_t          ret;
2966
0
    njs_parser_node_t  *func;
2967
2968
    /*
2969
     * ? . Arguments
2970
     * ? . [ Expression ]
2971
     * ? . IdentifierName
2972
     * ? . TemplateLiteral
2973
     * OptionalChain Arguments
2974
     * OptionalChain [ Expression ]
2975
     * OptionalChain . IdentifierName
2976
     * OptionalChain TemplateLiteral
2977
     */
2978
2979
0
    if (token->type != NJS_TOKEN_CONDITIONAL) {
2980
0
        return njs_parser_failed(parser);
2981
0
    }
2982
2983
0
    token = njs_lexer_peek_token(parser->lexer, token, 0);
2984
0
    if (token == NULL) {
2985
0
        return NJS_ERROR;
2986
0
    }
2987
2988
0
    if (token->type != NJS_TOKEN_DOT) {
2989
0
        return njs_parser_failed(parser);
2990
0
    }
2991
2992
0
    njs_lexer_consume_token(parser->lexer, 1);
2993
2994
0
    token = njs_lexer_token(parser->lexer, 0);
2995
0
    if (token == NULL) {
2996
0
        return NJS_ERROR;
2997
0
    }
2998
2999
0
    switch (token->type) {
3000
0
    case NJS_TOKEN_OPEN_PARENTHESIS:
3001
0
        func = njs_parser_create_call(parser, parser->node, 0);
3002
0
        if (func == NULL) {
3003
0
            return NJS_ERROR;
3004
0
        }
3005
3006
0
        func->token_line = token->line;
3007
0
        parser->node = func;
3008
3009
0
        njs_lexer_consume_token(parser->lexer, 2);
3010
0
        njs_parser_next(parser, njs_parser_arguments);
3011
3012
0
        ret = njs_parser_after(parser, current, func, 1,
3013
0
                               njs_parser_left_hand_side_expression_node);
3014
0
        if (ret != NJS_OK) {
3015
0
            return NJS_ERROR;
3016
0
        }
3017
3018
0
        break;
3019
3020
0
    default:
3021
0
        ret = njs_parser_property(parser, token, current);
3022
3023
0
        switch (ret) {
3024
0
        case NJS_DONE:
3025
0
        case NJS_DECLINED:
3026
0
            return njs_parser_failed(parser);
3027
3028
0
        default:
3029
0
            break;
3030
0
        }
3031
3032
0
        break;
3033
0
    }
3034
3035
0
    return njs_parser_after(parser, current, NULL, 1,
3036
0
                            njs_parser_optional_chain_after);
3037
0
}
3038
3039
3040
static njs_int_t
3041
njs_parser_optional_chain_after(njs_parser_t *parser, njs_lexer_token_t *token,
3042
    njs_queue_link_t *current)
3043
7.75k
{
3044
7.75k
    njs_int_t          ret;
3045
7.75k
    njs_parser_node_t  *func;
3046
3047
    /*
3048
     * OptionalChain Arguments
3049
     * OptionalChain [ Expression ]
3050
     * OptionalChain . IdentifierName
3051
     * OptionalChain TemplateLiteral
3052
     */
3053
3054
7.75k
    switch (token->type) {
3055
150
    case NJS_TOKEN_OPEN_PARENTHESIS:
3056
150
        func = njs_parser_create_call(parser, parser->node, 0);
3057
150
        if (func == NULL) {
3058
0
            return NJS_ERROR;
3059
0
        }
3060
3061
150
        func->token_line = token->line;
3062
150
        parser->node = func;
3063
3064
150
        njs_lexer_consume_token(parser->lexer, 1);
3065
150
        njs_parser_next(parser, njs_parser_arguments);
3066
3067
150
        ret = njs_parser_after(parser, current, func, 1,
3068
150
                               njs_parser_left_hand_side_expression_node);
3069
150
        if (ret != NJS_OK) {
3070
0
            return NJS_ERROR;
3071
0
        }
3072
3073
150
        break;
3074
3075
7.60k
    default:
3076
7.60k
        ret = njs_parser_property(parser, token, current);
3077
3078
7.60k
        switch (ret) {
3079
156
        case NJS_AGAIN:
3080
156
            return NJS_OK;
3081
3082
7.29k
        case NJS_DONE:
3083
7.29k
            return njs_parser_stack_pop(parser);
3084
3085
0
        case NJS_DECLINED:
3086
0
            return njs_parser_failed(parser);
3087
3088
156
        default:
3089
156
            break;
3090
7.60k
        }
3091
3092
156
        break;
3093
7.75k
    }
3094
3095
306
    return njs_parser_after(parser, current, NULL, 1,
3096
7.75k
                            njs_parser_optional_chain_after);
3097
7.75k
}
3098
3099
3100
static njs_int_t
3101
njs_parser_new_expression(njs_parser_t *parser, njs_lexer_token_t *token,
3102
    njs_queue_link_t *current)
3103
0
{
3104
0
    if (token->type != NJS_TOKEN_NEW) {
3105
0
        parser->node = NULL;
3106
3107
0
        njs_parser_next(parser, njs_parser_member_expression_new);
3108
3109
0
        return NJS_OK;
3110
0
    }
3111
3112
0
    njs_lexer_consume_token(parser->lexer, 1);
3113
3114
0
    return njs_parser_after(parser, current, NULL, 1,
3115
0
                            njs_parser_new_expression_after);
3116
0
}
3117
3118
3119
static njs_int_t
3120
njs_parser_new_expression_after(njs_parser_t *parser, njs_lexer_token_t *token,
3121
    njs_queue_link_t *current)
3122
0
{
3123
0
    njs_parser_node_t  *func;
3124
3125
0
    if (token->type == NJS_TOKEN_OPEN_PARENTHESIS) {
3126
0
        njs_parser_next(parser, njs_parser_member_expression_new_after);
3127
0
        return NJS_OK;
3128
0
    }
3129
3130
0
    func = njs_parser_create_call(parser, parser->node, 1);
3131
0
    if (func == NULL) {
3132
0
        return NJS_ERROR;
3133
0
    }
3134
3135
0
    func->token_line = token->line;
3136
0
    parser->node = func;
3137
3138
0
    return njs_parser_stack_pop(parser);
3139
0
}
3140
3141
3142
static njs_int_t
3143
njs_parser_left_hand_side_expression(njs_parser_t *parser,
3144
    njs_lexer_token_t *token, njs_queue_link_t *current)
3145
3.30M
{
3146
    /*
3147
     * NewExpression = new MemberExpression
3148
     * CallExpression = MemberExpression Arguments
3149
     * OptionalExpression = MemberExpression OptionalChain
3150
     */
3151
3152
3.30M
    switch (token->type) {
3153
    /* NewExpression or MemberExpression */
3154
223
    case NJS_TOKEN_NEW:
3155
223
        token = njs_lexer_peek_token(parser->lexer, token, 0);
3156
223
        if (token == NULL) {
3157
0
            return NJS_ERROR;
3158
0
        }
3159
3160
223
        if (token->type == NJS_TOKEN_NEW) {
3161
0
            njs_lexer_consume_token(parser->lexer, 1);
3162
3163
0
            njs_parser_next(parser, njs_parser_new_expression);
3164
3165
0
            return njs_parser_after(parser, current, NULL, 1,
3166
0
                                    njs_parser_left_hand_side_expression_after);
3167
0
        }
3168
3169
223
        break;
3170
3171
    /* CallExpression or MemberExpression */
3172
223
    case NJS_TOKEN_SUPER:
3173
0
    case NJS_TOKEN_IMPORT:
3174
0
        token = njs_lexer_peek_token(parser->lexer, token, 0);
3175
0
        if (token == NULL) {
3176
0
            return NJS_ERROR;
3177
0
        }
3178
3179
0
        if (token->type == NJS_TOKEN_OPEN_PARENTHESIS) {
3180
0
            njs_parser_next(parser, njs_parser_call_expression);
3181
0
            return NJS_OK;
3182
0
        }
3183
3184
0
        break;
3185
3186
3.30M
    default:
3187
3.30M
        break;
3188
3.30M
    }
3189
3190
3.30M
    njs_parser_next(parser, njs_parser_member_expression);
3191
3192
3.30M
    return njs_parser_after(parser, current, NULL, 1,
3193
3.30M
                            njs_parser_left_hand_side_expression_after);
3194
3.30M
}
3195
3196
3197
static njs_int_t
3198
njs_parser_left_hand_side_expression_after(njs_parser_t *parser,
3199
    njs_lexer_token_t *token, njs_queue_link_t *current)
3200
3.27M
{
3201
3.27M
    njs_int_t          ret;
3202
3.27M
    njs_parser_node_t  *func;
3203
3204
3.27M
    switch (token->type) {
3205
    /* CallExpression */
3206
13.5k
    case NJS_TOKEN_OPEN_PARENTHESIS:
3207
13.5k
        func = njs_parser_create_call(parser, parser->node, 0);
3208
13.5k
        if (func == NULL) {
3209
0
            return NJS_ERROR;
3210
0
        }
3211
3212
13.5k
        func->token_line = token->line;
3213
13.5k
        parser->node = func;
3214
3215
13.5k
        njs_lexer_consume_token(parser->lexer, 1);
3216
13.5k
        njs_parser_next(parser, njs_parser_arguments);
3217
3218
13.5k
        ret = njs_parser_after(parser, current, func, 1,
3219
13.5k
                               njs_parser_left_hand_side_expression_node);
3220
13.5k
        if (ret != NJS_OK) {
3221
0
            return NJS_ERROR;
3222
0
        }
3223
3224
13.5k
        return njs_parser_after(parser, current, NULL, 1,
3225
13.5k
                                njs_parser_left_hand_side_expression_optional);
3226
3227
    /* OptionalExpression */
3228
140
    case NJS_TOKEN_CONDITIONAL:
3229
140
        njs_parser_next(parser, njs_parser_optional_expression_after);
3230
140
        break;
3231
3232
3.26M
    default:
3233
3.26M
        return njs_parser_stack_pop(parser);
3234
3.27M
    }
3235
3236
140
    return NJS_OK;
3237
3.27M
}
3238
3239
3240
static njs_int_t
3241
njs_parser_left_hand_side_expression_node(njs_parser_t *parser,
3242
    njs_lexer_token_t *token, njs_queue_link_t *current)
3243
7.44k
{
3244
7.44k
    parser->node = parser->target;
3245
3246
7.44k
    return njs_parser_stack_pop(parser);
3247
7.44k
}
3248
3249
3250
static njs_int_t
3251
njs_parser_left_hand_side_expression_optional(njs_parser_t *parser,
3252
    njs_lexer_token_t *token, njs_queue_link_t *current)
3253
7.29k
{
3254
    /* OptionalExpression */
3255
7.29k
    if (token->type == NJS_TOKEN_CONDITIONAL) {
3256
0
        njs_parser_next(parser, njs_parser_optional_expression_after);
3257
0
        return NJS_OK;
3258
0
    }
3259
3260
7.29k
    njs_parser_next(parser, njs_parser_optional_chain_after);
3261
3262
7.29k
    return NJS_OK;
3263
7.29k
}
3264
3265
3266
static njs_int_t
3267
njs_parser_expression_node(njs_parser_t *parser, njs_lexer_token_t *token,
3268
    njs_queue_link_t *current, njs_token_type_t type, njs_vmcode_t operation,
3269
    njs_parser_state_func_t after)
3270
16.3M
{
3271
16.3M
    njs_parser_node_t  *node;
3272
3273
16.3M
    if (parser->target != NULL) {
3274
17.3k
        parser->target->right = parser->node;
3275
17.3k
        parser->target->right->dest = parser->target;
3276
17.3k
        parser->node = parser->target;
3277
17.3k
    }
3278
3279
16.3M
    if (token->type != type) {
3280
16.3M
        return njs_parser_stack_pop(parser);
3281
16.3M
    }
3282
3283
17.3k
    node = njs_parser_node_new(parser, type);
3284
17.3k
    if (node == NULL) {
3285
0
        return NJS_ERROR;
3286
0
    }
3287
3288
17.3k
    node->token_line = token->line;
3289
17.3k
    node->u.operation = operation;
3290
17.3k
    node->left = parser->node;
3291
17.3k
    node->left->dest = node;
3292
3293
17.3k
    njs_lexer_consume_token(parser->lexer, 1);
3294
3295
17.3k
    return njs_parser_after(parser, current, node, 1, after);
3296
17.3k
}
3297
3298
3299
/*
3300
 * 12.4 Update Expressions.
3301
 */
3302
static njs_int_t
3303
njs_parser_update_expression(njs_parser_t *parser, njs_lexer_token_t *token,
3304
    njs_queue_link_t *current)
3305
3.30M
{
3306
3.30M
    njs_vmcode_t       operation;
3307
3.30M
    njs_parser_node_t  *node;
3308
3309
3.30M
    switch (token->type) {
3310
1
    case NJS_TOKEN_INCREMENT:
3311
1
        operation = NJS_VMCODE_INCREMENT;
3312
1
        break;
3313
3314
2.75k
    case NJS_TOKEN_DECREMENT:
3315
2.75k
        operation = NJS_VMCODE_DECREMENT;
3316
2.75k
        break;
3317
3318
3.29M
    default:
3319
3.29M
        njs_parser_next(parser, njs_parser_left_hand_side_expression);
3320
3321
3.29M
        return njs_parser_after(parser, current, NULL, 1,
3322
3.30M
                                njs_parser_update_expression_post);
3323
3.30M
    }
3324
3325
2.75k
    node = njs_parser_node_new(parser, token->type);
3326
2.75k
    if (node == NULL) {
3327
0
        return NJS_ERROR;
3328
0
    }
3329
3330
2.75k
    node->token_line = token->line;
3331
2.75k
    node->u.operation = operation;
3332
3333
2.75k
    njs_lexer_consume_token(parser->lexer, 1);
3334
2.75k
    njs_parser_next(parser, njs_parser_left_hand_side_expression);
3335
3336
2.75k
    return njs_parser_after(parser, current, node, 1,
3337
2.75k
                            njs_parser_update_expression_unary);
3338
2.75k
}
3339
3340
3341
static njs_int_t
3342
njs_parser_update_expression_post(njs_parser_t *parser,
3343
    njs_lexer_token_t *token, njs_queue_link_t *current)
3344
3.25M
{
3345
3.25M
    njs_vmcode_t       operation;
3346
3.25M
    njs_token_type_t   type;
3347
3.25M
    njs_parser_node_t  *node;
3348
3349
    /* [no LineTerminator here] */
3350
3351
3.25M
    switch (token->type) {
3352
4.45k
    case NJS_TOKEN_INCREMENT:
3353
4.45k
        type = NJS_TOKEN_POST_INCREMENT;
3354
4.45k
        operation = NJS_VMCODE_POST_INCREMENT;
3355
4.45k
        break;
3356
3357
3.88k
    case NJS_TOKEN_DECREMENT:
3358
3.88k
        type = NJS_TOKEN_POST_DECREMENT;
3359
3.88k
        operation = NJS_VMCODE_POST_DECREMENT;
3360
3.88k
        break;
3361
3362
3.25M
    default:
3363
3.25M
        return njs_parser_stack_pop(parser);
3364
3.25M
    }
3365
3366
8.34k
    if (parser->lexer->prev_type == NJS_TOKEN_LINE_END) {
3367
7
        return njs_parser_stack_pop(parser);
3368
7
    }
3369
3370
8.33k
    if (!njs_parser_is_lvalue(parser->node)) {
3371
0
        njs_lexer_consume_token(parser->lexer, 1);
3372
3373
0
        njs_parser_ref_error(parser,
3374
0
                             "Invalid left-hand side in postfix operation");
3375
0
        return NJS_DONE;
3376
0
    }
3377
3378
8.33k
    node = njs_parser_node_new(parser, type);
3379
8.33k
    if (node == NULL) {
3380
0
        return NJS_ERROR;
3381
0
    }
3382
3383
8.33k
    node->token_line = token->line;
3384
8.33k
    node->u.operation = operation;
3385
8.33k
    node->left = parser->node;
3386
3387
8.33k
    parser->node = node;
3388
3389
8.33k
    njs_lexer_consume_token(parser->lexer, 1);
3390
3391
8.33k
    return njs_parser_stack_pop(parser);
3392
8.33k
}
3393
3394
3395
static njs_int_t
3396
njs_parser_update_expression_unary(njs_parser_t *parser,
3397
    njs_lexer_token_t *token, njs_queue_link_t *current)
3398
2.75k
{
3399
2.75k
    if (!njs_parser_is_lvalue(parser->node)) {
3400
0
        njs_parser_ref_error(parser,
3401
0
                             "Invalid left-hand side in prefix operation");
3402
0
        return NJS_DONE;
3403
0
    }
3404
3405
2.75k
    parser->target->left = parser->node;
3406
2.75k
    parser->node = parser->target;
3407
3408
2.75k
    return njs_parser_stack_pop(parser);
3409
2.75k
}
3410
3411
3412
/*
3413
 * 12.5 Unary Operators.
3414
 */
3415
static njs_int_t
3416
njs_parser_unary_expression(njs_parser_t *parser, njs_lexer_token_t *token,
3417
    njs_queue_link_t *current)
3418
3.32M
{
3419
3.32M
    njs_vmcode_t       operation;
3420
3.32M
    njs_token_type_t   type;
3421
3.32M
    njs_parser_node_t  *node;
3422
3423
3.32M
    switch (token->type) {
3424
16
    case NJS_TOKEN_DELETE:
3425
16
        type = NJS_TOKEN_DELETE;
3426
16
        operation = NJS_VMCODE_DELETE;
3427
16
        break;
3428
3429
10
    case NJS_TOKEN_VOID:
3430
10
        type = NJS_TOKEN_VOID;
3431
10
        operation = NJS_VMCODE_VOID;
3432
10
        break;
3433
3434
735
    case NJS_TOKEN_TYPEOF:
3435
735
        type = NJS_TOKEN_TYPEOF;
3436
735
        operation = NJS_VMCODE_TYPEOF;
3437
735
        break;
3438
3439
1.97k
    case NJS_TOKEN_ADDITION:
3440
1.97k
        type = NJS_TOKEN_UNARY_PLUS;
3441
1.97k
        operation = NJS_VMCODE_UNARY_PLUS;
3442
1.97k
        break;
3443
3444
12.8k
    case NJS_TOKEN_SUBTRACTION:
3445
12.8k
        type = NJS_TOKEN_UNARY_NEGATION;
3446
12.8k
        operation = NJS_VMCODE_UNARY_NEGATION;
3447
12.8k
        break;
3448
3449
4.44k
    case NJS_TOKEN_BITWISE_NOT:
3450
4.44k
        type = NJS_TOKEN_BITWISE_NOT;
3451
4.44k
        operation = NJS_VMCODE_BITWISE_NOT;
3452
4.44k
        break;
3453
3454
59
    case NJS_TOKEN_LOGICAL_NOT:
3455
59
        type = NJS_TOKEN_LOGICAL_NOT;
3456
59
        operation = NJS_VMCODE_LOGICAL_NOT;
3457
59
        break;
3458
3459
    /* AwaitExpression */
3460
43
    case NJS_TOKEN_AWAIT:
3461
43
        njs_parser_next(parser, njs_parser_await);
3462
43
        return NJS_OK;
3463
3464
3.30M
    default:
3465
3.30M
        njs_parser_next(parser, njs_parser_update_expression);
3466
3467
3.30M
        return njs_parser_after(parser, current, parser->target, 1,
3468
3.32M
                                njs_parser_unary_expression_after);
3469
3.32M
    }
3470
3471
20.0k
    node = njs_parser_node_new(parser, type);
3472
20.0k
    if (node == NULL) {
3473
0
        return NJS_ERROR;
3474
0
    }
3475
3476
20.0k
    node->token_line = token->line;
3477
20.0k
    node->u.operation = operation;
3478
3479
20.0k
    parser->target = node;
3480
3481
20.0k
    njs_lexer_consume_token(parser->lexer, 1);
3482
3483
20.0k
    return njs_parser_after(parser, current, node, 1,
3484
20.0k
                            njs_parser_unary_expression_next);
3485
20.0k
}
3486
3487
3488
static njs_int_t
3489
njs_parser_unary_expression_after(njs_parser_t *parser,
3490
    njs_lexer_token_t *token, njs_queue_link_t *current)
3491
3.26M
{
3492
3.26M
    if (parser->target == NULL &&
3493
3.26M
        token->type == NJS_TOKEN_EXPONENTIATION)
3494
36
    {
3495
36
        return njs_parser_exponentiation_expression_match(parser, token,
3496
36
                                                          current);
3497
36
    }
3498
3499
3.26M
    return njs_parser_stack_pop(parser);
3500
3.26M
}
3501
3502
3503
static njs_int_t
3504
njs_parser_unary_expression_next(njs_parser_t *parser,
3505
    njs_lexer_token_t *token, njs_queue_link_t *current)
3506
20.0k
{
3507
20.0k
    double             num;
3508
20.0k
    njs_token_type_t   type;
3509
20.0k
    njs_parser_node_t  *node;
3510
3511
20.0k
    type = parser->target->token_type;
3512
20.0k
    node = parser->node;
3513
3514
20.0k
    if (token->type == NJS_TOKEN_EXPONENTIATION) {
3515
0
        njs_parser_syntax_error(parser, "Either left-hand side or entire "
3516
0
                                "exponentiation must be parenthesized");
3517
0
        return NJS_DONE;
3518
0
    }
3519
3520
20.0k
    if (node->token_type == NJS_TOKEN_NUMBER) {
3521
8.45k
        if (type == NJS_TOKEN_UNARY_PLUS) {
3522
            /* Skip the unary plus of number. */
3523
16
            return njs_parser_stack_pop(parser);
3524
16
        }
3525
3526
8.43k
        if (type == NJS_TOKEN_UNARY_NEGATION) {
3527
            /* Optimization of common negative number. */
3528
8.38k
            num = -njs_number(&node->u.value);
3529
8.38k
            njs_set_number(&node->u.value, num);
3530
3531
8.38k
            return njs_parser_stack_pop(parser);
3532
8.38k
        }
3533
8.43k
    }
3534
3535
11.6k
    if (type == NJS_TOKEN_DELETE) {
3536
16
        switch (node->token_type) {
3537
3538
16
        case NJS_TOKEN_PROPERTY:
3539
16
            node->token_type = NJS_TOKEN_PROPERTY_DELETE;
3540
16
            node->u.operation = NJS_VMCODE_PROPERTY_DELETE;
3541
3542
16
            return njs_parser_stack_pop(parser);
3543
3544
0
        case NJS_TOKEN_NAME:
3545
0
            njs_parser_syntax_error(parser,
3546
0
                                    "Delete of an unqualified identifier");
3547
0
            return NJS_DONE;
3548
3549
0
        default:
3550
0
            break;
3551
16
        }
3552
16
    }
3553
3554
11.6k
    if (type == NJS_TOKEN_TYPEOF && node->token_type == NJS_TOKEN_NAME) {
3555
264
        node->u.reference.type = NJS_TYPEOF;
3556
264
    }
3557
3558
11.6k
    parser->target->left = parser->node;
3559
11.6k
    parser->target->left->dest = parser->target;
3560
11.6k
    parser->node = parser->target;
3561
3562
11.6k
    return njs_parser_stack_pop(parser);
3563
11.6k
}
3564
3565
3566
static njs_int_t
3567
njs_parser_await(njs_parser_t *parser, njs_lexer_token_t *token,
3568
    njs_queue_link_t *current)
3569
43
{
3570
43
    njs_parser_node_t   *node;
3571
3572
43
    if (!njs_function_scope(parser->scope)->async) {
3573
0
        njs_parser_syntax_error(parser,
3574
0
                                "await is only valid in async functions");
3575
0
        return NJS_ERROR;
3576
0
    }
3577
3578
43
    if (parser->scope->in_args > 0) {
3579
0
        njs_parser_syntax_error(parser, "await in arguments not supported");
3580
0
        return NJS_ERROR;
3581
0
    }
3582
3583
43
    node = njs_parser_node_new(parser, NJS_TOKEN_AWAIT);
3584
43
    if (njs_slow_path(node == NULL)) {
3585
0
        return NJS_ERROR;
3586
0
    }
3587
3588
43
    node->token_line = token->line;
3589
3590
43
    njs_lexer_consume_token(parser->lexer, 1);
3591
3592
43
    parser->node = NULL;
3593
3594
43
    njs_parser_next(parser, njs_parser_unary_expression);
3595
3596
43
    return njs_parser_after(parser, current, node, 0,
3597
43
                            njs_parser_await_after);
3598
43
}
3599
3600
3601
static njs_int_t
3602
njs_parser_await_after(njs_parser_t *parser, njs_lexer_token_t *token,
3603
    njs_queue_link_t *current)
3604
43
{
3605
43
    if (parser->ret != NJS_OK) {
3606
0
        return njs_parser_failed(parser);
3607
0
    }
3608
3609
43
    parser->target->right = parser->node;
3610
43
    parser->node = parser->target;
3611
3612
43
    return njs_parser_stack_pop(parser);
3613
43
}
3614
3615
3616
/*
3617
 * 12.6 Exponentiation Operator.
3618
 */
3619
static njs_int_t
3620
njs_parser_exponentiation_expression(njs_parser_t *parser,
3621
    njs_lexer_token_t *token, njs_queue_link_t *current)
3622
3.30M
{
3623
3.30M
    parser->target = NULL;
3624
3625
3.30M
    if (parser->use_lhs == 0) {
3626
3.30M
        njs_parser_next(parser, njs_parser_unary_expression);
3627
3628
        /* For UpdateExpression, see njs_parser_unary_expression_after. */
3629
3630
3.30M
        return NJS_OK;
3631
3.30M
    } else {
3632
0
        parser->use_lhs = 0;
3633
3634
0
        return njs_parser_update_expression_post(parser, token, current);
3635
0
    }
3636
3.30M
}
3637
3638
3639
static njs_int_t
3640
njs_parser_exponentiation_expression_match(njs_parser_t *parser,
3641
    njs_lexer_token_t *token, njs_queue_link_t *current)
3642
72
{
3643
72
    njs_parser_node_t  *node;
3644
3645
72
    if (parser->target != NULL) {
3646
36
        parser->target->right = parser->node;
3647
36
        parser->target->right->dest = parser->target;
3648
36
        parser->node = parser->target;
3649
3650
36
        return njs_parser_stack_pop(parser);
3651
36
    }
3652
3653
36
    if (token->type != NJS_TOKEN_EXPONENTIATION) {
3654
0
        return njs_parser_stack_pop(parser);
3655
0
    }
3656
3657
36
    node = njs_parser_node_new(parser, token->type);
3658
36
    if (node == NULL) {
3659
0
        return NJS_ERROR;
3660
0
    }
3661
3662
36
    node->token_line = token->line;
3663
36
    node->u.operation = NJS_VMCODE_EXPONENTIATION;
3664
36
    node->left = parser->node;
3665
36
    node->left->dest = node;
3666
3667
36
    njs_lexer_consume_token(parser->lexer, 1);
3668
3669
36
    njs_parser_next(parser, njs_parser_exponentiation_expression);
3670
3671
36
    return njs_parser_after(parser, current, node, 1,
3672
36
                            njs_parser_exponentiation_expression_match);
3673
36
}
3674
3675
3676
/*
3677
 * 12.7 Multiplicative Operators.
3678
 */
3679
static njs_int_t
3680
njs_parser_multiplicative_expression(njs_parser_t *parser,
3681
    njs_lexer_token_t *token, njs_queue_link_t *current)
3682
3.28M
{
3683
3.28M
    njs_parser_next(parser, njs_parser_exponentiation_expression);
3684
3685
3.28M
    return njs_parser_after(parser, current, NULL, 1,
3686
3.28M
                            njs_parser_multiplicative_expression_match);
3687
3.28M
}
3688
3689
3690
static njs_int_t
3691
njs_parser_multiplicative_expression_match(njs_parser_t *parser,
3692
    njs_lexer_token_t *token, njs_queue_link_t *current)
3693
3.26M
{
3694
3.26M
    njs_vmcode_t       operation;
3695
3.26M
    njs_parser_node_t  *node;
3696
3697
3.26M
    if (parser->target != NULL) {
3698
9.23k
        parser->target->right = parser->node;
3699
9.23k
        parser->target->right->dest = parser->target;
3700
9.23k
        parser->node = parser->target;
3701
9.23k
    }
3702
3703
3.26M
    switch (token->type) {
3704
3.83k
    case NJS_TOKEN_MULTIPLICATION:
3705
3.83k
        operation = NJS_VMCODE_MULTIPLICATION;
3706
3.83k
        break;
3707
3708
5.13k
    case NJS_TOKEN_DIVISION:
3709
5.13k
        operation = NJS_VMCODE_DIVISION;
3710
5.13k
        break;
3711
3712
3.38k
    case NJS_TOKEN_REMAINDER:
3713
3.38k
        operation = NJS_VMCODE_REMAINDER;
3714
3.38k
        break;
3715
3716
3.24M
    default:
3717
3.24M
        return njs_parser_stack_pop(parser);
3718
3.26M
    }
3719
3720
12.3k
    node = njs_parser_node_new(parser, token->type);
3721
12.3k
    if (node == NULL) {
3722
0
        return NJS_ERROR;
3723
0
    }
3724
3725
12.3k
    node->token_line = token->line;
3726
12.3k
    node->u.operation = operation;
3727
12.3k
    node->left = parser->node;
3728
12.3k
    node->left->dest = node;
3729
3730
12.3k
    njs_lexer_consume_token(parser->lexer, 1);
3731
3732
12.3k
    njs_parser_next(parser, njs_parser_exponentiation_expression);
3733
3734
12.3k
    return njs_parser_after(parser, current, node, 1,
3735
12.3k
                            njs_parser_multiplicative_expression_match);
3736
12.3k
}
3737
3738
3739
/*
3740
 * 12.8 Additive Operators.
3741
 */
3742
static njs_int_t
3743
njs_parser_additive_expression(njs_parser_t *parser, njs_lexer_token_t *token,
3744
    njs_queue_link_t *current)
3745
3.24M
{
3746
3.24M
    njs_parser_next(parser, njs_parser_multiplicative_expression);
3747
3748
3.24M
    return njs_parser_after(parser, current, NULL, 1,
3749
3.24M
                            njs_parser_additive_expression_match);
3750
3.24M
}
3751
3752
3753
static njs_int_t
3754
njs_parser_additive_expression_match(njs_parser_t *parser,
3755
    njs_lexer_token_t *token, njs_queue_link_t *current)
3756
3.24M
{
3757
3.24M
    njs_vmcode_t       operation;
3758
3.24M
    njs_parser_node_t  *node;
3759
3760
3.24M
    if (parser->target != NULL) {
3761
41.9k
        parser->target->right = parser->node;
3762
41.9k
        parser->target->right->dest = parser->target;
3763
41.9k
        parser->node = parser->target;
3764
41.9k
    }
3765
3766
3.24M
    switch (token->type) {
3767
5.74k
    case NJS_TOKEN_ADDITION:
3768
5.74k
        operation = NJS_VMCODE_ADDITION;
3769
5.74k
        break;
3770
3771
36.2k
    case NJS_TOKEN_SUBTRACTION:
3772
36.2k
        operation = NJS_VMCODE_SUBTRACTION;
3773
36.2k
        break;
3774
3775
3.20M
    default:
3776
3.20M
        return njs_parser_stack_pop(parser);
3777
3.24M
    }
3778
3779
41.9k
    node = njs_parser_node_new(parser, token->type);
3780
41.9k
    if (node == NULL) {
3781
0
        return NJS_ERROR;
3782
0
    }
3783
3784
41.9k
    node->token_line = token->line;
3785
41.9k
    node->u.operation = operation;
3786
41.9k
    node->left = parser->node;
3787
41.9k
    node->left->dest = node;
3788
3789
41.9k
    njs_lexer_consume_token(parser->lexer, 1);
3790
3791
41.9k
    njs_parser_next(parser, njs_parser_multiplicative_expression);
3792
3793
41.9k
    return njs_parser_after(parser, current, node, 1,
3794
41.9k
                            njs_parser_additive_expression_match);
3795
41.9k
}
3796
3797
3798
/*
3799
 * 12.9 Bitwise Shift Operators
3800
 */
3801
static njs_int_t
3802
njs_parser_shift_expression(njs_parser_t *parser, njs_lexer_token_t *token,
3803
    njs_queue_link_t *current)
3804
3.24M
{
3805
3.24M
    njs_parser_next(parser, njs_parser_additive_expression);
3806
3807
3.24M
    return njs_parser_after(parser, current, NULL, 1,
3808
3.24M
                            njs_parser_shift_expression_match);
3809
3.24M
}
3810
3811
3812
static njs_int_t
3813
njs_parser_shift_expression_match(njs_parser_t *parser,
3814
    njs_lexer_token_t *token, njs_queue_link_t *current)
3815
3.20M
{
3816
3.20M
    njs_vmcode_t       operation;
3817
3.20M
    njs_parser_node_t  *node;
3818
3819
3.20M
    if (parser->target != NULL) {
3820
3.35k
        parser->target->right = parser->node;
3821
3.35k
        parser->target->right->dest = parser->target;
3822
3.35k
        parser->node = parser->target;
3823
3.35k
    }
3824
3825
3.20M
    switch (token->type) {
3826
213
    case NJS_TOKEN_RIGHT_SHIFT:
3827
213
        operation = NJS_VMCODE_RIGHT_SHIFT;
3828
213
        break;
3829
3830
3.10k
    case NJS_TOKEN_LEFT_SHIFT:
3831
3.10k
        operation = NJS_VMCODE_LEFT_SHIFT;
3832
3.10k
        break;
3833
3834
36
    case NJS_TOKEN_UNSIGNED_RIGHT_SHIFT:
3835
36
        operation = NJS_VMCODE_UNSIGNED_RIGHT_SHIFT;
3836
36
        break;
3837
3838
3.20M
    default:
3839
3.20M
        return njs_parser_stack_pop(parser);
3840
3.20M
    }
3841
3842
3.35k
    node = njs_parser_node_new(parser, token->type);
3843
3.35k
    if (node == NULL) {
3844
0
        return NJS_ERROR;
3845
0
    }
3846
3847
3.35k
    node->token_line = token->line;
3848
3.35k
    node->u.operation = operation;
3849
3.35k
    node->left = parser->node;
3850
3.35k
    node->left->dest = node;
3851
3852
3.35k
    njs_lexer_consume_token(parser->lexer, 1);
3853
3854
3.35k
    njs_parser_next(parser, njs_parser_additive_expression);
3855
3856
3.35k
    return njs_parser_after(parser, current, node, 1,
3857
3.35k
                            njs_parser_shift_expression_match);
3858
3.35k
}
3859
3860
3861
/*
3862
 * 12.10 Relational Operators.
3863
 */
3864
static njs_int_t
3865
njs_parser_relational_expression(njs_parser_t *parser,
3866
    njs_lexer_token_t *token, njs_queue_link_t *current)
3867
3.23M
{
3868
3.23M
    njs_parser_next(parser, njs_parser_shift_expression);
3869
3870
3.23M
    return njs_parser_after(parser, current, NULL, 1,
3871
3.23M
                            njs_parser_relational_expression_match);
3872
3.23M
}
3873
3874
3875
static njs_int_t
3876
njs_parser_relational_expression_match(njs_parser_t *parser,
3877
    njs_lexer_token_t *token, njs_queue_link_t *current)
3878
3.20M
{
3879
3.20M
    njs_vmcode_t       operation;
3880
3.20M
    njs_parser_node_t  *node;
3881
3882
3.20M
    if (parser->target != NULL) {
3883
8.33k
        parser->target->right = parser->node;
3884
8.33k
        parser->target->right->dest = parser->target;
3885
8.33k
        parser->node = parser->target;
3886
8.33k
    }
3887
3888
3.20M
    switch (token->type) {
3889
357
    case NJS_TOKEN_LESS:
3890
357
        operation = NJS_VMCODE_LESS;
3891
357
        break;
3892
3893
4.74k
    case NJS_TOKEN_GREATER:
3894
4.74k
        operation = NJS_VMCODE_GREATER;
3895
4.74k
        break;
3896
3897
737
    case NJS_TOKEN_LESS_OR_EQUAL:
3898
737
        operation = NJS_VMCODE_LESS_OR_EQUAL;
3899
737
        break;
3900
3901
6
    case NJS_TOKEN_GREATER_OR_EQUAL:
3902
6
        operation = NJS_VMCODE_GREATER_OR_EQUAL;
3903
6
        break;
3904
3905
0
    case NJS_TOKEN_INSTANCEOF:
3906
0
        operation = NJS_VMCODE_INSTANCE_OF;
3907
0
        break;
3908
3909
2.48k
    case NJS_TOKEN_IN:
3910
2.48k
        if (njs_lexer_in_fail_get(parser->lexer)) {
3911
0
            njs_parser_syntax_error(parser, "Invalid left-hand side in for-loop");
3912
0
            return NJS_ERROR;
3913
0
        }
3914
2.48k
        operation = NJS_VMCODE_PROPERTY_IN;
3915
2.48k
        break;
3916
3917
3.19M
    default:
3918
3.19M
        return njs_parser_stack_pop(parser);
3919
3.20M
    }
3920
3921
8.33k
    node = njs_parser_node_new(parser, token->type);
3922
8.33k
    if (node == NULL) {
3923
0
        return NJS_ERROR;
3924
0
    }
3925
3926
8.33k
    node->token_line = token->line;
3927
8.33k
    node->u.operation = operation;
3928
8.33k
    node->left = parser->node;
3929
8.33k
    node->left->dest = node;
3930
3931
8.33k
    njs_lexer_consume_token(parser->lexer, 1);
3932
3933
8.33k
    njs_parser_next(parser, njs_parser_shift_expression);
3934
3935
8.33k
    return njs_parser_after(parser, current, node, 1,
3936
8.33k
                            njs_parser_relational_expression_match);
3937
8.33k
}
3938
3939
3940
/*
3941
 * 12.11 Equality Operators.
3942
 */
3943
static njs_int_t
3944
njs_parser_equality_expression(njs_parser_t *parser, njs_lexer_token_t *token,
3945
    njs_queue_link_t *current)
3946
3.23M
{
3947
3.23M
    njs_parser_next(parser, njs_parser_relational_expression);
3948
3949
3.23M
    return njs_parser_after(parser, current, NULL, 1,
3950
3.23M
                            njs_parser_equality_expression_match);
3951
3.23M
}
3952
3953
3954
static njs_int_t
3955
njs_parser_equality_expression_match(njs_parser_t *parser,
3956
    njs_lexer_token_t *token, njs_queue_link_t *current)
3957
3.19M
{
3958
3.19M
    njs_vmcode_t       operation;
3959
3.19M
    njs_parser_node_t  *node;
3960
3961
3.19M
    if (parser->target != NULL) {
3962
567
        parser->target->right = parser->node;
3963
567
        parser->target->right->dest = parser->target;
3964
567
        parser->node = parser->target;
3965
567
    }
3966
3967
3.19M
    switch (token->type) {
3968
146
    case NJS_TOKEN_EQUAL:
3969
146
        operation = NJS_VMCODE_EQUAL;
3970
146
        break;
3971
3972
117
    case NJS_TOKEN_NOT_EQUAL:
3973
117
        operation = NJS_VMCODE_NOT_EQUAL;
3974
117
        break;
3975
3976
166
    case NJS_TOKEN_STRICT_EQUAL:
3977
166
        operation = NJS_VMCODE_STRICT_EQUAL;
3978
166
        break;
3979
3980
138
    case NJS_TOKEN_STRICT_NOT_EQUAL:
3981
138
        operation = NJS_VMCODE_STRICT_NOT_EQUAL;
3982
138
        break;
3983
3984
3.19M
    default:
3985
3.19M
        return njs_parser_stack_pop(parser);
3986
3.19M
    }
3987
3988
567
    node = njs_parser_node_new(parser, token->type);
3989
567
    if (node == NULL) {
3990
0
        return NJS_ERROR;
3991
0
    }
3992
3993
567
    node->token_line = token->line;
3994
567
    node->u.operation = operation;
3995
567
    node->left = parser->node;
3996
567
    node->left->dest = node;
3997
3998
567
    njs_lexer_consume_token(parser->lexer, 1);
3999
4000
567
    njs_parser_next(parser, njs_parser_relational_expression);
4001
4002
567
    return njs_parser_after(parser, current, node, 1,
4003
567
                            njs_parser_equality_expression_match);
4004
567
}
4005
4006
4007
/*
4008
 * 12.12 Binary Bitwise Operators.
4009
 */
4010
static njs_int_t
4011
njs_parser_bitwise_AND_expression(njs_parser_t *parser,
4012
    njs_lexer_token_t *token, njs_queue_link_t *current)
4013
3.23M
{
4014
3.23M
    njs_parser_next(parser, njs_parser_equality_expression);
4015
4016
3.23M
    return njs_parser_after(parser, current, NULL, 1,
4017
3.23M
                            njs_parser_bitwise_AND_expression_and);
4018
3.23M
}
4019
4020
4021
static njs_int_t
4022
njs_parser_bitwise_AND_expression_and(njs_parser_t *parser,
4023
    njs_lexer_token_t *token, njs_queue_link_t *current)
4024
3.19M
{
4025
3.19M
    njs_parser_next(parser, njs_parser_equality_expression);
4026
4027
3.19M
    return njs_parser_expression_node(parser, token, current,
4028
3.19M
                                       NJS_TOKEN_BITWISE_AND,
4029
3.19M
                                       NJS_VMCODE_BITWISE_AND,
4030
3.19M
                                       njs_parser_bitwise_AND_expression_and);
4031
3.19M
}
4032
4033
4034
static njs_int_t
4035
njs_parser_bitwise_XOR_expression(njs_parser_t *parser,
4036
    njs_lexer_token_t *token, njs_queue_link_t *current)
4037
3.22M
{
4038
3.22M
    njs_parser_next(parser, njs_parser_bitwise_AND_expression);
4039
4040
3.22M
    return njs_parser_after(parser, current, NULL, 1,
4041
3.22M
                            njs_parser_bitwise_XOR_expression_xor);
4042
3.22M
}
4043
4044
4045
static njs_int_t
4046
njs_parser_bitwise_XOR_expression_xor(njs_parser_t *parser,
4047
    njs_lexer_token_t *token, njs_queue_link_t *current)
4048
3.19M
{
4049
3.19M
    njs_parser_next(parser, njs_parser_bitwise_AND_expression);
4050
4051
3.19M
    return njs_parser_expression_node(parser, token, current,
4052
3.19M
                                       NJS_TOKEN_BITWISE_XOR,
4053
3.19M
                                       NJS_VMCODE_BITWISE_XOR,
4054
3.19M
                                       njs_parser_bitwise_XOR_expression_xor);
4055
3.19M
}
4056
4057
4058
static njs_int_t
4059
njs_parser_bitwise_OR_expression(njs_parser_t *parser,
4060
    njs_lexer_token_t *token, njs_queue_link_t *current)
4061
3.22M
{
4062
3.22M
    njs_parser_next(parser, njs_parser_bitwise_XOR_expression);
4063
4064
3.22M
    return njs_parser_after(parser, current, NULL, 1,
4065
3.22M
                            njs_parser_bitwise_OR_expression_or);
4066
3.22M
}
4067
4068
4069
static njs_int_t
4070
njs_parser_bitwise_OR_expression_or(njs_parser_t *parser,
4071
    njs_lexer_token_t *token, njs_queue_link_t *current)
4072
3.19M
{
4073
3.19M
    njs_parser_next(parser, njs_parser_bitwise_XOR_expression);
4074
4075
3.19M
    return njs_parser_expression_node(parser, token, current,
4076
3.19M
                                       NJS_TOKEN_BITWISE_OR,
4077
3.19M
                                       NJS_VMCODE_BITWISE_OR,
4078
3.19M
                                       njs_parser_bitwise_OR_expression_or);
4079
3.19M
}
4080
4081
4082
/*
4083
 * 12.13 Binary Logical Operators.
4084
 */
4085
static njs_int_t
4086
njs_parser_logical_AND_expression(njs_parser_t *parser,
4087
    njs_lexer_token_t *token, njs_queue_link_t *current)
4088
3.22M
{
4089
3.22M
    njs_parser_next(parser, njs_parser_bitwise_OR_expression);
4090
4091
3.22M
    return njs_parser_after(parser, current, NULL, 1,
4092
3.22M
                            njs_parser_logical_AND_expression_and);
4093
3.22M
}
4094
4095
4096
static njs_int_t
4097
njs_parser_logical_AND_expression_and(njs_parser_t *parser,
4098
    njs_lexer_token_t *token, njs_queue_link_t *current)
4099
3.18M
{
4100
3.18M
    njs_parser_next(parser, njs_parser_bitwise_OR_expression);
4101
4102
3.18M
    return njs_parser_expression_node(parser, token, current,
4103
3.18M
                                       NJS_TOKEN_LOGICAL_AND,
4104
3.18M
                                       NJS_VMCODE_TEST_IF_FALSE,
4105
3.18M
                                       njs_parser_logical_AND_expression_and);
4106
3.18M
}
4107
4108
4109
static njs_int_t
4110
njs_parser_logical_OR_expression(njs_parser_t *parser,
4111
    njs_lexer_token_t *token, njs_queue_link_t *current)
4112
3.22M
{
4113
3.22M
    njs_parser_next(parser, njs_parser_logical_AND_expression);
4114
4115
3.22M
    return njs_parser_after(parser, current, NULL, 1,
4116
3.22M
                            njs_parser_logical_OR_expression_or);
4117
3.22M
}
4118
4119
4120
static njs_int_t
4121
njs_parser_logical_OR_expression_or(njs_parser_t *parser,
4122
    njs_lexer_token_t *token, njs_queue_link_t *current)
4123
3.18M
{
4124
3.18M
    njs_parser_next(parser, njs_parser_logical_AND_expression);
4125
4126
3.18M
    return njs_parser_expression_node(parser, token, current,
4127
3.18M
                                       NJS_TOKEN_LOGICAL_OR,
4128
3.18M
                                       NJS_VMCODE_TEST_IF_TRUE,
4129
3.18M
                                       njs_parser_logical_OR_expression_or);
4130
3.18M
}
4131
4132
4133
static njs_int_t
4134
njs_parser_coalesce_expression(njs_parser_t *parser, njs_lexer_token_t *token,
4135
    njs_queue_link_t *current)
4136
3.18M
{
4137
3.18M
    njs_token_type_t   type;
4138
3.18M
    njs_parser_node_t  *node;
4139
4140
3.18M
    node = parser->node;
4141
4142
3.18M
    if (parser->target != NULL) {
4143
12
        parser->target->right = node;
4144
12
        parser->target->right->dest = parser->target;
4145
12
        parser->node = parser->target;
4146
12
    }
4147
4148
3.18M
    if (token->type != NJS_TOKEN_COALESCE) {
4149
3.18M
        return njs_parser_stack_pop(parser);
4150
3.18M
    }
4151
4152
12
    type = node->token_type;
4153
4154
12
    if (parser->lexer->prev_type != NJS_TOKEN_CLOSE_PARENTHESIS
4155
12
        && (type == NJS_TOKEN_LOGICAL_OR || type == NJS_TOKEN_LOGICAL_AND))
4156
0
    {
4157
0
        return njs_parser_failed(parser);
4158
0
    }
4159
4160
12
    node = njs_parser_node_new(parser, NJS_TOKEN_COALESCE);
4161
12
    if (node == NULL) {
4162
0
        return NJS_ERROR;
4163
0
    }
4164
4165
12
    node->token_line = token->line;
4166
12
    node->u.operation = NJS_VMCODE_COALESCE;
4167
12
    node->left = parser->node;
4168
12
    node->left->dest = node;
4169
4170
12
    njs_lexer_consume_token(parser->lexer, 1);
4171
12
    njs_parser_next(parser, njs_parser_bitwise_OR_expression);
4172
4173
12
    return njs_parser_after(parser, current, node, 1,
4174
12
                            njs_parser_coalesce_expression);
4175
12
}
4176
4177
4178
static njs_int_t
4179
njs_parser_short_circuit_expression(njs_parser_t *parser,
4180
    njs_lexer_token_t *token, njs_queue_link_t *current)
4181
3.22M
{
4182
3.22M
    njs_parser_next(parser, njs_parser_logical_OR_expression);
4183
4184
3.22M
    return njs_parser_after(parser, current, NULL, 1,
4185
3.22M
                            njs_parser_coalesce_expression);
4186
3.22M
}
4187
4188
4189
/*
4190
 * 12.14 Conditional Operator ( ? : ).
4191
 */
4192
static njs_int_t
4193
njs_parser_conditional_expression(njs_parser_t *parser,
4194
    njs_lexer_token_t *token, njs_queue_link_t *current)
4195
3.22M
{
4196
3.22M
    njs_parser_next(parser, njs_parser_short_circuit_expression);
4197
4198
3.22M
    return njs_parser_after(parser, current, NULL, 1,
4199
3.22M
                            njs_parser_conditional_question_mark);
4200
3.22M
}
4201
4202
4203
static njs_int_t
4204
njs_parser_conditional_question_mark(njs_parser_t *parser,
4205
    njs_lexer_token_t *token, njs_queue_link_t *current)
4206
3.18M
{
4207
3.18M
    njs_parser_node_t  *node, *cond;
4208
4209
3.18M
    if (token->type != NJS_TOKEN_CONDITIONAL) {
4210
3.18M
        return njs_parser_stack_pop(parser);
4211
3.18M
    }
4212
4213
140
    cond = njs_parser_node_new(parser, NJS_TOKEN_CONDITIONAL);
4214
140
    if (cond == NULL) {
4215
0
        return NJS_ERROR;
4216
0
    }
4217
4218
140
    cond->token_line = token->line;
4219
140
    cond->left = parser->node;
4220
4221
140
    node = njs_parser_node_new(parser, NJS_TOKEN_BRANCHING);
4222
140
    if (node == NULL) {
4223
0
        return NJS_ERROR;
4224
0
    }
4225
4226
140
    node->token_line = token->line;
4227
140
    cond->right = node;
4228
4229
140
    njs_lexer_consume_token(parser->lexer, 1);
4230
4231
140
    if (njs_lexer_in_stack_push(parser->lexer) != NJS_OK) {
4232
0
        return NJS_ERROR;
4233
0
    }
4234
4235
140
    njs_parser_next(parser, njs_parser_assignment_expression);
4236
4237
140
    return njs_parser_after(parser, current, cond, 1,
4238
140
                            njs_parser_conditional_colon);
4239
140
}
4240
4241
4242
static njs_int_t
4243
njs_parser_conditional_colon(njs_parser_t *parser, njs_lexer_token_t *token,
4244
    njs_queue_link_t *current)
4245
2
{
4246
2
    njs_parser_node_t  *node;
4247
4248
2
    if (token->type != NJS_TOKEN_COLON) {
4249
0
        return njs_parser_failed(parser);
4250
0
    }
4251
4252
2
    njs_lexer_in_stack_pop(parser->lexer);
4253
4254
2
    njs_lexer_consume_token(parser->lexer, 1);
4255
4256
2
    node = parser->target->right;
4257
4258
2
    node->left = parser->node;
4259
2
    node->left->dest = parser->target;
4260
4261
2
    njs_parser_next(parser, njs_parser_assignment_expression);
4262
4263
2
    return njs_parser_after(parser, current, parser->target, 1,
4264
2
                            njs_parser_conditional_colon_after);
4265
2
}
4266
4267
4268
static njs_int_t
4269
njs_parser_conditional_colon_after(njs_parser_t *parser,
4270
    njs_lexer_token_t *token, njs_queue_link_t *current)
4271
2
{
4272
2
    njs_parser_node_t  *node;
4273
4274
2
    node = parser->target->right;
4275
4276
2
    node->right = parser->node;
4277
2
    node->right->dest = parser->target;
4278
4279
2
    parser->node = parser->target;
4280
4281
2
    return njs_parser_stack_pop(parser);
4282
2
}
4283
4284
4285
/*
4286
 * 12.15 Assignment Operators.
4287
 */
4288
static njs_int_t
4289
njs_parser_assignment_expression(njs_parser_t *parser,
4290
    njs_lexer_token_t *token, njs_queue_link_t *current)
4291
3.25M
{
4292
3.25M
    njs_int_t  ret;
4293
4294
3.25M
    if (!parser->use_lhs) {
4295
3.25M
        ret = njs_parser_match_arrow_expression(parser, token);
4296
3.25M
        if (ret == NJS_OK) {
4297
34.1k
            njs_parser_next(parser, njs_parser_arrow_function);
4298
4299
34.1k
            return NJS_OK;
4300
4301
3.22M
        } else if (ret == NJS_ERROR) {
4302
0
            return NJS_ERROR;
4303
0
        }
4304
3.25M
    }
4305
4306
3.22M
    njs_parser_next(parser, njs_parser_conditional_expression);
4307
4308
3.22M
    return njs_parser_after(parser, current, NULL, 1,
4309
3.25M
                            njs_parser_assignment_expression_after);
4310
3.25M
}
4311
4312
4313
/*
4314
 * TODO: this function is a crutch.
4315
 * See NJS_TOKEN_OPEN_PARENTHESIS in njs_parser_primary_expression_test.
4316
 * and look CoverParenthesizedExpressionAndArrowParameterList in spec.
4317
 */
4318
static njs_int_t
4319
njs_parser_match_arrow_expression(njs_parser_t *parser,
4320
    njs_lexer_token_t *token)
4321
3.26M
{
4322
3.26M
    njs_bool_t  rest_parameters;
4323
4324
3.26M
    if (token->type == NJS_TOKEN_ASYNC) {
4325
1.02k
        token = njs_lexer_peek_token(parser->lexer, token, 1);
4326
1.02k
        if (token == NULL) {
4327
0
            return NJS_ERROR;
4328
0
        }
4329
1.02k
    }
4330
4331
3.26M
    if (token->type != NJS_TOKEN_OPEN_PARENTHESIS
4332
3.26M
        && !njs_lexer_token_is_binding_identifier(token))
4333
2.79M
    {
4334
2.79M
        return NJS_DECLINED;
4335
2.79M
    }
4336
4337
465k
    if (njs_lexer_token_is_binding_identifier(token)) {
4338
447k
        goto arrow;
4339
447k
    }
4340
4341
18.0k
    token = njs_lexer_peek_token(parser->lexer, token, 0);
4342
18.0k
    if (token == NULL) {
4343
0
        return NJS_ERROR;
4344
0
    }
4345
4346
18.0k
    rest_parameters = 0;
4347
4348
18.5k
    while (token->type != NJS_TOKEN_CLOSE_PARENTHESIS) {
4349
4350
18.5k
        if (rest_parameters) {
4351
0
            return NJS_DECLINED;
4352
0
        }
4353
4354
18.5k
        if (token->type == NJS_TOKEN_ELLIPSIS) {
4355
0
            rest_parameters = 1;
4356
4357
0
            token = njs_lexer_peek_token(parser->lexer, token, 0);
4358
0
            if (token == NULL) {
4359
0
                return NJS_ERROR;
4360
0
            }
4361
0
        }
4362
4363
18.5k
        if (!njs_lexer_token_is_binding_identifier(token)) {
4364
18.0k
            return NJS_DECLINED;
4365
18.0k
        }
4366
4367
540
        token = njs_lexer_peek_token(parser->lexer, token, 0);
4368
540
        if (token == NULL) {
4369
0
            return NJS_ERROR;
4370
0
        }
4371
4372
540
        if (token->type == NJS_TOKEN_COMMA) {
4373
502
            token = njs_lexer_peek_token(parser->lexer, token, 0);
4374
502
            if (token == NULL) {
4375
0
               return NJS_ERROR;
4376
0
            }
4377
502
        }
4378
540
    }
4379
4380
447k
arrow:
4381
4382
447k
    token = njs_lexer_peek_token(parser->lexer, token, 1);
4383
447k
    if (token == NULL) {
4384
0
        return NJS_ERROR;
4385
0
    }
4386
4387
447k
    if (token->type == NJS_TOKEN_LINE_END) {
4388
268k
        return NJS_DECLINED;
4389
268k
    }
4390
4391
178k
    if (token->type != NJS_TOKEN_ARROW) {
4392
144k
        return NJS_DECLINED;
4393
144k
    }
4394
4395
34.1k
    return NJS_OK;
4396
178k
}
4397
4398
4399
static njs_int_t
4400
njs_parser_assignment_expression_after(njs_parser_t *parser,
4401
    njs_lexer_token_t *token, njs_queue_link_t *current)
4402
3.18M
{
4403
3.18M
    return njs_parser_assignment_operator(parser, token, current);
4404
3.18M
}
4405
4406
4407
static njs_int_t
4408
njs_parser_assignment_operator(njs_parser_t *parser, njs_lexer_token_t *token,
4409
    njs_queue_link_t *current)
4410
3.18M
{
4411
3.18M
    njs_vmcode_t       operation;
4412
3.18M
    njs_token_type_t   type;
4413
3.18M
    njs_parser_node_t  *node;
4414
4415
3.18M
    switch (token->type) {
4416
45.1k
    case NJS_TOKEN_ASSIGNMENT:
4417
45.1k
        njs_thread_log_debug("JS: =");
4418
45.1k
        operation = NJS_VMCODE_MOVE;
4419
45.1k
        break;
4420
4421
3.53k
    case NJS_TOKEN_MULTIPLICATION_ASSIGNMENT:
4422
3.53k
        njs_thread_log_debug("JS: *=");
4423
3.53k
        operation = NJS_VMCODE_MULTIPLICATION;
4424
3.53k
        break;
4425
4426
0
    case NJS_TOKEN_DIVISION_ASSIGNMENT:
4427
0
        njs_thread_log_debug("JS: /=");
4428
0
        operation = NJS_VMCODE_DIVISION;
4429
0
        break;
4430
4431
2.83k
    case NJS_TOKEN_REMAINDER_ASSIGNMENT:
4432
2.83k
        njs_thread_log_debug("JS: %=");
4433
2.83k
        operation = NJS_VMCODE_REMAINDER;
4434
2.83k
        break;
4435
4436
7.80k
    case NJS_TOKEN_ADDITION_ASSIGNMENT:
4437
7.80k
        njs_thread_log_debug("JS: +=");
4438
7.80k
        operation = NJS_VMCODE_ADDITION;
4439
7.80k
        break;
4440
4441
9.21k
    case NJS_TOKEN_SUBTRACTION_ASSIGNMENT:
4442
9.21k
        njs_thread_log_debug("JS: -=");
4443
9.21k
        operation = NJS_VMCODE_SUBTRACTION;
4444
9.21k
        break;
4445
4446
0
    case NJS_TOKEN_LEFT_SHIFT_ASSIGNMENT:
4447
0
        njs_thread_log_debug("JS: <<=");
4448
0
        operation = NJS_VMCODE_LEFT_SHIFT;
4449
0
        break;
4450
4451
0
    case NJS_TOKEN_RIGHT_SHIFT_ASSIGNMENT:
4452
0
        njs_thread_log_debug("JS: >>=");
4453
0
        operation = NJS_VMCODE_RIGHT_SHIFT;
4454
0
        break;
4455
4456
0
    case NJS_TOKEN_UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
4457
0
        njs_thread_log_debug("JS: >>>=");
4458
0
        operation = NJS_VMCODE_UNSIGNED_RIGHT_SHIFT;
4459
0
        break;
4460
4461
0
    case NJS_TOKEN_BITWISE_AND_ASSIGNMENT:
4462
0
        njs_thread_log_debug("JS: &=");
4463
0
        operation = NJS_VMCODE_BITWISE_AND;
4464
0
        break;
4465
4466
15
    case NJS_TOKEN_BITWISE_XOR_ASSIGNMENT:
4467
15
        njs_thread_log_debug("JS: ^=");
4468
15
        operation = NJS_VMCODE_BITWISE_XOR;
4469
15
        break;
4470
4471
5.49k
    case NJS_TOKEN_BITWISE_OR_ASSIGNMENT:
4472
5.49k
        njs_thread_log_debug("JS: |=");
4473
5.49k
        operation = NJS_VMCODE_BITWISE_OR;
4474
5.49k
        break;
4475
4476
0
    case NJS_TOKEN_EXPONENTIATION_ASSIGNMENT:
4477
0
        njs_thread_log_debug("JS: **=");
4478
0
        operation = NJS_VMCODE_EXPONENTIATION;
4479
0
        break;
4480
4481
3.11M
    default:
4482
3.11M
        return njs_parser_stack_pop(parser);
4483
3.18M
    }
4484
4485
74.0k
    if (!njs_parser_is_lvalue(parser->node)) {
4486
0
        type = parser->node->token_type;
4487
4488
0
        if (njs_parser_restricted_identifier(type)) {
4489
0
            njs_parser_syntax_error(parser, "Identifier \"%s\" "
4490
0
                                    "is forbidden as left-hand in assignment",
4491
0
                                    (type == NJS_TOKEN_EVAL) ? "eval"
4492
0
                                                             : "arguments");
4493
4494
0
        } else {
4495
0
            njs_parser_ref_error(parser,
4496
0
                                 "Invalid left-hand side in assignment");
4497
0
        }
4498
4499
0
        return NJS_DONE;
4500
0
    }
4501
4502
74.0k
    node = njs_parser_node_new(parser, token->type);
4503
74.0k
    if (node == NULL) {
4504
0
        return NJS_ERROR;
4505
0
    }
4506
4507
74.0k
    node->token_line = token->line;
4508
74.0k
    node->u.operation = operation;
4509
74.0k
    node->left = parser->node;
4510
4511
74.0k
    njs_lexer_consume_token(parser->lexer, 1);
4512
74.0k
    njs_parser_next(parser, njs_parser_assignment_expression);
4513
4514
74.0k
    return njs_parser_after(parser, current, node, 1,
4515
74.0k
                            njs_parser_assignment_operator_after);
4516
74.0k
}
4517
4518
4519
static njs_int_t
4520
njs_parser_assignment_operator_after(njs_parser_t *parser,
4521
    njs_lexer_token_t *token, njs_queue_link_t *current)
4522
70.9k
{
4523
70.9k
    parser->target->right = parser->node;
4524
70.9k
    parser->node = parser->target;
4525
4526
70.9k
    return njs_parser_stack_pop(parser);
4527
70.9k
}
4528
4529
4530
/*
4531
 * 12.16 Comma Operator ( , ).
4532
 */
4533
static njs_int_t
4534
njs_parser_expression(njs_parser_t *parser, njs_lexer_token_t *token,
4535
    njs_queue_link_t *current)
4536
409k
{
4537
409k
    njs_parser_next(parser, njs_parser_assignment_expression);
4538
4539
409k
    return njs_parser_after(parser, current, NULL, 1,
4540
409k
                            njs_parser_expression_comma);
4541
409k
}
4542
4543
4544
static njs_int_t
4545
njs_parser_expression_comma(njs_parser_t *parser, njs_lexer_token_t *token,
4546
    njs_queue_link_t *current)
4547
384k
{
4548
384k
    njs_parser_next(parser, njs_parser_assignment_expression);
4549
4550
384k
    return njs_parser_expression_node(parser, token, current, NJS_TOKEN_COMMA,
4551
384k
                                       0,
4552
384k
                                       njs_parser_expression_comma);
4553
384k
}
4554
4555
4556
/*
4557
 * 13 Statements and Declarations.
4558
 */
4559
static njs_int_t
4560
njs_parser_statement(njs_parser_t *parser, njs_lexer_token_t *token,
4561
    njs_queue_link_t *current)
4562
389k
{
4563
389k
    njs_int_t                 ret;
4564
389k
    njs_queue_link_t          *lnk;
4565
389k
    njs_parser_stack_entry_t  *entry;
4566
4567
389k
    if (token->type == NJS_TOKEN_END) {
4568
6.25k
        lnk = njs_queue_next(njs_queue_first(&parser->stack));
4569
4570
6.25k
        if (lnk == njs_queue_head(&parser->stack)) {
4571
0
            return njs_parser_reject(parser);
4572
0
        }
4573
4574
6.25k
        entry = njs_queue_link_data(lnk, njs_parser_stack_entry_t, link);
4575
4576
6.25k
        if (entry->state == njs_parser_check_error_state) {
4577
6.25k
            return NJS_DONE;
4578
6.25k
        }
4579
4580
6
        return njs_parser_reject(parser);
4581
6.25k
    }
4582
4583
383k
    switch (token->type) {
4584
3.76k
    case NJS_TOKEN_SEMICOLON:
4585
3.76k
        njs_lexer_consume_token(parser->lexer, 1);
4586
3.76k
        return njs_parser_stack_pop(parser);
4587
4588
0
    case NJS_TOKEN_EXPORT:
4589
0
        parser->line = token->line;
4590
4591
0
        njs_lexer_consume_token(parser->lexer, 1);
4592
0
        njs_parser_next(parser, njs_parser_export);
4593
4594
0
        return njs_parser_after(parser, current, parser->node, 1,
4595
0
                                njs_parser_statement_after);
4596
4597
0
    case NJS_TOKEN_IMPORT:
4598
0
        parser->line = token->line;
4599
4600
0
        njs_lexer_consume_token(parser->lexer, 1);
4601
0
        njs_parser_next(parser, njs_parser_import);
4602
4603
0
        return njs_parser_after(parser, current, parser->node, 1,
4604
0
                                njs_parser_statement_after);
4605
379k
    default:
4606
379k
        break;
4607
383k
    }
4608
4609
379k
    ret = njs_parser_statement_wo_node(parser, token, current);
4610
379k
    if (ret != NJS_OK) {
4611
0
        return ret;
4612
0
    }
4613
4614
379k
    return njs_parser_after(parser, current, parser->node, 1,
4615
379k
                            njs_parser_statement_after);
4616
379k
}
4617
4618
4619
static njs_int_t
4620
njs_parser_statement_wo_node(njs_parser_t *parser, njs_lexer_token_t *token,
4621
    njs_queue_link_t *current)
4622
397k
{
4623
397k
    switch (token->type) {
4624
8.55k
    case NJS_TOKEN_OPEN_BRACE:
4625
8.55k
        njs_parser_next(parser, njs_parser_block_statement);
4626
8.55k
        break;
4627
4628
4.39k
    case NJS_TOKEN_VAR:
4629
4.39k
        njs_lexer_consume_token(parser->lexer, 1);
4630
4.39k
        return njs_parser_variable_statement(parser, token, current);
4631
4632
15
    case NJS_TOKEN_SEMICOLON:
4633
15
        njs_lexer_consume_token(parser->lexer, 1);
4634
15
        return njs_parser_stack_pop(parser);
4635
4636
3.18k
    case NJS_TOKEN_IF:
4637
3.18k
        njs_parser_next(parser, njs_parser_if_statement);
4638
3.18k
        break;
4639
4640
    /* BreakableStatement */
4641
1.55k
    case NJS_TOKEN_DO:
4642
1.55k
        njs_parser_next(parser, njs_parser_iteration_statement_do);
4643
1.55k
        break;
4644
4645
311
    case NJS_TOKEN_WHILE:
4646
311
        njs_parser_next(parser, njs_parser_iteration_statement_while);
4647
311
        break;
4648
4649
10.5k
    case NJS_TOKEN_FOR:
4650
10.5k
        njs_parser_next(parser, njs_parser_iteration_statement_for);
4651
10.5k
        break;
4652
4653
276
    case NJS_TOKEN_SWITCH:
4654
276
        njs_parser_next(parser, njs_parser_switch_statement);
4655
276
        break;
4656
4657
215
    case NJS_TOKEN_CONTINUE:
4658
215
        njs_parser_next(parser, njs_parser_continue_statement);
4659
215
        break;
4660
4661
463
    case NJS_TOKEN_BREAK:
4662
463
        njs_parser_next(parser, njs_parser_break_statement);
4663
463
        break;
4664
4665
11.3k
    case NJS_TOKEN_RETURN:
4666
11.3k
        njs_parser_next(parser, njs_parser_return_statement);
4667
11.3k
        break;
4668
4669
0
    case NJS_TOKEN_WITH:
4670
0
        njs_parser_next(parser, njs_parser_with_statement);
4671
0
        break;
4672
4673
0
    case NJS_TOKEN_THROW:
4674
0
        njs_parser_next(parser, njs_parser_throw_statement);
4675
0
        break;
4676
4677
4.65k
    case NJS_TOKEN_TRY:
4678
4.65k
        njs_parser_next(parser, njs_parser_try_statement);
4679
4.65k
        break;
4680
4681
0
    case NJS_TOKEN_DEBUGGER:
4682
0
        njs_parser_next(parser, njs_parser_debugger_statement);
4683
0
        break;
4684
4685
0
    case NJS_TOKEN_END:
4686
0
        return njs_parser_failed(parser);
4687
4688
351k
    default:
4689
351k
        if (njs_lexer_token_is_identifier_reference(token)) {
4690
307k
            token = njs_lexer_peek_token(parser->lexer, token, 0);
4691
307k
            if (token == NULL) {
4692
0
                return NJS_ERROR;
4693
0
            }
4694
4695
307k
            if (token->type == NJS_TOKEN_COLON) {
4696
253
                njs_parser_next(parser, njs_parser_labelled_statement);
4697
253
                return NJS_OK;
4698
253
            }
4699
307k
        }
4700
4701
351k
        njs_parser_next(parser, njs_parser_expression_statement);
4702
351k
        return NJS_OK;
4703
397k
    }
4704
4705
41.0k
    parser->line = token->line;
4706
4707
41.0k
    njs_lexer_consume_token(parser->lexer, 1);
4708
4709
41.0k
    return NJS_OK;
4710
397k
}
4711
4712
4713
static njs_int_t
4714
njs_parser_statement_after(njs_parser_t *parser, njs_lexer_token_t *token,
4715
    njs_queue_link_t *current)
4716
359k
{
4717
359k
    njs_parser_node_t  *stmt, *last, *new_node, **child;
4718
359k
    njs_parser_node_t  *node, *top;
4719
4720
359k
    child = &parser->target;
4721
359k
    last = *child;
4722
4723
359k
    new_node = parser->node;
4724
4725
359k
    if (new_node != NULL) {
4726
359k
        if (new_node->hoist) {
4727
0
            child = &njs_parser_chain_top(parser);
4728
4729
0
            while (*child != NULL) {
4730
0
                node = *child;
4731
4732
0
                if (node->hoist) {
4733
0
                    break;
4734
0
                }
4735
4736
0
                child = &node->left;
4737
0
            }
4738
4739
0
            last = *child;
4740
0
        }
4741
4742
359k
        stmt = njs_parser_node_new(parser, NJS_TOKEN_STATEMENT);
4743
359k
        if (njs_slow_path(stmt == NULL)) {
4744
0
            return NJS_ERROR;
4745
0
        }
4746
4747
359k
        stmt->hoist = new_node->hoist;
4748
359k
        stmt->left = last;
4749
359k
        stmt->right = new_node;
4750
4751
359k
        *child = stmt;
4752
4753
359k
        top = (child != &parser->target) ? njs_parser_chain_top(parser)
4754
359k
                                         : stmt;
4755
4756
359k
        parser->node = top;
4757
4758
359k
       njs_parser_chain_top_set(parser, top);
4759
359k
    }
4760
4761
359k
    return njs_parser_stack_pop(parser);
4762
359k
}
4763
4764
4765
static njs_int_t
4766
njs_parser_declaration(njs_parser_t *parser, njs_lexer_token_t *token,
4767
    njs_queue_link_t *current)
4768
391k
{
4769
391k
    njs_int_t  ret;
4770
4771
391k
    ret = njs_parser_hoistable_declaration(parser, token, current);
4772
391k
    if (ret == NJS_OK) {
4773
234
        return NJS_OK;
4774
234
    }
4775
4776
391k
    switch (token->type) {
4777
0
    case NJS_TOKEN_CLASS:
4778
0
        njs_parser_next(parser, njs_parser_class_declaration);
4779
0
        return NJS_OK;
4780
4781
2.12k
    case NJS_TOKEN_LET:
4782
2.12k
    case NJS_TOKEN_CONST:
4783
2.12k
        token = njs_lexer_peek_token(parser->lexer, token, 0);
4784
2.12k
        if (token == NULL) {
4785
0
            return NJS_ERROR;
4786
0
        }
4787
4788
2.12k
        switch (token->type) {
4789
0
        case NJS_TOKEN_OPEN_BRACE:
4790
0
        case NJS_TOKEN_OPEN_BRACKET:
4791
0
            njs_parser_next(parser, njs_parser_lexical_declaration);
4792
0
            break;
4793
4794
2.12k
        default:
4795
2.12k
            if (njs_lexer_token_is_reserved(token)) {
4796
0
                njs_lexer_consume_token(parser->lexer, 1);
4797
0
                njs_parser_next(parser, njs_parser_failed_state);
4798
0
                return NJS_OK;
4799
0
            }
4800
4801
2.12k
            if (njs_lexer_token_is_binding_identifier(token)) {
4802
2.12k
                njs_parser_next(parser, njs_parser_lexical_declaration);
4803
2.12k
                break;
4804
2.12k
            }
4805
4806
0
            return NJS_DECLINED;
4807
2.12k
        }
4808
4809
2.12k
        break;
4810
4811
389k
    default:
4812
389k
        return NJS_DECLINED;
4813
391k
    }
4814
4815
2.12k
    return njs_parser_after(parser, current, parser->node, 1,
4816
391k
                            njs_parser_statement_after);
4817
391k
}
4818
4819
4820
static njs_int_t
4821
njs_parser_hoistable_declaration(njs_parser_t *parser,
4822
    njs_lexer_token_t *token, njs_queue_link_t *current)
4823
391k
{
4824
391k
    njs_int_t  ret;
4825
4826
391k
    ret = njs_parser_function_or_generator(parser, token, current);
4827
391k
    if (ret == NJS_OK) {
4828
234
        return NJS_OK;
4829
234
    }
4830
4831
391k
    ret = njs_parser_async_function_or_generator(parser, token, current);
4832
391k
    if (ret == NJS_OK) {
4833
0
        return NJS_OK;
4834
0
    }
4835
4836
391k
    return NJS_DECLINED;
4837
391k
}
4838
4839
4840
/*
4841
 * 13.2 Block.
4842
 */
4843
static njs_int_t
4844
njs_parser_block_statement(njs_parser_t *parser, njs_lexer_token_t *token,
4845
    njs_queue_link_t *current)
4846
13.2k
{
4847
13.2k
    void       *target;
4848
13.2k
    njs_int_t  ret;
4849
4850
13.2k
    ret = njs_parser_scope_begin(parser, NJS_SCOPE_BLOCK, 0);
4851
13.2k
    if (ret != NJS_OK) {
4852
0
        return NJS_ERROR;
4853
0
    }
4854
4855
13.2k
    target = (void *) (uintptr_t) parser->line;
4856
13.2k
    parser->node = NULL;
4857
4858
13.2k
    if (token->type == NJS_TOKEN_CLOSE_BRACE) {
4859
24
        parser->target = target;
4860
4861
24
        njs_parser_next(parser, njs_parser_block_statement_close_brace);
4862
24
        return NJS_OK;
4863
24
    }
4864
4865
13.1k
    njs_parser_next(parser, njs_parser_statement_list);
4866
4867
13.1k
    return njs_parser_after(parser, current, target, 0,
4868
13.2k
                            njs_parser_block_statement_close_brace);
4869
13.2k
}
4870
4871
4872
static njs_int_t
4873
njs_parser_block_statement_open_brace(njs_parser_t *parser,
4874
    njs_lexer_token_t *token, njs_queue_link_t *current)
4875
4.65k
{
4876
4.65k
    if (token->type != NJS_TOKEN_OPEN_BRACE) {
4877
0
        return njs_parser_failed(parser);
4878
0
    }
4879
4880
4.65k
    parser->line = token->line;
4881
4882
4.65k
    njs_lexer_consume_token(parser->lexer, 1);
4883
4884
4.65k
    token = njs_lexer_token(parser->lexer, 0);
4885
4.65k
    if (token == NULL) {
4886
0
        return NJS_ERROR;
4887
0
    }
4888
4889
4.65k
    return njs_parser_block_statement(parser, token, current);
4890
4.65k
}
4891
4892
4893
static njs_int_t
4894
njs_parser_block_statement_close_brace(njs_parser_t *parser,
4895
    njs_lexer_token_t *token, njs_queue_link_t *current)
4896
17.5k
{
4897
17.5k
    njs_parser_node_t  *node;
4898
4899
17.5k
    if (parser->ret != NJS_OK) {
4900
1
        return njs_parser_failed(parser);
4901
1
    }
4902
4903
17.5k
    if (token->type != NJS_TOKEN_CLOSE_BRACE) {
4904
0
        return njs_parser_failed(parser);
4905
0
    }
4906
4907
17.5k
    node = njs_parser_node_new(parser, NJS_TOKEN_BLOCK);
4908
17.5k
    if (node == NULL) {
4909
0
        return NJS_ERROR;
4910
0
    }
4911
4912
17.5k
    node->token_line = (uint32_t) (uintptr_t) parser->target;
4913
17.5k
    node->left = parser->node;
4914
17.5k
    node->right = NULL;
4915
4916
17.5k
    parser->target = NULL;
4917
17.5k
    parser->node = node;
4918
4919
17.5k
    njs_parser_scope_end(parser);
4920
4921
17.5k
    njs_lexer_consume_token(parser->lexer, 1);
4922
17.5k
    return njs_parser_stack_pop(parser);
4923
17.5k
}
4924
4925
4926
static njs_int_t
4927
njs_parser_statement_list(njs_parser_t *parser, njs_lexer_token_t *token,
4928
    njs_queue_link_t *current)
4929
61.0k
{
4930
61.0k
    njs_parser_next(parser, njs_parser_statement_list_item);
4931
4932
61.0k
    return njs_parser_after(parser, current, NULL, 1,
4933
61.0k
                            njs_parser_statement_list_next);
4934
61.0k
}
4935
4936
4937
static njs_int_t
4938
njs_parser_statement_list_next(njs_parser_t *parser, njs_lexer_token_t *token,
4939
    njs_queue_link_t *current)
4940
363k
{
4941
363k
    if (parser->ret != NJS_OK) {
4942
3
        if (token->type != NJS_TOKEN_CLOSE_BRACE) {
4943
3
            parser->node = parser->target;
4944
4945
3
            (void) njs_parser_stack_pop(parser);
4946
4947
3
            return parser->ret;
4948
3
        }
4949
4950
0
        return njs_parser_failed(parser);
4951
3
    }
4952
4953
363k
    if (token->type == NJS_TOKEN_CLOSE_BRACE) {
4954
32.8k
        return njs_parser_stack_pop(parser);
4955
32.8k
    }
4956
4957
330k
    njs_parser_next(parser, njs_parser_statement_list_item);
4958
4959
330k
    return njs_parser_after(parser, current, parser->node, 0,
4960
363k
                            njs_parser_statement_list_next);
4961
363k
}
4962
4963
4964
static njs_int_t
4965
njs_parser_statement_list_item(njs_parser_t *parser, njs_lexer_token_t *token,
4966
    njs_queue_link_t *current)
4967
391k
{
4968
391k
    njs_int_t  ret;
4969
4970
391k
    ret = njs_parser_declaration(parser, token, current);
4971
391k
    if (ret == NJS_OK) {
4972
2.36k
        return NJS_OK;
4973
2.36k
    }
4974
4975
389k
    njs_parser_next(parser, njs_parser_statement);
4976
4977
389k
    return NJS_OK;
4978
391k
}
4979
4980
4981
/*
4982
 * 13.3.1 Let and Const Declarations
4983
 */
4984
static njs_int_t
4985
njs_parser_lexical_declaration(njs_parser_t *parser, njs_lexer_token_t *token,
4986
    njs_queue_link_t *current)
4987
2.12k
{
4988
2.12k
    parser->var_type = (token->type == NJS_TOKEN_LET) ? NJS_VARIABLE_LET
4989
2.12k
                                                      : NJS_VARIABLE_CONST;
4990
4991
2.12k
    njs_lexer_consume_token(parser->lexer, 1);
4992
4993
2.12k
    njs_parser_next(parser, njs_parser_variable_declaration_list);
4994
4995
2.12k
    return njs_parser_after(parser, current, NULL, 1, njs_parser_semicolon);
4996
2.12k
}
4997
4998
4999
/*
5000
 * 13.3.2 Variable Statement
5001
 */
5002
static njs_int_t
5003
njs_parser_variable_statement(njs_parser_t *parser, njs_lexer_token_t *token,
5004
    njs_queue_link_t *current)
5005
4.39k
{
5006
4.39k
    parser->var_type = NJS_VARIABLE_VAR;
5007
5008
4.39k
    njs_parser_next(parser, njs_parser_variable_declaration_list);
5009
5010
4.39k
    return njs_parser_after(parser, current, NULL, 1, njs_parser_semicolon);
5011
4.39k
}
5012
5013
5014
static njs_int_t
5015
njs_parser_variable_declaration_list(njs_parser_t *parser,
5016
    njs_lexer_token_t *token, njs_queue_link_t *current)
5017
6.52k
{
5018
6.52k
    njs_parser_next(parser, njs_parser_variable_declaration);
5019
5020
6.52k
    return njs_parser_after(parser, current, NULL, 1,
5021
6.52k
                            njs_parser_variable_declaration_list_next);
5022
6.52k
}
5023
5024
5025
static njs_int_t
5026
njs_parser_variable_declaration_list_next(njs_parser_t *parser,
5027
    njs_lexer_token_t *token, njs_queue_link_t *current)
5028
6.79k
{
5029
6.79k
    njs_parser_node_t  *node;
5030
5031
6.79k
    if (parser->target != NULL) {
5032
272
        parser->node->left = parser->target;
5033
272
    }
5034
5035
6.79k
    if (token->type != NJS_TOKEN_COMMA) {
5036
6.52k
        return njs_parser_stack_pop(parser);
5037
6.52k
    }
5038
5039
272
    njs_lexer_consume_token(parser->lexer, 1);
5040
5041
272
    node = parser->node;
5042
5043
272
    parser->node = NULL;
5044
5045
272
    njs_parser_next(parser, njs_parser_variable_declaration);
5046
5047
272
    return njs_parser_after(parser, current, node, 1,
5048
6.79k
                            njs_parser_variable_declaration_list_next);
5049
6.79k
}
5050
5051
5052
static njs_int_t
5053
njs_parser_variable_declaration(njs_parser_t *parser,
5054
    njs_lexer_token_t *token, njs_queue_link_t *current)
5055
6.79k
{
5056
6.79k
    njs_int_t          ret;
5057
6.79k
    njs_variable_t     *var;
5058
6.79k
    njs_token_type_t   type;
5059
6.79k
    njs_parser_node_t  *name;
5060
5061
6.79k
    ret = njs_parser_binding_pattern(parser, token, current);
5062
6.79k
    if (ret == NJS_OK) {
5063
0
        return njs_parser_after(parser, current, NULL, 1,
5064
0
                                njs_parser_initializer);
5065
0
    }
5066
5067
6.79k
    if (!njs_lexer_token_is_binding_identifier(token)) {
5068
0
        return njs_parser_failed(parser);
5069
0
    }
5070
5071
6.79k
    if (njs_parser_restricted_identifier(token->type)) {
5072
0
        njs_parser_syntax_error(parser, "Identifier \"%V\" is forbidden in"
5073
0
                                " var declaration", &token->text);
5074
0
        return NJS_DONE;
5075
0
    }
5076
5077
6.79k
    name = njs_parser_variable_node(parser, token->unique_id, parser->var_type,
5078
6.79k
                                    &var);
5079
6.79k
    if (name == NULL) {
5080
0
        return NJS_ERROR;
5081
0
    }
5082
5083
6.79k
    if (var->self) {
5084
2.08k
        var->type = parser->var_type;
5085
2.08k
        var->self = 0;
5086
2.08k
    }
5087
5088
6.79k
    name->token_line = token->line;
5089
5090
6.79k
    parser->node = name;
5091
5092
6.79k
    njs_lexer_consume_token(parser->lexer, 1);
5093
5094
6.79k
    token = njs_lexer_token(parser->lexer, 0);
5095
6.79k
    if (token == NULL) {
5096
0
        return NJS_ERROR;
5097
0
    }
5098
5099
6.79k
    switch (parser->var_type) {
5100
2.12k
    case NJS_VARIABLE_LET:
5101
2.12k
        type = NJS_TOKEN_LET;
5102
2.12k
        break;
5103
5104
8
    case NJS_VARIABLE_CONST:
5105
8
        type = NJS_TOKEN_CONST;
5106
8
        break;
5107
5108
4.66k
    default:
5109
4.66k
        type = NJS_TOKEN_VAR;
5110
4.66k
        break;
5111
6.79k
    }
5112
5113
6.79k
    ret = njs_parser_initializer_assign(parser, type);
5114
6.79k
    if (ret != NJS_OK) {
5115
0
        return ret;
5116
0
    }
5117
5118
6.79k
    parser->node->token_line = token->line;
5119
5120
6.79k
    if (token->type == NJS_TOKEN_ASSIGNMENT) {
5121
4.08k
        njs_parser_next(parser, njs_parser_initializer);
5122
5123
4.08k
        return NJS_OK;
5124
4.08k
    }
5125
5126
2.71k
    parser->target = parser->node;
5127
2.71k
    parser->node = NULL;
5128
5129
2.71k
    njs_parser_next(parser, njs_parser_initializer_after);
5130
5131
2.71k
    return NJS_OK;
5132
6.79k
}
5133
5134
5135
/*
5136
 * 13.3.3 Destructuring Binding Patterns.
5137
 */
5138
static njs_int_t
5139
njs_parser_binding_pattern(njs_parser_t *parser, njs_lexer_token_t *token,
5140
    njs_queue_link_t *current)
5141
6.79k
{
5142
6.79k
    if (token->type == NJS_TOKEN_OPEN_BRACE) {
5143
0
        njs_parser_next(parser, njs_parser_object_binding_pattern);
5144
5145
6.79k
    } else if (token->type == NJS_TOKEN_OPEN_BRACKET) {
5146
0
        njs_parser_next(parser, njs_parser_array_binding_pattern);
5147
5148
6.79k
    } else {
5149
6.79k
        return NJS_DECLINED;
5150
6.79k
    }
5151
5152
0
    njs_lexer_consume_token(parser->lexer, 1);
5153
5154
0
    return NJS_OK;
5155
6.79k
}
5156
5157
static njs_int_t
5158
njs_parser_object_binding_pattern(njs_parser_t *parser,
5159
    njs_lexer_token_t *token, njs_queue_link_t *current)
5160
0
{
5161
0
    return njs_parser_not_supported(parser, token);
5162
0
}
5163
5164
5165
static njs_int_t
5166
njs_parser_array_binding_pattern(njs_parser_t *parser,
5167
    njs_lexer_token_t *token, njs_queue_link_t *current)
5168
0
{
5169
0
    return njs_parser_not_supported(parser, token);
5170
0
}
5171
5172
5173
/*
5174
 * 13.5 Expression Statement.
5175
 */
5176
static njs_int_t
5177
njs_parser_expression_statement(njs_parser_t *parser, njs_lexer_token_t *token,
5178
    njs_queue_link_t *current)
5179
351k
{
5180
351k
    njs_token_type_t   type;
5181
351k
    njs_lexer_token_t  *next;
5182
5183
351k
    switch (token->type) {
5184
0
    case NJS_TOKEN_FUNCTION:
5185
0
        njs_parser_syntax_error(parser, "Functions can only be declared "
5186
0
                                        "at top level or inside a block");
5187
0
        return NJS_DONE;
5188
5189
0
    case NJS_TOKEN_CLASS:
5190
0
        njs_parser_syntax_error(parser, "Class can only be declared "
5191
0
                                        "at top level or inside a block");
5192
0
        return NJS_DONE;
5193
5194
0
    case NJS_TOKEN_OPEN_BRACE:
5195
0
        return njs_parser_reject(parser);
5196
5197
0
    case NJS_TOKEN_ASYNC:
5198
0
        next = njs_lexer_peek_token(parser->lexer, token, 1);
5199
0
        if (next == NULL) {
5200
0
            return NJS_ERROR;
5201
0
        }
5202
5203
0
        if (next->type == NJS_TOKEN_FUNCTION) {
5204
0
            return njs_parser_not_supported(parser, token);
5205
0
        }
5206
5207
0
        break;
5208
5209
0
    case NJS_TOKEN_CONST:
5210
0
    case NJS_TOKEN_LET:
5211
0
        type = token->type;
5212
5213
0
        token = njs_lexer_peek_token(parser->lexer, token, 0);
5214
0
        if (token == NULL) {
5215
0
            return NJS_ERROR;
5216
0
        }
5217
5218
0
        if (token->type == NJS_TOKEN_NAME) {
5219
0
            njs_parser_syntax_error(parser, "%s declaration cannot appear "
5220
0
                                    "in a single-statement context",
5221
0
                                    (type == NJS_TOKEN_CONST ? "const"
5222
0
                                                             : "let" ));
5223
0
            return NJS_DONE;
5224
0
        }
5225
5226
0
        if (token->type == NJS_TOKEN_OPEN_BRACKET) {
5227
0
            return njs_parser_failed(parser);
5228
0
        }
5229
5230
0
        break;
5231
5232
351k
    default:
5233
351k
        break;
5234
351k
    }
5235
5236
351k
    parser->node = NULL;
5237
5238
351k
    njs_parser_next(parser, njs_parser_expression);
5239
5240
351k
    return njs_parser_after(parser, current, NULL, 1,
5241
351k
                            njs_parser_expression_statement_after);
5242
351k
}
5243
5244
5245
static njs_int_t
5246
njs_parser_expression_statement_after(njs_parser_t *parser,
5247
    njs_lexer_token_t *token, njs_queue_link_t *current)
5248
329k
{
5249
329k
    if (njs_parser_expect_semicolon(parser, token) != NJS_OK) {
5250
9
        return njs_parser_failed(parser);
5251
9
    }
5252
5253
329k
    return njs_parser_stack_pop(parser);
5254
329k
}
5255
5256
5257
/*
5258
 * 13.6 if Statement.
5259
 */
5260
static njs_int_t
5261
njs_parser_if_statement(njs_parser_t *parser, njs_lexer_token_t *token,
5262
    njs_queue_link_t *current)
5263
3.18k
{
5264
3.18k
    njs_int_t          ret;
5265
3.18k
    njs_parser_node_t  *node;
5266
5267
3.18k
    if (token->type != NJS_TOKEN_OPEN_PARENTHESIS) {
5268
0
        return njs_parser_failed(parser);
5269
0
    }
5270
5271
3.18k
    njs_lexer_consume_token(parser->lexer, 1);
5272
5273
3.18k
    node = njs_parser_node_new(parser, NJS_TOKEN_IF);
5274
3.18k
    if (node == NULL) {
5275
0
        return NJS_ERROR;
5276
0
    }
5277
5278
3.18k
    node->token_line = parser->line;
5279
5280
3.18k
    parser->node = NULL;
5281
5282
3.18k
    njs_parser_next(parser, njs_parser_expression);
5283
5284
3.18k
    ret = njs_parser_after(parser, current, node, 1,
5285
3.18k
                           njs_parser_if_close_parenthesis);
5286
3.18k
    if (ret != NJS_OK) {
5287
0
        return ret;
5288
0
    }
5289
5290
3.18k
    ret = njs_parser_after(parser, current, NULL, 1,
5291
3.18k
                           njs_parser_statement_wo_node);
5292
3.18k
    if (ret != NJS_OK) {
5293
0
        return ret;
5294
0
    }
5295
5296
3.18k
    return njs_parser_after(parser, current, node, 1,
5297
3.18k
                            njs_parser_else_statement);
5298
3.18k
}
5299
5300
5301
static njs_int_t
5302
njs_parser_if_close_parenthesis(njs_parser_t *parser, njs_lexer_token_t *token,
5303
    njs_queue_link_t *current)
5304
3.18k
{
5305
3.18k
    if (token->type != NJS_TOKEN_CLOSE_PARENTHESIS) {
5306
0
        return njs_parser_failed(parser);
5307
0
    }
5308
5309
3.18k
    njs_lexer_consume_token(parser->lexer, 1);
5310
5311
3.18k
    parser->target->left = parser->node;
5312
3.18k
    parser->node = NULL;
5313
5314
3.18k
    return njs_parser_stack_pop(parser);
5315
3.18k
}
5316
5317
5318
static njs_int_t
5319
njs_parser_else_statement(njs_parser_t *parser, njs_lexer_token_t *token,
5320
    njs_queue_link_t *current)
5321
3.18k
{
5322
3.18k
    njs_parser_node_t  *node;
5323
5324
3.18k
    parser->target->right = parser->node;
5325
3.18k
    parser->node = NULL;
5326
5327
3.18k
    if (token->type == NJS_TOKEN_ELSE) {
5328
1.91k
        node = njs_parser_node_new(parser, NJS_TOKEN_BRANCHING);
5329
1.91k
        if (node == NULL) {
5330
0
            return NJS_ERROR;
5331
0
        }
5332
5333
1.91k
        node->token_line = token->line;
5334
1.91k
        node->left = parser->target->right;
5335
5336
1.91k
        parser->target->right = node;
5337
5338
1.91k
        njs_lexer_consume_token(parser->lexer, 1);
5339
1.91k
        njs_parser_next(parser, njs_parser_statement_wo_node);
5340
5341
1.91k
        return njs_parser_after(parser, current, parser->target, 1,
5342
1.91k
                                njs_parser_else_statement_after);
5343
1.91k
    }
5344
5345
1.26k
    parser->node = parser->target;
5346
5347
1.26k
    return njs_parser_stack_pop(parser);
5348
3.18k
}
5349
5350
5351
static njs_int_t
5352
njs_parser_else_statement_after(njs_parser_t *parser, njs_lexer_token_t *token,
5353
    njs_queue_link_t *current)
5354
1.91k
{
5355
1.91k
    parser->target->right->right = parser->node;
5356
5357
1.91k
    parser->node = parser->target;
5358
5359
1.91k
    return njs_parser_stack_pop(parser);
5360
1.91k
}
5361
5362
5363
static njs_int_t
5364
njs_parser_iteration_statement_do(njs_parser_t *parser,
5365
    njs_lexer_token_t *token, njs_queue_link_t *current)
5366
1.55k
{
5367
1.55k
    njs_parser_node_t  *node;
5368
5369
1.55k
    node = njs_parser_node_new(parser, NJS_TOKEN_DO);
5370
1.55k
    if (node == NULL) {