@@ -3518,16 +3518,10 @@ ParserStatus Parser::parseInheritance(SmallVectorImpl<TypeLoc> &Inherited,
3518
3518
return Status;
3519
3519
}
3520
3520
3521
- enum class TokenProperty {
3522
- None,
3523
- StartsWithLess,
3524
- };
3525
-
3526
3521
static ParserStatus
3527
3522
parseIdentifierDeclName (Parser &P, Identifier &Result, SourceLoc &Loc,
3528
- StringRef DeclKindName, tok ResyncT1, tok ResyncT2,
3529
- tok ResyncT3, tok ResyncT4,
3530
- TokenProperty ResyncP1) {
3523
+ StringRef DeclKindName,
3524
+ llvm::function_ref<bool (const Token &)> canRecover) {
3531
3525
if (P.Tok .is (tok::identifier)) {
3532
3526
Loc = P.consumeIdentifier (&Result);
3533
3527
@@ -3566,9 +3560,7 @@ parseIdentifierDeclName(Parser &P, Identifier &Result, SourceLoc &Loc,
3566
3560
.fixItReplace (P.Tok .getLoc (), " `" + P.Tok .getText ().str () + " `" );
3567
3561
3568
3562
// Recover if the next token is one of the expected tokens.
3569
- auto Next = P.peekToken ();
3570
- if (Next.isAny (ResyncT1, ResyncT2, ResyncT3, ResyncT4) ||
3571
- (ResyncP1 != TokenProperty::None && P.startsWithLess (Next))) {
3563
+ if (canRecover (P.peekToken ())) {
3572
3564
llvm::SmallString<32 > Name (P.Tok .getText ());
3573
3565
// Append an invalid character so that nothing can resolve to this name.
3574
3566
Name += " #" ;
@@ -3585,38 +3577,6 @@ parseIdentifierDeclName(Parser &P, Identifier &Result, SourceLoc &Loc,
3585
3577
return makeParserError ();
3586
3578
}
3587
3579
3588
- static ParserStatus
3589
- parseIdentifierDeclName (Parser &P, Identifier &Result, SourceLoc &L,
3590
- StringRef DeclKindName, tok ResyncT1, tok ResyncT2) {
3591
- return parseIdentifierDeclName (P, Result, L, DeclKindName, ResyncT1, ResyncT2,
3592
- tok::NUM_TOKENS, tok::NUM_TOKENS,
3593
- TokenProperty::None);
3594
- }
3595
-
3596
- static ParserStatus
3597
- parseIdentifierDeclName (Parser &P, Identifier &Result, SourceLoc &L,
3598
- StringRef DeclKindName, tok ResyncT1, tok ResyncT2,
3599
- tok ResyncT3, tok ResyncT4) {
3600
- return parseIdentifierDeclName (P, Result, L, DeclKindName, ResyncT1, ResyncT2,
3601
- ResyncT3, ResyncT4, TokenProperty::None);
3602
- }
3603
-
3604
- static ParserStatus
3605
- parseIdentifierDeclName (Parser &P, Identifier &Result, SourceLoc &L,
3606
- StringRef DeclKindName, tok ResyncT1, tok ResyncT2,
3607
- TokenProperty ResyncP1) {
3608
- return parseIdentifierDeclName (P, Result, L, DeclKindName, ResyncT1, ResyncT2,
3609
- tok::NUM_TOKENS, tok::NUM_TOKENS, ResyncP1);
3610
- }
3611
-
3612
- static ParserStatus
3613
- parseIdentifierDeclName (Parser &P, Identifier &Result, SourceLoc &L,
3614
- StringRef DeclKindName, tok ResyncT1, tok ResyncT2,
3615
- tok ResyncT3, TokenProperty ResyncP1) {
3616
- return parseIdentifierDeclName (P, Result, L, DeclKindName, ResyncT1, ResyncT2,
3617
- ResyncT3, tok::NUM_TOKENS, ResyncP1);
3618
- }
3619
-
3620
3580
// / Add a fix-it to remove the space in consecutive identifiers.
3621
3581
// / Add a camel-cased option if it is different than the first option.
3622
3582
void Parser::diagnoseConsecutiveIDs (StringRef First, SourceLoc FirstLoc,
@@ -4135,8 +4095,9 @@ parseDeclTypeAlias(Parser::ParseDeclOptions Flags, DeclAttributes &Attributes) {
4135
4095
SourceLoc IdLoc;
4136
4096
ParserStatus Status;
4137
4097
4138
- Status |= parseIdentifierDeclName (*this , Id, IdLoc, " typealias" ,
4139
- tok::colon, tok::equal);
4098
+ Status |= parseIdentifierDeclName (
4099
+ *this , Id, IdLoc, " typealias" ,
4100
+ [](const Token &next) { return next.isAny (tok::colon, tok::equal); });
4140
4101
if (Status.isError ()) {
4141
4102
TmpCtxt->setTransparent ();
4142
4103
return nullptr ;
@@ -4250,9 +4211,10 @@ ParserResult<TypeDecl> Parser::parseDeclAssociatedType(Parser::ParseDeclOptions
4250
4211
} else {
4251
4212
AssociatedTypeLoc = consumeToken (tok::kw_associatedtype);
4252
4213
}
4253
-
4254
- Status = parseIdentifierDeclName (*this , Id, IdLoc, " associatedtype" ,
4255
- tok::colon, tok::equal);
4214
+
4215
+ Status = parseIdentifierDeclName (
4216
+ *this , Id, IdLoc, " associatedtype" ,
4217
+ [](const Token &next) { return next.isAny (tok::colon, tok::equal); });
4256
4218
if (Status.isError ())
4257
4219
return nullptr ;
4258
4220
@@ -5473,8 +5435,10 @@ ParserResult<FuncDecl> Parser::parseDeclFunc(SourceLoc StaticLoc,
5473
5435
// because we're aggressive about recovering/providing good diagnostics for
5474
5436
// beginners.
5475
5437
auto NameStatus = parseIdentifierDeclName (
5476
- *this , SimpleName, NameLoc, " function" , tok::l_paren, tok::arrow,
5477
- tok::l_brace, TokenProperty::StartsWithLess);
5438
+ *this , SimpleName, NameLoc, " function" , [&](const Token &next) {
5439
+ return next.isAny (tok::l_paren, tok::arrow, tok::l_brace) ||
5440
+ startsWithLess (next);
5441
+ });
5478
5442
if (NameStatus.isError ())
5479
5443
return nullptr ;
5480
5444
}
@@ -5717,9 +5681,10 @@ ParserResult<EnumDecl> Parser::parseDeclEnum(ParseDeclOptions Flags,
5717
5681
SourceLoc EnumNameLoc;
5718
5682
ParserStatus Status;
5719
5683
5720
- Status |= parseIdentifierDeclName (*this , EnumName, EnumNameLoc, " enum" ,
5721
- tok::colon, tok::l_brace,
5722
- TokenProperty::StartsWithLess);
5684
+ Status |= parseIdentifierDeclName (
5685
+ *this , EnumName, EnumNameLoc, " enum" , [&](const Token &next) {
5686
+ return next.isAny (tok::colon, tok::l_brace) || startsWithLess (next);
5687
+ });
5723
5688
if (Status.isError ())
5724
5689
return nullptr ;
5725
5690
@@ -5823,9 +5788,11 @@ Parser::parseDeclEnumCase(ParseDeclOptions Flags,
5823
5788
}
5824
5789
5825
5790
if (Tok.is (tok::identifier)) {
5826
- Status |= parseIdentifierDeclName (*this , Name, NameLoc, " enum 'case'" ,
5827
- tok::l_paren, tok::kw_case, tok::colon,
5828
- tok::r_brace);
5791
+ Status |= parseIdentifierDeclName (
5792
+ *this , Name, NameLoc, " enum 'case'" , [](const Token &next) {
5793
+ return next.isAny (tok::l_paren, tok::kw_case, tok::colon,
5794
+ tok::r_brace);
5795
+ });
5829
5796
assert (Status.isSuccess ());
5830
5797
if (DotLoc.isValid ())
5831
5798
diagnose (DotLoc, diag::enum_case_dot_prefix)
@@ -5997,9 +5964,10 @@ ParserResult<StructDecl> Parser::parseDeclStruct(ParseDeclOptions Flags,
5997
5964
SourceLoc StructNameLoc;
5998
5965
ParserStatus Status;
5999
5966
6000
- Status |= parseIdentifierDeclName (*this , StructName, StructNameLoc, " struct" ,
6001
- tok::colon, tok::l_brace,
6002
- TokenProperty::StartsWithLess);
5967
+ Status |= parseIdentifierDeclName (
5968
+ *this , StructName, StructNameLoc, " struct" , [&](const Token &next) {
5969
+ return next.isAny (tok::colon, tok::l_brace) || startsWithLess (next);
5970
+ });
6003
5971
if (Status.isError ())
6004
5972
return nullptr ;
6005
5973
@@ -6088,9 +6056,10 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,
6088
6056
SourceLoc ClassNameLoc;
6089
6057
ParserStatus Status;
6090
6058
6091
- Status |= parseIdentifierDeclName (*this , ClassName, ClassNameLoc, " class" ,
6092
- tok::colon, tok::l_brace,
6093
- TokenProperty::StartsWithLess);
6059
+ Status |= parseIdentifierDeclName (
6060
+ *this , ClassName, ClassNameLoc, " class" , [&](const Token &next) {
6061
+ return next.isAny (tok::colon, tok::l_brace) || startsWithLess (next);
6062
+ });
6094
6063
if (Status.isError ())
6095
6064
return nullptr ;
6096
6065
@@ -6207,8 +6176,9 @@ parseDeclProtocol(ParseDeclOptions Flags, DeclAttributes &Attributes) {
6207
6176
Identifier ProtocolName;
6208
6177
ParserStatus Status;
6209
6178
6210
- Status |= parseIdentifierDeclName (*this , ProtocolName, NameLoc, " protocol" ,
6211
- tok::colon, tok::l_brace);
6179
+ Status |= parseIdentifierDeclName (
6180
+ *this , ProtocolName, NameLoc, " protocol" ,
6181
+ [&](const Token &next) { return next.isAny (tok::colon, tok::l_brace); });
6212
6182
if (Status.isError ())
6213
6183
return nullptr ;
6214
6184
0 commit comments