@@ -525,6 +525,46 @@ extension Parser {
525
525
arena: self . arena
526
526
)
527
527
)
528
+
529
+ case ( . repeat , let handle) ? :
530
+ // 'repeat' is the start of a pack expansion expression.
531
+ return RawExprSyntax ( parsePackExpansionExpr ( repeatHandle: handle, flavor, pattern: pattern) )
532
+
533
+ case ( . each, let handle) ? :
534
+ // `each` is only contextually a keyword, if it's followed by an
535
+ // identifier or 'self' on the same line. We do this to ensure that we do
536
+ // not break any 'each' functions defined by users. This is following with
537
+ // what we have done for the consume keyword.
538
+ switch self . peek ( ) {
539
+ case TokenSpec ( . identifier, allowAtStartOfLine: false ) ,
540
+ TokenSpec ( . dollarIdentifier, allowAtStartOfLine: false ) ,
541
+ TokenSpec ( . self , allowAtStartOfLine: false ) :
542
+ break
543
+ default :
544
+ // Break out of `outer switch` on failure.
545
+ break EXPR_PREFIX
546
+ }
547
+
548
+ let each = self . eat ( handle)
549
+ let packRef = self . parseSequenceExpressionElement ( flavor, pattern: pattern)
550
+ return RawExprSyntax (
551
+ RawPackElementExprSyntax (
552
+ eachKeyword: each,
553
+ packRefExpr: packRef,
554
+ arena: self . arena
555
+ )
556
+ )
557
+
558
+ case ( . any, _) ? :
559
+ // `any` is only contextually a keyword if it's followed by an identifier
560
+ // on the same line.
561
+ guard case TokenSpec( . identifier, allowAtStartOfLine: false ) = self . peek ( ) else {
562
+ break EXPR_PREFIX
563
+ }
564
+ // 'any' is parsed as a part of 'type'.
565
+ let type = self . parseType ( )
566
+ return RawExprSyntax ( RawTypeExprSyntax ( type: type, arena: self . arena) )
567
+
528
568
case nil :
529
569
break
530
570
}
@@ -549,10 +589,6 @@ extension Parser {
549
589
// tryLexRegexLiteral(/*forUnappliedOperator*/ false)
550
590
551
591
switch self . currentToken {
552
- case TokenSpec ( . repeat ) :
553
- // 'repeat' is the start of a pack expansion expression.
554
- return RawExprSyntax ( parsePackExpansionExpr ( flavor, pattern: pattern) )
555
-
556
592
// Try parse an 'if' or 'switch' as an expression. Note we do this here in
557
593
// parseUnaryExpression as we don't allow postfix syntax to hang off such
558
594
// expressions to avoid ambiguities such as postfix '.member', which can
@@ -1234,27 +1270,6 @@ extension Parser {
1234
1270
return RawExprSyntax ( RawUnresolvedPatternExprSyntax ( pattern: pattern, arena: self . arena) )
1235
1271
}
1236
1272
1237
- // We might have a contextual keyword followed by an identifier.
1238
- // 'each <identifier>' is a pack element expr, and 'any <identifier>'
1239
- // is an existential type expr.
1240
- if self . peek ( ) . rawTokenKind == . identifier, !self . peek ( ) . isAtStartOfLine {
1241
- if self . at ( . keyword( . any) ) {
1242
- let ty = self . parseType ( )
1243
- return RawExprSyntax ( RawTypeExprSyntax ( type: ty, arena: self . arena) )
1244
- }
1245
-
1246
- if let each = self . consume ( if: . keyword( . each) ) {
1247
- let packRef = self . parseSequenceExpressionElement ( flavor, pattern: pattern)
1248
- return RawExprSyntax (
1249
- RawPackElementExprSyntax (
1250
- eachKeyword: each,
1251
- packRefExpr: packRef,
1252
- arena: self . arena
1253
- )
1254
- )
1255
- }
1256
- }
1257
-
1258
1273
return RawExprSyntax ( self . parseIdentifierExpression ( ) )
1259
1274
case ( . Self, _) ? : // Self
1260
1275
return RawExprSyntax ( self . parseIdentifierExpression ( ) )
@@ -1493,10 +1508,11 @@ extension Parser {
1493
1508
/// pack-expansion-expression → 'repeat' pattern-expression
1494
1509
/// pattern-expression → expression
1495
1510
mutating func parsePackExpansionExpr(
1511
+ repeatHandle: TokenConsumptionHandle ,
1496
1512
_ flavor: ExprFlavor ,
1497
1513
pattern: PatternContext
1498
1514
) -> RawPackExpansionExprSyntax {
1499
- let repeatKeyword = self . consumeAnyToken ( )
1515
+ let repeatKeyword = self . eat ( repeatHandle )
1500
1516
let patternExpr = self . parseExpression ( flavor, pattern: pattern)
1501
1517
1502
1518
return RawPackExpansionExprSyntax (
0 commit comments