Skip to content

Commit 1858fcd

Browse files
authored
Merge pull request #819 from DougGregor/prefix-postfix-operators
Add prefix/postfix operators to the built-in operator tables
2 parents 6238c29 + ef0191b commit 1858fcd

File tree

5 files changed

+48
-1
lines changed

5 files changed

+48
-1
lines changed

Sources/SwiftOperators/Operator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public struct Operator {
3939

4040
public init(
4141
kind: OperatorKind, name: OperatorName,
42-
precedenceGroup: PrecedenceGroupName?,
42+
precedenceGroup: PrecedenceGroupName? = nil,
4343
syntax: OperatorDeclSyntax? = nil
4444
) {
4545
self.kind = kind

Sources/SwiftOperators/OperatorTable+Defaults.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ extension OperatorTable {
2626
]
2727

2828
let operators: [Operator] = [
29+
Operator(kind: .prefix, name: "!"),
2930
Operator(kind: .infix, name: "&&",
3031
precedenceGroup: "LogicalConjunctionPrecedence"),
3132
Operator(kind: .infix, name: "||",
@@ -121,6 +122,21 @@ extension OperatorTable {
121122
]
122123

123124
let operators: [Operator] = [
125+
// Standard postfix operators.
126+
Operator(kind: .postfix, name: "++"),
127+
Operator(kind: .postfix, name: "--"),
128+
Operator(kind: .postfix, name: "..."),
129+
130+
// Standard prefix operators.
131+
Operator(kind: .prefix, name: "++"),
132+
Operator(kind: .prefix, name: "--"),
133+
Operator(kind: .prefix, name: "!"),
134+
Operator(kind: .prefix, name: "~"),
135+
Operator(kind: .prefix, name: "+"),
136+
Operator(kind: .prefix, name: "-"),
137+
Operator(kind: .prefix, name: "..."),
138+
Operator(kind: .prefix, name: "..<"),
139+
124140
// "Exponentiative"
125141
Operator(
126142
kind: .infix,

Sources/SwiftOperators/OperatorTable.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,18 @@ extension OperatorTable {
8989
return infixOperators[operatorName]
9090
}
9191

92+
/// Returns the ``Operator`` corresponding to the given prefix operator, or
93+
/// `nil` if it is not defined in the operator table.
94+
public func prefixOperator(named operatorName: OperatorName) -> Operator? {
95+
return prefixOperators[operatorName]
96+
}
97+
98+
/// Returns the ``Operator`` corresponding to the given prefix operator, or
99+
/// `nil` if it is not defined in the operator table.
100+
public func postfixOperator(named operatorName: OperatorName) -> Operator? {
101+
return postfixOperators[operatorName]
102+
}
103+
92104
/// Look for the precedence group corresponding to the given operator.
93105
func lookupOperatorPrecedenceGroupName(
94106
_ operatorName: OperatorName,

Tests/SwiftOperatorsTest/OperatorTableTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,4 +405,22 @@ public class OperatorPrecedenceTests: XCTestCase {
405405
}
406406
XCTAssertNil(opPrecedence.infixOperator(named: "^*^"))
407407
}
408+
409+
func testUnaryOperatorLookup() throws {
410+
let opPrecedence = OperatorTable.standardOperators
411+
do {
412+
let op = try XCTUnwrap(opPrecedence.prefixOperator(named: "-"))
413+
XCTAssertEqual(op.kind, .prefix)
414+
XCTAssertEqual(op.name, "-")
415+
XCTAssertNil(op.precedenceGroup)
416+
}
417+
418+
do {
419+
let op = try XCTUnwrap(opPrecedence.postfixOperator(named: "..."))
420+
XCTAssertEqual(op.kind, .postfix)
421+
XCTAssertEqual(op.name, "...")
422+
XCTAssertNil(op.precedenceGroup)
423+
}
424+
425+
}
408426
}

Tests/SwiftOperatorsTest/SyntaxSynthesisTests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public class SyntaxSynthesisTests: XCTestCase {
5252
precedencegroup LogicalDisjunctionPrecedence {
5353
associativity: left
5454
}
55+
prefix operator !
5556
infix operator &&: LogicalConjunctionPrecedence
5657
infix operator ||: LogicalDisjunctionPrecedence
5758

0 commit comments

Comments
 (0)