/
SyntaxNodes.swift.gyb.template
248 lines (231 loc) · 7.84 KB
/
SyntaxNodes.swift.gyb.template
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
%{
# -*- mode: Swift -*-
from gyb_syntax_support import *
from gyb_syntax_support.Traits import TRAITS
NODE_MAP = create_node_map()
# Ignore the following admonition it applies to the resulting .swift file only
}%
//// Automatically Generated From SyntaxNodes.swift.gyb.
//// Do Not Edit Directly!
//===------------ SyntaxNodes.swift - Syntax Node definitions -------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 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
//
//===----------------------------------------------------------------------===//
%{
"""
This gyb-file generates the syntax nodes for SwiftSyntax. To keep the generated
files at a managable file size, it is to be invoked multiple times with the
global variable EMIT_KIND set to a base kind listed in
gyb_syntax_support/kinds.py:SYNTAX_BASE_KINDES. It then only emits those syntax
nodes whose base kind are that specified kind.
"""
}%
% for node in SYNTAX_NODES:
% base_type = node.base_type
% if node.is_base():
% # Handled in SyntaxNodesBase.swift.gyb
% pass
% elif node.collection_element:
% # Handled in SyntaxCollections.swift.gyb
% pass
% elif node.base_kind != EMIT_KIND:
% pass
% else:
% # We are actually handling this node now
// MARK: - ${node.name}
% for line in dedented_lines(node.description):
/// ${line}
% end
public struct ${node.name}: ${base_type}Protocol, SyntaxHashable {
% # ======
% # Cursor
% # ======
%
% if node.children:
enum Cursor: Int {
% for child in node.children:
case ${child.swift_name}
% end
}
% end
% # ==============
% # Initialization
% # ==============
%
public let _syntaxNode: Syntax
/// Converts the given `Syntax` node to a `${node.name}` if possible. Returns
/// `nil` if the conversion is not possible.
public init?(_ syntax: Syntax) {
guard syntax.raw.kind == .${node.swift_syntax_kind} else { return nil }
self._syntaxNode = syntax
% if node.must_uphold_invariant:
if !isValid {
fatalError("Instance of ${node.name} is invalid.")
}
% end
}
/// Creates a `${node.name}` node from the given `SyntaxData`. This assumes
/// that the `SyntaxData` is of the correct kind. If it is not, the behaviour
/// is undefined.
% if node.must_uphold_invariant:
/// This initializer returns nil if the invariant is not satisfied.
internal init?(_ data: SyntaxData) {
% else:
internal init(_ data: SyntaxData) {
% end
assert(data.raw.kind == .${node.swift_syntax_kind})
self._syntaxNode = Syntax(data)
% if node.must_uphold_invariant:
if !isValid {
return nil
}
% end
}
public var syntaxNodeType: SyntaxProtocol.Type {
return Swift.type(of: self)
}
% for child in node.children:
% child_node = NODE_MAP.get(child.syntax_kind)
%
% # ===================
% # Children properties
% # ===================
%
% ret_type = child.type_name
% if child.is_optional:
% ret_type += '?'
% end
% for line in dedented_lines(child.description):
/// ${line}
% end
public var ${child.swift_name}: ${ret_type} {
get {
let childData = data.child(at: Cursor.${child.swift_name},
parent: Syntax(self))
% if child.is_optional:
if childData == nil { return nil }
% end
return ${child.type_name}(childData!)
}
% if not node.must_uphold_invariant:
set(value) {
self = with${child.name}(value)
}
% end
}
% if node.must_uphold_invariant:
public enum ${child.name}Error: Error, CustomStringConvertible {
case invalid(${child.swift_name}: ${ret_type})
public var description: String {
switch self {
case .invalid(let ${child.swift_name}):
return "attempted to use setter with invalid ${child.name} \"\(${child.swift_name})\""
}
}
}
mutating public func set${child.name}(_ ${child.swift_name}: ${ret_type}) throws {
if let childSyntax = with${child.name}(${child.swift_name}) {
self = childSyntax
} else {
throw ${child.name}Error.invalid(${child.swift_name}: ${child.swift_name})
}
}
% end
%
% # ===============
% # Adding children
% # ===============
% if child_node and child_node.is_syntax_collection():
% child_elt = child.collection_element_name
% child_elt_type = child_node.collection_element_type
% if not child_elt:
% raise Exception("'collection_element_name' should be set for '%s' of '%s'" % (child.name, node.name))
% end
/// Adds the provided `${child_elt}` to the node's `${child.swift_name}`
/// collection.
/// - param element: The new `${child_elt}` to add to the node's
/// `${child.swift_name}` collection.
/// - returns: A copy of the receiver with the provided `${child_elt}`
/// appended to its `${child.swift_name}` collection.
public func add${child_elt}(_ element: ${child_elt_type}) -> ${node.name} {
var collection: RawSyntax
if let col = raw[Cursor.${child.swift_name}] {
collection = col.appending(element.raw)
} else {
collection = RawSyntax.create(kind: SyntaxKind.${child_node.swift_syntax_kind},
layout: [element.raw], length: element.raw.totalLength, presence: .present)
}
let newData = data.replacingChild(collection,
at: Cursor.${child.swift_name})
return ${node.name}(newData)
}
% end
% # ===================
% # Replacing children
% # ===================
/// Returns a copy of the receiver with its `${child.swift_name}` replaced.
/// - param newChild: The new `${child.swift_name}` to replace the node's
/// current `${child.swift_name}`, if present.
public func with${child.name}(
% if node.must_uphold_invariant:
_ newChild: ${child.type_name}?) -> ${node.name}? {
% else:
_ newChild: ${child.type_name}?) -> ${node.name} {
% end
% if child.is_optional:
let raw = newChild?.raw
% else:
let raw = newChild?.raw ?? ${make_missing_swift_child(child)}
% end
let newData = data.replacingChild(raw, at: Cursor.${child.swift_name})
return ${node.name}(newData)
}
% end
% # ===================
% # Validate layout
% # ===================
public func _validateLayout() {
% if node.is_unknown():
// We are verifying an unknown node. Since we don’t know anything about it
// we need to assume it’s valid.
% else:
let rawChildren = Array(RawSyntaxChildren(Syntax(self)))
assert(rawChildren.count == ${len(node.children)})
% for i, child in enumerate(node.children):
// Check child #${i} child is ${child.type_name} ${"or missing" if child.is_optional else ""}
% if not child.is_optional:
assert(rawChildren[${i}].raw != nil)
% end
if let raw = rawChildren[${i}].raw {
let info = rawChildren[${i}].syntaxInfo
let absoluteRaw = AbsoluteRawSyntax(raw: raw, info: info)
let syntaxData = SyntaxData(absoluteRaw, parent: Syntax(self))
let syntaxChild = Syntax(syntaxData)
assert(syntaxChild.is(${child.type_name}.self))
}
% end
% end
}
}
extension ${node.name}: CustomReflectable {
public var customMirror: Mirror {
return Mirror(self, children: [
% for child in node.children:
% if child.is_optional:
"${child.swift_name}": ${child.swift_name}.map(Syntax.init)?.asProtocol(SyntaxProtocol.self) as Any,
% else:
"${child.swift_name}": Syntax(${child.swift_name}).asProtocol(SyntaxProtocol.self),
% end
% end
])
}
}
% end
% end