@@ -157,6 +157,22 @@ public struct ${node.name}: _SyntaxBase, Hashable, SyntaxCollection {
157
157
158
158
/// Conformance for `${node.name}`` to the Sequence protocol.
159
159
extension ${ node. name} : Sequence {
160
+ fileprivate static func nextElement< Iter > (
161
+ _ iterator: inout Iter, parent: _SyntaxBase
162
+ ) - > ${ node. collection_element_type} ? where Iter: AbsoluteRawSyntaxIteratorProtocol {
163
+ guard let absoluteRaw = iterator. next ( ) else { return nil }
164
+ let data = SyntaxData ( absoluteRaw, parent: parent)
165
+ % is_constructable = node. collection_element_type != 'Syntax' and \
166
+ % not ( element_node and element_node. is_base ( ) )
167
+ % if is_constructable:
168
+ return ${ node. collection_element_type} ( data)
169
+ % else :
170
+ % cast = '' if node. collection_element_type == 'Syntax' \
171
+ % else 'as! ' + node. collection_element_type
172
+ return ( makeSyntax ( data) ${ cast} )
173
+ % end
174
+ }
175
+
160
176
public struct Iterator : IteratorProtocol {
161
177
private let parent : _SyntaxBase
162
178
private var iterator : PresentRawSyntaxChildren . Iterator
@@ -167,24 +183,45 @@ extension ${node.name}: Sequence {
167
183
}
168
184
169
185
public mutating func next( ) -> ${ node. collection_element_type} ? {
170
- guard let absoluteRaw = iterator. next ( ) else { return nil }
171
- let data = SyntaxData ( absoluteRaw, parent: self . parent)
172
- % is_constructable = node. collection_element_type != 'Syntax' and \
173
- % not ( element_node and element_node. is_base ( ) )
174
- % if is_constructable:
175
- return ${ node. collection_element_type} ( data)
176
- % else :
177
- % cast = '' if node. collection_element_type == 'Syntax' \
178
- % else 'as! ' + node. collection_element_type
179
- return ( makeSyntax ( data) ${ cast} )
180
- % end
186
+ return ${ node. name} . nextElement( & iterator, parent: parent)
181
187
}
182
188
}
183
189
184
190
/// Returns an iterator over the elements of this syntax collection.
185
191
public func makeIterator( ) -> Iterator {
186
192
return Iterator ( collection: self )
187
193
}
194
+
195
+ public func reversed( ) -> Reversed {
196
+ return Reversed ( collection: self )
197
+ }
198
+
199
+ public struct Reversed : Sequence {
200
+ public struct Iterator : IteratorProtocol {
201
+ private let parent : _SyntaxBase
202
+ private var iterator : ReversedPresentRawSyntaxChildren . Iterator
203
+
204
+ public init ( collection node: ${ node. name} ) {
205
+ self . iterator = . init( parent: node. data. absoluteRaw)
206
+ self . parent = node
207
+ }
208
+
209
+ public mutating func next( ) -> ${ node. collection_element_type} ? {
210
+ return ${ node. name} . nextElement( & iterator, parent: parent)
211
+ }
212
+ }
213
+
214
+ let collection : ${ node. name}
215
+
216
+ /// Returns an iterator over the elements of this syntax collection.
217
+ public func makeIterator( ) -> Iterator {
218
+ return Iterator ( collection: collection)
219
+ }
220
+
221
+ public func reversed( ) -> ${ node. name} {
222
+ return collection
223
+ }
224
+ }
188
225
}
189
226
190
227
% end
0 commit comments