forked from facebook/hermes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSParserImpl-flow.cpp
More file actions
2676 lines (2400 loc) · 77.3 KB
/
JSParserImpl-flow.cpp
File metadata and controls
2676 lines (2400 loc) · 77.3 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) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "JSParserImpl.h"
#include "llvh/Support/SaveAndRestore.h"
using llvh::cast;
using llvh::dyn_cast;
using llvh::isa;
namespace hermes {
namespace parser {
namespace detail {
#if HERMES_PARSE_FLOW
Optional<ESTree::Node *> JSParserImpl::parseFlowDeclaration() {
assert(checkDeclaration());
SMLoc start = tok_->getStartLoc();
if (check(TokenKind::rw_enum)) {
auto optEnum = parseEnumDeclarationFlow();
if (!optEnum)
return None;
return *optEnum;
}
TypeAliasKind kind = TypeAliasKind::None;
if (checkAndEat(declareIdent_))
kind = TypeAliasKind::Declare;
else if (checkAndEat(opaqueIdent_))
kind = TypeAliasKind::Opaque;
if (kind == TypeAliasKind::Declare &&
!checkN(typeIdent_, interfaceIdent_, TokenKind::rw_interface)) {
error(tok_->getSourceRange(), "invalid token in type declaration");
return None;
}
if (kind == TypeAliasKind::Opaque && !check(typeIdent_)) {
error(tok_->getSourceRange(), "invalid token in opaque type declaration");
return None;
}
if (checkAndEat(typeIdent_)) {
auto optType = parseTypeAliasFlow(start, kind);
if (!optType)
return None;
return *optType;
}
if (checkN(interfaceIdent_, TokenKind::rw_interface)) {
auto optType = parseInterfaceDeclarationFlow(
kind == TypeAliasKind::Declare ? Optional<SMLoc>(start) : None);
if (!optType)
return None;
return *optType;
}
assert(
kind == TypeAliasKind::None &&
"checkDeclaration() returned true without 'type' or 'interface'");
return None;
}
Optional<ESTree::Node *> JSParserImpl::parseDeclareFLow(
SMLoc start,
AllowDeclareExportType allowDeclareExportType) {
if (checkAndEat(typeIdent_)) {
return parseTypeAliasFlow(start, TypeAliasKind::Declare);
}
if (checkAndEat(opaqueIdent_)) {
if (!check(typeIdent_)) {
error(tok_->getStartLoc(), "'type' required in opaque type declaration");
return None;
}
advance(JSLexer::GrammarContext::Type);
return parseTypeAliasFlow(start, TypeAliasKind::DeclareOpaque);
}
if (checkN(TokenKind::rw_interface, interfaceIdent_)) {
return parseInterfaceDeclarationFlow(start);
}
if (check(TokenKind::rw_class)) {
return parseDeclareClassFlow(start);
}
if (check(TokenKind::rw_function)) {
return parseDeclareFunctionFlow(start);
}
if (check(moduleIdent_)) {
return parseDeclareModuleFlow(start);
}
if (checkAndEat(TokenKind::rw_var)) {
auto optIdent = parseBindingIdentifier(Param{});
if (!optIdent) {
errorExpected(
TokenKind::identifier,
"in var declaration",
"start of declaration",
start);
return None;
}
if (!eatSemi())
return None;
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::DeclareVariableNode(*optIdent));
}
if (!check(TokenKind::rw_export)) {
errorExpected(
{TokenKind::rw_export,
TokenKind::rw_interface,
TokenKind::rw_function,
TokenKind::rw_class,
TokenKind::rw_var},
"in declared type",
"start of declare",
start);
return None;
}
return parseDeclareExportFlow(start, allowDeclareExportType);
}
Optional<ESTree::Node *> JSParserImpl::parseTypeAliasFlow(
SMLoc start,
TypeAliasKind kind) {
if (!need(
TokenKind::identifier, "in type alias", "start of type alias", start))
return None;
ESTree::Node *id = setLocation(
tok_,
tok_,
new (context_)
ESTree::IdentifierNode(tok_->getIdentifier(), nullptr, false));
advance(JSLexer::GrammarContext::Type);
ESTree::Node *typeParams = nullptr;
if (check(TokenKind::less)) {
auto optTypeParams = parseTypeParamsFlow();
if (!optTypeParams)
return None;
typeParams = *optTypeParams;
}
ESTree::Node *supertype = nullptr;
if ((kind == TypeAliasKind::Opaque || kind == TypeAliasKind::DeclareOpaque) &&
checkAndEat(TokenKind::colon, JSLexer::GrammarContext::Type)) {
auto optSuper = parseTypeAnnotationFlow();
if (!optSuper)
return None;
supertype = *optSuper;
}
ESTree::Node *right = nullptr;
if (kind != TypeAliasKind::DeclareOpaque) {
if (!eat(
TokenKind::equal,
JSLexer::GrammarContext::Type,
"in type alias",
"start of type alias",
start))
return None;
auto optRight = parseTypeAnnotationFlow();
if (!optRight)
return None;
right = *optRight;
}
eatSemi(true);
if (kind == TypeAliasKind::DeclareOpaque) {
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_)
ESTree::DeclareOpaqueTypeNode(id, typeParams, right, supertype));
}
if (kind == TypeAliasKind::Declare) {
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::DeclareTypeAliasNode(id, typeParams, right));
}
if (kind == TypeAliasKind::Opaque) {
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_)
ESTree::OpaqueTypeNode(id, typeParams, right, supertype));
}
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::TypeAliasNode(id, typeParams, right));
}
Optional<ESTree::Node *> JSParserImpl::parseInterfaceDeclarationFlow(
Optional<SMLoc> declareStart) {
assert(checkN(TokenKind::rw_interface, interfaceIdent_));
SMLoc start = advance(JSLexer::GrammarContext::Type).Start;
if (!need(
TokenKind::identifier,
"in interface declaration",
"start of interface",
start))
return None;
auto *id = setLocation(
tok_,
tok_,
new (context_)
ESTree::IdentifierNode(tok_->getIdentifier(), nullptr, false));
advance(JSLexer::GrammarContext::Type);
ESTree::Node *typeParams = nullptr;
if (check(TokenKind::less)) {
auto optParams = parseTypeParamsFlow();
if (!optParams)
return None;
typeParams = *optParams;
}
ESTree::NodeList extends{};
auto optBody = parseInterfaceTailFlow(start, extends);
if (!optBody)
return None;
if (declareStart.hasValue()) {
return setLocation(
*declareStart,
*optBody,
new (context_) ESTree::DeclareInterfaceNode(
id, typeParams, std::move(extends), *optBody));
}
return setLocation(
start,
*optBody,
new (context_) ESTree::InterfaceDeclarationNode(
id, typeParams, std::move(extends), *optBody));
}
Optional<ESTree::Node *> JSParserImpl::parseInterfaceTailFlow(
SMLoc start,
ESTree::NodeList &extends) {
if (checkAndEat(TokenKind::rw_extends)) {
do {
if (!need(
TokenKind::identifier,
"in extends clause",
"location of interface",
start))
return None;
if (!parseInterfaceExtends(start, extends))
return None;
} while (checkAndEat(TokenKind::comma, JSLexer::GrammarContext::Type));
}
if (!need(TokenKind::l_brace, "in interface", "location of interface", start))
return None;
return parseObjectTypeAnnotationFlow(
AllowProtoProperty::No, AllowStaticProperty::No, AllowSpreadProperty::No);
}
bool JSParserImpl::parseInterfaceExtends(
SMLoc start,
ESTree::NodeList &extends) {
assert(check(TokenKind::identifier));
auto optGeneric = parseGenericTypeFlow();
if (!optGeneric)
return false;
ESTree::GenericTypeAnnotationNode *generic = *optGeneric;
extends.push_back(*setLocation(
generic,
generic,
new (context_) ESTree::InterfaceExtendsNode(
generic->_id, generic->_typeParameters)));
return true;
}
Optional<ESTree::Node *> JSParserImpl::parseDeclareFunctionFlow(SMLoc start) {
assert(check(TokenKind::rw_function));
advance(JSLexer::GrammarContext::Type);
if (!need(
TokenKind::identifier,
"in declare function type",
"location of declare",
start))
return None;
UniqueString *id = tok_->getIdentifier();
SMLoc idStart = advance(JSLexer::GrammarContext::Type).Start;
SMLoc funcStart = tok_->getStartLoc();
ESTree::Node *typeParams = nullptr;
if (check(TokenKind::less)) {
auto optParams = parseTypeParamsFlow();
if (!optParams)
return None;
typeParams = *optParams;
}
if (!need(
TokenKind::l_paren,
"in declare function type",
"location of declare",
start))
return None;
ESTree::NodeList params{};
ESTree::Node *thisConstraint = nullptr;
auto optRest = parseFunctionTypeAnnotationParamsFlow(params, thisConstraint);
if (!optRest)
return None;
if (!eat(
TokenKind::colon,
JSLexer::GrammarContext::Type,
"in declare function type",
"location of declare",
start))
return None;
auto optReturn = parseTypeAnnotationFlow();
if (!optReturn)
return None;
ESTree::Node *returnType = *optReturn;
SMLoc funcEnd = getPrevTokenEndLoc();
ESTree::Node *predicate = nullptr;
if (check(checksIdent_)) {
auto optPred = parsePredicateFlow();
if (!optPred)
return None;
predicate = *optPred;
}
if (!eatSemi())
return None;
auto *func = setLocation(
funcStart,
funcEnd,
new (context_) ESTree::TypeAnnotationNode(setLocation(
funcStart,
funcEnd,
new (context_) ESTree::FunctionTypeAnnotationNode(
std::move(params),
thisConstraint,
returnType,
*optRest,
typeParams))));
auto *ident = setLocation(
idStart, func, new (context_) ESTree::IdentifierNode(id, func, false));
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::DeclareFunctionNode(ident, predicate));
}
Optional<ESTree::Node *> JSParserImpl::parseDeclareModuleFlow(SMLoc start) {
assert(check(moduleIdent_));
advance(JSLexer::GrammarContext::Type);
if (checkAndEat(TokenKind::period, JSLexer::GrammarContext::Type)) {
if (!checkAndEat(exportsIdent_, JSLexer::GrammarContext::Type)) {
error(tok_->getSourceRange(), "expected module.exports declaration");
return None;
}
SMLoc annotStart = tok_->getStartLoc();
if (!eat(
TokenKind::colon,
JSLexer::GrammarContext::Type,
"in module.exports declaration",
"start of declaration",
start))
return None;
auto optType = parseTypeAnnotationFlow(annotStart);
if (!optType)
return None;
eatSemi(true);
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::DeclareModuleExportsNode(*optType));
}
// declare module Identifier {[opt]
// ^
ESTree::Node *id = nullptr;
if (check(TokenKind::string_literal)) {
id = setLocation(
tok_,
tok_,
new (context_) ESTree::StringLiteralNode(tok_->getStringLiteral()));
} else {
if (!need(
TokenKind::identifier,
"in module declaration",
"start of declaration",
start))
return None;
id = setLocation(
tok_,
tok_,
new (context_)
ESTree::IdentifierNode(tok_->getIdentifier(), nullptr, false));
}
advance(JSLexer::GrammarContext::Type);
// declare module Identifier {
// ^
SMLoc bodyStart = tok_->getStartLoc();
if (!eat(
TokenKind::l_brace,
JSLexer::GrammarContext::Type,
"in module declaration",
"start of declaration",
start))
return None;
UniqueString *kind = nullptr;
ESTree::NodeList declarations{};
while (!check(TokenKind::r_brace)) {
if (check(TokenKind::rw_import)) {
// 'import' can be bare without a 'declare' before it.
auto optImport = parseImportDeclaration();
if (!optImport)
return None;
ESTree::ImportDeclarationNode *import = *optImport;
if (import->_importKind == valueIdent_) {
error(
import->getSourceRange(),
"imports within a `declare module` body must always be "
"`import type` or `import typeof`");
}
declarations.push_back(*import);
continue;
}
if (!check(declareIdent_)) {
error(
tok_->getSourceRange(),
"expected 'declare' in module declaration body");
return None;
}
SMLoc declarationStart = advance(JSLexer::GrammarContext::Type).Start;
auto optDecl =
parseDeclareFLow(declarationStart, AllowDeclareExportType::Yes);
if (!optDecl)
return None;
ESTree::Node *decl = *optDecl;
switch (decl->getKind()) {
case ESTree::NodeKind::DeclareModuleExports:
if (kind != nullptr && kind != commonJSIdent_) {
error(
decl->getSourceRange(),
"cannot use CommonJS export in ES module");
}
kind = commonJSIdent_;
break;
case ESTree::NodeKind::DeclareExportDeclaration:
if (llvh::dyn_cast_or_null<ESTree::InterfaceDeclarationNode>(
llvh::cast<ESTree::DeclareExportDeclarationNode>(decl)
->_declaration)) {
// declare export interface can show up in either module kind.
// Ignore it.
break;
}
if (llvh::dyn_cast_or_null<ESTree::TypeAliasNode>(
llvh::cast<ESTree::DeclareExportDeclarationNode>(decl)
->_declaration)) {
// declare export type can show up in either module kind.
// Ignore it.
break;
}
if (kind != nullptr && kind != esIdent_) {
error(
decl->getSourceRange(),
"cannot use ESM export in CommonJS module");
}
kind = esIdent_;
break;
case ESTree::NodeKind::DeclareExportAllDeclaration:
if (kind != nullptr && kind != esIdent_) {
error(
decl->getSourceRange(),
"cannot use ESM export in CommonJS module");
}
kind = esIdent_;
break;
default:
break;
}
declarations.push_back(*decl);
}
SMLoc bodyEnd = advance(JSLexer::GrammarContext::Type).End;
ESTree::Node *body = setLocation(
bodyStart,
bodyEnd,
new (context_) ESTree::BlockStatementNode(std::move(declarations)));
if (kind == nullptr) {
// Default to CommonJS if we weren't able to figure it out based on
// declarations themselves.
kind = commonJSIdent_;
}
return setLocation(
start, body, new (context_) ESTree::DeclareModuleNode(id, body, kind));
}
Optional<ESTree::Node *> JSParserImpl::parseDeclareClassFlow(SMLoc start) {
assert(check(TokenKind::rw_class));
advance(JSLexer::GrammarContext::Type);
// NOTE: Class definition is always strict mode code.
SaveStrictModeAndSeenDirectives saveStrictMode{this};
setStrictMode(true);
if (!need(
TokenKind::identifier,
"in class declaration",
"start of declaration",
start))
return None;
auto *id = setLocation(
tok_,
tok_,
new (context_)
ESTree::IdentifierNode(tok_->getIdentifier(), nullptr, false));
advance(JSLexer::GrammarContext::Type);
ESTree::Node *typeParams = nullptr;
if (check(TokenKind::less)) {
auto optParams = parseTypeParamsFlow();
if (!optParams)
return None;
typeParams = *optParams;
}
ESTree::NodeList extends{};
if (checkAndEat(TokenKind::rw_extends)) {
if (!need(
TokenKind::identifier,
"in class 'extends'",
"start of declaration",
start))
return None;
if (!parseInterfaceExtends(start, extends))
return None;
}
ESTree::NodeList mixins{};
if (checkAndEat(mixinsIdent_)) {
do {
if (!need(
TokenKind::identifier,
"in class 'mixins'",
"start of declaration",
start))
return None;
if (!parseInterfaceExtends(start, mixins))
return None;
} while (checkAndEat(TokenKind::comma, JSLexer::GrammarContext::Type));
}
ESTree::NodeList implements{};
if (checkAndEat(TokenKind::rw_implements)) {
do {
if (!need(
TokenKind::identifier,
"in class 'implements'",
"start of declaration",
start))
return None;
auto optImpl = parseClassImplementsFlow();
if (!optImpl)
return None;
implements.push_back(**optImpl);
} while (checkAndEat(TokenKind::comma, JSLexer::GrammarContext::Type));
}
if (!need(
TokenKind::l_brace,
"in declared class",
"start of declaration",
start))
return None;
auto optBody = parseObjectTypeAnnotationFlow(
AllowProtoProperty::Yes,
AllowStaticProperty::Yes,
AllowSpreadProperty::No);
if (!optBody)
return None;
return setLocation(
start,
*optBody,
new (context_) ESTree::DeclareClassNode(
id,
typeParams,
std::move(extends),
std::move(implements),
std::move(mixins),
*optBody));
}
Optional<ESTree::Node *> JSParserImpl::parseExportTypeDeclarationFlow(
SMLoc startLoc) {
assert(check(typeIdent_));
SMLoc typeIdentLoc = advance().Start;
if (checkAndEat(TokenKind::star)) {
// export type * FromClause;
// ^
auto optFromClause = parseFromClause();
if (!optFromClause) {
return None;
}
if (!eatSemi()) {
return None;
}
return setLocation(
startLoc,
getPrevTokenEndLoc(),
new (context_)
ESTree::ExportAllDeclarationNode(*optFromClause, typeIdent_));
}
if (check(TokenKind::l_brace)) {
ESTree::NodeList specifiers{};
llvh::SmallVector<SMRange, 2> invalids{};
auto optExportClause = parseExportClause(specifiers, invalids);
if (!optExportClause) {
return None;
}
ESTree::Node *source = nullptr;
if (check(fromIdent_)) {
// export ExportClause FromClause ;
auto optFromClause = parseFromClause();
if (!optFromClause) {
return None;
}
source = *optFromClause;
} else {
// export ExportClause ;
// ES9.0 15.2.3.1
// When there is no FromClause, any ranges added to invalids are
// actually invalid, and should be reported as errors.
for (const SMRange &range : invalids) {
error(range, "Invalid exported name");
}
}
if (!eatSemi()) {
return None;
}
return setLocation(
startLoc,
getPrevTokenEndLoc(),
new (context_) ESTree::ExportNamedDeclarationNode(
nullptr, std::move(specifiers), source, typeIdent_));
}
if (check(TokenKind::identifier)) {
auto optAlias = parseTypeAliasFlow(typeIdentLoc, TypeAliasKind::None);
if (!optAlias)
return None;
return setLocation(
startLoc,
*optAlias,
new (context_) ESTree::ExportNamedDeclarationNode(
*optAlias, {}, nullptr, typeIdent_));
}
errorExpected(
{TokenKind::star, TokenKind::l_brace, TokenKind::identifier},
"in export type declaration",
"start of export",
startLoc);
return None;
}
Optional<ESTree::Node *> JSParserImpl::parseDeclareExportFlow(
SMLoc start,
AllowDeclareExportType allowDeclareExportType) {
assert(check(TokenKind::rw_export));
advance(JSLexer::GrammarContext::Type);
SMLoc declareStart = tok_->getStartLoc();
if (checkAndEat(TokenKind::rw_default, JSLexer::GrammarContext::Type)) {
declareStart = tok_->getStartLoc();
if (check(TokenKind::rw_function)) {
auto optFunc = parseDeclareFunctionFlow(declareStart);
if (!optFunc)
return None;
return setLocation(
start,
*optFunc,
new (context_) ESTree::DeclareExportDeclarationNode(
*optFunc, {}, nullptr, true));
}
if (check(TokenKind::rw_class)) {
auto optClass = parseDeclareClassFlow(declareStart);
if (!optClass)
return None;
return setLocation(
start,
*optClass,
new (context_) ESTree::DeclareExportDeclarationNode(
*optClass, {}, nullptr, true));
}
auto optType = parseTypeAnnotationFlow();
if (!optType)
return None;
if (!eatSemi())
return None;
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_)
ESTree::DeclareExportDeclarationNode(*optType, {}, nullptr, true));
}
if (check(TokenKind::rw_function)) {
auto optFunc = parseDeclareFunctionFlow(declareStart);
if (!optFunc)
return None;
return setLocation(
start,
*optFunc,
new (context_)
ESTree::DeclareExportDeclarationNode(*optFunc, {}, nullptr, false));
}
if (check(TokenKind::rw_class)) {
auto optClass = parseDeclareClassFlow(declareStart);
if (!optClass)
return None;
return setLocation(
start,
*optClass,
new (context_) ESTree::DeclareExportDeclarationNode(
*optClass, {}, nullptr, false));
}
if (check(TokenKind::rw_var)) {
SMLoc varStart = advance(JSLexer::GrammarContext::Type).Start;
auto optIdent = parseBindingIdentifier(Param{});
if (!optIdent) {
errorExpected(
TokenKind::identifier,
"in var declaration",
"start of declaration",
start);
return None;
}
if (!eatSemi())
return None;
SMLoc end = getPrevTokenEndLoc();
return setLocation(
start,
end,
new (context_) ESTree::DeclareExportDeclarationNode(
setLocation(
varStart,
end,
new (context_) ESTree::DeclareVariableNode(*optIdent)),
{},
nullptr,
false));
}
if (checkAndEat(opaqueIdent_, JSLexer::GrammarContext::Type)) {
if (!check(typeIdent_)) {
error(tok_->getStartLoc(), "'type' required in opaque type declaration");
return None;
}
advance(JSLexer::GrammarContext::Type);
auto optType =
parseTypeAliasFlow(declareStart, TypeAliasKind::DeclareOpaque);
if (!optType)
return None;
return setLocation(
start,
*optType,
new (context_)
ESTree::DeclareExportDeclarationNode(*optType, {}, nullptr, false));
}
if (allowDeclareExportType == AllowDeclareExportType::Yes &&
check(typeIdent_)) {
advance(JSLexer::GrammarContext::Type);
auto optType = parseTypeAliasFlow(declareStart, TypeAliasKind::None);
if (!optType)
return None;
return setLocation(
start,
*optType,
new (context_)
ESTree::DeclareExportDeclarationNode(*optType, {}, nullptr, false));
}
if (checkN(TokenKind::rw_interface, interfaceIdent_)) {
auto optInterface = parseInterfaceDeclarationFlow();
if (!optInterface)
return None;
return setLocation(
start,
*optInterface,
new (context_) ESTree::DeclareExportDeclarationNode(
*optInterface, {}, nullptr, false));
}
if (checkAndEat(TokenKind::star, JSLexer::GrammarContext::Type)) {
// declare export * from 'foo';
// ^
if (!check(fromIdent_)) {
error(
tok_->getStartLoc(), "expected 'from' clause in export declaration");
return None;
}
auto optSource = parseFromClause();
if (!optSource)
return None;
if (!eatSemi())
return None;
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::DeclareExportAllDeclarationNode(*optSource));
}
if (!need(
TokenKind::l_brace, "in export specifier", "start of declare", start))
return None;
ESTree::NodeList specifiers{};
llvh::SmallVector<SMRange, 2> invalids{};
if (!parseExportClause(specifiers, invalids))
return None;
ESTree::Node *source = nullptr;
if (check(fromIdent_)) {
auto optSource = parseFromClause();
if (!optSource)
return None;
source = *optSource;
}
if (!eatSemi())
return None;
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::DeclareExportDeclarationNode(
nullptr, std::move(specifiers), source, false));
}
Optional<ESTree::Node *> JSParserImpl::parseTypeAnnotationFlow(
Optional<SMLoc> wrappedStart,
AllowAnonFunctionType allowAnonFunctionType) {
llvh::SaveAndRestore<bool> saveParam(
allowAnonFunctionType_,
allowAnonFunctionType == AllowAnonFunctionType::Yes);
auto optType = parseUnionTypeAnnotationFlow();
if (!optType)
return None;
if (wrappedStart) {
return setLocation(
*wrappedStart,
getPrevTokenEndLoc(),
new (context_) ESTree::TypeAnnotationNode(*optType));
}
return *optType;
}
Optional<ESTree::Node *> JSParserImpl::parseUnionTypeAnnotationFlow() {
SMLoc start = tok_->getStartLoc();
checkAndEat(TokenKind::pipe, JSLexer::GrammarContext::Type);
auto optFirst = parseIntersectionTypeAnnotationFlow();
if (!optFirst)
return None;
if (!check(TokenKind::pipe)) {
// Done with the union, move on.
return *optFirst;
}
ESTree::NodeList types{};
types.push_back(**optFirst);
while (checkAndEat(TokenKind::pipe, JSLexer::GrammarContext::Type)) {
auto optInt = parseIntersectionTypeAnnotationFlow();
if (!optInt)
return None;
types.push_back(**optInt);
}
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::UnionTypeAnnotationNode(std::move(types)));
}
Optional<ESTree::Node *> JSParserImpl::parseIntersectionTypeAnnotationFlow() {
SMLoc start = tok_->getStartLoc();
checkAndEat(TokenKind::amp, JSLexer::GrammarContext::Type);
auto optFirst = parseAnonFunctionWithoutParensTypeAnnotationFlow();
if (!optFirst)
return None;
if (!check(TokenKind::amp)) {
// Done with the union, move on.
return *optFirst;
}
ESTree::NodeList types{};
types.push_back(**optFirst);
while (checkAndEat(TokenKind::amp, JSLexer::GrammarContext::Type)) {
auto optInt = parseAnonFunctionWithoutParensTypeAnnotationFlow();
if (!optInt)
return None;
types.push_back(**optInt);
}
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::IntersectionTypeAnnotationNode(std::move(types)));
}
Optional<ESTree::Node *>
JSParserImpl::parseAnonFunctionWithoutParensTypeAnnotationFlow() {
SMLoc start = tok_->getStartLoc();
auto optParam = parsePrefixTypeAnnotationFlow();
if (!optParam)
return None;
if (allowAnonFunctionType_ && check(TokenKind::equalgreater)) {
// ParamType => ReturnType
// ^
ESTree::NodeList params{};
// "Reparse" the param into a FunctionTypeParam so it can be used for
// parseFunctionTypeAnnotationWithParamsFlow.
params.push_back(*setLocation(
*optParam,
*optParam,
new (context_) ESTree::FunctionTypeParamNode(
/* name */ nullptr, *optParam, /* optional */ false)));
ESTree::Node *rest = nullptr;
ESTree::Node *typeParams = nullptr;
return parseFunctionTypeAnnotationWithParamsFlow(
start, std::move(params), nullptr, rest, typeParams);
}
return *optParam;
}
Optional<ESTree::Node *> JSParserImpl::parsePrefixTypeAnnotationFlow() {
SMLoc start = tok_->getStartLoc();
if (checkAndEat(TokenKind::question, JSLexer::GrammarContext::Type)) {
auto optPrefix = parsePrefixTypeAnnotationFlow();
if (!optPrefix)
return None;
return setLocation(
start,
getPrevTokenEndLoc(),
new (context_) ESTree::NullableTypeAnnotationNode(*optPrefix));
}
return parsePostfixTypeAnnotationFlow();
}
Optional<ESTree::Node *> JSParserImpl::parsePostfixTypeAnnotationFlow() {