Skip to content

Commit e51695d

Browse files
committed
Unify forDirective and ExprFlavor
1 parent 1248cf7 commit e51695d

File tree

3 files changed

+19
-40
lines changed

3 files changed

+19
-40
lines changed

Sources/SwiftParser/Declarations.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,6 @@ extension Parser {
12511251
)
12521252
),
12531253
.basic,
1254-
forDirective: false,
12551254
pattern: .none
12561255
)
12571256
initializer = RawInitializerClauseSyntax(

Sources/SwiftParser/Directives.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ extension Parser {
7070

7171
// Parse #if
7272
let (unexpectedBeforePoundIf, poundIf) = self.expect(.poundIf)
73-
let condition = RawExprSyntax(self.parseSequenceExpression(.basic, forDirective: true))
73+
let condition = RawExprSyntax(self.parseSequenceExpression(.poundIfDirective))
7474
let unexpectedBetweenConditionAndElements = self.consumeRemainingTokenOnLine()
7575

7676
clauses.append(
@@ -95,14 +95,14 @@ extension Parser {
9595
switch match {
9696
case .poundElseif:
9797
(unexpectedBeforePound, pound) = self.eat(handle)
98-
condition = RawExprSyntax(self.parseSequenceExpression(.basic, forDirective: true))
98+
condition = RawExprSyntax(self.parseSequenceExpression(.poundIfDirective))
9999
unexpectedBetweenConditionAndElements = self.consumeRemainingTokenOnLine()
100100
case .poundElse:
101101
(unexpectedBeforePound, pound) = self.eat(handle)
102102
if let ifToken = self.consume(if: .init(.if, allowAtStartOfLine: false)) {
103103
unexpectedBeforePound = RawUnexpectedNodesSyntax(combining: unexpectedBeforePound, pound, ifToken, arena: self.arena)
104104
pound = self.missingToken(.poundElseif)
105-
condition = RawExprSyntax(self.parseSequenceExpression(.basic, forDirective: true))
105+
condition = RawExprSyntax(self.parseSequenceExpression(.poundIfDirective))
106106
} else {
107107
condition = nil
108108
}
@@ -115,7 +115,7 @@ extension Parser {
115115
}
116116
unexpectedBeforePound = RawUnexpectedNodesSyntax(combining: unexpectedBeforePound, pound, elif, arena: self.arena)
117117
pound = self.missingToken(.poundElseif)
118-
condition = RawExprSyntax(self.parseSequenceExpression(.basic, forDirective: true))
118+
condition = RawExprSyntax(self.parseSequenceExpression(.poundIfDirective))
119119
unexpectedBetweenConditionAndElements = self.consumeRemainingTokenOnLine()
120120
} else {
121121
break LOOP

Sources/SwiftParser/Expressions.swift

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extension Parser {
6565
enum ExprFlavor {
6666
case basic
6767
case trailingClosure
68+
case poundIfDirective
6869
}
6970

7071
enum PatternContext {
@@ -115,10 +116,9 @@ extension Parser {
115116
/// Parse a sequence of expressions.
116117
mutating func parseSequenceExpression(
117118
_ flavor: ExprFlavor,
118-
forDirective: Bool = false,
119119
pattern: PatternContext = .none
120120
) -> RawExprSyntax {
121-
if forDirective && self.atStartOfLine {
121+
if flavor == .poundIfDirective && self.atStartOfLine {
122122
return RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
123123
}
124124

@@ -132,15 +132,14 @@ extension Parser {
132132

133133
lastElement = self.parseSequenceExpressionElement(
134134
flavor,
135-
forDirective: forDirective,
136135
pattern: pattern
137136
)
138137

139138
var loopProgress = LoopProgressCondition()
140139
while self.hasProgressed(&loopProgress) {
141140
guard
142141
!lastElement.is(RawMissingExprSyntax.self),
143-
!(forDirective && self.atStartOfLine)
142+
!(flavor == .poundIfDirective && self.atStartOfLine)
144143
else {
145144
break
146145
}
@@ -160,14 +159,13 @@ extension Parser {
160159
if let rhsExpr {
161160
// Operator parsing returned the RHS.
162161
lastElement = rhsExpr
163-
} else if forDirective && self.atStartOfLine {
162+
} else if flavor == .poundIfDirective && self.atStartOfLine {
164163
// Don't allow RHS at a newline for `#if` conditions.
165164
lastElement = RawExprSyntax(RawMissingExprSyntax(arena: self.arena))
166165
break
167166
} else {
168167
lastElement = self.parseSequenceExpressionElement(
169168
flavor,
170-
forDirective: forDirective,
171169
pattern: pattern
172170
)
173171
}
@@ -365,7 +363,6 @@ extension Parser {
365363
/// Parse an expression sequence element.
366364
mutating func parseSequenceExpressionElement(
367365
_ flavor: ExprFlavor,
368-
forDirective: Bool = false,
369366
pattern: PatternContext = .none
370367
) -> RawExprSyntax {
371368
// Try to parse '@' sign or 'inout' as an attributed typerepr.
@@ -387,7 +384,6 @@ extension Parser {
387384
let awaitTok = self.eat(handle)
388385
let sub = self.parseSequenceExpressionElement(
389386
flavor,
390-
forDirective: forDirective,
391387
pattern: pattern
392388
)
393389
return RawExprSyntax(
@@ -403,7 +399,6 @@ extension Parser {
403399

404400
let expression = self.parseSequenceExpressionElement(
405401
flavor,
406-
forDirective: forDirective,
407402
pattern: pattern
408403
)
409404
return RawExprSyntax(
@@ -418,7 +413,6 @@ extension Parser {
418413
let moveKeyword = self.eat(handle)
419414
let sub = self.parseSequenceExpressionElement(
420415
flavor,
421-
forDirective: forDirective,
422416
pattern: pattern
423417
)
424418
return RawExprSyntax(
@@ -432,7 +426,6 @@ extension Parser {
432426
let borrowTok = self.eat(handle)
433427
let sub = self.parseSequenceExpressionElement(
434428
flavor,
435-
forDirective: forDirective,
436429
pattern: pattern
437430
)
438431
return RawExprSyntax(
@@ -451,7 +444,6 @@ extension Parser {
451444
let copyTok = self.eat(handle)
452445
let sub = self.parseSequenceExpressionElement(
453446
flavor,
454-
forDirective: forDirective,
455447
pattern: pattern
456448
)
457449
return RawExprSyntax(
@@ -470,7 +462,6 @@ extension Parser {
470462
let consumeKeyword = self.eat(handle)
471463
let sub = self.parseSequenceExpressionElement(
472464
flavor,
473-
forDirective: forDirective,
474465
pattern: pattern
475466
)
476467
return RawExprSyntax(
@@ -512,13 +503,12 @@ extension Parser {
512503
case nil:
513504
break
514505
}
515-
return self.parseUnaryExpression(flavor, forDirective: forDirective, pattern: pattern)
506+
return self.parseUnaryExpression(flavor, pattern: pattern)
516507
}
517508

518509
/// Parse an optional prefix operator followed by an expression.
519510
mutating func parseUnaryExpression(
520511
_ flavor: ExprFlavor,
521-
forDirective: Bool = false,
522512
pattern: PatternContext = .none
523513
) -> RawExprSyntax {
524514
// First check to see if we have the start of a regex literal `/.../`.
@@ -541,7 +531,7 @@ extension Parser {
541531
switch self.at(anyIn: ExpressionPrefixOperator.self) {
542532
case (.prefixAmpersand, let handle)?:
543533
let amp = self.eat(handle)
544-
let expr = self.parseUnaryExpression(flavor, forDirective: forDirective, pattern: pattern)
534+
let expr = self.parseUnaryExpression(flavor, pattern: pattern)
545535
return RawExprSyntax(
546536
RawInOutExprSyntax(
547537
ampersand: amp,
@@ -551,11 +541,11 @@ extension Parser {
551541
)
552542

553543
case (.backslash, _)?:
554-
return RawExprSyntax(self.parseKeyPathExpression(forDirective: forDirective, pattern: pattern))
544+
return RawExprSyntax(self.parseKeyPathExpression(pattern: pattern))
555545

556546
case (.prefixOperator, let handle)?:
557547
let op = self.eat(handle)
558-
let postfix = self.parseUnaryExpression(flavor, forDirective: forDirective, pattern: pattern)
548+
let postfix = self.parseUnaryExpression(flavor, pattern: pattern)
559549
return RawExprSyntax(
560550
RawPrefixOperatorExprSyntax(
561551
operator: op,
@@ -568,7 +558,6 @@ extension Parser {
568558
// If the next token is not an operator, just parse this as expr-postfix.
569559
return self.parsePostfixExpression(
570560
flavor,
571-
forDirective: forDirective,
572561
pattern: pattern
573562
)
574563
}
@@ -577,17 +566,15 @@ extension Parser {
577566
/// Parse a postfix expression applied to another expression.
578567
mutating func parsePostfixExpression(
579568
_ flavor: ExprFlavor,
580-
forDirective: Bool,
581569
pattern: PatternContext
582570
) -> RawExprSyntax {
583-
let head = self.parsePrimaryExpression(pattern: pattern, forDirective: forDirective, flavor: flavor)
571+
let head = self.parsePrimaryExpression(pattern: pattern, flavor: flavor)
584572
guard !head.is(RawMissingExprSyntax.self) else {
585573
return head
586574
}
587575
return self.parsePostfixExpressionSuffix(
588576
head,
589577
flavor,
590-
forDirective: forDirective,
591578
pattern: pattern
592579
)
593580
}
@@ -661,8 +648,7 @@ extension Parser {
661648

662649
mutating func parseIfConfigExpressionSuffix(
663650
_ start: RawExprSyntax?,
664-
_ flavor: ExprFlavor,
665-
forDirective: Bool
651+
_ flavor: ExprFlavor
666652
) -> RawExprSyntax {
667653
precondition(self.at(.poundIf))
668654

@@ -674,15 +660,14 @@ extension Parser {
674660
if parser.at(.period) {
675661
head = parser.parseDottedExpressionSuffix(nil)
676662
} else if parser.at(.poundIf) {
677-
head = parser.parseIfConfigExpressionSuffix(nil, flavor, forDirective: forDirective)
663+
head = parser.parseIfConfigExpressionSuffix(nil, flavor)
678664
} else {
679665
// TODO: diagnose and skip.
680666
return nil
681667
}
682668
let result = parser.parsePostfixExpressionSuffix(
683669
head,
684670
flavor,
685-
forDirective: forDirective,
686671
pattern: .none
687672
)
688673

@@ -709,14 +694,13 @@ extension Parser {
709694
mutating func parsePostfixExpressionSuffix(
710695
_ start: RawExprSyntax,
711696
_ flavor: ExprFlavor,
712-
forDirective: Bool,
713697
pattern: PatternContext
714698
) -> RawExprSyntax {
715699
// Handle suffix expressions.
716700
var leadingExpr = start
717701
var loopProgress = LoopProgressCondition()
718702
while self.hasProgressed(&loopProgress) {
719-
if forDirective && self.atStartOfLine {
703+
if flavor == .poundIfDirective && self.atStartOfLine {
720704
return leadingExpr
721705
}
722706

@@ -881,8 +865,7 @@ extension Parser {
881865

882866
leadingExpr = self.parseIfConfigExpressionSuffix(
883867
leadingExpr,
884-
flavor,
885-
forDirective: forDirective
868+
flavor
886869
)
887870
continue
888871
}
@@ -957,7 +940,7 @@ extension Parser {
957940
}
958941

959942
/// Parse a keypath expression.
960-
mutating func parseKeyPathExpression(forDirective: Bool, pattern: PatternContext) -> RawKeyPathExprSyntax {
943+
mutating func parseKeyPathExpression(pattern: PatternContext) -> RawKeyPathExprSyntax {
961944
// Consume '\'.
962945
let (unexpectedBeforeBackslash, backslash) = self.expect(.backslash)
963946

@@ -1079,12 +1062,9 @@ extension Parser {
10791062
/// Swift expression grammar.
10801063
mutating func parsePrimaryExpression(
10811064
pattern: PatternContext,
1082-
forDirective: Bool,
10831065
flavor: ExprFlavor
10841066
) -> RawExprSyntax {
1085-
if forDirective == true,
1086-
let directiveExpr = self.parsePrimaryExprForDirective()
1087-
{
1067+
if flavor == .poundIfDirective, let directiveExpr = self.parsePrimaryExprForDirective() {
10881068
return RawExprSyntax(directiveExpr)
10891069
}
10901070

0 commit comments

Comments
 (0)