-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathParser.cpp
More file actions
5895 lines (5248 loc) · 291 KB
/
Parser.cpp
File metadata and controls
5895 lines (5248 loc) · 291 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 1999-2001 Harri Porten (porten@kde.org)
* Copyright (C) 2001 Peter Kelly (pmk@post.com)
* Copyright (C) 2003-2023 Apple Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
*/
#include "config.h"
#include "Parser.h"
#include "ASTBuilder.h"
#include "BuiltinNames.h"
#include "DebuggerParseData.h"
#include "JSCJSValueInlines.h"
#include "VM.h"
#include <utility>
#include <wtf/Scope.h>
#include <wtf/SetForScope.h>
#include <wtf/StringPrintStream.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/MakeString.h>
#define updateErrorMessage(shouldPrintToken, ...) do {\
propagateError(); \
logError(shouldPrintToken, __VA_ARGS__); \
} while (0)
#define propagateError() do { if (hasError()) [[unlikely]] return 0; } while (0)
#define internalFailWithMessage(shouldPrintToken, ...) do { updateErrorMessage(shouldPrintToken, __VA_ARGS__); return 0; } while (0)
#define handleErrorToken() do { if (m_token.m_type == EOFTOK || m_token.m_type & CanBeErrorTokenFlag) { failDueToUnexpectedToken(); } } while (0)
#define failWithMessage(...) do { { handleErrorToken(); updateErrorMessage(true, __VA_ARGS__); } return 0; } while (0)
#define failWithStackOverflow() do { updateErrorMessage(false, "Stack exhausted"); m_hasStackOverflow = true; return 0; } while (0)
#define failIfFalse(cond, ...) do { if (!(cond)) [[unlikely]] { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define failIfTrue(cond, ...) do { if ((cond)) [[unlikely]] { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define failIfTrueIfStrict(cond, ...) do { if ((cond) && strictMode()) [[unlikely]] internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define failIfFalseIfStrict(cond, ...) do { if ((!(cond)) && strictMode()) [[unlikely]] internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define consumeOrFail(tokenType, ...) do { if (!consume(tokenType)) [[unlikely]] { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define consumeOrFailWithFlags(tokenType, flags, ...) do { if (!consume(tokenType, flags)) [[unlikely]] { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define matchOrFail(tokenType, ...) do { if (!match(tokenType)) [[unlikely]] { handleErrorToken(); internalFailWithMessage(true, __VA_ARGS__); } } while (0)
#define failIfStackOverflow() do { if (!canRecurse()) [[unlikely]] failWithStackOverflow(); } while (0)
#define semanticFail(...) do { internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define semanticFailIfTrue(cond, ...) do { if ((cond)) [[unlikely]] internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define semanticFailIfFalse(cond, ...) do { if (!(cond)) [[unlikely]] internalFailWithMessage(false, __VA_ARGS__); } while (0)
#define regexFail(failure) do { setErrorMessage(failure); return 0; } while (0)
#define failDueToUnexpectedToken() do {\
logError(true);\
return 0;\
} while (0)
#define handleProductionOrFail(token, tokenString, operation, production) do {\
consumeOrFail(token, "Expected '", tokenString, "' to ", operation, " a ", production);\
} while (0)
#define handleProductionOrFail2(token, tokenString, operation, production) do {\
consumeOrFail(token, "Expected '", tokenString, "' to ", operation, " an ", production);\
} while (0)
#define semanticFailureDueToKeywordCheckingToken(token, ...) do { \
semanticFailIfTrue(strictMode() && token.m_type == RESERVED_IF_STRICT, "Cannot use the reserved word '", getToken(token), "' as a ", __VA_ARGS__, " in strict mode"); \
semanticFailIfTrue(token.m_type == RESERVED || token.m_type == RESERVED_IF_STRICT, "Cannot use the reserved word '", getToken(token), "' as a ", __VA_ARGS__); \
if (token.m_type & KeywordTokenFlag) { \
semanticFailIfFalse(isContextualKeyword(token), "Cannot use the keyword '", getToken(token), "' as a ", __VA_ARGS__); \
semanticFailIfTrue(token.m_type == LET && strictMode(), "Cannot use 'let' as a ", __VA_ARGS__, " ", disallowedIdentifierLetReason()); \
semanticFailIfTrue(token.m_type == AWAIT && !canUseIdentifierAwait(), "Cannot use 'await' as a ", __VA_ARGS__, " ", disallowedIdentifierAwaitReason()); \
semanticFailIfTrue(token.m_type == YIELD && !canUseIdentifierYield(), "Cannot use 'yield' as a ", __VA_ARGS__, " ", disallowedIdentifierYieldReason()); \
} \
} while (0)
#define semanticFailureDueToKeyword(...) semanticFailureDueToKeywordCheckingToken(m_token, __VA_ARGS__);
namespace JSC {
std::atomic<unsigned> globalParseCount { 0 };
WTF_MAKE_TZONE_ALLOCATED_IMPL(ModuleScopeData);
ALWAYS_INLINE static SourceParseMode getAsyncFunctionBodyParseMode(SourceParseMode parseMode)
{
if (isAsyncGeneratorWrapperParseMode(parseMode))
return SourceParseMode::AsyncGeneratorBodyMode;
if (parseMode == SourceParseMode::AsyncArrowFunctionMode)
return SourceParseMode::AsyncArrowFunctionBodyMode;
return SourceParseMode::AsyncFunctionBodyMode;
}
template <typename LexerType>
void Parser<LexerType>::logError(bool)
{
if (hasError())
return;
StringPrintStream stream;
printUnexpectedTokenText(stream);
setErrorMessage(stream.toStringWithLatin1Fallback());
}
template <typename LexerType> template <typename... Args>
void Parser<LexerType>::logError(bool shouldPrintToken, Args&&... args)
{
if (hasError())
return;
StringPrintStream stream;
if (shouldPrintToken) {
printUnexpectedTokenText(stream);
stream.print(". ");
}
stream.print(std::forward<Args>(args)..., ".");
setErrorMessage(stream.toStringWithLatin1Fallback());
}
template <typename LexerType>
Parser<LexerType>::Parser(VM& vm, const SourceCode& source, ImplementationVisibility implementationVisibility, JSParserBuiltinMode builtinMode, LexicallyScopedFeatures lexicallyScopedFeatures, JSParserScriptMode scriptMode, SourceParseMode parseMode, FunctionMode functionMode, SuperBinding superBinding, ConstructorKind constructorKind, DerivedContextType derivedContextType, bool isEvalContext, EvalContextType evalContextType, DebuggerParseData* debuggerParseData, bool isInsideOrdinaryFunction)
: m_vm(vm)
, m_allowsIn(true)
, m_immediateParentAllowsFunctionDeclarationInStatement(false)
, m_parseMode(parseMode)
, m_statementDepth(0)
, m_scriptMode(scriptMode)
, m_source(&source)
, m_functionMode(functionMode)
, m_superBinding(superBinding)
, m_implementationVisibility(implementationVisibility)
, m_parsingBuiltin(builtinMode == JSParserBuiltinMode::Builtin)
, m_isInsideOrdinaryFunction(isInsideOrdinaryFunction)
, m_hasStackOverflow(false)
, m_debuggerParseData(debuggerParseData)
{
m_lexer = makeUnique<LexerType>(vm, builtinMode, scriptMode);
m_lexer->setCode(source, &m_parserArena);
m_token.m_startPosition.line = source.firstLine().oneBasedInt();
m_token.m_startPosition.offset = source.startOffset();
m_token.m_startPosition.lineStartOffset = source.startOffset();
m_token.m_endPosition.offset = source.startOffset();
m_functionCache = vm.addSourceProviderCache(source.provider());
Scope* scope = pushScope();
scope->setLexicallyScopedFeatures(lexicallyScopedFeatures);
scope->setSourceParseMode(parseMode);
scope->setIsEvalContext(isEvalContext);
if (isEvalContext)
scope->setEvalContextType(evalContextType);
if (scope->isFunction())
scope->setConstructorKind(constructorKind);
else
ASSERT(constructorKind == ConstructorKind::None);
scope->setDerivedContextType(derivedContextType);
if (derivedContextType != DerivedContextType::None)
scope->setExpectedSuperBinding(SuperBinding::Needed);
if (isModuleParseMode(parseMode))
m_moduleScopeData = ModuleScopeData::create();
next();
}
class Scope::MaybeParseAsGeneratorFunctionForScope {
public:
MaybeParseAsGeneratorFunctionForScope(Scope* scope, bool shouldParseAsGeneratorFunction)
: m_scope(scope)
, m_oldValue(scope->m_isGeneratorFunction)
{
m_scope->m_isGeneratorFunction = shouldParseAsGeneratorFunction;
}
~MaybeParseAsGeneratorFunctionForScope()
{
m_scope->m_isGeneratorFunction = m_oldValue;
}
private:
Scope* m_scope;
bool m_oldValue;
};
struct DepthManager : private SetForScope<int> {
public:
DepthManager(int* depth)
: SetForScope(*depth, *depth)
{
}
};
template <typename LexerType>
Parser<LexerType>::~Parser()
{
}
void JSToken::dump(PrintStream& out) const
{
out.print(*m_data.cooked);
}
static ALWAYS_INLINE bool NODELETE isPrivateFieldName(UniquedStringImpl* uid)
{
return uid->length() && uid->at(0) == '#';
}
template <typename LexerType>
Expected<typename Parser<LexerType>::ParseInnerResult, String> Parser<LexerType>::parseInner(const Identifier& calleeName, ParsingContext parsingContext, std::optional<int> functionConstructorParametersEndPosition, const FixedVector<UnlinkedFunctionExecutable::ClassElementDefinition>* classElementDefinitions, const PrivateNameEnvironment* parentScopePrivateNames)
{
ASTBuilder context(const_cast<VM&>(m_vm), m_parserArena, const_cast<SourceCode*>(m_source));
SourceParseMode parseMode = sourceParseMode();
Scope* scope = currentScope();
scope->setIsLexicalScope();
bool hasPrivateNames = scope->isEvalContext() && parentScopePrivateNames && parentScopePrivateNames->size();
if (hasPrivateNames) {
scope->setIsPrivateNameScope();
scope->lexicalVariables().addPrivateNamesFrom(parentScopePrivateNames);
}
SetForScope functionParsePhasePoisoner(m_parserState.functionParsePhase, FunctionParsePhase::Body);
FunctionParameters* parameters = nullptr;
bool isArrowFunctionBodyExpression = parseMode == SourceParseMode::AsyncArrowFunctionBodyMode && !match(OPENBRACE);
if (m_lexer->isReparsingFunction()) {
ParserFunctionInfo<ASTBuilder> functionInfo;
if (isGeneratorOrAsyncFunctionBodyParseMode(parseMode))
parameters = createGeneratorParameters(context, functionInfo.parameterCount);
else if (parseMode == SourceParseMode::ClassFieldInitializerMode)
parameters = context.createFormalParameterList();
else
parameters = parseFunctionParameters(context, functionInfo);
if (SourceParseModeSet(SourceParseMode::ArrowFunctionMode, SourceParseMode::AsyncArrowFunctionMode).contains(parseMode) && !hasError()) {
// FIXME:
// Logically, this should be an assert, since we already successfully parsed the arrow
// function when syntax checking. So logically, we should see the arrow token here.
// But we're seeing crashes in the wild when making this an assert. Instead, we'll just
// handle it as an error in release builds, and an assert on debug builds, with the hopes
// of fixing it in the future.
// https://bugs.webkit.org/show_bug.cgi?id=221633
if (!match(ARROWFUNCTION)) [[unlikely]] {
ASSERT_NOT_REACHED();
return makeUnexpected("Parser error"_s);
}
next();
isArrowFunctionBodyExpression = !match(OPENBRACE);
}
}
if (functionNameIsInScope(calleeName, functionMode()))
scope->declareCallee(&calleeName);
if (m_lexer->isReparsingFunction())
m_statementDepth--;
SourceElements* sourceElements = nullptr;
// The only way we can error this early is if we reparse a function and we run out of stack space.
if (!hasError()) {
if (isAsyncFunctionWrapperParseMode(parseMode))
sourceElements = parseAsyncFunctionSourceElements(context, calleeName, isArrowFunctionBodyExpression, CheckForStrictMode);
else if (isArrowFunctionBodyExpression)
sourceElements = parseArrowFunctionSingleExpressionBodySourceElements(context);
else if (isModuleParseMode(parseMode))
sourceElements = parseModuleSourceElements(context);
else if (isGeneratorWrapperParseMode(parseMode))
sourceElements = parseGeneratorFunctionSourceElements(context, calleeName, CheckForStrictMode);
else if (isAsyncGeneratorWrapperParseMode(parseMode))
sourceElements = parseAsyncGeneratorFunctionSourceElements(context, calleeName, isArrowFunctionBodyExpression, CheckForStrictMode);
else if (parsingContext == ParsingContext::FunctionConstructor)
sourceElements = parseSingleFunction(context, functionConstructorParametersEndPosition);
else if (parseMode == SourceParseMode::ClassFieldInitializerMode) {
ASSERT(classElementDefinitions && !classElementDefinitions->isEmpty());
sourceElements = parseClassFieldInitializerSourceElements(context, *classElementDefinitions);
} else
sourceElements = parseSourceElements(context, CheckForStrictMode);
}
bool validEnding = consume(EOFTOK);
if (!sourceElements || !validEnding)
return makeUnexpected(hasError() ? m_errorMessage : "Parser error"_s);
if (!m_lexer->isReparsingFunction() && m_seenPrivateNameUseInNonReparsingFunctionMode) {
String errorMessage;
scope->forEachUsedVariable([&] (UniquedStringImpl* impl) {
if (!isPrivateFieldName(impl))
return IterationStatus::Continue;
if (parentScopePrivateNames && parentScopePrivateNames->contains(impl))
return IterationStatus::Continue;
if (scope->lexicalVariables().contains(impl))
return IterationStatus::Continue;
errorMessage = makeString("Cannot reference undeclared private names: \""_s, StringView(impl), '"');
return IterationStatus::Done;
});
if (!errorMessage.isNull())
return makeUnexpected(errorMessage);
}
// It's essential to finalize the hoisting before computing captured variables.
scope->finalizeSloppyModeFunctionHoisting();
IdentifierSet capturedVariables;
scope->getCapturedVars(capturedVariables);
VariableEnvironment& varDeclarations = scope->declaredVariables();
for (auto& entry : capturedVariables)
varDeclarations.markVariableAsCaptured(entry.get());
scope->finalizeLexicalEnvironment();
if (isGeneratorWrapperParseMode(parseMode) || isAsyncFunctionOrAsyncGeneratorWrapperParseMode(parseMode)) {
if (scope->usedVariablesContains(m_vm.propertyNames->arguments.impl()))
context.propagateArgumentsUse();
}
CodeFeatures features = context.features();
if (scope->shadowsArguments())
features |= ShadowsArgumentsFeature;
if (m_seenTaggedTemplateInNonReparsingFunctionMode)
features |= NoEvalCacheFeature;
if (scope->hasNonSimpleParameterList())
features |= NonSimpleParameterListFeature;
if (scope->usesImportMeta())
features |= ImportMetaFeature;
if (m_seenArgumentsDotLength && scope->hasDeclaredGlobalArguments())
features |= ArgumentsFeature;
if (scope->asyncFunctionBodyDoesNotUseAwait())
features |= AsyncFunctionWithoutAwaitFeature;
#if ASSERT_ENABLED
if (m_parsingBuiltin && isProgramParseMode(parseMode)) {
VariableEnvironment& lexicalVariables = scope->lexicalVariables();
auto& closedVariableCandidates = scope->closedVariableCandidates();
for (UniquedStringImpl* candidate : closedVariableCandidates) {
// FIXME: We allow async to leak because it appearing as a closed variable is a side effect of trying to parse async arrow functions.
if (!lexicalVariables.contains(candidate) && !varDeclarations.contains(candidate) && !candidate->isSymbol() && candidate != m_vm.propertyNames->async.impl()) {
dataLog("Bad global capture in builtin: '", candidate, "'\n");
dataLog(m_source->view());
CRASH();
}
}
}
#endif // ASSERT_ENABLED
return ParseInnerResult { parameters, sourceElements, scope->takeFunctionDeclarations(), scope->takeDeclaredVariables(), scope->takeLexicalEnvironment(), features, context.numConstants() };
}
template <typename LexerType>
template <class TreeBuilder> bool Parser<LexerType>::isArrowFunctionParameters(TreeBuilder& context)
{
if (match(OPENPAREN)) {
SavePoint saveArrowFunctionPoint = createSavePoint(context);
next();
bool isArrowFunction = false;
if (consume(CLOSEPAREN))
isArrowFunction = match(ARROWFUNCTION);
else {
SyntaxChecker syntaxChecker(const_cast<VM&>(m_vm), m_lexer.get());
// We make fake scope, otherwise parseFormalParameters will add variable to current scope that lead to errors
AutoPopScope fakeScope(this, pushScope());
fakeScope->setSourceParseMode(SourceParseMode::ArrowFunctionMode);
resetImplementationVisibilityIfNeeded();
unsigned parametersCount = 0;
bool isArrowFunctionParameterList = true;
bool isMethod = false;
isArrowFunction = parseFormalParameters(syntaxChecker, syntaxChecker.createFormalParameterList(), isArrowFunctionParameterList, isMethod, parametersCount) && consume(CLOSEPAREN) && match(ARROWFUNCTION);
propagateError();
popScope(fakeScope, syntaxChecker.NeedsFreeVariableInfo);
}
restoreSavePoint(context, saveArrowFunctionPoint);
return isArrowFunction;
}
if (matchSpecIdentifier()) {
semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), "Cannot use 'await' as a parameter name ", disallowedIdentifierAwaitReason());
SavePoint saveArrowFunctionPoint = createSavePoint(context);
next();
bool isArrowFunction = match(ARROWFUNCTION);
restoreSavePoint(context, saveArrowFunctionPoint);
return isArrowFunction;
}
return false;
}
template <typename LexerType>
bool Parser<LexerType>::allowAutomaticSemicolon()
{
return match(CLOSEBRACE) || match(EOFTOK) || m_lexer->hasLineTerminatorBeforeToken();
}
template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSourceElements(TreeBuilder& context, SourceElementsMode mode)
{
const unsigned lengthOfUseStrictLiteral = 12; // "use strict".length
TreeSourceElements sourceElements = context.createSourceElements();
const Identifier* directive = nullptr;
unsigned directiveLiteralLength = 0;
auto savePoint = createSavePoint(context);
bool shouldCheckForUseStrict = mode == CheckForStrictMode;
while (TreeStatement statement = parseStatementListItem(context, directive, &directiveLiteralLength)) {
if (shouldCheckForUseStrict) {
if (directive) {
// "use strict" must be the exact literal without escape sequences or line continuation.
if (directiveLiteralLength == lengthOfUseStrictLiteral && m_vm.propertyNames->useStrictIdentifier == *directive) {
setStrictMode();
shouldCheckForUseStrict = false; // We saw "use strict", there is no need to keep checking for it.
if (!isValidStrictMode()) {
if (m_parserState.lastFunctionName) {
semanticFailIfTrue(m_vm.propertyNames->arguments == *m_parserState.lastFunctionName, "Cannot name a function 'arguments' in strict mode");
semanticFailIfTrue(m_vm.propertyNames->eval == *m_parserState.lastFunctionName, "Cannot name a function 'eval' in strict mode");
}
semanticFailIfTrue(hasDeclaredVariable(m_vm.propertyNames->arguments), "Cannot declare a variable named 'arguments' in strict mode");
semanticFailIfTrue(hasDeclaredVariable(m_vm.propertyNames->eval), "Cannot declare a variable named 'eval' in strict mode");
semanticFailIfTrue(currentScope()->hasNonSimpleParameterList(), "'use strict' directive not allowed inside a function with a non-simple parameter list");
semanticFailIfFalse(isValidStrictMode(), "Invalid parameters or function name in strict mode");
}
// Since strict mode is changed, restoring lexer state by calling next() may cause errors.
restoreSavePoint(context, savePoint);
propagateError();
continue;
}
// We saw a directive, but it wasn't "use strict". We reset our state to
// see if the next statement we parse is also a directive.
directive = nullptr;
} else {
// We saw a statement that wasn't in the form of a directive. The spec says that "use strict"
// is only allowed as the first statement, or after a sequence of directives before it, but
// not after non-directive statements.
shouldCheckForUseStrict = false;
}
}
context.appendStatement(sourceElements, statement);
}
propagateError();
return sourceElements;
}
template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseModuleSourceElements(TreeBuilder& context)
{
TreeSourceElements sourceElements = context.createSourceElements();
SyntaxChecker syntaxChecker(const_cast<VM&>(m_vm), m_lexer.get());
while (true) {
TreeStatement statement = 0;
switch (m_token.m_type) {
case EXPORT_:
statement = parseExportDeclaration(context);
if (statement)
recordPauseLocation(context.breakpointLocation(statement));
break;
case IMPORT: {
SavePoint savePoint = createSavePoint(context);
next();
bool isImportDeclaration = !match(OPENPAREN) && !match(DOT);
restoreSavePoint(context, savePoint);
if (isImportDeclaration) {
statement = parseImportDeclaration(context);
if (statement)
recordPauseLocation(context.breakpointLocation(statement));
break;
}
// This is `import("...")` call or `import.meta` meta property case.
[[fallthrough]];
}
default: {
const Identifier* directive = nullptr;
unsigned directiveLiteralLength = 0;
if (sourceParseMode() == SourceParseMode::ModuleAnalyzeMode) {
if (!parseStatementListItem(syntaxChecker, directive, &directiveLiteralLength))
goto end;
continue;
}
statement = parseStatementListItem(context, directive, &directiveLiteralLength);
break;
}
}
if (!statement)
goto end;
context.appendStatement(sourceElements, statement);
}
end:
propagateError();
for (const auto& pair : m_moduleScopeData->exportedBindings()) {
const auto& uid = pair.key;
if (currentScope()->hasDeclaredVariable(uid.get())) {
currentScope()->declaredVariables().markVariableAsExported(uid.get());
continue;
}
if (currentScope()->hasLexicallyDeclaredVariable(uid.get())) {
currentScope()->lexicalVariables().markVariableAsExported(uid.get());
continue;
}
semanticFail("Exported binding '", uid.get(), "' needs to refer to a top-level declared variable");
}
return sourceElements;
}
template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseGeneratorFunctionSourceElements(TreeBuilder& context, const Identifier& name, SourceElementsMode mode)
{
auto sourceElements = context.createSourceElements();
unsigned functionStart = tokenStart();
JSTokenLocation startLocation(tokenLocation());
JSTextPosition start = tokenStartPosition();
unsigned startColumn = tokenColumn();
int functionNameStart = m_token.m_startPosition.offset;
int parametersStart = functionNameStart;
ParserFunctionInfo<TreeBuilder> info;
info.name = &m_vm.propertyNames->nullIdentifier;
createGeneratorParameters(context, info.parameterCount);
info.startOffset = parametersStart;
info.startLine = tokenLine();
{
AutoPopScope generatorBodyScope(this, pushScope());
generatorBodyScope->setSourceParseMode(SourceParseMode::GeneratorBodyMode);
resetImplementationVisibilityIfNeeded();
generatorBodyScope->setConstructorKind(ConstructorKind::None);
generatorBodyScope->setExpectedSuperBinding(m_superBinding);
SyntaxChecker generatorFunctionContext(const_cast<VM&>(m_vm), m_lexer.get());
failIfFalse(parseSourceElements(generatorFunctionContext, mode), "Cannot parse the body of a generator");
popScope(generatorBodyScope, TreeBuilder::NeedsFreeVariableInfo);
}
info.body = context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, tokenColumn(), functionStart, functionNameStart, parametersStart, implementationVisibility(), lexicallyScopedFeatures(), ConstructorKind::None, m_superBinding, info.parameterCount, SourceParseMode::GeneratorBodyMode, false);
info.endLine = tokenLine();
info.endOffset = m_token.m_data.offset;
info.parametersStartColumn = startColumn;
auto functionExpr = context.createGeneratorFunctionBody(startLocation, info, name);
auto statement = context.createExprStatement(startLocation, functionExpr, start, m_lastTokenEndPosition.line);
context.appendStatement(sourceElements, statement);
return sourceElements;
}
template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseAsyncFunctionSourceElements(TreeBuilder& context, const Identifier& calleeName, bool isArrowFunctionBodyExpression, SourceElementsMode mode)
{
ASSERT(isAsyncFunctionOrAsyncGeneratorWrapperParseMode(sourceParseMode()));
unsigned functionStart = tokenStart();
JSTokenLocation startLocation(tokenLocation());
JSTextPosition start = tokenStartPosition();
unsigned startColumn = tokenColumn();
int functionNameStart = m_token.m_startPosition.offset;
int parametersStart = functionNameStart;
int startLine = tokenLine();
SourceParseMode bodyParseMode = getAsyncFunctionBodyParseMode(sourceParseMode());
SetForScope innerParseMode(m_parseMode, bodyParseMode);
bool bodyUsesAwait = false;
SavePoint bodySavePoint = createSavePoint(context);
{
AutoPopScope asyncFunctionBodyScope(this, pushScope());
asyncFunctionBodyScope->setSourceParseMode(sourceParseMode());
resetImplementationVisibilityIfNeeded();
SyntaxChecker syntaxChecker(const_cast<VM&>(m_vm), m_lexer.get());
if (isArrowFunctionBodyExpression) {
if (m_debuggerParseData)
failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(context), "Cannot parse the body of async arrow function");
else
failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(syntaxChecker), "Cannot parse the body of async arrow function");
} else {
if (m_debuggerParseData)
failIfFalse(parseSourceElements(context, mode), "Cannot parse the body of async function");
else
failIfFalse(parseSourceElements(syntaxChecker, mode), "Cannot parse the body of async function");
}
bodyUsesAwait = asyncFunctionBodyScope->usesAwait();
// When body doesn't use await, we'll inline it directly in the wrapper.
// In this case, there's no body function, so we don't need to track closed variables
// (which would unnecessarily mark parameters as captured).
popScope(asyncFunctionBodyScope, bodyUsesAwait ? TreeBuilder::NeedsFreeVariableInfo : false);
}
// If the body doesn't use await, we can inline it directly into the wrapper.
// without creating a separate body function or generator infrastructure.
// For example,
//
// async function test(a, b) {
// return 42;
// }
//
if (!bodyUsesAwait) {
// Re-parse with ASTBuilder to get the actual body AST.
// Parse directly in the wrapper's scope (not a separate body scope) so that
// lexical variables (let, const) are registered in the wrapper's scope.
restoreSavePoint(context, bodySavePoint);
currentFunctionScope()->setAsyncFunctionBodyDoesNotUseAwait();
if (isArrowFunctionBodyExpression)
return parseArrowFunctionSingleExpressionBodySourceElements(context);
return parseSourceElements(context, mode);
}
// Full async function path (has await) - create body function with generator parameters.
auto sourceElements = context.createSourceElements();
ParserFunctionInfo<TreeBuilder> info;
info.name = &m_vm.propertyNames->nullIdentifier;
createGeneratorParameters(context, info.parameterCount);
info.startOffset = parametersStart;
info.startLine = startLine;
ImplementationVisibility implementationVisibility = this->implementationVisibility();
if (implementationVisibility == ImplementationVisibility::Private)
implementationVisibility = ImplementationVisibility::Public;
info.body = context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, tokenColumn(), functionStart, functionNameStart, parametersStart, implementationVisibility, lexicallyScopedFeatures(), ConstructorKind::None, m_superBinding, info.parameterCount, sourceParseMode(), isArrowFunctionBodyExpression);
info.endLine = tokenLine();
info.endOffset = isArrowFunctionBodyExpression ? tokenLocation().endOffset : m_token.m_data.offset;
info.parametersStartColumn = startColumn;
auto functionExpr = context.createAsyncFunctionBody(startLocation, info, bodyParseMode, calleeName);
auto statement = context.createExprStatement(startLocation, functionExpr, start, m_lastTokenEndPosition.line);
context.appendStatement(sourceElements, statement);
return sourceElements;
}
template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseAsyncGeneratorFunctionSourceElements(TreeBuilder& context, const Identifier& calleeName, bool isArrowFunctionBodyExpression, SourceElementsMode mode)
{
ASSERT(isAsyncGeneratorWrapperParseMode(sourceParseMode()));
auto sourceElements = context.createSourceElements();
unsigned functionStart = tokenStart();
JSTokenLocation startLocation(tokenLocation());
JSTextPosition start = tokenStartPosition();
unsigned startColumn = tokenColumn();
int functionNameStart = m_token.m_startPosition.offset;
int parametersStart = functionNameStart;
ParserFunctionInfo<TreeBuilder> info;
info.name = &m_vm.propertyNames->nullIdentifier;
createGeneratorParameters(context, info.parameterCount);
info.startOffset = parametersStart;
info.startLine = tokenLine();
SourceParseMode parseMode = SourceParseMode::AsyncGeneratorBodyMode;
SetForScope innerParseMode(m_parseMode, parseMode);
{
AutoPopScope asyncFunctionBodyScope(this, pushScope());
asyncFunctionBodyScope->setSourceParseMode(sourceParseMode());
resetImplementationVisibilityIfNeeded();
SyntaxChecker syntaxChecker(const_cast<VM&>(m_vm), m_lexer.get());
if (isArrowFunctionBodyExpression) {
if (m_debuggerParseData)
failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(context), "Cannot parse the body of async arrow function");
else
failIfFalse(parseArrowFunctionSingleExpressionBodySourceElements(syntaxChecker), "Cannot parse the body of async arrow function");
} else {
if (m_debuggerParseData)
failIfFalse(parseSourceElements(context, mode), "Cannot parse the body of async function");
else
failIfFalse(parseSourceElements(syntaxChecker, mode), "Cannot parse the body of async function");
}
popScope(asyncFunctionBodyScope, TreeBuilder::NeedsFreeVariableInfo);
}
info.body = context.createFunctionMetadata(startLocation, tokenLocation(), startColumn, tokenColumn(), functionStart, functionNameStart, parametersStart, implementationVisibility(), lexicallyScopedFeatures(), ConstructorKind::None, m_superBinding, info.parameterCount, parseMode, isArrowFunctionBodyExpression);
info.endLine = tokenLine();
info.endOffset = isArrowFunctionBodyExpression ? tokenLocation().endOffset : m_token.m_data.offset;
info.parametersStartColumn = startColumn;
auto functionExpr = context.createAsyncFunctionBody(startLocation, info, parseMode, calleeName);
auto statement = context.createExprStatement(startLocation, functionExpr, start, m_lastTokenEndPosition.line);
context.appendStatement(sourceElements, statement);
return sourceElements;
}
template <typename LexerType>
template <class TreeBuilder> TreeSourceElements Parser<LexerType>::parseSingleFunction(TreeBuilder& context, std::optional<int> functionConstructorParametersEndPosition)
{
TreeSourceElements sourceElements = context.createSourceElements();
TreeStatement statement = 0;
switch (m_token.m_type) {
case FUNCTION:
statement = parseFunctionDeclaration(context, FunctionDeclarationType::Declaration, ExportType::NotExported, DeclarationDefaultContext::Standard, functionConstructorParametersEndPosition);
break;
case IDENT:
if (*m_token.m_data.ident == m_vm.propertyNames->async && !m_token.m_data.escaped) [[likely]] {
unsigned functionStart = m_token.m_startPosition;
next();
failIfFalse(match(FUNCTION) && !m_lexer->hasLineTerminatorBeforeToken(), "Cannot parse the async function");
statement = parseAsyncFunctionDeclaration(context, functionStart, ExportType::NotExported, DeclarationDefaultContext::Standard, functionConstructorParametersEndPosition);
break;
}
[[fallthrough]];
default:
failDueToUnexpectedToken();
}
if (statement) {
context.setEndOffset(statement, m_lastTokenEndPosition.offset);
context.appendStatement(sourceElements, statement);
}
propagateError();
return sourceElements;
}
template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseStatementListItem(TreeBuilder& context, const Identifier*& directive, unsigned* directiveLiteralLength)
{
// The grammar is documented here:
// http://www.ecma-international.org/ecma-262/6.0/index.html#sec-statements
DepthManager statementDepth(&m_statementDepth);
m_statementDepth++;
failIfStackOverflow();
TreeStatement result = 0;
bool shouldSetEndOffset = true;
bool shouldSetPauseLocation = false;
switch (m_token.m_type) {
case CONSTTOKEN:
result = parseVariableDeclaration(context, DeclarationType::ConstDeclaration);
shouldSetPauseLocation = true;
break;
case LET: {
bool shouldParseVariableDeclaration = true;
if (!strictMode()) {
SavePoint savePoint = createSavePoint(context);
next();
// Intentionally use `matchIdentifierOrPossiblyEscapedContextualKeyword()` and not `matchSpecIdentifier()`.
// We would like contextual keywords to fall under parseVariableDeclaration even when not used as identifiers.
// For example, under a generator context, matchSpecIdentifier() for "yield" returns `false`.
// But we would like to enter parseVariableDeclaration and raise an error under the context of parseVariableDeclaration
// to raise consistent errors between "var", "const" and "let".
if (!matchIdentifierOrPossiblyEscapedContextualKeyword() && !match(OPENBRACE) && !match(OPENBRACKET))
shouldParseVariableDeclaration = false;
restoreSavePoint(context, savePoint);
}
if (shouldParseVariableDeclaration)
result = parseVariableDeclaration(context, DeclarationType::LetDeclaration);
else {
bool allowFunctionDeclarationAsStatement = true;
result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
}
shouldSetPauseLocation = !context.shouldSkipPauseLocation(result);
break;
}
case CLASSTOKEN:
result = parseClassDeclaration(context);
break;
case FUNCTION:
result = parseFunctionDeclaration(context);
break;
case ESCAPED_KEYWORD:
if (!matchAllowedEscapedContextualKeyword()) [[unlikely]]
failDueToUnexpectedToken();
[[fallthrough]];
case IDENT:
if (Options::useExplicitResourceManagement()
&& *m_token.m_data.ident == m_vm.propertyNames->usingIdentifier
&& !m_token.m_data.escaped) [[unlikely]] {
SavePoint savePoint = createSavePoint(context);
next();
if (!m_lexer->hasLineTerminatorBeforeToken() && matchSpecIdentifier()) {
restoreSavePoint(context, savePoint);
semanticFailIfTrue(currentScope()->isGlobalCode() && !currentScope()->isModuleCode() && m_statementDepth == 1, "'using' declaration is not allowed at the top level of a script or eval");
semanticFailIfTrue(m_insideSwitchCaseBody, "'using' declaration is not allowed directly in a switch case or default clause");
result = parseVariableDeclaration(context, DeclarationType::UsingDeclaration);
shouldSetPauseLocation = true;
break;
}
restoreSavePoint(context, savePoint);
}
if (*m_token.m_data.ident == m_vm.propertyNames->async && !m_token.m_data.escaped) [[unlikely]] {
// Eagerly parse as AsyncFunctionDeclaration. This is the uncommon case,
// but could be mistakenly parsed as an AsyncFunctionExpression.
SavePoint savePoint = createSavePoint(context);
unsigned functionStart = m_token.m_startPosition;
next();
if (match(FUNCTION) && !m_lexer->hasLineTerminatorBeforeToken()) [[unlikely]] {
result = parseAsyncFunctionDeclaration(context, functionStart);
break;
}
restoreSavePoint(context, savePoint);
}
[[fallthrough]];
case AWAIT:
if (Options::useExplicitResourceManagement()
&& match(AWAIT)
&& !m_parserState.classFieldInitMasksAsync
&& (currentFunctionScope()->isAsyncFunctionBoundary() || isModuleParseMode(sourceParseMode()))) [[unlikely]] {
SavePoint savePoint = createSavePoint(context);
next();
if (!m_lexer->hasLineTerminatorBeforeToken()
&& match(IDENT)
&& *m_token.m_data.ident == m_vm.propertyNames->usingIdentifier
&& !m_token.m_data.escaped) {
next();
if (!m_lexer->hasLineTerminatorBeforeToken() && matchSpecIdentifier()) {
restoreSavePoint(context, savePoint);
semanticFailIfTrue(currentScope()->isGlobalCode() && !currentScope()->isModuleCode() && m_statementDepth == 1, "'await using' declaration is not allowed at the top level of a script or eval");
semanticFailIfTrue(m_insideSwitchCaseBody, "'await using' declaration is not allowed directly in a switch case or default clause");
currentFunctionScope()->setUsesAwait();
result = parseVariableDeclaration(context, DeclarationType::AwaitUsingDeclaration);
shouldSetPauseLocation = true;
break;
}
}
restoreSavePoint(context, savePoint);
}
[[fallthrough]];
case YIELD: {
if (currentScope()->isStaticBlock()) [[unlikely]] {
failIfTrue(match(YIELD), "Cannot use 'yield' within static block");
failIfTrue(match(AWAIT), "Cannot use 'await' within static block");
}
// This is a convenient place to notice labeled statements
// (even though we also parse them as normal statements)
// because we allow the following type of code in sloppy mode:
// ``` function foo() { label: function bar() { } } ```
bool allowFunctionDeclarationAsStatement = true;
result = parseExpressionOrLabelStatement(context, allowFunctionDeclarationAsStatement);
shouldSetPauseLocation = !context.shouldSkipPauseLocation(result);
break;
}
default:
m_statementDepth--; // parseStatement() increments the depth.
result = parseStatement(context, directive, directiveLiteralLength);
shouldSetEndOffset = false;
break;
}
if (result) {
if (shouldSetEndOffset)
context.setEndOffset(result, m_lastTokenEndPosition.offset);
if (shouldSetPauseLocation)
recordPauseLocation(context.breakpointLocation(result));
}
return result;
}
template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseVariableDeclaration(TreeBuilder& context, DeclarationType declarationType, ExportType exportType)
{
ASSERT(match(VAR) || match(LET) || match(CONSTTOKEN)
|| (declarationType == DeclarationType::UsingDeclaration && match(IDENT))
|| (declarationType == DeclarationType::AwaitUsingDeclaration && match(AWAIT)));
JSTokenLocation location(tokenLocation());
int start = tokenLine();
int end = 0;
int scratch;
TreeDestructuringPattern scratch1 = 0;
TreeExpression scratch2 = 0;
JSTextPosition scratch3;
bool scratchBool;
TreeExpression variableDecls = parseVariableDeclarationList(context, scratch, scratch1, scratch2, scratch3, scratch3, scratch3, VarDeclarationContext, declarationType, exportType, scratchBool);
propagateError();
failIfFalse(autoSemiColon(), "Expected ';' after variable declaration");
return context.createDeclarationStatement(location, variableDecls, start, end);
}
template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseDoWhileStatement(TreeBuilder& context)
{
ASSERT(match(DO));
int startLine = tokenLine();
next();
const Identifier* unused = nullptr;
startLoop();
TreeStatement statement = parseStatement(context, unused);
endLoop();
failIfFalse(statement, "Expected a statement following 'do'");
int endLine = tokenLine();
JSTokenLocation location(tokenLocation());
handleProductionOrFail(WHILE, "while", "end", "do-while loop");
handleProductionOrFail(OPENPAREN, "(", "start", "do-while loop condition");
semanticFailIfTrue(match(CLOSEPAREN), "Must provide an expression as a do-while loop condition");
TreeExpression expr = parseExpression(context);
failIfFalse(expr, "Unable to parse do-while loop condition");
recordPauseLocation(context.breakpointLocation(expr));
handleProductionOrFail(CLOSEPAREN, ")", "end", "do-while loop condition");
consume(SEMICOLON); // Always performs automatic semicolon insertion.
return context.createDoWhileStatement(location, statement, expr, startLine, endLine);
}
template <typename LexerType>
template <class TreeBuilder> TreeStatement Parser<LexerType>::parseWhileStatement(TreeBuilder& context)
{
ASSERT(match(WHILE));
JSTokenLocation location(tokenLocation());
int startLine = tokenLine();
next();
handleProductionOrFail(OPENPAREN, "(", "start", "while loop condition");
semanticFailIfTrue(match(CLOSEPAREN), "Must provide an expression as a while loop condition");
TreeExpression expr = parseExpression(context);
failIfFalse(expr, "Unable to parse while loop condition");
recordPauseLocation(context.breakpointLocation(expr));
int endLine = tokenLine();
handleProductionOrFail(CLOSEPAREN, ")", "end", "while loop condition");
const Identifier* unused = nullptr;
startLoop();
TreeStatement statement = parseStatement(context, unused);
endLoop();
failIfFalse(statement, "Expected a statement as the body of a while loop");
return context.createWhileStatement(location, expr, statement, startLine, endLine);
}
template <typename LexerType>
template <class TreeBuilder> TreeExpression Parser<LexerType>::parseVariableDeclarationList(TreeBuilder& context, int& declarations, TreeDestructuringPattern& lastPattern, TreeExpression& lastInitializer, JSTextPosition& identStart, JSTextPosition& initStart, JSTextPosition& initEnd, VarDeclarationListContext declarationListContext, DeclarationType declarationType, ExportType exportType, bool& forLoopConstDoesNotHaveInitializer)
{
ASSERT(declarationType == DeclarationType::LetDeclaration || declarationType == DeclarationType::VarDeclaration || declarationType == DeclarationType::ConstDeclaration || declarationType == DeclarationType::UsingDeclaration || declarationType == DeclarationType::AwaitUsingDeclaration);
TreeExpression head = 0;
JSTokenLocation headLocation;
TreeExpression tail = 0;
const Identifier* lastIdent;
JSToken lastIdentToken;
AssignmentContext assignmentContext = assignmentContextFromDeclarationType(declarationType);
bool isUsingDeclaration = declarationType == DeclarationType::UsingDeclaration || declarationType == DeclarationType::AwaitUsingDeclaration;
do {
lastPattern = TreeDestructuringPattern(0);
lastIdent = nullptr;
JSTokenLocation location(tokenLocation());
next();
if (!head && declarationType == DeclarationType::AwaitUsingDeclaration) {
ASSERT(match(IDENT) && *m_token.m_data.ident == m_vm.propertyNames->usingIdentifier);
next();
}
if (head) {
// Move the location of subsequent declarations after the comma.
location = tokenLocation();
}
TreeExpression node = 0;
declarations++;
bool hasInitializer = false;
failIfTrue(match(PRIVATENAME), "Cannot use a private name to declare a variable");
if (isUsingDeclaration) {
// 'using' declarations cannot have a destructuring pattern.
failIfTrue(match(OPENBRACE) || match(OPENBRACKET), "'using' declarations cannot have a destructuring pattern");
failIfFalse(matchSpecIdentifier(), "Expected an identifier name in 'using' declaration");
}
if (matchSpecIdentifier()) {
semanticFailIfTrue(currentScope()->isStaticBlock() && isArgumentsIdentifier(), "Cannot use 'arguments' as an identifier in static block");
failIfTrue(isPossiblyEscapedLet(m_token) && (declarationType == DeclarationType::LetDeclaration || declarationType == DeclarationType::ConstDeclaration || isUsingDeclaration),
"Cannot use 'let' as an identifier name for a LexicalDeclaration");
semanticFailIfTrue(isDisallowedIdentifierAwait(m_token), "Cannot use 'await' as a ", declarationTypeToVariableKind(declarationType), " ", disallowedIdentifierAwaitReason());
JSTextPosition varStart = tokenStartPosition();
JSTokenLocation varStartLocation(tokenLocation());
identStart = varStart;
const Identifier* name = m_token.m_data.ident;
lastIdent = name;
lastIdentToken = m_token;
next();
hasInitializer = match(EQUAL);
DeclarationResultMask declarationResult = declareVariable(name, declarationType);
if (declarationResult != DeclarationResult::Valid) {
failIfTrueIfStrict(declarationResult & DeclarationResult::InvalidStrictMode, "Cannot declare a variable named ", name->impl(), " in strict mode");
if (declarationResult & DeclarationResult::InvalidDuplicateDeclaration) [[unlikely]] {
semanticFailIfTrue(declarationType == DeclarationType::LetDeclaration, "Cannot declare a let variable twice: '", name->impl(), "'");
semanticFailIfTrue(declarationType == DeclarationType::ConstDeclaration, "Cannot declare a const variable twice: '", name->impl(), "'");
semanticFailIfTrue(isUsingDeclaration, "Cannot declare a using variable twice: '", name->impl(), "'");
ASSERT(declarationType == DeclarationType::VarDeclaration);