@@ -65,6 +65,7 @@ extension Parser {
65
65
enum ExprFlavor {
66
66
case basic
67
67
case trailingClosure
68
+ case poundIfDirective
68
69
}
69
70
70
71
enum PatternContext {
@@ -115,10 +116,9 @@ extension Parser {
115
116
/// Parse a sequence of expressions.
116
117
mutating func parseSequenceExpression(
117
118
_ flavor: ExprFlavor ,
118
- forDirective: Bool = false ,
119
119
pattern: PatternContext = . none
120
120
) -> RawExprSyntax {
121
- if forDirective && self . atStartOfLine {
121
+ if flavor == . poundIfDirective && self . atStartOfLine {
122
122
return RawExprSyntax ( RawMissingExprSyntax ( arena: self . arena) )
123
123
}
124
124
@@ -132,15 +132,14 @@ extension Parser {
132
132
133
133
lastElement = self . parseSequenceExpressionElement (
134
134
flavor,
135
- forDirective: forDirective,
136
135
pattern: pattern
137
136
)
138
137
139
138
var loopProgress = LoopProgressCondition ( )
140
139
while self . hasProgressed ( & loopProgress) {
141
140
guard
142
141
!lastElement. is ( RawMissingExprSyntax . self) ,
143
- !( forDirective && self . atStartOfLine)
142
+ !( flavor == . poundIfDirective && self . atStartOfLine)
144
143
else {
145
144
break
146
145
}
@@ -160,14 +159,13 @@ extension Parser {
160
159
if let rhsExpr {
161
160
// Operator parsing returned the RHS.
162
161
lastElement = rhsExpr
163
- } else if forDirective && self . atStartOfLine {
162
+ } else if flavor == . poundIfDirective && self . atStartOfLine {
164
163
// Don't allow RHS at a newline for `#if` conditions.
165
164
lastElement = RawExprSyntax ( RawMissingExprSyntax ( arena: self . arena) )
166
165
break
167
166
} else {
168
167
lastElement = self . parseSequenceExpressionElement (
169
168
flavor,
170
- forDirective: forDirective,
171
169
pattern: pattern
172
170
)
173
171
}
@@ -365,7 +363,6 @@ extension Parser {
365
363
/// Parse an expression sequence element.
366
364
mutating func parseSequenceExpressionElement(
367
365
_ flavor: ExprFlavor ,
368
- forDirective: Bool = false ,
369
366
pattern: PatternContext = . none
370
367
) -> RawExprSyntax {
371
368
// Try to parse '@' sign or 'inout' as an attributed typerepr.
@@ -387,7 +384,6 @@ extension Parser {
387
384
let awaitTok = self . eat ( handle)
388
385
let sub = self . parseSequenceExpressionElement (
389
386
flavor,
390
- forDirective: forDirective,
391
387
pattern: pattern
392
388
)
393
389
return RawExprSyntax (
@@ -403,7 +399,6 @@ extension Parser {
403
399
404
400
let expression = self . parseSequenceExpressionElement (
405
401
flavor,
406
- forDirective: forDirective,
407
402
pattern: pattern
408
403
)
409
404
return RawExprSyntax (
@@ -418,7 +413,6 @@ extension Parser {
418
413
let moveKeyword = self . eat ( handle)
419
414
let sub = self . parseSequenceExpressionElement (
420
415
flavor,
421
- forDirective: forDirective,
422
416
pattern: pattern
423
417
)
424
418
return RawExprSyntax (
@@ -432,7 +426,6 @@ extension Parser {
432
426
let borrowTok = self . eat ( handle)
433
427
let sub = self . parseSequenceExpressionElement (
434
428
flavor,
435
- forDirective: forDirective,
436
429
pattern: pattern
437
430
)
438
431
return RawExprSyntax (
@@ -451,7 +444,6 @@ extension Parser {
451
444
let copyTok = self . eat ( handle)
452
445
let sub = self . parseSequenceExpressionElement (
453
446
flavor,
454
- forDirective: forDirective,
455
447
pattern: pattern
456
448
)
457
449
return RawExprSyntax (
@@ -470,7 +462,6 @@ extension Parser {
470
462
let consumeKeyword = self . eat ( handle)
471
463
let sub = self . parseSequenceExpressionElement (
472
464
flavor,
473
- forDirective: forDirective,
474
465
pattern: pattern
475
466
)
476
467
return RawExprSyntax (
@@ -512,13 +503,12 @@ extension Parser {
512
503
case nil :
513
504
break
514
505
}
515
- return self . parseUnaryExpression ( flavor, forDirective : forDirective , pattern: pattern)
506
+ return self . parseUnaryExpression ( flavor, pattern: pattern)
516
507
}
517
508
518
509
/// Parse an optional prefix operator followed by an expression.
519
510
mutating func parseUnaryExpression(
520
511
_ flavor: ExprFlavor ,
521
- forDirective: Bool = false ,
522
512
pattern: PatternContext = . none
523
513
) -> RawExprSyntax {
524
514
// First check to see if we have the start of a regex literal `/.../`.
@@ -541,7 +531,7 @@ extension Parser {
541
531
switch self . at ( anyIn: ExpressionPrefixOperator . self) {
542
532
case ( . prefixAmpersand, let handle) ? :
543
533
let amp = self . eat ( handle)
544
- let expr = self . parseUnaryExpression ( flavor, forDirective : forDirective , pattern: pattern)
534
+ let expr = self . parseUnaryExpression ( flavor, pattern: pattern)
545
535
return RawExprSyntax (
546
536
RawInOutExprSyntax (
547
537
ampersand: amp,
@@ -551,11 +541,11 @@ extension Parser {
551
541
)
552
542
553
543
case ( . backslash, _) ? :
554
- return RawExprSyntax ( self . parseKeyPathExpression ( forDirective : forDirective , pattern: pattern) )
544
+ return RawExprSyntax ( self . parseKeyPathExpression ( pattern: pattern) )
555
545
556
546
case ( . prefixOperator, let handle) ? :
557
547
let op = self . eat ( handle)
558
- let postfix = self . parseUnaryExpression ( flavor, forDirective : forDirective , pattern: pattern)
548
+ let postfix = self . parseUnaryExpression ( flavor, pattern: pattern)
559
549
return RawExprSyntax (
560
550
RawPrefixOperatorExprSyntax (
561
551
operator: op,
@@ -568,7 +558,6 @@ extension Parser {
568
558
// If the next token is not an operator, just parse this as expr-postfix.
569
559
return self . parsePostfixExpression (
570
560
flavor,
571
- forDirective: forDirective,
572
561
pattern: pattern
573
562
)
574
563
}
@@ -577,17 +566,15 @@ extension Parser {
577
566
/// Parse a postfix expression applied to another expression.
578
567
mutating func parsePostfixExpression(
579
568
_ flavor: ExprFlavor ,
580
- forDirective: Bool ,
581
569
pattern: PatternContext
582
570
) -> RawExprSyntax {
583
- let head = self . parsePrimaryExpression ( pattern: pattern, forDirective : forDirective , flavor: flavor)
571
+ let head = self . parsePrimaryExpression ( pattern: pattern, flavor: flavor)
584
572
guard !head. is ( RawMissingExprSyntax . self) else {
585
573
return head
586
574
}
587
575
return self . parsePostfixExpressionSuffix (
588
576
head,
589
577
flavor,
590
- forDirective: forDirective,
591
578
pattern: pattern
592
579
)
593
580
}
@@ -661,8 +648,7 @@ extension Parser {
661
648
662
649
mutating func parseIfConfigExpressionSuffix(
663
650
_ start: RawExprSyntax ? ,
664
- _ flavor: ExprFlavor ,
665
- forDirective: Bool
651
+ _ flavor: ExprFlavor
666
652
) -> RawExprSyntax {
667
653
precondition ( self . at ( . poundIf) )
668
654
@@ -674,15 +660,14 @@ extension Parser {
674
660
if parser. at ( . period) {
675
661
head = parser. parseDottedExpressionSuffix ( nil )
676
662
} else if parser. at ( . poundIf) {
677
- head = parser. parseIfConfigExpressionSuffix ( nil , flavor, forDirective : forDirective )
663
+ head = parser. parseIfConfigExpressionSuffix ( nil , flavor)
678
664
} else {
679
665
// TODO: diagnose and skip.
680
666
return nil
681
667
}
682
668
let result = parser. parsePostfixExpressionSuffix (
683
669
head,
684
670
flavor,
685
- forDirective: forDirective,
686
671
pattern: . none
687
672
)
688
673
@@ -709,14 +694,13 @@ extension Parser {
709
694
mutating func parsePostfixExpressionSuffix(
710
695
_ start: RawExprSyntax ,
711
696
_ flavor: ExprFlavor ,
712
- forDirective: Bool ,
713
697
pattern: PatternContext
714
698
) -> RawExprSyntax {
715
699
// Handle suffix expressions.
716
700
var leadingExpr = start
717
701
var loopProgress = LoopProgressCondition ( )
718
702
while self . hasProgressed ( & loopProgress) {
719
- if forDirective && self . atStartOfLine {
703
+ if flavor == . poundIfDirective && self . atStartOfLine {
720
704
return leadingExpr
721
705
}
722
706
@@ -881,8 +865,7 @@ extension Parser {
881
865
882
866
leadingExpr = self . parseIfConfigExpressionSuffix (
883
867
leadingExpr,
884
- flavor,
885
- forDirective: forDirective
868
+ flavor
886
869
)
887
870
continue
888
871
}
@@ -957,7 +940,7 @@ extension Parser {
957
940
}
958
941
959
942
/// Parse a keypath expression.
960
- mutating func parseKeyPathExpression( forDirective : Bool , pattern: PatternContext ) -> RawKeyPathExprSyntax {
943
+ mutating func parseKeyPathExpression( pattern: PatternContext ) -> RawKeyPathExprSyntax {
961
944
// Consume '\'.
962
945
let ( unexpectedBeforeBackslash, backslash) = self . expect ( . backslash)
963
946
@@ -1079,12 +1062,9 @@ extension Parser {
1079
1062
/// Swift expression grammar.
1080
1063
mutating func parsePrimaryExpression(
1081
1064
pattern: PatternContext ,
1082
- forDirective: Bool ,
1083
1065
flavor: ExprFlavor
1084
1066
) -> RawExprSyntax {
1085
- if forDirective == true ,
1086
- let directiveExpr = self . parsePrimaryExprForDirective ( )
1087
- {
1067
+ if flavor == . poundIfDirective, let directiveExpr = self . parsePrimaryExprForDirective ( ) {
1088
1068
return RawExprSyntax ( directiveExpr)
1089
1069
}
1090
1070
0 commit comments