@@ -24,169 +24,6 @@ public enum CaptureStructure: Equatable {
24
24
}
25
25
}
26
26
27
- // TODO: Below are all flattening constructors. Instead create
28
- // a builder/visitor that can store the structuralization
29
- // approach
30
-
31
- extension CaptureStructure {
32
- public struct Constructor {
33
- var strategy : Strategy
34
-
35
- public init ( _ strategy: Strategy = . flatten) {
36
- guard strategy == . flatten else {
37
- fatalError ( " TODO: adjust creator methods " )
38
- }
39
- self . strategy = strategy
40
- }
41
- }
42
- }
43
-
44
- extension CaptureStructure . Constructor {
45
- public mutating func alternating< C: Collection > (
46
- _ children: C
47
- ) -> CaptureStructure where C. Element: _TreeNode {
48
- return children. map {
49
- $0. _captureStructure ( & self )
50
- } . reduce ( . empty, + )
51
- . map ( CaptureStructure . optional)
52
- }
53
- public mutating func concatenating< C: Collection > (
54
- _ children: C
55
- ) -> CaptureStructure where C. Element: _TreeNode {
56
- return children. map {
57
- $0. _captureStructure ( & self )
58
- } . reduce ( . empty, + )
59
- }
60
-
61
- public mutating func grouping< T: _TreeNode > (
62
- _ child: T ,
63
- as kind: AST . Group . Kind
64
- ) -> CaptureStructure {
65
- switch kind {
66
- case . capture:
67
- return capturing ( child)
68
- case . namedCapture( let name) :
69
- return capturing ( name: name. value, child)
70
- case . balancedCapture( let b) :
71
- return capturing ( name: b. name? . value, child)
72
- default :
73
- precondition ( !kind. isCapturing)
74
- return child. _captureStructure ( & self )
75
- }
76
- }
77
-
78
- public mutating func capturing< T: _TreeNode > (
79
- name: String ? = nil ,
80
- _ child: T ,
81
- withType type: AnyType ? = nil
82
- ) -> CaptureStructure {
83
- . atom( name: name, type: type)
84
- + child. _captureStructure ( & self )
85
- }
86
-
87
- // TODO: We'll likely want/need a generalization of
88
- // conditional's condition kind.
89
- public mutating func condition< T: _TreeNode > (
90
- _ condition: AST . Conditional . Condition . Kind ,
91
- trueBranch: T ,
92
- falseBranch: T
93
- ) -> CaptureStructure {
94
- // A conditional's capture structure is effectively that of an alternation
95
- // between the true and false branches. However the condition may also
96
- // have captures in the case of a group condition.
97
- var captures = CaptureStructure . empty
98
- switch condition {
99
- case . group( let g) :
100
- captures = captures + AST. Node. group ( g) . _captureStructure ( & self )
101
- default :
102
- break
103
- }
104
- let branchCaptures = trueBranch. _captureStructure ( & self ) +
105
- falseBranch. _captureStructure ( & self )
106
- return captures + branchCaptures. map ( CaptureStructure . optional)
107
- }
108
-
109
- public mutating func quantifying< T: _TreeNode > (
110
- _ child: T , amount: AST . Quantification . Amount
111
- ) -> CaptureStructure {
112
- let result = child. _captureStructure ( & self )
113
- return amount. bounds. atLeast == 0
114
- ? result. map ( CaptureStructure . optional) : result
115
- }
116
-
117
- // TODO: Will need to adjust for DSLTree support, and
118
- // "absent" isn't the best name for these.
119
- public mutating func absent(
120
- _ kind: AST . AbsentFunction . Kind
121
- ) -> CaptureStructure {
122
- // Only the child of an expression absent function is relevant, as the
123
- // other expressions don't actually get matched against.
124
- switch kind {
125
- case . expression( _, _, let child) :
126
- return child. _captureStructure ( & self )
127
- case . clearer, . repeater, . stopper:
128
- return . empty
129
- }
130
- }
131
- }
132
-
133
- extension AST . Node {
134
- public func _captureStructure(
135
- _ constructor: inout CaptureStructure . Constructor
136
- ) -> CaptureStructure {
137
- guard constructor. strategy == . flatten else {
138
- fatalError ( " TODO " )
139
- }
140
- return _captureList. _captureStructure ( nestOptionals: true )
141
- }
142
- }
143
-
144
- // MARK: - Combination and transformation
145
-
146
- extension CaptureStructure {
147
- /// Returns a capture structure by concatenating any tuples in `self` and
148
- /// `other`.
149
- func concatenating( with other: CaptureStructure ) -> CaptureStructure {
150
- switch ( self , other) {
151
- // (T...) + (U...) ==> (T..., U...)
152
- case let ( . tuple( lhs) , . tuple( rhs) ) :
153
- return . tuple( lhs + rhs)
154
- // T + () ==> T
155
- case ( _, . tuple( let rhs) ) where rhs. isEmpty:
156
- return self
157
- // () + T ==> T
158
- case ( . tuple( let lhs) , _) where lhs. isEmpty:
159
- return other
160
- // (T...) + U ==> (T..., U)
161
- case let ( . tuple( lhs) , _) :
162
- return . tuple( lhs + [ other] )
163
- // T + (U...) ==> (T, U...)
164
- case let ( _, . tuple( rhs) ) :
165
- return . tuple( [ self ] + rhs)
166
- // T + U ==> (T, U)
167
- default :
168
- return . tuple( [ self , other] )
169
- }
170
- }
171
-
172
- static func + (
173
- lhs: CaptureStructure , rhs: CaptureStructure
174
- ) -> CaptureStructure {
175
- lhs. concatenating ( with: rhs)
176
- }
177
-
178
- /// Returns a capture structure by transforming any tuple element of `self`
179
- /// or transforming `self` directly if it is not a tuple.
180
- func map(
181
- _ transform: ( CaptureStructure ) -> CaptureStructure
182
- ) -> CaptureStructure {
183
- if case . tuple( let children) = self {
184
- return . tuple( children. map ( transform) )
185
- }
186
- return transform ( self )
187
- }
188
- }
189
-
190
27
// MARK: - Common properties
191
28
192
29
extension CaptureStructure {
@@ -415,11 +252,3 @@ extension CaptureStructure: CustomStringConvertible {
415
252
}
416
253
}
417
254
}
418
-
419
- extension CaptureStructure . Constructor {
420
- public enum Strategy {
421
- case flatten
422
- case nest
423
- // case drop(after: Int)...
424
- }
425
- }
0 commit comments