Skip to content

[gardening] Cleanup/simplify Executor codepaths #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Sources/_StringProcessing/Capture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ extension StructuredCapture {
value: storedCapture?.value,
optionalCount: optionalCount)
}

func slice(from input: String) -> Substring? {
guard let r = storedCapture?.range else { return nil }
return input[r]
}
}

extension Sequence where Element == StructuredCapture {
Expand All @@ -86,5 +91,10 @@ extension Sequence where Element == StructuredCapture {
})
return TypeConstruction.tuple(of: caps)
}

func slices(from input: String) -> [Substring?] {
self.map { $0.slice(from: input) }
}
}


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary trailing newline?

8 changes: 1 addition & 7 deletions Sources/_StringProcessing/Engine/Consume.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,10 @@ extension Engine {
}

extension Engine where Input == String {
func consume(
_ input: Input
) -> (Input.Index, CaptureList)? {
consume(input, in: input.startIndex ..< input.endIndex)
}

func consume(
_ input: Input,
in range: Range<Input.Index>,
matchMode: MatchMode = .partialFromFront
matchMode: MatchMode
) -> (Input.Index, CaptureList)? {
if enableTracing {
print("Consume: \(input)")
Expand Down
76 changes: 20 additions & 56 deletions Sources/_StringProcessing/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,69 +19,33 @@ struct Executor {
self.engine = Engine(program, enableTracing: enablesTracing)
}

struct Result {
var range: Range<String.Index>
var captures: [StructuredCapture]
var referencedCaptureOffsets: [ReferenceID: Int]

var destructure: (
matched: Range<String.Index>,
captures: [StructuredCapture],
referencedCaptureOffsets: [ReferenceID: Int]
) {
(range, captures, referencedCaptureOffsets)
}

init(
_ matched: Range<String.Index>, _ captures: [StructuredCapture],
_ referencedCaptureOffsets: [ReferenceID: Int]
) {
self.range = matched
self.captures = captures
self.referencedCaptureOffsets = referencedCaptureOffsets
}
}

func execute(
input: String,
in range: Range<String.Index>,
mode: MatchMode = .wholeString
) -> Result? {
func match<Match>(
_ input: String,
in inputRange: Range<String.Index>,
_ mode: MatchMode
) throws -> RegexMatch<Match>? {
guard let (endIdx, capList) = engine.consume(
input, in: range, matchMode: mode
input, in: inputRange, matchMode: mode
) else {
return nil
}
let capStruct = engine.program.captureStructure
do {
let range = range.lowerBound..<endIdx

let caps = try capStruct.structuralize(
let range = inputRange.lowerBound..<endIdx
let caps = try capStruct.structuralize(
capList, input)
return Result(range, caps, capList.referencedCaptureOffsets)
} catch {
fatalError(String(describing: error))
}
}
func execute(
input: Substring,
mode: MatchMode = .wholeString
) -> Result? {
self.execute(
input: input.base,
in: input.startIndex..<input.endIndex,
mode: mode)

return RegexMatch(
input: input,
range: range,
rawCaptures: caps,
referencedCaptureOffsets: capList.referencedCaptureOffsets)
}

func executeFlat(
input: String,
in range: Range<String.Index>,
mode: MatchMode = .wholeString
) -> (Range<String.Index>, CaptureList)? {
engine.consume(
input, in: range, matchMode: mode
).map { endIndex, capture in
(range.lowerBound..<endIndex, capture)
}
func dynamicMatch(
_ input: String,
in inputRange: Range<String.Index>,
_ mode: MatchMode
) throws -> RegexMatch<(Substring, DynamicCaptures)>? {
try match(input, in: inputRange, mode)
}
}
13 changes: 4 additions & 9 deletions Sources/_StringProcessing/RegexDSL/Match.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,11 @@ extension RegexProtocol {
mode: MatchMode = .wholeString
) -> RegexMatch<Match>? {
let executor = Executor(program: regex.program.loweredProgram)
guard let (range, captures, captureOffsets) = executor.execute(
input: input, in: inputRange, mode: mode
)?.destructure else {
return nil
do {
return try executor.match(input, in: inputRange, mode)
} catch {
fatalError(String(describing: error))
}
return RegexMatch(
input: input,
range: range,
rawCaptures: captures,
referencedCaptureOffsets: captureOffsets)
}
}

Expand Down
Loading