Skip to content

Commit f629a4a

Browse files
committed
Anchor is a better API name than Assertion
`lookahead` is moved to a free function, since it isn't an anchor like the others
1 parent c674db9 commit f629a4a

File tree

2 files changed

+38
-51
lines changed

2 files changed

+38
-51
lines changed

Sources/_StringProcessing/RegexDSL/Assertion.swift

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import _MatchingEngine
1313

14-
public struct Assertion {
14+
public struct Anchor {
1515
internal enum Kind {
1616
case startOfSubject
1717
case endOfSubjectBeforeNewline
@@ -21,15 +21,14 @@ public struct Assertion {
2121
case startOfLine
2222
case endOfLine
2323
case wordBoundary
24-
case lookahead(DSLTree.Node)
2524
}
2625

2726
var kind: Kind
2827
var isInverted: Bool = false
2928
}
3029

31-
extension Assertion: RegexProtocol {
32-
var astAssertion: AST.Atom.AssertionKind? {
30+
extension Anchor: RegexProtocol {
31+
var astAssertion: AST.Atom.AssertionKind {
3332
if !isInverted {
3433
switch kind {
3534
case .startOfSubject: return .startOfSubject
@@ -40,7 +39,6 @@ extension Assertion: RegexProtocol {
4039
case .startOfLine: return .startOfLine
4140
case .endOfLine: return .endOfLine
4241
case .wordBoundary: return .wordBoundary
43-
default: return nil
4442
}
4543
} else {
4644
switch kind {
@@ -52,83 +50,72 @@ extension Assertion: RegexProtocol {
5250
case .startOfLine: fatalError("Not yet supported")
5351
case .endOfLine: fatalError("Not yet supported")
5452
case .wordBoundary: return .notWordBoundary
55-
default: return nil
5653
}
5754
}
5855
}
5956

6057
public var regex: Regex<Substring> {
61-
if let assertionKind = astAssertion {
62-
return Regex(node: .atom(.assertion(assertionKind)))
63-
}
64-
65-
switch (kind, isInverted) {
66-
case let (.lookahead(node), false):
67-
return Regex(node: .group(.lookahead, node))
68-
case let (.lookahead(node), true):
69-
return Regex(node: .group(.negativeLookahead, node))
70-
71-
default:
72-
fatalError("Unsupported assertion")
73-
}
58+
Regex(node: .atom(.assertion(astAssertion)))
7459
}
7560
}
7661

7762
// MARK: - Public API
7863

79-
extension Assertion {
80-
public static var startOfSubject: Assertion {
81-
Assertion(kind: .startOfSubject)
64+
extension Anchor {
65+
public static var startOfSubject: Anchor {
66+
Anchor(kind: .startOfSubject)
8267
}
8368

84-
public static var endOfSubjectBeforeNewline: Assertion {
85-
Assertion(kind: .endOfSubjectBeforeNewline)
69+
public static var endOfSubjectBeforeNewline: Anchor {
70+
Anchor(kind: .endOfSubjectBeforeNewline)
8671
}
8772

88-
public static var endOfSubject: Assertion {
89-
Assertion(kind: .endOfSubject)
73+
public static var endOfSubject: Anchor {
74+
Anchor(kind: .endOfSubject)
9075
}
9176

9277
// TODO: Are we supporting this?
93-
// public static var resetStartOfMatch: Assertion {
94-
// Assertion(kind: resetStartOfMatch)
78+
// public static var resetStartOfMatch: Anchor {
79+
// Anchor(kind: resetStartOfMatch)
9580
// }
9681

97-
public static var firstMatchingPositionInSubject: Assertion {
98-
Assertion(kind: .firstMatchingPositionInSubject)
82+
public static var firstMatchingPositionInSubject: Anchor {
83+
Anchor(kind: .firstMatchingPositionInSubject)
9984
}
10085

101-
public static var textSegmentBoundary: Assertion {
102-
Assertion(kind: .textSegmentBoundary)
86+
public static var textSegmentBoundary: Anchor {
87+
Anchor(kind: .textSegmentBoundary)
10388
}
10489

105-
public static var startOfLine: Assertion {
106-
Assertion(kind: .startOfLine)
90+
public static var startOfLine: Anchor {
91+
Anchor(kind: .startOfLine)
10792
}
10893

109-
public static var endOfLine: Assertion {
110-
Assertion(kind: .endOfLine)
94+
public static var endOfLine: Anchor {
95+
Anchor(kind: .endOfLine)
11196
}
11297

113-
public static var wordBoundary: Assertion {
114-
Assertion(kind: .wordBoundary)
98+
public static var wordBoundary: Anchor {
99+
Anchor(kind: .wordBoundary)
115100
}
116101

117-
public var inverted: Assertion {
102+
public var inverted: Anchor {
118103
var result = self
119104
result.isInverted.toggle()
120105
return result
121106
}
122107
}
123108

124-
extension Assertion {
125-
public static func lookahead<R: RegexProtocol>(
126-
@RegexBuilder _ content: () -> R
127-
) -> Assertion {
128-
lookahead(content())
129-
}
109+
public func lookahead<R: RegexProtocol>(
110+
isNegative: Bool = false,
111+
@RegexBuilder _ content: () -> R
112+
) -> Regex<R.Match> {
113+
Regex(node: .group(isNegative ? .negativeLookahead : .lookahead, content().regex.root))
114+
}
130115

131-
public static func lookahead<R: RegexProtocol>(_ component: R) -> Assertion {
132-
Assertion(kind: .lookahead(component.regex.root))
133-
}
116+
public func lookahead<R: RegexProtocol>(
117+
_ component: R,
118+
isNegative: Bool = false
119+
) -> Regex<R.Match> {
120+
Regex(node: .group(isNegative ? .negativeLookahead : .lookahead, component.regex.root))
134121
}

Tests/RegexTests/RegexDSLTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,10 @@ class RegexDSLTests: XCTestCase {
188188
("aaaaabc", nil),
189189
captureType: Substring.self, ==)
190190
{
191-
Assertion.startOfLine
191+
Anchor.startOfLine
192192
"a".+
193193
"b"
194-
Assertion.endOfLine
194+
Anchor.endOfLine
195195
}
196196

197197
try _testDSLCaptures(
@@ -201,7 +201,7 @@ class RegexDSLTests: XCTestCase {
201201
captureType: Substring.self, ==)
202202
{
203203
"a".+
204-
Assertion.lookahead(CharacterClass.digit)
204+
lookahead(CharacterClass.digit)
205205
CharacterClass.word
206206
}
207207
}

0 commit comments

Comments
 (0)