Skip to content

Commit faa7f40

Browse files
Refactor
1 parent e74b196 commit faa7f40

File tree

6 files changed

+57
-46
lines changed

6 files changed

+57
-46
lines changed

Public/js/views/debugger_highlighter.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,35 @@ export default class DebuggerHighlighter {
2121

2222
for (const trace of traces) {
2323
const className = "debuggermatch";
24-
const location = Editor.calcRangePos(
25-
this.editor,
26-
trace.location.start,
27-
trace.location.end - trace.location.start
28-
);
29-
marks.push(
30-
doc.markText(location.startPos, location.endPos, {
31-
className: className,
32-
})
33-
);
34-
35-
if (trace.location.start === trace.location.end) {
24+
25+
if (trace.location.start !== trace.location.end) {
26+
const location = Editor.calcRangePos(
27+
this.editor,
28+
trace.location.start,
29+
trace.location.end - trace.location.start
30+
);
31+
32+
marks.push(
33+
doc.markText(location.startPos, location.endPos, {
34+
className: className,
35+
})
36+
);
37+
} else {
3638
const pos = doc.posFromIndex(trace.location.start);
3739

3840
const widget = document.createElement("span");
3941
widget.className = className;
4042

41-
widget.style.position = "absolute";
42-
widget.style.zIndex = "10";
43-
4443
widget.style.height = `${defaultTextHeight * 1.5}px`;
4544
widget.style.width = "1px";
45+
widget.style.zIndex = "10";
46+
47+
this.editor.addWidget(pos, widget);
4648

4749
const coords = editor.charCoords(pos, "local");
4850
widget.style.left = `${coords.left}px`;
4951
widget.style.top = `${coords.top + 2}px`;
5052

51-
editor.getWrapperElement().appendChild(widget);
52-
5353
this.widgets.push(widget);
5454
}
5555
}
@@ -60,17 +60,16 @@ export default class DebuggerHighlighter {
6060
const widget = document.createElement("span");
6161
widget.className = "debuggerbacktrack";
6262

63-
widget.style.position = "absolute";
64-
widget.style.zIndex = "10";
6563
widget.style.height = `${defaultTextHeight * 1.5}px`;
6664
widget.style.width = `${editor.defaultCharWidth()}px`;
65+
widget.style.zIndex = "10";
66+
67+
this.editor.addWidget(pos, widget);
6768

6869
const coords = editor.charCoords(pos, "local");
6970
widget.style.left = `${coords.left}px`;
7071
widget.style.top = `${coords.top + 2}px`;
7172

72-
editor.getWrapperElement().appendChild(widget);
73-
7473
this.widgets.push(widget);
7574
}
7675
});
@@ -85,7 +84,7 @@ export default class DebuggerHighlighter {
8584
marks.length = 0;
8685

8786
for (const widget of this.widgets) {
88-
widget.remove();
87+
widget.parentNode.removeChild(widget);
8988
}
9089
this.widgets.length = 0;
9190
});

