11public struct Descender {
22
3- enum Mutation < T: Node > {
4- case replace( T )
5- case remove
6- }
73 private let visitor : Visitor
84
95 fileprivate var parentStack : [ VisitorParent ] = [ ]
106 private var path : [ AnyKeyPath ] = [ ]
117 private var isBreaking = false
128
13- fileprivate mutating func go< H: Node > ( node: inout H , key: AnyKeyPath ? ) -> Mutation < H > ? {
14- if isBreaking { return nil }
9+ /**
10+ The meat of the traversal
11+
12+ - Returns
13+ `true` if node should be removed
14+ */
15+ fileprivate mutating func go< H: Node > ( node: inout H , key: AnyKeyPath ? ) -> Bool {
16+ if isBreaking { return false }
1517 let parent = parentStack. last
1618 let newPath : [ AnyKeyPath ]
1719 if let key = key {
@@ -20,23 +22,23 @@ public struct Descender {
2022 newPath = path
2123 }
2224
23- var mutation : Mutation < H > ? = nil
25+ var shouldRemove = false
2426
2527 switch visitor. enter ( node: node, key: key, parent: parent, path: newPath, ancestors: parentStack) {
2628 case . skip:
27- return nil // .node(Optional(result))
29+ return false
2830 case . continue:
2931 break
3032 case let . node( newNode) :
3133 if let newNode = newNode {
32- mutation = . replace ( newNode)
34+ node = newNode
3335 } else {
3436 // TODO: Should we still be traversing the children here?
35- mutation = . remove
37+ shouldRemove = true
3638 }
3739 case . break:
3840 isBreaking = true
39- return nil
41+ return false
4042 }
4143 parentStack. append ( . node( node) )
4244 if let key = key {
@@ -48,46 +50,39 @@ public struct Descender {
4850 }
4951 parentStack. removeLast ( )
5052
51- if isBreaking { return mutation }
53+ if isBreaking { return shouldRemove }
5254
5355 switch visitor. leave ( node: node, key: key, parent: parent, path: newPath, ancestors: parentStack) {
5456 case . skip, . continue:
55- return mutation
57+ return shouldRemove
5658 case let . node( newNode) :
5759 if let newNode = newNode {
58- return . replace( newNode)
60+ node = newNode
61+ return false
5962 } else {
6063 // TODO: Should we still be traversing the children here?
61- return . remove
64+ return true
6265 }
6366 case . break:
6467 isBreaking = true
65- return mutation
68+ return shouldRemove
6669 }
6770 }
6871
6972
7073 mutating func descend< T: Node , U: Node > ( _ node: inout T , _ kp: WritableKeyPath < T , U > ) {
71- switch go ( node: & node[ keyPath: kp] , key: kp) {
72- case nil :
73- break
74- case . replace( let child) :
75- node [ keyPath: kp] = child
76- case . remove:
74+ if go ( node: & node[ keyPath: kp] , key: kp) {
7775 fatalError ( " Can't remove this node " )
7876 }
7977 }
8078 mutating func descend< T, U: Node > ( _ node: inout T , _ kp: WritableKeyPath < T , U ? > ) {
8179 guard var oldVal = node [ keyPath: kp] else {
8280 return
8381 }
84- switch go ( node: & oldVal, key: kp) {
85- case nil :
86- node [ keyPath: kp] = oldVal
87- case . replace( let child) :
88- node [ keyPath: kp] = child
89- case . remove:
82+ if go ( node: & oldVal, key: kp) {
9083 node [ keyPath: kp] = nil
84+ } else {
85+ node [ keyPath: kp] = oldVal
9186 }
9287 }
9388 mutating func descend< T, U: Node > ( _ node: inout T , _ kp: WritableKeyPath < T , [ U ] > ) {
@@ -97,12 +92,7 @@ public struct Descender {
9792
9893 var i = node [ keyPath: kp] . startIndex
9994 while i != node [ keyPath: kp] . endIndex {
100- switch go ( node: & node[ keyPath: kp] [ i] , key: \[ U ] . [ i] ) {
101- case nil :
102- break
103- case . replace( let child) :
104- node [ keyPath: kp] [ i] = child
105- case . remove:
95+ if go ( node: & node[ keyPath: kp] [ i] , key: \[ U ] . [ i] ) {
10696 toRemove. append ( i)
10797 }
10898 i = node [ keyPath: kp] . index ( after: i)
@@ -112,12 +102,7 @@ public struct Descender {
112102 }
113103
114104 mutating func descend< T: Node > ( enumCase: inout T ) {
115- switch go ( node: & enumCase, key: nil ) {
116- case nil :
117- break
118- case . replace( let node) :
119- enumCase = node
120- case . remove:
105+ if go ( node: & enumCase, key: nil ) {
121106 //TODO: figure this out
122107 fatalError ( " What happens here? " )
123108 }
@@ -169,14 +154,10 @@ public func visit<T: Node, V: Visitor>(root: T, visitor: V) -> T {
169154 var descender = Descender ( visitor: visitor)
170155
171156 var result = root
172- switch descender. go ( node: & result, key: nil ) {
173- case . remove:
157+ if descender. go ( node: & result, key: nil ) {
174158 fatalError ( " Root node in the AST was removed " )
175- case . replace( let node) :
176- return node
177- case nil :
178- return result
179159 }
160+ return result
180161}
181162
182163public enum VisitorParent {
0 commit comments