9
9
//
10
10
//===----------------------------------------------------------------------===//
11
11
12
+ // TODO: Remove and serialize CaptureList instead
13
+
12
14
// A tree representing the type of some captures.
13
15
enum CaptureStructure : Equatable {
14
16
case atom( name: String ? = nil , type: AnyType ? = nil )
@@ -29,47 +31,12 @@ enum CaptureStructure: Equatable {
29
31
extension CaptureStructure {
30
32
/// Returns a Boolean indicating whether the structure does not contain any
31
33
/// captures.
32
- var isEmpty : Bool {
34
+ private var isEmpty : Bool {
33
35
if case . tuple( let elements) = self , elements. isEmpty {
34
36
return true
35
37
}
36
38
return false
37
39
}
38
-
39
- func type( withAtomType atomType: Any . Type ) -> Any . Type {
40
- switch self {
41
- case . atom( _, type: nil ) :
42
- return atomType
43
- case . atom( _, type: let type? ) :
44
- return type. base
45
- case . optional( let child) :
46
- return TypeConstruction . optionalType ( of: child. type ( withAtomType: atomType) )
47
- case . tuple( let children) :
48
- return TypeConstruction . tupleType ( of: children. map {
49
- $0. type ( withAtomType: atomType)
50
- } )
51
- }
52
- }
53
-
54
- typealias DefaultAtomType = Substring
55
-
56
- var type : Any . Type {
57
- type ( withAtomType: DefaultAtomType . self)
58
- }
59
-
60
- var atomType : AnyType {
61
- switch self {
62
- case . atom( _, type: nil ) :
63
- return . init( Substring . self)
64
- case . atom( _, type: let type? ) :
65
- return type
66
- case . optional( let child) :
67
- return child. atomType
68
- case . tuple:
69
- fatalError ( " Recursive nesting has no single atom type " )
70
- }
71
-
72
- }
73
40
}
74
41
75
42
// MARK: - Serialization
@@ -252,3 +219,10 @@ extension CaptureStructure: CustomStringConvertible {
252
219
}
253
220
}
254
221
}
222
+
223
+ extension AST {
224
+ /// The capture structure of this AST tree.
225
+ var captureStructure : CaptureStructure {
226
+ root. _captureList. _captureStructure ( nestOptionals: true )
227
+ }
228
+ }
0 commit comments