11
11
12
12
import _MatchingEngine
13
13
14
- public struct Assertion {
14
+ public struct Anchor {
15
15
internal enum Kind {
16
16
case startOfSubject
17
17
case endOfSubjectBeforeNewline
@@ -21,15 +21,14 @@ public struct Assertion {
21
21
case startOfLine
22
22
case endOfLine
23
23
case wordBoundary
24
- case lookahead( DSLTree . Node )
25
24
}
26
25
27
26
var kind : Kind
28
27
var isInverted : Bool = false
29
28
}
30
29
31
- extension Assertion : RegexProtocol {
32
- var astAssertion : AST . Atom . AssertionKind ? {
30
+ extension Anchor : RegexProtocol {
31
+ var astAssertion : AST . Atom . AssertionKind {
33
32
if !isInverted {
34
33
switch kind {
35
34
case . startOfSubject: return . startOfSubject
@@ -40,7 +39,6 @@ extension Assertion: RegexProtocol {
40
39
case . startOfLine: return . startOfLine
41
40
case . endOfLine: return . endOfLine
42
41
case . wordBoundary: return . wordBoundary
43
- default : return nil
44
42
}
45
43
} else {
46
44
switch kind {
@@ -52,83 +50,72 @@ extension Assertion: RegexProtocol {
52
50
case . startOfLine: fatalError ( " Not yet supported " )
53
51
case . endOfLine: fatalError ( " Not yet supported " )
54
52
case . wordBoundary: return . notWordBoundary
55
- default : return nil
56
53
}
57
54
}
58
55
}
59
56
60
57
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) ) )
74
59
}
75
60
}
76
61
77
62
// MARK: - Public API
78
63
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)
82
67
}
83
68
84
- public static var endOfSubjectBeforeNewline : Assertion {
85
- Assertion ( kind: . endOfSubjectBeforeNewline)
69
+ public static var endOfSubjectBeforeNewline : Anchor {
70
+ Anchor ( kind: . endOfSubjectBeforeNewline)
86
71
}
87
72
88
- public static var endOfSubject : Assertion {
89
- Assertion ( kind: . endOfSubject)
73
+ public static var endOfSubject : Anchor {
74
+ Anchor ( kind: . endOfSubject)
90
75
}
91
76
92
77
// 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)
95
80
// }
96
81
97
- public static var firstMatchingPositionInSubject : Assertion {
98
- Assertion ( kind: . firstMatchingPositionInSubject)
82
+ public static var firstMatchingPositionInSubject : Anchor {
83
+ Anchor ( kind: . firstMatchingPositionInSubject)
99
84
}
100
85
101
- public static var textSegmentBoundary : Assertion {
102
- Assertion ( kind: . textSegmentBoundary)
86
+ public static var textSegmentBoundary : Anchor {
87
+ Anchor ( kind: . textSegmentBoundary)
103
88
}
104
89
105
- public static var startOfLine : Assertion {
106
- Assertion ( kind: . startOfLine)
90
+ public static var startOfLine : Anchor {
91
+ Anchor ( kind: . startOfLine)
107
92
}
108
93
109
- public static var endOfLine : Assertion {
110
- Assertion ( kind: . endOfLine)
94
+ public static var endOfLine : Anchor {
95
+ Anchor ( kind: . endOfLine)
111
96
}
112
97
113
- public static var wordBoundary : Assertion {
114
- Assertion ( kind: . wordBoundary)
98
+ public static var wordBoundary : Anchor {
99
+ Anchor ( kind: . wordBoundary)
115
100
}
116
101
117
- public var inverted : Assertion {
102
+ public var inverted : Anchor {
118
103
var result = self
119
104
result. isInverted. toggle ( )
120
105
return result
121
106
}
122
107
}
123
108
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
+ }
130
115
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) )
134
121
}
0 commit comments