Skip to content

Commit b67fdaa

Browse files
committed
Make Operator, PrecedenceGroup, and OperatorTable CustomStringConvertible.
Make the string representation of the main data types be their syntactic form. This makes it easier to inspect and reason about their values.
1 parent 7d43bb1 commit b67fdaa

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

Sources/SwiftOperators/Operator.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ public struct Operator {
4848
self.syntax = syntax
4949
}
5050
}
51+
52+
extension Operator: CustomStringConvertible {
53+
/// The description of an operator is the source code that produces it.
54+
public var description: String {
55+
(syntax ?? synthesizedSyntax()).description
56+
}
57+
}

Sources/SwiftOperators/OperatorTable.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,31 @@ extension OperatorTable {
9797
return op.precedenceGroup
9898
}
9999
}
100+
101+
extension OperatorTable: CustomStringConvertible {
102+
/// The description of an operator table is the source code that produces it.
103+
public var description: String {
104+
var result = ""
105+
106+
// Turn all of the dictionary values into their string representations.
107+
func add<Key: Comparable, Value: CustomStringConvertible>(
108+
_ dict: [Key : Value]
109+
) {
110+
if dict.isEmpty {
111+
return
112+
}
113+
114+
result.append(contentsOf: dict.sorted { $0.key < $1.key }
115+
.map { $0.value.description }
116+
.joined(separator: "\n"))
117+
118+
result += "\n"
119+
}
120+
121+
add(precedenceGraph.precedenceGroups)
122+
add(prefixOperators)
123+
add(postfixOperators)
124+
add(infixOperators)
125+
return result
126+
}
127+
}

Sources/SwiftOperators/PrecedenceGroup.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,10 @@ public struct PrecedenceGroup {
118118
self.syntax = syntax
119119
}
120120
}
121+
122+
extension PrecedenceGroup: CustomStringConvertible {
123+
/// The description of a precedence group is the source code that produces it.
124+
public var description: String {
125+
(syntax ?? synthesizedSyntax()).description
126+
}
127+
}

Tests/SwiftOperatorsTest/SyntaxSynthesisTests.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,23 @@ public class SyntaxSynthesisTests: XCTestCase {
3939
}
4040
""")
4141
}
42+
43+
func testLogicalOperatorTable() {
44+
let table = OperatorTable.logicalOperators
45+
XCTAssertEqual(
46+
table.description,
47+
"""
48+
precedencegroup LogicalConjunctionPrecedence {
49+
associativity: left
50+
higherThan: LogicalDisjunctionPrecedence
51+
}
52+
precedencegroup LogicalDisjunctionPrecedence {
53+
associativity: left
54+
}
55+
infix operator &&: LogicalConjunctionPrecedence
56+
infix operator ||: LogicalDisjunctionPrecedence
57+
58+
"""
59+
)
60+
}
4261
}

0 commit comments

Comments
 (0)