Skip to content
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
18 changes: 11 additions & 7 deletions Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,15 @@ extension Processor {
return true
}

mutating func advance(to nextIndex: Input.Index) {
assert(nextIndex >= bounds.lowerBound)
assert(nextIndex <= bounds.upperBound)
assert(nextIndex > currentPosition)
currentPosition = nextIndex
/// Continue matching at the specified index.
///
/// - Precondition: `bounds.contains(index) || index == bounds.upperBound`
/// - Precondition: `index >= currentPosition`
mutating func resume(at index: Input.Index) {
assert(index >= bounds.lowerBound)
assert(index <= bounds.upperBound)
assert(index >= currentPosition)
Copy link
Member

Choose a reason for hiding this comment

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

Why is this assertion here?

currentPosition = index
}

func doPrint(_ s: String) {
Expand Down Expand Up @@ -358,7 +362,7 @@ extension Processor {
signalFailure()
return
}
advance(to: nextIndex)
resume(at: nextIndex)
controller.step()

case .assertBy:
Expand Down Expand Up @@ -386,7 +390,7 @@ extension Processor {
return
}
registers[valReg] = val
advance(to: nextIdx)
resume(at: nextIdx)
controller.step()
} catch {
abort(error)
Expand Down
36 changes: 36 additions & 0 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,42 @@ class RegexDSLTests: XCTestCase {
XCTAssertEqual(str.wholeMatch(of: parser)?.1, version)
}
}

func testZeroWidthConsumer() throws {
struct Trace: CustomConsumingRegexComponent {
typealias RegexOutput = Void
var label: String
init(_ label: String) { self.label = label }

static var traceOutput = ""

func consuming(_ input: String, startingAt index: String.Index, in bounds: Range<String.Index>) throws -> (upperBound: String.Index, output: Void)? {
print("Matching '\(label)'", to: &Self.traceOutput)
print(input, to: &Self.traceOutput)
let dist = input.distance(from: input.startIndex, to: index)
print(String(repeating: " ", count: dist) + "^", to: &Self.traceOutput)
return (index, ())
}
}

let regex = Regex {
OneOrMore(.word)
Trace("end of key")
":"
Trace("start of value")
OneOrMore(.word)
}
XCTAssertNotNil("hello:goodbye".firstMatch(of: regex))
XCTAssertEqual(Trace.traceOutput, """
Matching 'end of key'
hello:goodbye
^
Matching 'start of value'
hello:goodbye
^

""")
}
}

extension Unicode.Scalar {
Expand Down