@@ -526,6 +526,46 @@ extension Parser {
526
526
arena: self . arena
527
527
)
528
528
)
529
+
530
+ case ( . repeat , let handle) ? :
531
+ // 'repeat' is the start of a pack expansion expression.
532
+ return RawExprSyntax ( parsePackExpansionExpr ( repeatHandle: handle, flavor, pattern: pattern) )
533
+
534
+ case ( . each, let handle) ? :
535
+ // `each` is only contextually a keyword, if it's followed by an
536
+ // identifier or 'self' on the same line. We do this to ensure that we do
537
+ // not break any 'each' functions defined by users. This is following with
538
+ // what we have done for the consume keyword.
539
+ switch self . peek ( ) {
540
+ case TokenSpec ( . identifier, allowAtStartOfLine: false ) ,
541
+ TokenSpec ( . dollarIdentifier, allowAtStartOfLine: false ) ,
542
+ TokenSpec ( . self , allowAtStartOfLine: false ) :
543
+ break
544
+ default :
545
+ // Break out of `outer switch` on failure.
546
+ break EXPR_PREFIX
547
+ }
548
+
549
+ let each = self . eat ( handle)
550
+ let packRef = self . parseSequenceExpressionElement ( flavor, pattern: pattern)
551
+ return RawExprSyntax (
552
+ RawPackElementExprSyntax (
553
+ eachKeyword: each,
554
+ packRefExpr: packRef,
555
+ arena: self . arena
556
+ )
557
+ )
558
+
559
+ case ( . any, _) ? :
560
+ // `any` is only contextually a keyword if it's followed by an identifier
561
+ // on the same line.
562
+ guard case TokenSpec( . identifier, allowAtStartOfLine: false ) = self . peek ( ) else {
563
+ break EXPR_PREFIX
564
+ }
565
+ // 'any' is parsed as a part of 'type'.
566
+ let type = self . parseType ( )
567
+ return RawExprSyntax ( RawTypeExprSyntax ( type: type, arena: self . arena) )
568
+
529
569
case nil :
530
570
break
531
571
}
@@ -550,10 +590,6 @@ extension Parser {
550
590
// tryLexRegexLiteral(/*forUnappliedOperator*/ false)
551
591
552
592
switch self . currentToken {
553
- case TokenSpec ( . repeat ) :
554
- // 'repeat' is the start of a pack expansion expression.
555
- return RawExprSyntax ( parsePackExpansionExpr ( flavor, pattern: pattern) )
556
-
557
593
// Try parse an 'if' or 'switch' as an expression. Note we do this here in
558
594
// parseUnaryExpression as we don't allow postfix syntax to hang off such
559
595
// expressions to avoid ambiguities such as postfix '.member', which can
@@ -1224,27 +1260,6 @@ extension Parser {
1224
1260
return RawExprSyntax ( RawUnresolvedPatternExprSyntax ( pattern: pattern, arena: self . arena) )
1225
1261
}
1226
1262
1227
- // We might have a contextual keyword followed by an identifier.
1228
- // 'each <identifier>' is a pack element expr, and 'any <identifier>'
1229
- // is an existential type expr.
1230
- if self . peek ( ) . rawTokenKind == . identifier, !self . peek ( ) . isAtStartOfLine {
1231
- if self . at ( . keyword( . any) ) {
1232
- let ty = self . parseType ( )
1233
- return RawExprSyntax ( RawTypeExprSyntax ( type: ty, arena: self . arena) )
1234
- }
1235
-
1236
- if let each = self . consume ( if: . keyword( . each) ) {
1237
- let packRef = self . parseSequenceExpressionElement ( flavor, pattern: pattern)
1238
- return RawExprSyntax (
1239
- RawPackElementExprSyntax (
1240
- eachKeyword: each,
1241
- packRefExpr: packRef,
1242
- arena: self . arena
1243
- )
1244
- )
1245
- }
1246
- }
1247
-
1248
1263
return RawExprSyntax ( self . parseIdentifierExpression ( ) )
1249
1264
case ( . Self, _) ? : // Self
1250
1265
return RawExprSyntax ( self . parseIdentifierExpression ( ) )
@@ -1441,10 +1456,11 @@ extension Parser {
1441
1456
/// pack-expansion-expression → 'repeat' pattern-expression
1442
1457
/// pattern-expression → expression
1443
1458
mutating func parsePackExpansionExpr(
1459
+ repeatHandle: TokenConsumptionHandle ,
1444
1460
_ flavor: ExprFlavor ,
1445
1461
pattern: PatternContext
1446
1462
) -> RawPackExpansionExprSyntax {
1447
- let repeatKeyword = self . consumeAnyToken ( )
1463
+ let repeatKeyword = self . eat ( repeatHandle )
1448
1464
let patternExpr = self . parseExpression ( flavor, pattern: pattern)
1449
1465
1450
1466
return RawPackExpansionExprSyntax (
0 commit comments