Skip to content

Throwing customization hooks #261

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 3 commits into from
Apr 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 4 additions & 8 deletions Sources/_RegexParser/Regex/AST/AST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public struct CaptureTransform: Equatable, Hashable, CustomStringConvertible {
self.init(resultType: resultType, closure: .throwing(closure))
}

public func callAsFunction(_ input: Substring) -> Any? {
public func callAsFunction(_ input: Substring) throws -> Any? {
switch closure {
case .nonfailable(let closure):
let result = closure(input)
Expand All @@ -354,13 +354,9 @@ public struct CaptureTransform: Equatable, Hashable, CustomStringConvertible {
assert(type(of: result) == resultType)
return result
case .throwing(let closure):
do {
let result = try closure(input)
assert(type(of: result) == resultType)
return result
} catch {
return nil
}
let result = try closure(input)
assert(type(of: result) == resultType)
return result
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ extension Compiler.ByteCodeGen {
) throws {
let transform = builder.makeTransformFunction {
input, range in
t(input[range])
try t(input[range])
}
builder.buildBeginCapture(cap)
try emitNode(child)
Expand Down
1 change: 1 addition & 0 deletions Sources/_StringProcessing/Engine/Consume.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extension Engine {
}

extension Processor where Input == String {
// TODO: Should we throw here?
mutating func consume() -> Input.Index? {
while true {
switch self.state {
Expand Down
6 changes: 3 additions & 3 deletions Sources/_StringProcessing/Engine/MEProgram.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import _RegexParser
struct MEProgram<Input: Collection> where Input.Element: Equatable {
typealias ConsumeFunction = (Input, Range<Input.Index>) -> Input.Index?
typealias AssertionFunction =
(Input, Input.Index, Range<Input.Index>) -> Bool
(Input, Input.Index, Range<Input.Index>) throws -> Bool
typealias TransformFunction =
(Input, Range<Input.Index>) -> Any?
(Input, Range<Input.Index>) throws -> Any?
typealias MatcherFunction =
(Input, Input.Index, Range<Input.Index>) -> (Input.Index, Any)?
(Input, Input.Index, Range<Input.Index>) throws -> (Input.Index, Any)?

var instructions: InstructionList<Instruction>

Expand Down
53 changes: 38 additions & 15 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ struct Processor<

var state: State = .inProgress

var failureReason: Error? = nil

var isTracingEnabled: Bool

var storedCaptures: Array<_StoredCapture>
Expand Down Expand Up @@ -181,6 +183,13 @@ extension Processor {
registers.ints = intRegisters
}

mutating func abort(_ e: Error? = nil) {
if let e = e {
self.failureReason = e
}
self.state = .fail
}

mutating func tryAccept() {
switch (currentPosition, matchMode) {
// When reaching the end of the match bounds or when we are only doing a
Expand Down Expand Up @@ -355,24 +364,34 @@ extension Processor {
case .assertBy:
let reg = payload.assertion
let assertion = registers[reg]
guard assertion(input, currentPosition, bounds) else {
signalFailure()
do {
guard try assertion(input, currentPosition, bounds) else {
signalFailure()
return
}
} catch {
abort(error)
return
}
controller.step()

case .matchBy:
let (matcherReg, valReg) = payload.pairedMatcherValue
let matcher = registers[matcherReg]
guard let (nextIdx, val) = matcher(
input, currentPosition, bounds
) else {
signalFailure()
do {
guard let (nextIdx, val) = try matcher(
input, currentPosition, bounds
) else {
signalFailure()
return
}
registers[valReg] = val
advance(to: nextIdx)
controller.step()
} catch {
abort(error)
return
}
registers[valReg] = val
advance(to: nextIdx)
controller.step()

case .print:
// TODO: Debug stream
Expand Down Expand Up @@ -431,14 +450,18 @@ extension Processor {
fatalError(
"Unreachable: transforming without a capture")
}
// FIXME: Pass input or the slice?
guard let value = transform(input, range) else {
signalFailure()
do {
// FIXME: Pass input or the slice?
guard let value = try transform(input, range) else {
signalFailure()
return
}
storedCaptures[capNum].registerValue(value)
controller.step()
} catch {
abort(error)
return
}
storedCaptures[capNum].registerValue(value)

controller.step()

case .captureValue:
let (val, cap) = payload.pairedValueCapture
Expand Down
3 changes: 3 additions & 0 deletions Sources/_StringProcessing/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ struct Executor {
input: input, bounds: inputRange, matchMode: mode)

guard let endIdx = cpu.consume() else {
if let e = cpu.failureReason {
throw e
}
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/Regex/DSLTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,14 @@ extension DSLTree {
@_spi(RegexBuilder)
public typealias _ConsumerInterface = (
String, Range<String.Index>
) -> String.Index?
) throws -> String.Index?

// Type producing consume
// TODO: better name
@_spi(RegexBuilder)
public typealias _MatcherInterface = (
String, String.Index, Range<String.Index>
) -> (String.Index, Any)?
) throws -> (String.Index, Any)?

// Character-set (post grapheme segmentation)
@_spi(RegexBuilder)
Expand Down
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/Regex/Match.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ extension RegexComponent {
mode: MatchMode = .wholeString
) throws -> Regex<Output>.Match? {
let executor = Executor(program: regex.program.loweredProgram)
return try executor.match(input, in: inputRange, mode)
return try executor.match(input, in: inputRange, mode)
}

func _firstMatch(
Expand Down
51 changes: 51 additions & 0 deletions Tests/RegexBuilderTests/CustomTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,55 @@ class CustomRegexComponentTests: XCTestCase {
XCTAssertEqual(res4.output.0, "123")
XCTAssertEqual(res4.output.1, 3)
}

func testRegexAbort() {

enum Radix: Hashable {
case dot
case comma
}
struct Abort: Error, Hashable {}

let hexRegex = Regex {
Capture { OneOrMore(.hexDigit) }
TryCapture { CharacterClass.any } transform: { c -> Radix? in
switch c {
case ".": return Radix.dot
case ",": return Radix.comma
case "❗️":
// Malicious! Toxic levels of emphasis detected.
throw Abort()
default:
// Not a radix
return nil
}
}
Capture { OneOrMore(.hexDigit) }
}
// hexRegex: Regex<(Substring, Substring, Radix?, Substring)>
// TODO: Why is Radix optional?

do {
guard let m = try hexRegex.matchWhole("123aef.345") else {
XCTFail()
return
}
XCTAssertEqual(m.0, "123aef.345")
XCTAssertEqual(m.1, "123aef")
XCTAssertEqual(m.2, .dot)
XCTAssertEqual(m.3, "345")
} catch {
XCTFail()
}

do {
_ = try hexRegex.matchWhole("123aef❗️345")
XCTFail()
} catch let e as Abort {
XCTAssertEqual(e, Abort())
} catch {
XCTFail()
}

}
}