Skip to content

Commit b8178c2

Browse files
authored
Merge pull request #403 from rxwei/1
Introduce `One`
2 parents d3ea692 + baf9f22 commit b8178c2

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

Sources/RegexBuilder/DSL.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,19 @@ extension DSLTree.Node {
127127
}
128128
}
129129

130+
/// A regex component that matches exactly one occurrence of its underlying
131+
/// component.
132+
@available(SwiftStdlib 5.7, *)
133+
public struct One<Output>: RegexComponent {
134+
public var regex: Regex<Output>
135+
136+
public init<Component: RegexComponent>(
137+
_ component: Component
138+
) where Component.RegexOutput == Output {
139+
self.regex = component.regex
140+
}
141+
}
142+
130143
@available(SwiftStdlib 5.7, *)
131144
public struct OneOrMore<Output>: _BuiltinRegexComponent {
132145
public var regex: Regex<Output>

Tests/RegexBuilderTests/RegexDSLTests.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class RegexDSLTests: XCTestCase {
6666
("a c", ("a c", " ", "c")),
6767
matchType: (Substring, Substring, Substring).self, ==)
6868
{
69-
.any
69+
One(.any)
7070
Capture(.whitespace) // Substring
7171
Capture("c") // Substring
7272
}
@@ -344,7 +344,7 @@ class RegexDSLTests: XCTestCase {
344344
matchType: (Substring, Substring).self, ==)
345345
{
346346
OneOrMore(.reluctant) {
347-
.word
347+
One(.word)
348348
}.repetitionBehavior(.possessive)
349349
Capture(.digit)
350350
ZeroOrMore(.any)
@@ -615,13 +615,13 @@ class RegexDSLTests: XCTestCase {
615615
func testUnicodeScalarPostProcessing() throws {
616616
let spaces = Regex {
617617
ZeroOrMore {
618-
.whitespace
618+
One(.whitespace)
619619
}
620620
}
621621

622622
let unicodeScalar = Regex {
623623
OneOrMore {
624-
.hexDigit
624+
One(.hexDigit)
625625
}
626626
spaces
627627
}
@@ -637,14 +637,10 @@ class RegexDSLTests: XCTestCase {
637637
spaces
638638

639639
Capture {
640-
OneOrMore {
641-
.word
642-
}
640+
OneOrMore(.word)
643641
}
644642

645-
ZeroOrMore {
646-
.any
647-
}
643+
ZeroOrMore(.any)
648644
}
649645

650646
// Assert the inferred capture type.
@@ -841,7 +837,7 @@ class RegexDSLTests: XCTestCase {
841837
let a = Reference(Substring.self)
842838
ChoiceOf<(Substring, Substring?)> {
843839
Regex {
844-
.word
840+
One(.word)
845841
a
846842
}
847843
Regex {
@@ -890,7 +886,7 @@ class RegexDSLTests: XCTestCase {
890886
let a = Reference(Substring.self)
891887
ChoiceOf<(Substring, Substring?)> {
892888
Regex {
893-
.word
889+
One(.word)
894890
a
895891
}
896892
Regex {

0 commit comments

Comments
 (0)