Skip to content

Add prefix/postfix operators to the built-in operator tables #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftOperators/Operator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public struct Operator {

public init(
kind: OperatorKind, name: OperatorName,
precedenceGroup: PrecedenceGroupName?,
precedenceGroup: PrecedenceGroupName? = nil,
syntax: OperatorDeclSyntax? = nil
) {
self.kind = kind
Expand Down
16 changes: 16 additions & 0 deletions Sources/SwiftOperators/OperatorTable+Defaults.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ extension OperatorTable {
]

let operators: [Operator] = [
Operator(kind: .prefix, name: "!"),
Operator(kind: .infix, name: "&&",
precedenceGroup: "LogicalConjunctionPrecedence"),
Operator(kind: .infix, name: "||",
Expand Down Expand Up @@ -121,6 +122,21 @@ extension OperatorTable {
]

let operators: [Operator] = [
// Standard postfix operators.
Operator(kind: .postfix, name: "++"),
Operator(kind: .postfix, name: "--"),
Operator(kind: .postfix, name: "..."),

// Standard prefix operators.
Operator(kind: .prefix, name: "++"),
Operator(kind: .prefix, name: "--"),
Operator(kind: .prefix, name: "!"),
Operator(kind: .prefix, name: "~"),
Operator(kind: .prefix, name: "+"),
Operator(kind: .prefix, name: "-"),
Operator(kind: .prefix, name: "..."),
Operator(kind: .prefix, name: "..<"),

// "Exponentiative"
Operator(
kind: .infix,
Expand Down
12 changes: 12 additions & 0 deletions Sources/SwiftOperators/OperatorTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ extension OperatorTable {
return infixOperators[operatorName]
}

/// Returns the ``Operator`` corresponding to the given prefix operator, or
/// `nil` if it is not defined in the operator table.
public func prefixOperator(named operatorName: OperatorName) -> Operator? {
return prefixOperators[operatorName]
}

/// Returns the ``Operator`` corresponding to the given prefix operator, or
/// `nil` if it is not defined in the operator table.
public func postfixOperator(named operatorName: OperatorName) -> Operator? {
return postfixOperators[operatorName]
}

/// Look for the precedence group corresponding to the given operator.
func lookupOperatorPrecedenceGroupName(
_ operatorName: OperatorName,
Expand Down
18 changes: 18 additions & 0 deletions Tests/SwiftOperatorsTest/OperatorTableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,22 @@ public class OperatorPrecedenceTests: XCTestCase {
}
XCTAssertNil(opPrecedence.infixOperator(named: "^*^"))
}

func testUnaryOperatorLookup() throws {
let opPrecedence = OperatorTable.standardOperators
do {
let op = try XCTUnwrap(opPrecedence.prefixOperator(named: "-"))
XCTAssertEqual(op.kind, .prefix)
XCTAssertEqual(op.name, "-")
XCTAssertNil(op.precedenceGroup)
}

do {
let op = try XCTUnwrap(opPrecedence.postfixOperator(named: "..."))
XCTAssertEqual(op.kind, .postfix)
XCTAssertEqual(op.name, "...")
XCTAssertNil(op.precedenceGroup)
}

}
}
1 change: 1 addition & 0 deletions Tests/SwiftOperatorsTest/SyntaxSynthesisTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class SyntaxSynthesisTests: XCTestCase {
precedencegroup LogicalDisjunctionPrecedence {
associativity: left
}
prefix operator !
infix operator &&: LogicalConjunctionPrecedence
infix operator ||: LogicalDisjunctionPrecedence

Expand Down