@@ -11,14 +11,6 @@ extension Array {
11
11
}
12
12
}
13
13
14
- extension Collection {
15
- public var only : Element {
16
- if isEmpty { fatalError ( " Empty Collection. " ) }
17
- else if count > 1 { fatalError ( " More than one element " ) }
18
- return first!
19
- }
20
- }
21
-
22
14
extension UnsafePointer {
23
15
public var raw : UnsafeMutableRawPointer {
24
16
UnsafeMutableRawPointer ( mutating: self )
@@ -38,12 +30,17 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
38
30
// return nil
39
31
// }
40
32
33
+ @_disfavoredOverload
34
+ public func visit( _ node: SourceFileSyntax ) -> UnsafeMutableRawPointer {
35
+ fatalError ( " Use other overload. " )
36
+ }
37
+
41
38
public func visit( _ node: SourceFileSyntax ) -> [ UnsafeMutableRawPointer ] {
42
39
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
43
40
var out = [ UnsafeMutableRawPointer] ( )
44
41
45
42
for element in node. statements {
46
- let swiftASTNodes = visit ( element) . only
43
+ let swiftASTNodes = visit ( element)
47
44
if element. item. is ( StmtSyntax . self) || element. item. is ( ExprSyntax . self) {
48
45
out. append ( SwiftTopLevelCodeDecl_create ( ctx, declContext, loc, swiftASTNodes) )
49
46
} else {
@@ -55,103 +52,103 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
55
52
return out
56
53
}
57
54
58
- public func visit( _ node: FunctionCallExprSyntax ) -> [ UnsafeMutableRawPointer ] {
59
- let args = visit ( node. argumentList) . only
55
+ public func visit( _ node: FunctionCallExprSyntax ) -> UnsafeMutableRawPointer {
56
+ let args = visit ( node. argumentList)
60
57
// TODO: hack
61
- let callee = visit ( node. calledExpression) . only
62
- let call = SwiftFunctionCallExpr_create ( self . ctx, callee, args)
63
-
64
- return [ call]
58
+ let callee = visit ( node. calledExpression)
59
+ return SwiftFunctionCallExpr_create ( self . ctx, callee, args)
65
60
}
66
61
67
- public func visit( _ node: IdentifierExprSyntax ) -> [ UnsafeMutableRawPointer ] {
62
+ public func visit( _ node: IdentifierExprSyntax ) -> UnsafeMutableRawPointer {
68
63
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
69
64
70
65
var text = node. identifier. text
71
66
let id = text. withUTF8 { buf in
72
67
return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
73
68
}
74
69
75
- return [ SwiftIdentifierExpr_create ( ctx, id, loc) ]
70
+ return SwiftIdentifierExpr_create ( ctx, id, loc)
76
71
}
77
72
78
- public func visit( _ node: SimpleTypeIdentifierSyntax ) -> [ UnsafeMutableRawPointer ] {
73
+ public func visit( _ node: SimpleTypeIdentifierSyntax ) -> UnsafeMutableRawPointer {
79
74
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
80
75
81
76
var text = node. name. text
82
77
let id = text. withUTF8 { buf in
83
78
return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
84
79
}
85
80
86
- return [ SimpleIdentTypeRepr_create ( ctx, loc, id) ]
81
+ return SimpleIdentTypeRepr_create ( ctx, loc, id)
87
82
}
88
83
89
- public func visit( _ node: IdentifierPatternSyntax ) -> [ UnsafeMutableRawPointer ] {
84
+ public func visit( _ node: IdentifierPatternSyntax ) -> UnsafeMutableRawPointer {
90
85
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
91
86
92
87
var text = node. identifier. text
93
88
let id = text. withUTF8 { buf in
94
89
return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
95
90
}
96
91
97
- return [ SwiftIdentifierExpr_create ( ctx, id, loc) ]
92
+ return SwiftIdentifierExpr_create ( ctx, id, loc)
98
93
}
99
94
100
- public func visit( _ node: MemberAccessExprSyntax ) -> [ UnsafeMutableRawPointer ] {
95
+ public func visit( _ node: MemberAccessExprSyntax ) -> UnsafeMutableRawPointer {
101
96
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
102
- let base = visit ( node. base!) . only
97
+ let base = visit ( node. base!)
103
98
var nameText = node. name. text
104
99
let name = nameText. withUTF8 { buf in
105
100
return SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
106
101
}
107
102
108
- return [ UnresolvedDotExpr_create ( ctx, base, loc, name, loc) ]
103
+ return UnresolvedDotExpr_create ( ctx, base, loc, name, loc)
109
104
}
110
105
106
+ public func visit( _ node: TupleExprElementSyntax ) -> UnsafeMutableRawPointer {
107
+ visit ( node. expression)
108
+ }
111
109
112
- public func visit( _ node: TupleExprElementListSyntax ) -> [ UnsafeMutableRawPointer ] {
113
- let elements = node. map { visit ( $0 ) . only }
110
+ public func visit( _ node: TupleExprElementListSyntax ) -> UnsafeMutableRawPointer {
111
+ let elements = node. map ( self . visit )
114
112
115
113
// TODO: find correct paren locs.
116
114
let lParenLoc = self . base. advanced ( by: node. position. utf8Offset) . raw
117
115
let rParenLoc = self . base. advanced ( by: node. position. utf8Offset) . raw
118
116
119
- return [ elements. withBridgedArrayRef { elementsRef in
117
+ return elements. withBridgedArrayRef { elementsRef in
120
118
SwiftTupleExpr_create ( self . ctx, lParenLoc, elementsRef, rParenLoc)
121
- } ]
122
- }
123
-
124
- public func visit( _ node: PatternBindingSyntax ) -> [ UnsafeMutableRawPointer ] {
125
- let pattern = visit ( node. pattern)
126
- let initializer = visit ( node. initializer!)
127
-
128
- return [ pattern. only, initializer. only]
119
+ }
129
120
}
130
121
131
- public func visit( _ node: VariableDeclSyntax ) -> [ UnsafeMutableRawPointer ] {
132
- let components = visit ( node. bindings)
133
- assert ( components. count == 2 )
134
- let pattern = components. first!
135
- let initializer = components. last!
122
+ public func visit( _ node: VariableDeclSyntax ) -> UnsafeMutableRawPointer {
123
+ let pattern = visit ( node. bindings. first!. pattern)
124
+ let initializer = visit ( node. bindings. first!. initializer!)
136
125
137
126
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
138
127
let isStateic = false // TODO: compute this
139
128
let isLet = node. letOrVarKeyword. tokenKind == . letKeyword
140
129
141
130
// TODO: don't drop "initializer" on the floor.
142
- return [ SwiftVarDecl_create ( ctx, pattern, loc, isStateic, isLet, declContext) ]
131
+ return SwiftVarDecl_create ( ctx, pattern, loc, isStateic, isLet, declContext)
143
132
}
144
133
145
- public func visit( _ node: CodeBlockSyntax ) -> [ UnsafeMutableRawPointer ] {
146
- let statements = visit ( node. statements)
134
+ public func visit( _ node: ConditionElementSyntax ) -> UnsafeMutableRawPointer {
135
+ visit ( node. condition)
136
+ }
137
+
138
+ public func visit( _ node: CodeBlockItemSyntax ) -> UnsafeMutableRawPointer {
139
+ visit ( node. item)
140
+ }
141
+
142
+ public func visit( _ node: CodeBlockSyntax ) -> UnsafeMutableRawPointer {
143
+ let statements = node. statements. map ( self . visit)
147
144
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
148
145
149
- return [ statements. withBridgedArrayRef { ref in
146
+ return statements. withBridgedArrayRef { ref in
150
147
BraceStmt_create ( ctx, loc, ref, loc)
151
- } ]
148
+ }
152
149
}
153
150
154
- public func visit( _ node: FunctionParameterSyntax ) -> [ UnsafeMutableRawPointer ] {
151
+ public func visit( _ node: FunctionParameterSyntax ) -> UnsafeMutableRawPointer {
155
152
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
156
153
157
154
let firstName : UnsafeMutableRawPointer ?
@@ -175,10 +172,10 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
175
172
secondName = nil
176
173
}
177
174
178
- return [ ParamDecl_create ( ctx, loc, loc, firstName, loc, secondName, declContext) ]
175
+ return ParamDecl_create ( ctx, loc, loc, firstName, loc, secondName, declContext)
179
176
}
180
177
181
- public func visit( _ node: FunctionDeclSyntax ) -> [ UnsafeMutableRawPointer ] {
178
+ public func visit( _ node: FunctionDeclSyntax ) -> UnsafeMutableRawPointer {
182
179
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
183
180
184
181
var nameText = node. identifier. text
@@ -188,55 +185,53 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
188
185
189
186
let body : UnsafeMutableRawPointer ?
190
187
if let nodeBody = node. body {
191
- body = visit ( nodeBody) . only
188
+ body = visit ( nodeBody)
192
189
} else {
193
190
body = nil
194
191
}
195
192
196
193
let returnType : UnsafeMutableRawPointer ?
197
194
if let output = node. signature. output {
198
- returnType = visit ( output. returnType) . only
195
+ returnType = visit ( output. returnType)
199
196
} else {
200
197
returnType = nil
201
198
}
202
199
203
- let params = node. signature. input. parameterList. map { visit ( $0) . only }
204
- return [ params. withBridgedArrayRef { ref in
200
+ let params = node. signature. input. parameterList. map { visit ( $0) }
201
+ return params. withBridgedArrayRef { ref in
205
202
FuncDecl_create ( ctx, loc, false , loc, name, loc, false , nil , false , nil , loc, ref, loc, body, returnType, declContext)
206
- } ]
203
+ }
207
204
}
208
205
209
- public func visit( _ node: IfStmtSyntax ) -> [ UnsafeMutableRawPointer ] {
210
- let conditions = node. conditions. map { self . visit ( $0 ) . only }
206
+ public func visit( _ node: IfStmtSyntax ) -> UnsafeMutableRawPointer {
207
+ let conditions = node. conditions. map ( self . visit)
211
208
assert ( conditions. count == 1 ) // TODO: handle multiple conditions.
212
209
213
- let body = visit ( node. body) . only
210
+ let body = visit ( node. body)
214
211
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
215
212
216
213
if let elseBody = node. elseBody, node. elseKeyword != nil {
217
- let elseStmt = visit ( elseBody. data) . only // TODO: don't use SyntaxData
218
- return [ IfStmt_create ( ctx, loc, conditions. only, body, loc,
219
- elseStmt) ]
214
+ return IfStmt_create ( ctx, loc, conditions. first!, body, loc, visit ( elseBody) )
220
215
}
221
216
222
- return [ IfStmt_create ( ctx, loc, conditions. only , body, nil , nil ) ]
217
+ return IfStmt_create ( ctx, loc, conditions. first! , body, nil , nil )
223
218
}
224
219
225
- public func visit( _ node: StringLiteralExprSyntax ) -> [ UnsafeMutableRawPointer ] {
220
+ public func visit( _ node: StringLiteralExprSyntax ) -> UnsafeMutableRawPointer {
226
221
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
227
- var segment = node. segments. only . as ( StringSegmentSyntax . self) !. content. text
222
+ var segment = node. segments. first! . as ( StringSegmentSyntax . self) !. content. text
228
223
return segment. withUTF8 { buf in
229
224
let id = SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
230
- return [ SwiftStringLiteralExpr_create ( ctx, id, buf. count, loc) ]
225
+ return SwiftStringLiteralExpr_create ( ctx, id, buf. count, loc)
231
226
}
232
227
}
233
228
234
- public func visit( _ node: IntegerLiteralExprSyntax ) -> [ UnsafeMutableRawPointer ] {
229
+ public func visit( _ node: IntegerLiteralExprSyntax ) -> UnsafeMutableRawPointer {
235
230
let loc = self . base. advanced ( by: node. position. utf8Offset) . raw
236
231
var segment = node. digits. text
237
232
return segment. withUTF8 { buf in
238
233
let id = SwiftASTContext_getIdentifier ( ctx, buf. baseAddress, buf. count)
239
- return [ SwiftIntegerLiteralExpr_create ( ctx, id, buf. count, loc) ]
234
+ return SwiftIntegerLiteralExpr_create ( ctx, id, buf. count, loc)
240
235
}
241
236
}
242
237
}
@@ -249,7 +244,7 @@ public func parseTopLevelSwift(
249
244
callback: @convention ( c) ( UnsafeMutableRawPointer , UnsafeMutableRawPointer ) -> Void
250
245
) {
251
246
let syntax = try ! Parser . parse ( source: String ( cString: buffer) )
252
- // dump(syntax)
247
+ dump ( syntax)
253
248
ASTGenVisitor ( ctx: ctx, base: buffer, declContext: declContext)
254
249
. visit ( syntax)
255
250
. forEach { callback ( $0, outputContext) }
0 commit comments