1
- //===-------------- SyntaxCollection.swift - Syntax Collection ------------===//
1
+ % {
2
+ from gyb_syntax_support import *
3
+ # -*- mode: Swift -*-
4
+ # Ignore the following admonition it applies to the resulting . swift file only
5
+ } %
6
+ //// Automatically Generated From SyntaxCollections.swift.gyb.
7
+ //// Do Not Edit Directly!
8
+ //===------------ SyntaxCollections.swift.gyb - Syntax Collection ---------===//
2
9
//
3
10
// This source file is part of the Swift.org open source project
4
11
//
12
19
13
20
import Foundation
14
21
22
+ % for node in SYNTAX_NODES:
23
+ % if node. collection_element:
24
+
15
25
/// Represents a collection of Syntax nodes of a specific type. SyntaxCollection
16
26
/// behaves as a regular Swift collection, and has accessors that return new
17
27
/// versions of the collection with different children.
18
- public class SyntaxCollection < SyntaxElement : Syntax > : _SyntaxBase {
28
+ public struct $ { node . name } : _SyntaxBase {
19
29
var _root : SyntaxData
20
- unowned var _data : SyntaxData
30
+ unowned var _data : SyntaxData
21
31
22
32
/// Creates a Syntax node from the provided root and data.
23
33
internal init ( root: SyntaxData , data: SyntaxData ) {
@@ -28,52 +38,52 @@ public class SyntaxCollection<SyntaxElement: Syntax>: _SyntaxBase {
28
38
#endif
29
39
}
30
40
31
- /// Creates a new SyntaxCollection by replacing the underlying layout with
41
+ /// Creates a new `${node.name}` by replacing the underlying layout with
32
42
/// a different set of raw syntax nodes.
33
43
///
34
44
/// - Parameter layout: The new list of raw syntax nodes underlying this
35
45
/// collection.
36
- /// - Returns: A new SyntaxCollection with the new layout underlying it.
46
+ /// - Returns: A new `${node.name}` with the new layout underlying it.
37
47
internal func replacingLayout(
38
- _ layout: [ RawSyntax ] ) -> SyntaxCollection < SyntaxElement > {
48
+ _ layout: [ RawSyntax ] ) -> $ { node . name } {
39
49
let newRaw = data. raw. replacingLayout ( layout)
40
50
let ( newRoot, newData) = data. replacingSelf ( newRaw)
41
- return SyntaxCollection < SyntaxElement > ( root: newRoot, data: newData)
51
+ return $ { node . name } ( root: newRoot, data: newData)
42
52
}
43
53
44
- /// Creates a new SyntaxCollection by appending the provided syntax element
54
+ /// Creates a new `${node.name}` by appending the provided syntax element
45
55
/// to the children.
46
56
///
47
57
/// - Parameter syntax: The element to append.
48
- /// - Returns: A new SyntaxCollection with that element appended to the end.
58
+ /// - Returns: A new `${node.name}` with that element appended to the end.
49
59
public func appending(
50
- _ syntax: SyntaxElement ) -> SyntaxCollection < SyntaxElement > {
60
+ _ syntax: $ { node . collection_element_type } ) -> $ { node . name } {
51
61
var newLayout = data. raw. layout
52
62
newLayout. append ( syntax. raw)
53
63
return replacingLayout ( newLayout)
54
64
}
55
65
56
- /// Creates a new SyntaxCollection by prepending the provided syntax element
66
+ /// Creates a new `${node.name}` by prepending the provided syntax element
57
67
/// to the children.
58
68
///
59
69
/// - Parameter syntax: The element to prepend.
60
- /// - Returns: A new SyntaxCollection with that element prepended to the
70
+ /// - Returns: A new `${node.name}` with that element prepended to the
61
71
/// beginning.
62
72
public func prepending(
63
- _ syntax: SyntaxElement ) -> SyntaxCollection < SyntaxElement > {
73
+ _ syntax: $ { node . collection_element_type } ) -> $ { node . name } {
64
74
return inserting ( syntax, at: 0 )
65
75
}
66
76
67
- /// Creates a new SyntaxCollection by inserting the provided syntax element
77
+ /// Creates a new `${node.name}` by inserting the provided syntax element
68
78
/// at the provided index in the children.
69
79
///
70
80
/// - Parameters:
71
81
/// - syntax: The element to insert.
72
82
/// - index: The index at which to insert the element in the collection.
73
83
///
74
- /// - Returns: A new SyntaxCollection with that element appended to the end.
75
- public func inserting( _ syntax: SyntaxElement ,
76
- at index: Int ) -> SyntaxCollection < SyntaxElement > {
84
+ /// - Returns: A new `${node.name}` with that element appended to the end.
85
+ public func inserting( _ syntax: $ { node . collection_element_type } ,
86
+ at index: Int) -> $ { node . name } {
77
87
var newLayout = data. raw. layout
78
88
/// Make sure the index is a valid insertion index (0 to 1 past the end)
79
89
precondition ( ( newLayout. startIndex... newLayout. endIndex) . contains ( index) ,
@@ -82,67 +92,71 @@ public class SyntaxCollection<SyntaxElement: Syntax>: _SyntaxBase {
82
92
return replacingLayout ( newLayout)
83
93
}
84
94
85
- /// Creates a new SyntaxCollection by removing the syntax element at the
95
+ /// Creates a new `${node.name}` by removing the syntax element at the
86
96
/// provided index.
87
97
///
88
98
/// - Parameter index: The index of the element to remove from the collection.
89
- /// - Returns: A new SyntaxCollection with the element at the provided index
99
+ /// - Returns: A new `${node.name}` with the element at the provided index
90
100
/// removed.
91
- public func removing( childAt index: Int ) -> SyntaxCollection < SyntaxElement > {
101
+ public func removing( childAt index: Int ) -> $ { node . name } {
92
102
var newLayout = data. raw. layout
93
103
newLayout. remove ( at: index)
94
104
return replacingLayout( newLayout)
95
105
}
96
106
97
- /// Creates a new SyntaxCollection by removing the first element.
107
+ /// Creates a new `${node.name}` by removing the first element.
98
108
///
99
- /// - Returns: A new SyntaxCollection with the first element removed.
100
- public func removingFirst( ) -> SyntaxCollection < SyntaxElement > {
109
+ /// - Returns: A new `${node.name}` with the first element removed.
110
+ public func removingFirst( ) -> $ { node . name } {
101
111
var newLayout = data. raw. layout
102
112
newLayout. removeFirst ( )
103
113
return replacingLayout( newLayout)
104
114
}
105
115
106
- /// Creates a new SyntaxCollection by removing the last element.
116
+ /// Creates a new `${node.name}` by removing the last element.
107
117
///
108
- /// - Returns: A new SyntaxCollection with the last element removed.
109
- public func removingLast( ) -> SyntaxCollection < SyntaxElement > {
118
+ /// - Returns: A new `${node.name}` with the last element removed.
119
+ public func removingLast( ) -> $ { node . name } {
110
120
var newLayout = data. raw. layout
111
121
newLayout. removeLast ( )
112
122
return replacingLayout( newLayout)
113
123
}
114
124
115
125
/// Returns an iterator over the elements of this syntax collection.
116
- public func makeIterator( ) -> SyntaxCollectionIterator < SyntaxElement > {
117
- return SyntaxCollectionIterator ( collection: self )
126
+ public func makeIterator( ) -> $ { node . name } Iterator {
127
+ return $ { node . name } Iterator ( collection: self )
118
128
}
119
129
}
120
130
121
- /// Conformance for SyntaxCollection to the Collection protocol.
122
- extension SyntaxCollection : Collection {
131
+ /// Conformance for `${node.name}`` to the Collection protocol.
132
+ extension $ { node . name } : Collection {
123
133
public var startIndex: Int {
124
134
return data. childCaches. startIndex
125
135
}
126
-
136
+
127
137
public var endIndex : Int {
128
138
return data. childCaches. endIndex
129
139
}
130
-
140
+
131
141
public func index( after i: Int ) -> Int {
132
142
return data. childCaches. index ( after: i)
133
143
}
134
-
135
- public subscript( _ index: Int ) -> SyntaxElement {
136
- return child ( at: index) ! as! SyntaxElement
144
+
145
+ public subscript( _ index: Int) - > ${ node. collection_element_type} {
146
+ % cast = '' if node. collection_element_type == 'Syntax' \
147
+ % else 'as! ' + node. collection_element_type
148
+ return child( at: index) ! ${ cast}
137
149
}
138
150
}
139
151
140
- /// A type that iterates over a syntax collection using its indices.
141
- public struct SyntaxCollectionIterator < Element: Syntax > : IteratorProtocol {
142
- private let collection : SyntaxCollection < Element >
143
- private var index : SyntaxCollection < Element > . Index
152
+ /// A type that iterates over a `${node.name}` using its indices.
153
+ public struct ${ node. name} Iterator: IteratorProtocol {
154
+ public typealias Element = ${ node. collection_element_type}
155
+
156
+ private let collection : ${ node. name}
157
+ private var index : ${ node. name} . Index
144
158
145
- fileprivate init ( collection: SyntaxCollection < Element > ) {
159
+ fileprivate init( collection: $ { node . name } ) {
146
160
self . collection = collection
147
161
self . index = collection. startIndex
148
162
}
@@ -160,3 +174,5 @@ public struct SyntaxCollectionIterator<Element: Syntax>: IteratorProtocol {
160
174
}
161
175
}
162
176
177
+ % end
178
+ % end
0 commit comments