File tree Expand file tree Collapse file tree 4 files changed +61
-0
lines changed Expand file tree Collapse file tree 4 files changed +61
-0
lines changed Original file line number Diff line number Diff line change @@ -48,3 +48,10 @@ public struct Operator {
48
48
self . syntax = syntax
49
49
}
50
50
}
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
+ }
Original file line number Diff line number Diff line change @@ -97,3 +97,31 @@ extension OperatorTable {
97
97
return op. precedenceGroup
98
98
}
99
99
}
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
+ }
Original file line number Diff line number Diff line change @@ -118,3 +118,10 @@ public struct PrecedenceGroup {
118
118
self . syntax = syntax
119
119
}
120
120
}
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
+ }
Original file line number Diff line number Diff line change @@ -39,4 +39,23 @@ public class SyntaxSynthesisTests: XCTestCase {
39
39
}
40
40
""" )
41
41
}
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
+ }
42
61
}
You can’t perform that action at this time.
0 commit comments