Skip to content

Commit c357aa4

Browse files
committed
Add sequence folding for the assignment operator.
1 parent f6f76a3 commit c357aa4

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

Sources/SwiftOperatorPrecedence/OperatorPrecedence+Folding.swift

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ extension OperatorPrecedence {
6060
}
6161

6262
// The ternary operator has a fixed precedence group name.
63-
if let ternaryExpr = expr.as(UnresolvedTernaryExprSyntax.self) {
63+
if expr.is(UnresolvedTernaryExprSyntax.self) {
6464
return "TernaryPrecedence"
6565
}
6666

67+
// An assignment operator has fixed precedence.
68+
if expr.is(AssignmentExprSyntax.self) {
69+
return "AssignmentPrecedence"
70+
}
71+
6772
// FIXME: Handle all of the language-defined precedence relationships.
6873
return nil
6974
}
@@ -81,8 +86,7 @@ extension OperatorPrecedence {
8186
return ExprSyntax(
8287
InfixOperatorExprSyntax(
8388
leftOperand: lhs,
84-
binaryOperatorExpr.unexpectedBeforeOperatorToken,
85-
operatorOperand: op,
89+
operatorOperand: ExprSyntax(binaryOperatorExpr),
8690
rightOperand: rhs)
8791
)
8892
}
@@ -102,6 +106,16 @@ extension OperatorPrecedence {
102106
)
103107
}
104108

109+
// An assignment operator x = y.
110+
if let assignExpr = op.as(AssignmentExprSyntax.self) {
111+
return ExprSyntax(
112+
InfixOperatorExprSyntax(
113+
leftOperand: lhs,
114+
operatorOperand: ExprSyntax(assignExpr),
115+
rightOperand: rhs)
116+
)
117+
}
118+
105119
// FIXME: Fallback that we should never need
106120
fatalError("Unknown binary operator")
107121
}

Tests/SwiftOperatorPrecedenceTest/OperatorPrecedence.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ public class OperatorPrecedenceTests: XCTestCase {
118118
XCTAssertFalse(foldedAll.containsExprSequence)
119119
}
120120

121+
func testAssignExprs() throws {
122+
let opPrecedence = OperatorPrecedence.standardOperators
123+
try opPrecedence.assertExpectedFold("a = b + c", "(a = (b + c))")
124+
try opPrecedence.assertExpectedFold("a = b = c", "(a = (b = c))")
125+
}
126+
121127
func testParsedLogicalExprs() throws {
122128
let logicalOperatorSources =
123129
"""

0 commit comments

Comments
 (0)