-
Notifications
You must be signed in to change notification settings - Fork 441
/
Copy pathRawSyntaxNodesFile.swift
250 lines (223 loc) · 8.23 KB
/
RawSyntaxNodesFile.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import SwiftSyntax
import SwiftSyntaxBuilder
import SyntaxSupport
import Utils
func rawSyntaxNodesFile(nodesStartingWith: [Character]) -> SourceFileSyntax {
return SourceFileSyntax(leadingTrivia: copyrightHeader) {
for node in SYNTAX_NODES
where node.kind.isBase
&& nodesStartingWith.contains(node.kind.syntaxType.description.droppingLeadingUnderscores.first!)
&& !node.kind.isDeprecated
{
DeclSyntax(
"""
\(node.apiAttributes(forRaw: true))\
public protocol \(node.kind.raw.protocolType): \(node.base.raw.protocolType) {}
"""
)
}
for node in SYNTAX_NODES
where nodesStartingWith.contains(node.kind.syntaxType.description.droppingLeadingUnderscores.first!) {
try! StructDeclSyntax(
"""
\(node.apiAttributes(forRaw: true))\
public struct \(node.kind.raw.syntaxType): \(node.kind.isBase ? node.kind.raw.protocolType : node.base.raw.protocolType)
"""
) {
for childNodeChoices in node.childrenNodeChoices(forRaw: true) {
childNodeChoices.rawEnumDecl
}
DeclSyntax(
"""
@_spi(RawSyntax)
public var layoutView: RawSyntaxLayoutView {
return raw.layoutView!
}
"""
)
try FunctionDeclSyntax("public static func isKindOf(_ raw: RawSyntax) -> Bool") {
if node.kind.isBase {
let cases = SwitchCaseItemListSyntax {
for n in SYNTAX_NODES where n.base == node.kind {
SwitchCaseItemSyntax(
pattern: ExpressionPatternSyntax(
expression: ExprSyntax(".\(n.memberCallName)")
)
)
}
}
ExprSyntax(
"""
switch raw.kind {
case \(cases): return true
default: return false
}
"""
)
} else {
StmtSyntax("return raw.kind == .\(node.memberCallName)")
}
}
DeclSyntax("public var raw: RawSyntax")
DeclSyntax(
"""
init(raw: RawSyntax) {
precondition(Self.isKindOf(raw))
self.raw = raw
}
"""
)
DeclSyntax(
"""
private init(unchecked raw: RawSyntax) {
self.raw = raw
}
"""
)
DeclSyntax(
"""
public init?(_ other: some RawSyntaxNodeProtocol) {
guard Self.isKindOf(other.raw) else { return nil }
self.init(unchecked: other.raw)
}
"""
)
if node.kind.isBase {
DeclSyntax(
"""
public init(_ other: some \(node.kind.raw.protocolType)) {
self.init(unchecked: other.raw)
}
"""
)
}
if let node = node.collectionNode {
let element =
node.elementChoices.only != nil && !node.disableSameTypeForUniqueChoice
? node.elementChoices.only!.raw.syntaxType : "Element"
DeclSyntax(
"""
public init(elements: [\(element)], arena: __shared SyntaxArena) {
let raw = RawSyntax.makeLayout(
kind: .\(node.memberCallName), uninitializedCount: elements.count, arena: arena) { layout in
guard var ptr = layout.baseAddress else { return }
for elem in elements {
ptr.initialize(to: elem.raw)
ptr += 1
}
}
self.init(unchecked: raw)
}
"""
)
DeclSyntax(
"""
public var elements: [Raw\(node.collectionElementType.syntaxBaseName)] {
layoutView.children.map { Raw\(node.collectionElementType.syntaxBaseName)(raw: $0!) }
}
"""
)
}
if let node = node.layoutNode {
let params = FunctionParameterListSyntax {
for child in node.children {
FunctionParameterSyntax(
firstName: child.isUnexpectedNodes ? .wildcardToken(trailingTrivia: .space) : child.labelDeclName,
secondName: child.isUnexpectedNodes ? child.labelDeclName : nil,
colon: .colonToken(),
type: child.rawParameterType,
defaultValue: child.isUnexpectedNodes ? child.defaultInitialization : nil
)
}
FunctionParameterSyntax("arena: __shared SyntaxArena")
}
try InitializerDeclSyntax("public init(\(params))") {
if !node.children.isEmpty {
let list = ExprListSyntax {
ExprSyntax("layout.initialize(repeating: nil)")
for (index, child) in node.children.enumerated() {
let optionalMark = child.isOptional ? "?" : ""
ExprSyntax(
"layout[\(raw: index)] = \(child.baseCallName)\(raw: optionalMark).raw"
)
.with(\.leadingTrivia, .newline)
}
}
DeclSyntax(
"""
let raw = RawSyntax.makeLayout(
kind: .\(node.memberCallName), uninitializedCount: \(raw: node.children.count), arena: arena) { layout in
\(list)
}
"""
)
} else {
DeclSyntax("let raw = RawSyntax.makeEmptyLayout(kind: .\(node.memberCallName), arena: arena)")
}
ExprSyntax("self.init(unchecked: raw)")
}
for (index, child) in node.children.enumerated() {
try VariableDeclSyntax(
"public var \(child.varDeclName): Raw\(child.buildableType.buildable)"
) {
let exclamationMark = child.isOptional ? "" : "!"
if child.syntaxNodeKind == .syntax {
ExprSyntax("layoutView.children[\(raw: index)]\(raw: exclamationMark)")
} else {
ExprSyntax(
"layoutView.children[\(raw: index)].map(\(child.syntaxNodeKind.raw.syntaxType).init(raw:))\(raw: exclamationMark)"
)
}
}
}
}
}
}
}
}
private extension ChildNodeChoices {
var rawEnumDecl: EnumDeclSyntax {
try! EnumDeclSyntax("public enum \(self.name): RawSyntaxNodeProtocol") {
for choice in self.choices {
choice.enumCaseDecl
}
self.isKindOfFuncDecl(parameterName: "raw", parameterType: "RawSyntax")
self.syntaxGetter(propertyName: "raw", propertyType: "RawSyntax")
self.syntaxInitDecl(inputType: "__shared some RawSyntaxNodeProtocol")
for choice in self.choices {
if let baseTypeInitDecl = choice.baseTypeInitDecl(hasArgumentName: true) {
baseTypeInitDecl
}
}
}
}
}
fileprivate extension Child {
var rawParameterType: TypeSyntax {
var paramType: TypeSyntax
if !kind.isNodeChoicesEmpty {
paramType = "\(syntaxChoicesType)"
} else if hasBaseType && !isOptional {
// we restrict the use of generic type to non-optional parameter types, otherwise call sites would no longer be
// able to just pass `nil` to this parameter without specializing `(some Raw<Kind>SyntaxNodeProtocol)?`
//
// we've opted out of providing a default value to the parameter (e.g. `RawExprSyntax?.none`) as a workaround,
// as passing an explicit `nil` would prompt developers to think clearly whether this parameter should be parsed
paramType = "some \(syntaxNodeKind.raw.protocolType)"
} else {
paramType = syntaxNodeKind.raw.syntaxType
}
return buildableType.optionalWrapped(type: paramType)
}
}