Public/js/views/test_highlighter.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export default class TestHighlighter extends EventDispatcher {
9393
addLeftAnchor(location, attributes = {}) {
9494
const widget = document.createElement("span");
9595
widget.className = "match-left";
96+
9697
widget.style.height = `${this.textHeight * 1.5}px`;
9798
widget.style.width = "1px";
9899
widget.style.zIndex = "10";
@@ -113,6 +114,7 @@ export default class TestHighlighter extends EventDispatcher {
113114
addRightAnchor(location, attributes = {}) {
114115
const widget = document.createElement("span");
115116
widget.className = "match-right";
117+
116118
widget.style.height = `${this.textHeight * 1.5}px`;
117119
widget.style.width = "1px";
118120
widget.style.zIndex = "10";

Sources/App/Debugger/Context.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ import Foundation
22

33
extension Debugger {
44
class Context {
5-
static let shared = Context()
6-
75
var instructions: [String] = []
86
var programCounter = 0
97

@@ -18,7 +16,7 @@ extension Debugger {
1816
var resets = 0
1917
var backtracks = 0
2018

21-
private init(stepCount: Int = 0, breakPoint: Int? = nil) {
19+
init(stepCount: Int = 0, breakPoint: Int? = nil) {
2220
self.stepCount = stepCount
2321
self.breakPoint = breakPoint
2422
}

Sources/App/Debugger/Debugger.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Foundation
33
@testable @_spi(RegexBenchmark) import _StringProcessing
44

55
struct Debugger {
6-
func run(pattern: String, text: String, matchingOptions: [String] = []) throws {
6+
func run(pattern: String, text: String, matchingOptions: [String] = [], context: Debugger.Context) throws {
77
let ast = try _RegexParser.parse(pattern, .traditional)
88

99
var sequence = [AST.MatchingOption]()
@@ -32,7 +32,7 @@ struct Debugger {
3232

3333
let program = try compile(ast, options: options)
3434

35-
Debugger.Context.shared.instructions = program.instructions.map {
35+
context.instructions = program.instructions.map {
3636
$0.description
3737
}
3838

@@ -49,7 +49,8 @@ struct Debugger {
4949
do {
5050
_ = try Executor<AnyRegexOutput>._firstMatch(
5151
program,
52-
using: &cpu
52+
using: &cpu,
53+
context: context
5354
)
5455
} catch {}
5556
}

Sources/App/Debugger/Executor.swift

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,42 @@ enum Executor<Output> {
66
_ program: MEProgram,
77
_ input: String,
88
subjectBounds: Range<String.Index>,
9-
searchBounds: Range<String.Index>
9+
searchBounds: Range<String.Index>,
10+
context: Debugger.Context
1011
) throws -> Regex<Output>.Match? {
1112
try Executor._run(
1213
program,
1314
input,
1415
subjectBounds: subjectBounds,
1516
searchBounds: searchBounds,
16-
mode: .partialFromFront)
17+
mode: .partialFromFront,
18+
context: context
19+
)
1720
}
1821

1922
static func wholeMatch(
2023
_ program: MEProgram,
2124
_ input: String,
2225
subjectBounds: Range<String.Index>,
23-
searchBounds: Range<String.Index>
26+
searchBounds: Range<String.Index>,
27+
context: Debugger.Context
2428
) throws -> Regex<Output>.Match? {
2529
try Executor._run(
2630
program,
2731
input,
2832
subjectBounds: subjectBounds,
2933
searchBounds: searchBounds,
30-
mode: .wholeString)
34+
mode: .wholeString,
35+
context: context
36+
)
3137
}
3238

3339
static func firstMatch(
3440
_ program: MEProgram,
3541
_ input: String,
3642
subjectBounds: Range<String.Index>,
37-
searchBounds: Range<String.Index>
43+
searchBounds: Range<String.Index>,
44+
context: Debugger.Context
3845
) throws -> Regex<Output>.Match? {
3946
var cpu = Processor(
4047
program: program,
@@ -45,19 +52,22 @@ enum Executor<Output> {
4552
)
4653
return try Executor._firstMatch(
4754
program,
48-
using: &cpu)
55+
using: &cpu,
56+
context: context
57+
)
4958
}
5059

5160
static func _firstMatch(
5261
_ program: MEProgram,
53-
using cpu: inout Processor
62+
using cpu: inout Processor,
63+
context: Debugger.Context
5464
) throws -> Regex<Output>.Match? {
5565
let isGraphemeSemantic = program.initialOptions.semanticLevel == .graphemeCluster
5666

5767
var low = cpu.searchBounds.lowerBound
5868
let high = cpu.searchBounds.upperBound
5969
while true {
60-
if let m = try Executor._run(program, &cpu) {
70+
if let m = try Executor._run(program, &cpu, context) {
6171
return m
6272
}
6373
// Fast-path for start-anchored regex
@@ -84,24 +94,26 @@ extension Executor {
8494
_ input: String,
8595
subjectBounds: Range<String.Index>,
8696
searchBounds: Range<String.Index>,
87-
mode: MatchMode
97+
mode: MatchMode,
98+
context: Debugger.Context
8899
) throws -> Regex<Output>.Match? {
89100
var cpu = Processor(
90101
program: program,
91102
input: input,
92103
subjectBounds: subjectBounds,
93104
searchBounds: searchBounds,
94105
matchMode: mode)
95-
return try _run(program, &cpu)
106+
return try _run(program, &cpu, context)
96107
}
97108

98109
static func _run(
99110
_ program: MEProgram,
100-
_ cpu: inout Processor
111+
_ cpu: inout Processor,
112+
_ context: Debugger.Context
101113
) throws -> Regex<Output>.Match? {
102114
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 {
105117
return nil
106118
}
107119
let range = startPosition..<endIdx
@@ -113,7 +125,7 @@ extension Executor {
113125
}}
114126

115127
extension Processor {
116-
fileprivate mutating func run() throws -> Input.Index? {
128+
fileprivate mutating func run(_ context: Debugger.Context) throws -> Input.Index? {
117129
if self.state == .fail {
118130
if let e = failureReason {
119131
throw e
@@ -122,7 +134,6 @@ extension Processor {
122134
}
123135
assert(isReset())
124136
while true {
125-
let context = Debugger.Context.shared
126137
context.programCounter = controller.pc.rawValue
127138

128139
switch self.state {

Sources/App/routes.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,16 @@ func routes(_ app: Application) throws {
127127
}
128128

129129
func debug(pattern: String, text: String, matchOptions: [String], step: String?) throws -> ResultResponse {
130+
let context = Debugger.Context()
131+
130132
func run(pattern: String, text: String, matchingOptions: [String] = [], until step: Int? = nil) throws {
131-
let context = Debugger.Context.shared
132133
context.reset()
133134
context.breakPoint = step
134135

135136
let debugger = Debugger()
136-
try debugger.run(pattern: pattern, text: text, matchingOptions: matchingOptions)
137+
try debugger.run(pattern: pattern, text: text, matchingOptions: matchingOptions, context: context)
137138
}
138139

139-
let context = Debugger.Context.shared
140140
let breakPoint: Int?
141141
if let step {
142142
breakPoint = Int(step)

0 commit comments

Comments
 (0)