diff --git a/Sources/Exercises/Participants/RegexParticipant.swift b/Sources/Exercises/Participants/RegexParticipant.swift index 300832a7f..df615c5df 100644 --- a/Sources/Exercises/Participants/RegexParticipant.swift +++ b/Sources/Exercises/Participants/RegexParticipant.swift @@ -67,16 +67,16 @@ private func graphemeBreakPropertyData( forLine line: String ) -> GraphemeBreakEntry? { graphemeBreakPropertyData(forLine: line, using: Regex { - OneOrMore(CharacterClass.hexDigit).capture() + OneOrMore(.hexDigit).capture() Optionally { ".." - OneOrMore(CharacterClass.hexDigit).capture() + OneOrMore(.hexDigit).capture() } - OneOrMore(CharacterClass.whitespace) + OneOrMore(.whitespace) ";" - OneOrMore(CharacterClass.whitespace) - OneOrMore(CharacterClass.word).capture() - Repeat(CharacterClass.any) + OneOrMore(.whitespace) + OneOrMore(.word).capture() + Repeat(.any) }) } diff --git a/Sources/_StringProcessing/RegexDSL/Core.swift b/Sources/_StringProcessing/RegexDSL/Core.swift index 9ff07de88..6907311b3 100644 --- a/Sources/_StringProcessing/RegexDSL/Core.swift +++ b/Sources/_StringProcessing/RegexDSL/Core.swift @@ -27,6 +27,28 @@ public protocol RegexProtocol { var regex: Regex { get } } +/// A `RegexProtocol` that has a single component child. +/// +/// This protocol adds an init supporting static lookup for character classes +public protocol RegexProtocolWithComponent: RegexProtocol { + associatedtype Component: RegexProtocol + + // Label needed for disambiguation + init(component: Component) +} +extension RegexProtocolWithComponent +where Component == CharacterClass { + // This gives us static member lookup + public init(_ component: Component) { + self.init(component: component) + } +} +extension RegexProtocolWithComponent { + public init(_ component: Component) { + self.init(component: component) + } +} + /// A regular expression. public struct Regex: RegexProtocol { /// A program representation that caches any lowered representation for diff --git a/Sources/_StringProcessing/RegexDSL/DSL.swift b/Sources/_StringProcessing/RegexDSL/DSL.swift index 2ffb1b3c6..9fea6d553 100644 --- a/Sources/_StringProcessing/RegexDSL/DSL.swift +++ b/Sources/_StringProcessing/RegexDSL/DSL.swift @@ -60,12 +60,12 @@ extension CharacterClass: RegexProtocol { // MARK: Repetition /// A regular expression. -public struct OneOrMore: RegexProtocol { +public struct OneOrMore: RegexProtocolWithComponent { public typealias Match = Tuple2 public let regex: Regex - public init(_ component: Component) { + public init(component: Component) { self.regex = .init(ast: oneOrMore(.eager, component.regex.ast) ) @@ -86,12 +86,12 @@ public postfix func .+ ( public struct Repeat< Component: RegexProtocol ->: RegexProtocol { +>: RegexProtocolWithComponent { public typealias Match = Tuple2 public let regex: Regex - public init(_ component: Component) { + public init(component: Component) { self.regex = .init(ast: zeroOrMore(.eager, component.regex.ast)) } @@ -109,12 +109,12 @@ public postfix func .* ( .init(lhs) } -public struct Optionally: RegexProtocol { +public struct Optionally: RegexProtocolWithComponent { public typealias Match = Tuple2 public let regex: Regex - public init(_ component: Component) { + public init(component: Component) { self.regex = .init(ast: zeroOrOne(.eager, component.regex.ast)) }