Skip to content

Allow CustomConsuming types to match w/ zero width #479

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
Jun 17, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/Engine/Processor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ extension Processor {
mutating func advance(to nextIndex: Input.Index) {
assert(nextIndex >= bounds.lowerBound)
assert(nextIndex <= bounds.upperBound)
assert(nextIndex > currentPosition)
assert(nextIndex >= currentPosition)
Copy link
Member

Choose a reason for hiding this comment

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

Is this the better change, or is it better to not call advance(to:) if there's nowhere to advance to?

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't seem necessary to require the precondition for this method, given that a zero-width "advance" is a reasonable output for a consumer/other regex component. Would it sound better to you to rename this to continue(at:)?

Copy link
Member

Choose a reason for hiding this comment

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

I'd prefer changing the name if we're changing the meaning here. That way the call sites are clearer that we're not guaranteeing forward progress (e.g. empty matches)

currentPosition = nextIndex
}

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