Skip to content

Add in static lookup for character classes in result builders #115

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
Jan 16, 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
12 changes: 6 additions & 6 deletions Sources/Exercises/Participants/RegexParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand Down
22 changes: 22 additions & 0 deletions Sources/_StringProcessing/RegexDSL/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ public protocol RegexProtocol {
var regex: Regex<Match> { 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<Match: MatchProtocol>: RegexProtocol {
/// A program representation that caches any lowered representation for
Expand Down
12 changes: 6 additions & 6 deletions Sources/_StringProcessing/RegexDSL/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ extension CharacterClass: RegexProtocol {
// MARK: Repetition

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

public let regex: Regex<Match>

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

public struct Repeat<
Component: RegexProtocol
>: RegexProtocol {
>: RegexProtocolWithComponent {
public typealias Match = Tuple2<Substring, [Component.Match.Capture]>

public let regex: Regex<Match>

public init(_ component: Component) {
public init(component: Component) {
self.regex = .init(ast:
zeroOrMore(.eager, component.regex.ast))
}
Expand All @@ -109,12 +109,12 @@ public postfix func .* <R: RegexProtocol>(
.init(lhs)
}

public struct Optionally<Component: RegexProtocol>: RegexProtocol {
public struct Optionally<Component: RegexProtocol>: RegexProtocolWithComponent {
public typealias Match = Tuple2<Substring, Component.Match.Capture?>

public let regex: Regex<Match>

public init(_ component: Component) {
public init(component: Component) {
self.regex = .init(ast:
zeroOrOne(.eager, component.regex.ast))
}
Expand Down