Skip to content

Commit aa82a85

Browse files
authored
Merge pull request #41848 from CodaFi/parse-tial-credit
2 parents 78f6baf + cc0bc22 commit aa82a85

File tree

6 files changed

+78
-12
lines changed

6 files changed

+78
-12
lines changed

include/swift/Parse/Parser.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,6 +1194,8 @@ class Parser {
11941194

11951195
ParserStatus parsePrimaryAssociatedTypes(
11961196
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
1197+
ParserStatus parsePrimaryAssociatedTypeList(
1198+
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes);
11971199
ParserResult<ProtocolDecl> parseDeclProtocol(ParseDeclOptions Flags,
11981200
DeclAttributes &Attributes);
11991201

lib/Parse/ParseDecl.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7909,8 +7909,30 @@ ParserResult<ClassDecl> Parser::parseDeclClass(ParseDeclOptions Flags,
79097909

79107910
ParserStatus Parser::parsePrimaryAssociatedTypes(
79117911
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes) {
7912+
SyntaxParsingContext GPSContext(SyntaxContext,
7913+
SyntaxKind::PrimaryAssociatedTypeClause);
7914+
79127915
SourceLoc LAngleLoc = consumeStartingLess();
79137916

7917+
auto Result = parsePrimaryAssociatedTypeList(AssocTypes);
7918+
7919+
// Parse the closing '>'.
7920+
SourceLoc RAngleLoc;
7921+
if (startsWithGreater(Tok)) {
7922+
RAngleLoc = consumeStartingGreater();
7923+
} else {
7924+
diagnose(Tok, diag::expected_rangle_primary_associated_type_list);
7925+
diagnose(LAngleLoc, diag::opening_angle);
7926+
7927+
// Skip until we hit the '>'.
7928+
RAngleLoc = skipUntilGreaterInTypeList();
7929+
}
7930+
7931+
return Result;
7932+
}
7933+
7934+
ParserStatus Parser::parsePrimaryAssociatedTypeList(
7935+
SmallVectorImpl<AssociatedTypeDecl *> &AssocTypes) {
79147936
ParserStatus Result;
79157937
SyntaxParsingContext PATContext(SyntaxContext,
79167938
SyntaxKind::PrimaryAssociatedTypeList);
@@ -7992,18 +8014,6 @@ ParserStatus Parser::parsePrimaryAssociatedTypes(
79928014
HasNextParam = consumeIf(tok::comma);
79938015
} while (HasNextParam);
79948016

7995-
// Parse the closing '>'.
7996-
SourceLoc RAngleLoc;
7997-
if (startsWithGreater(Tok)) {
7998-
RAngleLoc = consumeStartingGreater();
7999-
} else {
8000-
diagnose(Tok, diag::expected_rangle_primary_associated_type_list);
8001-
diagnose(LAngleLoc, diag::opening_angle);
8002-
8003-
// Skip until we hit the '>'.
8004-
RAngleLoc = skipUntilGreaterInTypeList();
8005-
}
8006-
80078017
return Result;
80088018
}
80098019

unittests/Syntax/DeclSyntaxTests.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,3 +633,44 @@ TEST(DeclSyntaxTests, FunctionDeclWithAPIs) {
633633
TEST(DeclSyntaxTests, FunctionDeclBuilderAPIs) {
634634

635635
}
636+
637+
#pragma mark - parameterized protocol-decl
638+
639+
TEST(DeclSyntaxTests, ProtocolMakeAPIs) {
640+
RC<SyntaxArena> Arena = SyntaxArena::make();
641+
SyntaxFactory Factory(Arena);
642+
{
643+
SmallString<1> Scratch;
644+
llvm::raw_svector_ostream OS(Scratch);
645+
Factory.makeBlankProtocolDecl().print(OS);
646+
ASSERT_EQ(OS.str().str(), "");
647+
}
648+
{
649+
SmallString<64> Scratch;
650+
llvm::raw_svector_ostream OS(Scratch);
651+
auto Protocol = Factory.makeProtocolKeyword("", " ");
652+
auto MyCollection = Factory.makeIdentifier("MyCollection", "", "");
653+
auto ElementName = Factory.makeIdentifier("Element", "", "");
654+
auto ElementParam =
655+
Factory.makePrimaryAssociatedType(None, ElementName, None, None, None, None);
656+
auto LeftAngle = Factory.makeLeftAngleToken("", "");
657+
auto RightAngle = Factory.makeRightAngleToken("", " ");
658+
auto PrimaryAssocs = PrimaryAssociatedTypeClauseSyntaxBuilder(Arena)
659+
.useLeftAngleBracket(LeftAngle)
660+
.useRightAngleBracket(RightAngle)
661+
.addPrimaryAssociatedType(ElementParam)
662+
.build();
663+
664+
auto LeftBrace = Factory.makeLeftBraceToken("", "");
665+
auto RightBrace = Factory.makeRightBraceToken("", "");
666+
auto Members = MemberDeclBlockSyntaxBuilder(Arena)
667+
.useLeftBrace(LeftBrace)
668+
.useRightBrace(RightBrace)
669+
.build();
670+
Factory
671+
.makeProtocolDecl(None, None, Protocol, MyCollection, PrimaryAssocs, None, None, Members)
672+
.print(OS);
673+
ASSERT_EQ(OS.str().str(),
674+
"protocol MyCollection<Element> {}");
675+
}
676+
}

utils/gyb_syntax_support/DeclNodes.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@
257257
collection_element_name='Modifier', is_optional=True),
258258
Child('ProtocolKeyword', kind='ProtocolToken'),
259259
Child('Identifier', kind='IdentifierToken'),
260+
Child('PrimaryAssociatedTypeClause',
261+
kind='PrimaryAssociatedTypeClause',
262+
is_optional=True),
260263
Child('InheritanceClause', kind='TypeInheritanceClause',
261264
is_optional=True),
262265
Child('GenericWhereClause', kind='GenericWhereClause',

utils/gyb_syntax_support/GenericNodes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,13 @@
101101
Child('Colon', kind='ColonToken'),
102102
Child('RightTypeIdentifier', kind='Type'),
103103
]),
104+
105+
# primary-associated-type-clause -> '<' primary-associated-type-list '>'
106+
Node('PrimaryAssociatedTypeClause', kind='Syntax',
107+
children=[
108+
Child('LeftAngleBracket', kind='LeftAngleToken'),
109+
Child('PrimaryAssociatedTypeList', kind='PrimaryAssociatedTypeList',
110+
collection_element_name='PrimaryAssociatedType'),
111+
Child('RightAngleBracket', kind='RightAngleToken'),
112+
]),
104113
]

utils/gyb_syntax_support/NodeSerializationCodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@
257257
'RegexLiteralExpr': 253,
258258
'PrimaryAssociatedTypeList' : 254,
259259
'PrimaryAssociatedType' : 255,
260+
'PrimaryAssociatedTypeClause' : 256,
260261
}
261262

262263

0 commit comments

Comments
 (0)