Skip to content

Commit b0f8805

Browse files
committed
Address review feedback
Clean up some code, allow `if` and `switch` to count as the start of an expression, and add some substructure test assertions.
1 parent 5ab7b72 commit b0f8805

File tree

5 files changed

+328
-52
lines changed

5 files changed

+328
-52
lines changed

Sources/SwiftParser/Expressions.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,19 +505,21 @@ extension Parser {
505505
// First check to see if we have the start of a regex literal `/.../`.
506506
// tryLexRegexLiteral(/*forUnappliedOperator*/ false)
507507

508+
switch self.currentToken.tokenKind {
508509
// Try parse an 'if' or 'switch' as an expression. Note we do this here in
509510
// parseUnaryExpression as we don't allow postfix syntax to hang off such
510511
// expressions to avoid ambiguities such as postfix '.member', which can
511512
// currently be parsed as a static dot member for a result builder.
512-
if self.at(.keyword(.switch)) {
513+
case .switchKeyword:
513514
return RawExprSyntax(
514-
parseSwitchExpression(switchHandle: .constant(.keyword(.switch)))
515+
parseSwitchExpression(switchHandle: .constant(.switchKeyword))
515516
)
516-
}
517-
if self.at(.keyword(.if)) {
517+
case .ifKeyword:
518518
return RawExprSyntax(
519-
parseIfExpression(ifHandle: .constant(.keyword(.if)))
519+
parseIfExpression(ifHandle: .constant(.ifKeyword))
520520
)
521+
default:
522+
break
521523
}
522524

523525
switch self.at(anyIn: ExpressionPrefixOperator.self) {
@@ -2495,8 +2497,8 @@ extension Parser.Lookahead {
24952497
}
24962498

24972499
// If this is the start of a switch body, this isn't a trailing closure.
2498-
if self.peek().rawTokenKind == .keyword(.case) {
2499-
return false;
2500+
if self.peek().tokenKind == .caseKeyword {
2501+
return false
25002502
}
25012503

25022504
// If this is a normal expression (not an expr-basic) then trailing closures

Sources/SwiftParser/RawTokenKindSubset.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,17 +616,17 @@ enum IfOrSwitch: RawTokenKindSubset {
616616
case switchKeyword
617617

618618
init?(lexeme: Lexer.Lexeme) {
619-
switch lexeme {
620-
case RawTokenKindMatch(.keyword(.if)): self = .ifKeyword
621-
case RawTokenKindMatch(.keyword(.switch)): self = .switchKeyword
619+
switch lexeme.tokenKind {
620+
case .ifKeyword: self = .ifKeyword
621+
case .switchKeyword: self = .switchKeyword
622622
default: return nil
623623
}
624624
}
625625

626626
var rawTokenKind: RawTokenKind {
627627
switch self {
628-
case .ifKeyword: return .keyword(.if)
629-
case .switchKeyword: return .keyword(.switch)
628+
case .ifKeyword: return .ifKeyword
629+
case .switchKeyword: return .switchKeyword
630630
}
631631
}
632632
}
@@ -795,6 +795,7 @@ enum ExpressionStart: RawTokenKindSubset {
795795
case expressionPrefixOperator(ExpressionPrefixOperator)
796796
case matchingPatternStart(MatchingPatternStart)
797797
case primaryExpressionStart(PrimaryExpressionStart)
798+
case ifOrSwitch(IfOrSwitch)
798799

799800
init?(lexeme: Lexer.Lexeme) {
800801
if let subset = AwaitTryMove(lexeme: lexeme) {
@@ -805,6 +806,8 @@ enum ExpressionStart: RawTokenKindSubset {
805806
self = .matchingPatternStart(subset)
806807
} else if let subset = PrimaryExpressionStart(lexeme: lexeme) {
807808
self = .primaryExpressionStart(subset)
809+
} else if let subset = IfOrSwitch(lexeme: lexeme) {
810+
self = .ifOrSwitch(subset)
808811
} else {
809812
return nil
810813
}
@@ -815,6 +818,7 @@ enum ExpressionStart: RawTokenKindSubset {
815818
+ ExpressionPrefixOperator.allCases.map(Self.expressionPrefixOperator)
816819
+ MatchingPatternStart.allCases.map(Self.matchingPatternStart)
817820
+ PrimaryExpressionStart.allCases.map(Self.primaryExpressionStart)
821+
+ IfOrSwitch.allCases.map(Self.ifOrSwitch)
818822
}
819823

820824
var rawTokenKind: RawTokenKind {
@@ -823,6 +827,7 @@ enum ExpressionStart: RawTokenKindSubset {
823827
case .expressionPrefixOperator(let underlyingKind): return underlyingKind.rawTokenKind
824828
case .matchingPatternStart(let underlyingKind): return underlyingKind.rawTokenKind
825829
case .primaryExpressionStart(let underlyingKind): return underlyingKind.rawTokenKind
830+
case .ifOrSwitch(let underlyingKind): return underlyingKind.rawTokenKind
826831
}
827832
}
828833

@@ -832,6 +837,7 @@ enum ExpressionStart: RawTokenKindSubset {
832837
case .expressionPrefixOperator(let underlyingKind): return underlyingKind.contextualKeyword
833838
case .matchingPatternStart(let underlyingKind): return underlyingKind.contextualKeyword
834839
case .primaryExpressionStart(let underlyingKind): return underlyingKind.contextualKeyword
840+
case .ifOrSwitch(let underlyingKind): return underlyingKind.contextualKeyword
835841
}
836842
}
837843
}

Sources/SwiftParser/TopLevel.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,12 @@ extension Parser {
202202
// We could also achieve this by more eagerly attempting to parse an 'if'
203203
// or 'switch' as an expression when in statement position, but that
204204
// could result in less useful recovery behavior.
205-
if at(.keyword(.as)), let stmtExpr = stmt.as(RawExpressionStmtSyntax.self) {
206-
let expr = stmtExpr.expression
205+
if at(.asKeyword),
206+
let expr = stmt.as(RawExpressionStmtSyntax.self)?.expression
207+
{
207208
if expr.is(RawIfExprSyntax.self) || expr.is(RawSwitchExprSyntax.self) {
208209
let (op, rhs) = parseUnresolvedAsExpr(
209-
handle: .init(tokenKind: .keyword(.as))
210+
handle: .init(tokenKind: .asKeyword)
210211
)
211212
let sequence = RawExprSyntax(
212213
RawSequenceExprSyntax(

0 commit comments

Comments
 (0)