Skip to content

Untangle _RegexParser from RegexBuilder #299

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
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
37 changes: 12 additions & 25 deletions Sources/RegexBuilder/Anchor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _RegexParser
@_implementationOnly import _RegexParser
@_spi(RegexBuilder) import _StringProcessing

@available(SwiftStdlib 5.7, *)
Expand All @@ -31,34 +31,21 @@ public struct Anchor {

@available(SwiftStdlib 5.7, *)
extension Anchor: RegexComponent {
var astAssertion: AST.Atom.AssertionKind {
if !isInverted {
switch kind {
case .startOfSubject: return .startOfSubject
case .endOfSubjectBeforeNewline: return .endOfSubjectBeforeNewline
case .endOfSubject: return .endOfSubject
case .firstMatchingPositionInSubject: return .firstMatchingPositionInSubject
case .textSegmentBoundary: return .textSegment
case .startOfLine: return .startOfLine
case .endOfLine: return .endOfLine
case .wordBoundary: return .wordBoundary
}
} else {
switch kind {
case .startOfSubject: fatalError("Not yet supported")
case .endOfSubjectBeforeNewline: fatalError("Not yet supported")
case .endOfSubject: fatalError("Not yet supported")
case .firstMatchingPositionInSubject: fatalError("Not yet supported")
case .textSegmentBoundary: return .notTextSegment
case .startOfLine: fatalError("Not yet supported")
case .endOfLine: fatalError("Not yet supported")
case .wordBoundary: return .notWordBoundary
}
var baseAssertion: DSLTree._AST.AssertionKind {
switch kind {
case .startOfSubject: return .startOfSubject(isInverted)
case .endOfSubjectBeforeNewline: return .endOfSubjectBeforeNewline(isInverted)
case .endOfSubject: return .endOfSubject(isInverted)
case .firstMatchingPositionInSubject: return .firstMatchingPositionInSubject(isInverted)
case .textSegmentBoundary: return .textSegmentBoundary(isInverted)
case .startOfLine: return .startOfLine(isInverted)
case .endOfLine: return .endOfLine(isInverted)
case .wordBoundary: return .wordBoundary(isInverted)
}
}

public var regex: Regex<Substring> {
Regex(node: .atom(.assertion(astAssertion)))
Regex(node: .atom(.assertion(baseAssertion)))
}
}

Expand Down
61 changes: 5 additions & 56 deletions Sources/RegexBuilder/CharacterClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _RegexParser
@_implementationOnly import _RegexParser
@_spi(RegexBuilder) import _StringProcessing

@available(SwiftStdlib 5.7, *)
Expand All @@ -21,19 +21,10 @@ public struct CharacterClass {
}

init(unconverted model: _CharacterClassModel) {
// FIXME: Implement in DSLTree instead of wrapping an AST atom
switch model.makeAST() {
case .atom(let atom):
self.ccc = .init(members: [.atom(.unconverted(atom))])
default:
fatalError("Unsupported _CharacterClassModel")
guard let ccc = model.makeDSLTreeCharacterClass() else {
fatalError("Unsupported character class")
}
}

init(property: AST.Atom.CharacterProperty) {
// FIXME: Implement in DSLTree instead of wrapping an AST atom
let astAtom = AST.Atom(.property(property), .fake)
self.ccc = .init(members: [.atom(.unconverted(astAtom))])
self.ccc = ccc
}
}

Expand Down Expand Up @@ -119,11 +110,7 @@ extension RegexComponent where Self == CharacterClass {
@available(SwiftStdlib 5.7, *)
extension CharacterClass {
public static func generalCategory(_ category: Unicode.GeneralCategory) -> CharacterClass {
guard let extendedCategory = category.extendedGeneralCategory else {
fatalError("Unexpected general category")
}
return CharacterClass(property:
.init(.generalCategory(extendedCategory), isInverted: false, isPOSIX: false))
return CharacterClass(.generalCategory(category))
}
}

Expand All @@ -144,44 +131,6 @@ public func ...(lhs: UnicodeScalar, rhs: UnicodeScalar) -> CharacterClass {
return CharacterClass(ccc)
}

extension Unicode.GeneralCategory {
var extendedGeneralCategory: Unicode.ExtendedGeneralCategory? {
switch self {
case .uppercaseLetter: return .uppercaseLetter
case .lowercaseLetter: return .lowercaseLetter
case .titlecaseLetter: return .titlecaseLetter
case .modifierLetter: return .modifierLetter
case .otherLetter: return .otherLetter
case .nonspacingMark: return .nonspacingMark
case .spacingMark: return .spacingMark
case .enclosingMark: return .enclosingMark
case .decimalNumber: return .decimalNumber
case .letterNumber: return .letterNumber
case .otherNumber: return .otherNumber
case .connectorPunctuation: return .connectorPunctuation
case .dashPunctuation: return .dashPunctuation
case .openPunctuation: return .openPunctuation
case .closePunctuation: return .closePunctuation
case .initialPunctuation: return .initialPunctuation
case .finalPunctuation: return .finalPunctuation
case .otherPunctuation: return .otherPunctuation
case .mathSymbol: return .mathSymbol
case .currencySymbol: return .currencySymbol
case .modifierSymbol: return .modifierSymbol
case .otherSymbol: return .otherSymbol
case .spaceSeparator: return .spaceSeparator
case .lineSeparator: return .lineSeparator
case .paragraphSeparator: return .paragraphSeparator
case .control: return .control
case .format: return .format
case .surrogate: return .surrogate
case .privateUse: return .privateUse
case .unassigned: return .unassigned
@unknown default: return nil
}
}
}

// MARK: - Set algebra methods

@available(SwiftStdlib 5.7, *)
Expand Down
12 changes: 6 additions & 6 deletions Sources/RegexBuilder/DSL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _RegexParser
@_implementationOnly import _RegexParser
@_spi(RegexBuilder) import _StringProcessing

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -105,7 +105,7 @@ public struct QuantificationBehavior {

var kind: Kind

internal var astKind: AST.Quantification.Kind {
internal var astKind: DSLTree._AST.QuantificationKind {
switch kind {
case .eagerly: return .eager
case .reluctantly: return .reluctant
Expand Down Expand Up @@ -136,13 +136,13 @@ extension DSLTree.Node {
return .quantification(.oneOrMore, kind, node)
case _ where range.count == 1: // ..<1 or ...0 or any range with count == 1
// Note: `behavior` is ignored in this case
return .quantification(.exactly(.init(faking: range.lowerBound)), .default, node)
return .quantification(.exactly(range.lowerBound), .default, node)
case (0, _): // 0..<n or 0...n or ..<n or ...n
return .quantification(.upToN(.init(faking: range.upperBound)), kind, node)
return .quantification(.upToN(range.upperBound), kind, node)
case (_, Int.max): // n...
return .quantification(.nOrMore(.init(faking: range.lowerBound)), kind, node)
return .quantification(.nOrMore(range.lowerBound), kind, node)
default: // any other range
return .quantification(.range(.init(faking: range.lowerBound), .init(faking: range.upperBound)), kind, node)
return .quantification(.range(range.lowerBound, range.upperBound), kind, node)
}
}
}
Expand Down
45 changes: 22 additions & 23 deletions Sources/RegexBuilder/Variadics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

// BEGIN AUTO-GENERATED CONTENT

import _RegexParser
@_spi(RegexBuilder) import _StringProcessing

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -709,7 +708,7 @@ extension Repeat {
) where RegexOutput == Substring {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -720,7 +719,7 @@ extension Repeat {
) where RegexOutput == Substring {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -835,7 +834,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?), Component.RegexOutput == (W, C0) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -845,7 +844,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?), Component.RegexOutput == (W, C0) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -958,7 +957,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?), Component.RegexOutput == (W, C0, C1) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -968,7 +967,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?), Component.RegexOutput == (W, C0, C1) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1081,7 +1080,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?), Component.RegexOutput == (W, C0, C1, C2) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1091,7 +1090,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?), Component.RegexOutput == (W, C0, C1, C2) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1204,7 +1203,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?), Component.RegexOutput == (W, C0, C1, C2, C3) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1214,7 +1213,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?), Component.RegexOutput == (W, C0, C1, C2, C3) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1327,7 +1326,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?), Component.RegexOutput == (W, C0, C1, C2, C3, C4) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1337,7 +1336,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?), Component.RegexOutput == (W, C0, C1, C2, C3, C4) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1450,7 +1449,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1460,7 +1459,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1573,7 +1572,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1583,7 +1582,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1696,7 +1695,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6, C7) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1706,7 +1705,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6, C7) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1819,7 +1818,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1829,7 +1828,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down Expand Up @@ -1942,7 +1941,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component.regex.root))
self.init(node: .quantification(.exactly(count), .default, component.regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand All @@ -1952,7 +1951,7 @@ extension Repeat {
) where RegexOutput == (Substring, C0?, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?), Component.RegexOutput == (W, C0, C1, C2, C3, C4, C5, C6, C7, C8, C9) {
assert(count > 0, "Must specify a positive count")
// TODO: Emit a warning about `repeatMatch(count: 0)` or `repeatMatch(count: 1)`
self.init(node: .quantification(.exactly(.init(faking: count)), .default, component().regex.root))
self.init(node: .quantification(.exactly(count), .default, component().regex.root))
}

@available(SwiftStdlib 5.7, *)
Expand Down
Loading