Skip to content

Static lookup of character classes in generic contexts #157

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
Feb 11, 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
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/CharacterClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public struct CharacterClass: Hashable {
}
}

extension CharacterClass {
extension RegexProtocol where Self == CharacterClass {
public static var any: CharacterClass {
.init(cc: .any, matchLevel: .graphemeCluster)
}
Expand Down Expand Up @@ -216,7 +216,7 @@ extension CharacterClass {
}

public static func custom(
_ components: [CharacterSetComponent]
_ components: [CharacterClass.CharacterSetComponent]
) -> CharacterClass {
.init(cc: .custom(components), matchLevel: .graphemeCluster)
}
Expand Down
22 changes: 0 additions & 22 deletions Sources/_StringProcessing/RegexDSL/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,28 +145,6 @@ postfix operator .?
postfix operator .*
postfix operator .+

// Overloads for quantifying over a character class.
public func zeroOrOne(
_ cc: CharacterClass,
_ behavior: QuantificationBehavior = .eagerly
) -> Regex<Substring> {
.init(node: .quantification(.zeroOrOne, behavior.astKind, cc.regex.root))
}

public func many(
_ cc: CharacterClass,
_ behavior: QuantificationBehavior = .eagerly
) -> Regex<Substring> {
.init(node: .quantification(.zeroOrMore, behavior.astKind, cc.regex.root))
}

public func oneOrMore(
_ cc: CharacterClass,
_ behavior: QuantificationBehavior = .eagerly
) -> Regex<Substring> {
.init(node: .quantification(.oneOrMore, behavior.astKind, cc.regex.root))
}

// MARK: Alternation

// TODO: Variadic generics
Expand Down
46 changes: 23 additions & 23 deletions Tests/RegexTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ class RegexDSLTests: XCTestCase {
("a c", ("a c", " ", "c")),
captureType: (Substring, Substring, Substring).self, ==)
{
CharacterClass.any
capture(CharacterClass.whitespace) // Substring
.any
capture(.whitespace) // Substring
capture("c") // Substring
}
}
Expand Down Expand Up @@ -140,7 +140,7 @@ class RegexDSLTests: XCTestCase {
"a".+
capture(oneOrMore(Character("b"))) // Substring
capture(many("c")) // Substring
capture(CharacterClass.hexDigit).* // [Substring]
capture(.hexDigit).* // [Substring]
"e".?
capture("t" | "k") // Substring
oneOf { capture("k"); capture("j") } // (Substring?, Substring?)
Expand All @@ -154,7 +154,7 @@ class RegexDSLTests: XCTestCase {
{
oneOrMore {
oneOrMore(.word)
capture(CharacterClass.digit)
capture(.digit)
}
}

Expand All @@ -164,7 +164,7 @@ class RegexDSLTests: XCTestCase {
{
oneOrMore {
oneOrMore(.word, .reluctantly)
capture(CharacterClass.digit)
capture(.digit)
}
}

Expand All @@ -174,9 +174,9 @@ class RegexDSLTests: XCTestCase {
{
oneOrMore {
oneOrMore(.reluctantly) {
CharacterClass.word
.word
}
capture(CharacterClass.digit)
capture(.digit)
}
}
}
Expand Down Expand Up @@ -293,13 +293,13 @@ class RegexDSLTests: XCTestCase {
func testUnicodeScalarPostProcessing() throws {
let spaces = Regex {
many {
CharacterClass.whitespace
.whitespace
}
}

let unicodeScalar = Regex {
oneOrMore {
CharacterClass.hexDigit
.hexDigit
}
spaces
}
Expand All @@ -316,12 +316,12 @@ class RegexDSLTests: XCTestCase {

capture {
oneOrMore {
CharacterClass.word
.word
}
}

many {
CharacterClass.any
.any
}
}

Expand All @@ -342,19 +342,19 @@ class RegexDSLTests: XCTestCase {

let regexWithCapture = Regex {
capture {
oneOrMore(CharacterClass.hexDigit)
oneOrMore(.hexDigit)
} transform: { Unicode.Scalar(hex: $0) }
optionally {
".."
capture {
oneOrMore(CharacterClass.hexDigit)
oneOrMore(.hexDigit)
} transform: { Unicode.Scalar(hex: $0) }
}
oneOrMore(CharacterClass.whitespace)
oneOrMore(.whitespace)
";"
oneOrMore(CharacterClass.whitespace)
capture(oneOrMore(CharacterClass.word))
many(CharacterClass.any)
oneOrMore(.whitespace)
capture(oneOrMore(.word))
many(.any)
} // Regex<(Substring, Unicode.Scalar?, Unicode.Scalar??, Substring)>
do {
// Assert the inferred capture type.
Expand All @@ -373,23 +373,23 @@ class RegexDSLTests: XCTestCase {

let regexWithTryCapture = Regex {
tryCapture {
oneOrMore(CharacterClass.hexDigit)
oneOrMore(.hexDigit)
} transform: {
Unicode.Scalar(hex: $0)
}
optionally {
".."
tryCapture {
oneOrMore(CharacterClass.hexDigit)
oneOrMore(.hexDigit)
} transform: {
Unicode.Scalar(hex: $0)
}
}
oneOrMore(CharacterClass.whitespace)
oneOrMore(.whitespace)
";"
oneOrMore(CharacterClass.whitespace)
capture(oneOrMore(CharacterClass.word))
many(CharacterClass.any)
oneOrMore(.whitespace)
capture(oneOrMore(.word))
many(.any)
} // Regex<(Substring, Unicode.Scalar, Unicode.Scalar?, Substring)>
do {
// Assert the inferred capture type.
Expand Down