Skip to content

Commit 1f37d34

Browse files
authored
Add in static lookup for character classes in result builders (#115)
1 parent 3902577 commit 1f37d34

File tree

3 files changed

+34
-12
lines changed

3 files changed

+34
-12
lines changed

Sources/Exercises/Participants/RegexParticipant.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,16 @@ private func graphemeBreakPropertyData(
6767
forLine line: String
6868
) -> GraphemeBreakEntry? {
6969
graphemeBreakPropertyData(forLine: line, using: Regex {
70-
OneOrMore(CharacterClass.hexDigit).capture()
70+
OneOrMore(.hexDigit).capture()
7171
Optionally {
7272
".."
73-
OneOrMore(CharacterClass.hexDigit).capture()
73+
OneOrMore(.hexDigit).capture()
7474
}
75-
OneOrMore(CharacterClass.whitespace)
75+
OneOrMore(.whitespace)
7676
";"
77-
OneOrMore(CharacterClass.whitespace)
78-
OneOrMore(CharacterClass.word).capture()
79-
Repeat(CharacterClass.any)
77+
OneOrMore(.whitespace)
78+
OneOrMore(.word).capture()
79+
Repeat(.any)
8080
})
8181
}
8282

Sources/_StringProcessing/RegexDSL/Core.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,28 @@ public protocol RegexProtocol {
2727
var regex: Regex<Match> { get }
2828
}
2929

30+
/// A `RegexProtocol` that has a single component child.
31+
///
32+
/// This protocol adds an init supporting static lookup for character classes
33+
public protocol RegexProtocolWithComponent: RegexProtocol {
34+
associatedtype Component: RegexProtocol
35+
36+
// Label needed for disambiguation
37+
init(component: Component)
38+
}
39+
extension RegexProtocolWithComponent
40+
where Component == CharacterClass {
41+
// This gives us static member lookup
42+
public init(_ component: Component) {
43+
self.init(component: component)
44+
}
45+
}
46+
extension RegexProtocolWithComponent {
47+
public init(_ component: Component) {
48+
self.init(component: component)
49+
}
50+
}
51+
3052
/// A regular expression.
3153
public struct Regex<Match: MatchProtocol>: RegexProtocol {
3254
/// A program representation that caches any lowered representation for

Sources/_StringProcessing/RegexDSL/DSL.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ extension CharacterClass: RegexProtocol {
6060
// MARK: Repetition
6161

6262
/// A regular expression.
63-
public struct OneOrMore<Component: RegexProtocol>: RegexProtocol {
63+
public struct OneOrMore<Component: RegexProtocol>: RegexProtocolWithComponent {
6464
public typealias Match = Tuple2<Substring, [Component.Match.Capture]>
6565

6666
public let regex: Regex<Match>
6767

68-
public init(_ component: Component) {
68+
public init(component: Component) {
6969
self.regex = .init(ast:
7070
oneOrMore(.eager, component.regex.ast)
7171
)
@@ -86,12 +86,12 @@ public postfix func .+ <R: RegexProtocol>(
8686

8787
public struct Repeat<
8888
Component: RegexProtocol
89-
>: RegexProtocol {
89+
>: RegexProtocolWithComponent {
9090
public typealias Match = Tuple2<Substring, [Component.Match.Capture]>
9191

9292
public let regex: Regex<Match>
9393

94-
public init(_ component: Component) {
94+
public init(component: Component) {
9595
self.regex = .init(ast:
9696
zeroOrMore(.eager, component.regex.ast))
9797
}
@@ -109,12 +109,12 @@ public postfix func .* <R: RegexProtocol>(
109109
.init(lhs)
110110
}
111111

112-
public struct Optionally<Component: RegexProtocol>: RegexProtocol {
112+
public struct Optionally<Component: RegexProtocol>: RegexProtocolWithComponent {
113113
public typealias Match = Tuple2<Substring, Component.Match.Capture?>
114114

115115
public let regex: Regex<Match>
116116

117-
public init(_ component: Component) {
117+
public init(component: Component) {
118118
self.regex = .init(ast:
119119
zeroOrOne(.eager, component.regex.ast))
120120
}

0 commit comments

Comments
 (0)