File tree Expand file tree Collapse file tree 3 files changed +10
-6
lines changed
Sources/_MatchingEngine/Regex/Parse Expand file tree Collapse file tree 3 files changed +10
-6
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,7 @@ enum ParseError: Error, Hashable {
31
31
case expectedASCII( Character )
32
32
33
33
case expectedNonEmptyContents
34
+ case expectedEscape
34
35
35
36
case unknownGroupKind( String )
36
37
@@ -70,6 +71,8 @@ extension ParseError: CustomStringConvertible {
70
71
return s
71
72
case . expectedNonEmptyContents:
72
73
return " expected non-empty contents "
74
+ case . expectedEscape:
75
+ return " expected escape sequence "
73
76
case let . unknownGroupKind( str) :
74
77
return " unknown group kind '( \( str) ' "
75
78
case let . invalidMatchingOption( c) :
Original file line number Diff line number Diff line change @@ -105,18 +105,18 @@ extension Source {
105
105
/// Throws an unexpected end of input error if not matched
106
106
///
107
107
/// Note: much of the time, but not always, we can vend a more specific error.
108
- mutating func expectNonEmpty( ) throws {
108
+ mutating func expectNonEmpty(
109
+ _ error: ParseError = . unexpectedEndOfInput
110
+ ) throws {
109
111
_ = try recordLoc { src in
110
- if src. isEmpty { throw ParseError . unexpectedEndOfInput }
112
+ if src. isEmpty { throw error }
111
113
}
112
114
}
113
115
114
116
mutating func tryEatNonEmpty< C: Collection > ( sequence c: C ) throws -> Bool
115
117
where C. Element == Char
116
118
{
117
- _ = try recordLoc { src in
118
- guard !src. isEmpty else { throw ParseError . expected ( String ( c) ) }
119
- }
119
+ try expectNonEmpty ( . expected( String ( c) ) )
120
120
return tryEat ( sequence: c)
121
121
}
122
122
@@ -430,7 +430,7 @@ extension Source {
430
430
// Ignore escapes if we're allowed to. lexUntil will consume the next
431
431
// character.
432
432
if ignoreEscaped, src. tryEat ( " \\ " ) {
433
- try src. expectNonEmpty ( )
433
+ try src. expectNonEmpty ( . expectedEscape )
434
434
}
435
435
return false
436
436
} . value
Original file line number Diff line number Diff line change @@ -1093,6 +1093,7 @@ extension RegexTests {
1093
1093
diagnosticTest ( " \\ Qab \\ " , . expected( " \\ E " ) )
1094
1094
diagnosticTest ( #""ab"# , . expected( " \" " ) , syntax: . experimental)
1095
1095
diagnosticTest ( #""ab\""# , . expected( " \" " ) , syntax: . experimental)
1096
+ diagnosticTest ( " \" ab \\ " , . expectedEscape, syntax: . experimental)
1096
1097
1097
1098
// MARK: Text Segment options
1098
1099
You can’t perform that action at this time.
0 commit comments