3232
3333extension Processor {
3434 struct _StoredCapture {
35- // Set whenever we push the very first capture, allows us
36- // to theoretically re-compute anything we want to later.
37- fileprivate var startState : SavePoint ? = nil
38-
39- // Save the entire history as we go, so that backtracking
40- // can just lop-off aborted runs.
41- //
42- // Backtracking entries can specify a per-capture stack
43- // index so that we can abort anything that came after.
44- //
45- // By remembering the entire history, we waste space, but
46- // we get flexibility for now.
47- //
48- fileprivate var history : Array < ( range: Range < Position > , value: Any ? ) > = [ ]
35+ var range : Range < Position > ? = nil
36+
37+ var value : Any ? = nil
4938
5039 // An in-progress capture start
5140 fileprivate var currentCaptureBegin : Position ? = nil
5241
5342 fileprivate func _invariantCheck( ) {
54- if startState == nil {
55- assert ( history. isEmpty)
56- assert ( currentCaptureBegin == nil )
57- } else if currentCaptureBegin == nil {
58- assert ( !history. isEmpty)
43+ if range == nil {
44+ assert ( value == nil )
5945 }
6046 }
6147
6248 // MARK: - IPI
6349
64- var isEmpty : Bool { history. isEmpty }
65-
66- var latest : ( range: Range < Position > , value: Any ? ) ? { history. last }
50+ var deconstructed : ( range: Range < Position > , value: Any ? ) ? {
51+ guard let r = range else { return nil }
52+ return ( r, value)
53+ }
6754
6855 /// Start a new capture. If the previously started one was un-ended,
69- /// will clear it and restart. If this is the first start, will save `initial`.
56+ /// will clear it and restart.
7057 mutating func startCapture(
71- _ idx: Position , initial : SavePoint
58+ _ idx: Position
7259 ) {
7360 _invariantCheck ( )
7461 defer { _invariantCheck ( ) }
7562
76- if self . startState == nil {
77- self . startState = initial
78- }
7963 currentCaptureBegin = idx
8064 }
8165
8266 mutating func endCapture( _ idx: Position ) {
8367 _invariantCheck ( )
84- assert ( currentCaptureBegin != nil )
8568 defer { _invariantCheck ( ) }
8669
87- history. append ( ( currentCaptureBegin! ..< idx, value: nil ) )
70+ guard let low = currentCaptureBegin else {
71+ fatalError ( " Invariant violated: ending unstarted capture " )
72+ }
73+
74+ range = low..< idx
75+ value = nil // TODO: cleaner IPI around this...
76+ currentCaptureBegin = nil
8877 }
8978
9079 mutating func registerValue(
@@ -93,28 +82,15 @@ extension Processor {
9382 ) {
9483 _invariantCheck ( )
9584 defer { _invariantCheck ( ) }
96- if let sp = overwriteInitial {
97- self . startState = sp
98- }
99- history [ history. endIndex - 1 ] . value = value
100- }
101-
102- mutating func fail( truncatingAt stackIdx: Int ) {
103- _invariantCheck ( )
104- assert ( stackIdx <= history. endIndex)
105- defer { _invariantCheck ( ) }
10685
107- history. removeSubrange ( stackIdx... )
108- if history. isEmpty {
109- startState = nil
110- }
86+ self . value = value
11187 }
11288 }
11389}
11490
11591extension Processor . _StoredCapture : CustomStringConvertible {
11692 var description : String {
117- return String ( describing: history )
93+ return String ( describing: self )
11894 }
11995}
12096
@@ -124,10 +100,10 @@ struct MECaptureList {
124100
125101 func latestUntyped( from input: String ) -> Array < Substring ? > {
126102 values. map {
127- guard let last = $0. latest else {
103+ guard let range = $0. range else {
128104 return nil
129105 }
130- return input [ last . 0 ]
106+ return input [ range ]
131107 }
132108 }
133109}
0 commit comments