Skip to content

Commit 2eb7e4e

Browse files
authored
Cleanup/simplify Executor codepaths (#190)
1 parent 75d4931 commit 2eb7e4e

File tree

7 files changed

+49
-368
lines changed

7 files changed

+49
-368
lines changed

Sources/_StringProcessing/Capture.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ extension StructuredCapture {
7171
value: storedCapture?.value,
7272
optionalCount: optionalCount)
7373
}
74+
75+
func slice(from input: String) -> Substring? {
76+
guard let r = storedCapture?.range else { return nil }
77+
return input[r]
78+
}
7479
}
7580

7681
extension Sequence where Element == StructuredCapture {
@@ -86,5 +91,10 @@ extension Sequence where Element == StructuredCapture {
8691
})
8792
return TypeConstruction.tuple(of: caps)
8893
}
94+
95+
func slices(from input: String) -> [Substring?] {
96+
self.map { $0.slice(from: input) }
97+
}
8998
}
9099

100+

Sources/_StringProcessing/Engine/Consume.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,10 @@ extension Engine {
2525
}
2626

2727
extension Engine where Input == String {
28-
func consume(
29-
_ input: Input
30-
) -> (Input.Index, CaptureList)? {
31-
consume(input, in: input.startIndex ..< input.endIndex)
32-
}
33-
3428
func consume(
3529
_ input: Input,
3630
in range: Range<Input.Index>,
37-
matchMode: MatchMode = .partialFromFront
31+
matchMode: MatchMode
3832
) -> (Input.Index, CaptureList)? {
3933
if enableTracing {
4034
print("Consume: \(input)")

Sources/_StringProcessing/Executor.swift

Lines changed: 20 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,33 @@ struct Executor {
1919
self.engine = Engine(program, enableTracing: enablesTracing)
2020
}
2121

22-
struct Result {
23-
var range: Range<String.Index>
24-
var captures: [StructuredCapture]
25-
var referencedCaptureOffsets: [ReferenceID: Int]
26-
27-
var destructure: (
28-
matched: Range<String.Index>,
29-
captures: [StructuredCapture],
30-
referencedCaptureOffsets: [ReferenceID: Int]
31-
) {
32-
(range, captures, referencedCaptureOffsets)
33-
}
34-
35-
init(
36-
_ matched: Range<String.Index>, _ captures: [StructuredCapture],
37-
_ referencedCaptureOffsets: [ReferenceID: Int]
38-
) {
39-
self.range = matched
40-
self.captures = captures
41-
self.referencedCaptureOffsets = referencedCaptureOffsets
42-
}
43-
}
44-
45-
func execute(
46-
input: String,
47-
in range: Range<String.Index>,
48-
mode: MatchMode = .wholeString
49-
) -> Result? {
22+
func match<Match>(
23+
_ input: String,
24+
in inputRange: Range<String.Index>,
25+
_ mode: MatchMode
26+
) throws -> RegexMatch<Match>? {
5027
guard let (endIdx, capList) = engine.consume(
51-
input, in: range, matchMode: mode
28+
input, in: inputRange, matchMode: mode
5229
) else {
5330
return nil
5431
}
5532
let capStruct = engine.program.captureStructure
56-
do {
57-
let range = range.lowerBound..<endIdx
58-
59-
let caps = try capStruct.structuralize(
33+
let range = inputRange.lowerBound..<endIdx
34+
let caps = try capStruct.structuralize(
6035
capList, input)
61-
return Result(range, caps, capList.referencedCaptureOffsets)
62-
} catch {
63-
fatalError(String(describing: error))
64-
}
65-
}
66-
func execute(
67-
input: Substring,
68-
mode: MatchMode = .wholeString
69-
) -> Result? {
70-
self.execute(
71-
input: input.base,
72-
in: input.startIndex..<input.endIndex,
73-
mode: mode)
36+
37+
return RegexMatch(
38+
input: input,
39+
range: range,
40+
rawCaptures: caps,
41+
referencedCaptureOffsets: capList.referencedCaptureOffsets)
7442
}
7543

76-
func executeFlat(
77-
input: String,
78-
in range: Range<String.Index>,
79-
mode: MatchMode = .wholeString
80-
) -> (Range<String.Index>, CaptureList)? {
81-
engine.consume(
82-
input, in: range, matchMode: mode
83-
).map { endIndex, capture in
84-
(range.lowerBound..<endIndex, capture)
85-
}
44+
func dynamicMatch(
45+
_ input: String,
46+
in inputRange: Range<String.Index>,
47+
_ mode: MatchMode
48+
) throws -> RegexMatch<(Substring, DynamicCaptures)>? {
49+
try match(input, in: inputRange, mode)
8650
}
8751
}

Sources/_StringProcessing/RegexDSL/Match.swift

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,11 @@ extension RegexProtocol {
6969
mode: MatchMode = .wholeString
7070
) -> RegexMatch<Match>? {
7171
let executor = Executor(program: regex.program.loweredProgram)
72-
guard let (range, captures, captureOffsets) = executor.execute(
73-
input: input, in: inputRange, mode: mode
74-
)?.destructure else {
75-
return nil
72+
do {
73+
return try executor.match(input, in: inputRange, mode)
74+
} catch {
75+
fatalError(String(describing: error))
7676
}
77-
return RegexMatch(
78-
input: input,
79-
range: range,
80-
rawCaptures: captures,
81-
referencedCaptureOffsets: captureOffsets)
8277
}
8378
}
8479

0 commit comments

Comments
 (0)