@@ -130,7 +130,7 @@ extension Parser {
130
130
case ( . enumKeyword, _) ? :
131
131
return RawDeclSyntax ( self . parseEnumDeclaration ( attrs) )
132
132
case ( . caseKeyword, _) ? :
133
- return RawDeclSyntax ( self . parseDeclEnumCase ( attrs) )
133
+ return RawDeclSyntax ( self . parseEnumCaseDeclaration ( attrs) )
134
134
case ( . structKeyword, _) ? :
135
135
return RawDeclSyntax ( self . parseStructDeclaration ( attrs) )
136
136
case ( . protocolKeyword, _) ? :
@@ -715,7 +715,7 @@ extension Parser {
715
715
/// raw-value-assignment → = raw-value-literal
716
716
/// raw-value-literal → numeric-literal | static-string-literal | boolean-literal
717
717
@_spi ( RawSyntax)
718
- public mutating func parseDeclEnumCase ( _ attrs: DeclAttributes ) -> RawEnumCaseDeclSyntax {
718
+ public mutating func parseEnumCaseDeclaration ( _ attrs: DeclAttributes ) -> RawEnumCaseDeclSyntax {
719
719
let ( unexpectedBeforeCaseKeyword, caseKeyword) = self . expect ( . caseKeyword)
720
720
var elements = [ RawEnumCaseElementSyntax] ( )
721
721
do {
@@ -726,7 +726,7 @@ extension Parser {
726
726
727
727
let associatedValue : RawParameterClauseSyntax ?
728
728
if self . at ( . leftParen, where: { !$0. isAtStartOfLine } ) {
729
- associatedValue = self . parseParameterClause ( )
729
+ associatedValue = self . parseParameterClause ( for : . enumCase )
730
730
} else {
731
731
associatedValue = nil
732
732
}
@@ -1142,27 +1142,52 @@ extension Parser {
1142
1142
1143
1143
extension Parser {
1144
1144
@_spi ( RawSyntax)
1145
- public mutating func parseParameterClause( isClosure: Bool = false ) -> RawParameterClauseSyntax {
1145
+ public enum ParameterSubject {
1146
+ case closure
1147
+ case enumCase
1148
+ case functionParameters
1149
+ case indices
1150
+
1151
+ var isClosure : Bool {
1152
+ switch self {
1153
+ case . closure: return true
1154
+ case . enumCase: return false
1155
+ case . functionParameters: return false
1156
+ case . indices: return false
1157
+ }
1158
+ }
1159
+ }
1160
+
1161
+ @_spi ( RawSyntax)
1162
+ public mutating func parseParameterClause( for subject: ParameterSubject ) -> RawParameterClauseSyntax {
1146
1163
let ( unexpectedBeforeLParen, lparen) = self . expect ( . leftParen)
1147
1164
var elements = [ RawFunctionParameterSyntax] ( )
1148
- // If we are missing the left parenthesis and the next token doesn't appear to be an argument label, don't parse any parameters.
1165
+ // If we are missing the left parenthesis and the next token doesn't appear
1166
+ // to be an argument label, don't parse any parameters.
1149
1167
let shouldSkipParameterParsing = lparen. isMissing && ( !currentToken. canBeArgumentLabel || currentToken. isKeyword)
1150
1168
if !shouldSkipParameterParsing {
1151
1169
var keepGoing = true
1152
1170
var loopProgress = LoopProgressCondition ( )
1153
1171
while !self . at ( any: [ . eof, . rightParen] )
1154
1172
&& keepGoing
1155
1173
&& loopProgress. evaluate ( currentToken) {
1156
- // Attributes.
1157
- let attrs = self . parseAttributeList ( )
1174
+ // Parse any declaration attributes. The exception here is enum cases
1175
+ // which only allow types, so we do not consume attributes to allow the
1176
+ // type attribute grammar a chance to examine them.
1177
+ let attrs : RawAttributeListSyntax ?
1178
+ if case . enumCase = subject {
1179
+ attrs = nil
1180
+ } else {
1181
+ attrs = self . parseAttributeList ( )
1182
+ }
1158
1183
1159
1184
let firstName : RawTokenSyntax ?
1160
1185
let secondName : RawTokenSyntax ?
1161
1186
let unexpectedBeforeColon : RawUnexpectedNodesSyntax ?
1162
1187
let colon : RawTokenSyntax ?
1163
1188
let shouldParseType : Bool
1164
1189
1165
- if self . lookahead ( ) . startsParameterName ( isClosure) {
1190
+ if self . lookahead ( ) . startsParameterName ( subject . isClosure) {
1166
1191
if self . currentToken. canBeArgumentLabel {
1167
1192
firstName = self . parseArgumentLabel ( )
1168
1193
} else {
@@ -1174,7 +1199,7 @@ extension Parser {
1174
1199
} else {
1175
1200
secondName = nil
1176
1201
}
1177
- if isClosure {
1202
+ if subject . isClosure {
1178
1203
unexpectedBeforeColon = nil
1179
1204
colon = self . consume ( if: . colon)
1180
1205
shouldParseType = ( colon != nil )
@@ -1314,7 +1339,7 @@ extension Parser {
1314
1339
1315
1340
@_spi ( RawSyntax)
1316
1341
public mutating func parseFunctionSignature( ) -> RawFunctionSignatureSyntax {
1317
- let input = self . parseParameterClause ( )
1342
+ let input = self . parseParameterClause ( for : . functionParameters )
1318
1343
1319
1344
let async = self . consumeIfContextualKeyword ( " async " )
1320
1345
@@ -1361,7 +1386,7 @@ extension Parser {
1361
1386
genericParameterClause = nil
1362
1387
}
1363
1388
1364
- let indices = self . parseParameterClause ( )
1389
+ let indices = self . parseParameterClause ( for : . indices )
1365
1390
1366
1391
let result : RawReturnClauseSyntax
1367
1392
if self . at ( . arrow) {
0 commit comments