@@ -6,35 +6,42 @@ enum Executor<Output> {
6
6
_ program: MEProgram ,
7
7
_ input: String ,
8
8
subjectBounds: Range < String . Index > ,
9
- searchBounds: Range < String . Index >
9
+ searchBounds: Range < String . Index > ,
10
+ context: Debugger . Context
10
11
) throws -> Regex < Output > . Match ? {
11
12
try Executor . _run (
12
13
program,
13
14
input,
14
15
subjectBounds: subjectBounds,
15
16
searchBounds: searchBounds,
16
- mode: . partialFromFront)
17
+ mode: . partialFromFront,
18
+ context: context
19
+ )
17
20
}
18
21
19
22
static func wholeMatch(
20
23
_ program: MEProgram ,
21
24
_ input: String ,
22
25
subjectBounds: Range < String . Index > ,
23
- searchBounds: Range < String . Index >
26
+ searchBounds: Range < String . Index > ,
27
+ context: Debugger . Context
24
28
) throws -> Regex < Output > . Match ? {
25
29
try Executor . _run (
26
30
program,
27
31
input,
28
32
subjectBounds: subjectBounds,
29
33
searchBounds: searchBounds,
30
- mode: . wholeString)
34
+ mode: . wholeString,
35
+ context: context
36
+ )
31
37
}
32
38
33
39
static func firstMatch(
34
40
_ program: MEProgram ,
35
41
_ input: String ,
36
42
subjectBounds: Range < String . Index > ,
37
- searchBounds: Range < String . Index >
43
+ searchBounds: Range < String . Index > ,
44
+ context: Debugger . Context
38
45
) throws -> Regex < Output > . Match ? {
39
46
var cpu = Processor (
40
47
program: program,
@@ -45,19 +52,22 @@ enum Executor<Output> {
45
52
)
46
53
return try Executor . _firstMatch (
47
54
program,
48
- using: & cpu)
55
+ using: & cpu,
56
+ context: context
57
+ )
49
58
}
50
59
51
60
static func _firstMatch(
52
61
_ program: MEProgram ,
53
- using cpu: inout Processor
62
+ using cpu: inout Processor ,
63
+ context: Debugger . Context
54
64
) throws -> Regex < Output > . Match ? {
55
65
let isGraphemeSemantic = program. initialOptions. semanticLevel == . graphemeCluster
56
66
57
67
var low = cpu. searchBounds. lowerBound
58
68
let high = cpu. searchBounds. upperBound
59
69
while true {
60
- if let m = try Executor . _run ( program, & cpu) {
70
+ if let m = try Executor . _run ( program, & cpu, context ) {
61
71
return m
62
72
}
63
73
// Fast-path for start-anchored regex
@@ -84,24 +94,26 @@ extension Executor {
84
94
_ input: String ,
85
95
subjectBounds: Range < String . Index > ,
86
96
searchBounds: Range < String . Index > ,
87
- mode: MatchMode
97
+ mode: MatchMode ,
98
+ context: Debugger . Context
88
99
) throws -> Regex < Output > . Match ? {
89
100
var cpu = Processor (
90
101
program: program,
91
102
input: input,
92
103
subjectBounds: subjectBounds,
93
104
searchBounds: searchBounds,
94
105
matchMode: mode)
95
- return try _run ( program, & cpu)
106
+ return try _run ( program, & cpu, context )
96
107
}
97
108
98
109
static func _run(
99
110
_ program: MEProgram ,
100
- _ cpu: inout Processor
111
+ _ cpu: inout Processor ,
112
+ _ context: Debugger . Context
101
113
) throws -> Regex < Output > . Match ? {
102
114
let startPosition = cpu. currentPosition
103
- Debugger . Context . shared . start = startPosition. utf16Offset ( in: cpu. input)
104
- guard let endIdx = try cpu. run ( ) else {
115
+ context . start = startPosition. utf16Offset ( in: cpu. input)
116
+ guard let endIdx = try cpu. run ( context ) else {
105
117
return nil
106
118
}
107
119
let range = startPosition..< endIdx
@@ -113,7 +125,7 @@ extension Executor {
113
125
} }
114
126
115
127
extension Processor {
116
- fileprivate mutating func run( ) throws -> Input . Index ? {
128
+ fileprivate mutating func run( _ context : Debugger . Context ) throws -> Input . Index ? {
117
129
if self . state == . fail {
118
130
if let e = failureReason {
119
131
throw e
@@ -122,7 +134,6 @@ extension Processor {
122
134
}
123
135
assert ( isReset ( ) )
124
136
while true {
125
- let context = Debugger . Context. shared
126
137
context. programCounter = controller. pc. rawValue
127
138
128
139
switch self . state {
0 commit comments