@@ -19,14 +19,14 @@ extension Operator {
19
19
let modifiers = ModifierListSyntax (
20
20
[ DeclModifierSyntax ( name: . identifier( " \( kind) " ) , detail: nil ) ]
21
21
)
22
- let operatorKeyword = TokenSyntax . operatorKeyword ( leadingTrivia: . spaces ( 1 ) )
22
+ let operatorKeyword = TokenSyntax . operatorKeyword ( leadingTrivia: . space )
23
23
let identifierSyntax =
24
- TokenSyntax . identifier ( name, leadingTrivia: . spaces ( 1 ) )
24
+ TokenSyntax . identifier ( name, leadingTrivia: . space )
25
25
let precedenceGroupSyntax = precedenceGroup. map { groupName in
26
26
OperatorPrecedenceAndTypesSyntax (
27
27
colon: . colonToken( ) ,
28
28
precedenceGroupAndDesignatedTypes: IdentifierListSyntax (
29
- [ . identifier( groupName, leadingTrivia: . spaces ( 1 ) ) ]
29
+ [ . identifier( groupName, leadingTrivia: . space ) ]
30
30
)
31
31
)
32
32
}
@@ -38,3 +38,88 @@ extension Operator {
38
38
)
39
39
}
40
40
}
41
+
42
+ extension PrecedenceGroup {
43
+ /// Synthesize a syntactic representation of this precedence group based on
44
+ /// its semantic definition.
45
+ public func synthesizedSyntax(
46
+ indentation: Int = 4
47
+ ) -> PrecedenceGroupDeclSyntax {
48
+ let precedencegroupKeyword = TokenSyntax . precedencegroupKeyword ( )
49
+ let identifierSyntax =
50
+ TokenSyntax . identifier ( name, leadingTrivia: . space)
51
+ let leftBrace = TokenSyntax . leftBraceToken ( leadingTrivia: . space)
52
+
53
+ var groupAttributes : [ Syntax ] = [ ]
54
+
55
+ switch associativity {
56
+ case . left, . right:
57
+ groupAttributes. append (
58
+ Syntax (
59
+ PrecedenceGroupAssociativitySyntax (
60
+ associativityKeyword:
61
+ . identifier(
62
+ " associativity " ,
63
+ leadingTrivia: [ . newlines( 1 ) , . spaces( indentation) ]
64
+ ) ,
65
+ colon: . colonToken( ) ,
66
+ value: . identifier( " \( associativity) " , leadingTrivia: . space)
67
+ )
68
+ )
69
+ )
70
+
71
+ case . none:
72
+ // None is the default associativity.
73
+ break
74
+ }
75
+
76
+ if assignment {
77
+ groupAttributes. append (
78
+ Syntax (
79
+ PrecedenceGroupAssignmentSyntax (
80
+ assignmentKeyword:
81
+ . identifier(
82
+ " assignment " ,
83
+ leadingTrivia: [ . newlines( 1 ) , . spaces( indentation) ]
84
+ ) ,
85
+ colon: . colonToken( ) ,
86
+ flag: . trueKeyword( leadingTrivia: . space)
87
+ )
88
+ )
89
+ )
90
+ }
91
+
92
+ for relation in relations {
93
+ groupAttributes. append (
94
+ Syntax (
95
+ PrecedenceGroupRelationSyntax (
96
+ higherThanOrLowerThan: . contextualKeyword(
97
+ " \( relation. kind) " ,
98
+ leadingTrivia: [ . newlines( 1 ) , . spaces( indentation) ]
99
+ ) ,
100
+ colon: . colonToken( ) ,
101
+ otherNames: PrecedenceGroupNameListSyntax (
102
+ [
103
+ PrecedenceGroupNameElementSyntax (
104
+ name: . identifier( relation. groupName, leadingTrivia: . space) ,
105
+ trailingComma: nil )
106
+ ]
107
+ )
108
+ )
109
+ )
110
+ )
111
+ }
112
+
113
+ let rightBrace = TokenSyntax . rightBraceToken (
114
+ leadingTrivia: groupAttributes. isEmpty ? . space : . newline
115
+ )
116
+
117
+ return PrecedenceGroupDeclSyntax (
118
+ attributes: nil , modifiers: nil ,
119
+ precedencegroupKeyword: precedencegroupKeyword,
120
+ identifier: identifierSyntax, leftBrace: leftBrace,
121
+ groupAttributes: PrecedenceGroupAttributeListSyntax ( groupAttributes) ,
122
+ rightBrace: rightBrace
123
+ )
124
+ }
125
+ }
0 commit comments