Skip to content

Introduce One #403

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
May 12, 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
13 changes: 13 additions & 0 deletions Sources/RegexBuilder/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ extension DSLTree.Node {
}
}

/// A regex component that matches exactly one occurrence of its underlying
/// component.
@available(SwiftStdlib 5.7, *)
public struct One<Output>: RegexComponent {
public var regex: Regex<Output>

public init<Component: RegexComponent>(
_ component: Component
) where Component.RegexOutput == Output {
self.regex = component.regex
}
}

@available(SwiftStdlib 5.7, *)
public struct OneOrMore<Output>: _BuiltinRegexComponent {
public var regex: Regex<Output>
Expand Down
20 changes: 8 additions & 12 deletions Tests/RegexBuilderTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class RegexDSLTests: XCTestCase {
("a c", ("a c", " ", "c")),
matchType: (Substring, Substring, Substring).self, ==)
{
.any
One(.any)
Capture(.whitespace) // Substring
Capture("c") // Substring
}
Expand Down Expand Up @@ -344,7 +344,7 @@ class RegexDSLTests: XCTestCase {
matchType: (Substring, Substring).self, ==)
{
OneOrMore(.reluctant) {
.word
One(.word)
}.repetitionBehavior(.possessive)
Capture(.digit)
ZeroOrMore(.any)
Expand Down Expand Up @@ -615,13 +615,13 @@ class RegexDSLTests: XCTestCase {
func testUnicodeScalarPostProcessing() throws {
let spaces = Regex {
ZeroOrMore {
.whitespace
One(.whitespace)
}
}

let unicodeScalar = Regex {
OneOrMore {
.hexDigit
One(.hexDigit)
}
spaces
}
Expand All @@ -637,14 +637,10 @@ class RegexDSLTests: XCTestCase {
spaces

Capture {
OneOrMore {
.word
}
OneOrMore(.word)
}

ZeroOrMore {
.any
}
ZeroOrMore(.any)
}

// Assert the inferred capture type.
Expand Down Expand Up @@ -841,7 +837,7 @@ class RegexDSLTests: XCTestCase {
let a = Reference(Substring.self)
ChoiceOf<(Substring, Substring?)> {
Regex {
.word
One(.word)
a
}
Regex {
Expand Down Expand Up @@ -890,7 +886,7 @@ class RegexDSLTests: XCTestCase {
let a = Reference(Substring.self)
ChoiceOf<(Substring, Substring?)> {
Regex {
.word
One(.word)
a
}
Regex {
Expand Down