Skip to content

Commit f779459

Browse files
authored
Catch more unquantifiable elements (#391)
This adds start/end anchors ^ and $, groups that form zero-width assertions, and option-changing groups without content `(?i)...`
1 parent b209e4f commit f779459

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

Sources/_RegexParser/Regex/AST/AST.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,9 @@ extension AST.Node {
125125
switch self {
126126
case .atom(let a):
127127
return a.isQuantifiable
128-
case .group, .conditional, .customCharacterClass, .absentFunction:
128+
case .group(let g):
129+
return g.isQuantifiable
130+
case .conditional, .customCharacterClass, .absentFunction:
129131
return true
130132
case .alternation, .concatenation, .quantification, .quote, .trivia,
131133
.empty:

Sources/_RegexParser/Regex/AST/Atom.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,8 @@ extension AST.Atom {
808808
// TODO: Are callouts quantifiable?
809809
case .escaped(let esc):
810810
return esc.isQuantifiable
811+
case .startOfLine, .endOfLine:
812+
return false
811813
default:
812814
return true
813815
}

Sources/_RegexParser/Regex/AST/Group.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,18 @@ extension AST.Group {
136136
}
137137
}
138138
}
139+
140+
extension AST.Group {
141+
var isQuantifiable: Bool {
142+
switch kind.value {
143+
case .capture, .namedCapture, .balancedCapture, .nonCapture,
144+
.nonCaptureReset, .atomicNonCapturing, .scriptRun, .atomicScriptRun,
145+
.changeMatchingOptions:
146+
return true
147+
148+
case .lookahead, .negativeLookahead, .nonAtomicLookahead,
149+
.lookbehind, .negativeLookbehind, .nonAtomicLookbehind:
150+
return false
151+
}
152+
}
153+
}

Tests/RegexTests/ParseTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2652,6 +2652,10 @@ extension RegexTests {
26522652
diagnosticTest(#"\Z??"#, .notQuantifiable)
26532653
diagnosticTest(#"\G*?"#, .notQuantifiable)
26542654
diagnosticTest(#"\z+?"#, .notQuantifiable)
2655+
diagnosticTest(#"^*"#, .notQuantifiable)
2656+
diagnosticTest(#"$?"#, .notQuantifiable)
2657+
diagnosticTest(#"(?=a)+"#, .notQuantifiable)
2658+
diagnosticTest(#"(?i)*"#, .notQuantifiable)
26552659
diagnosticTest(#"\K{1}"#, .unsupported(#"'\K'"#))
26562660
diagnosticTest(#"\y{2,5}"#, .notQuantifiable)
26572661
diagnosticTest(#"\Y{3,}"#, .notQuantifiable)

0 commit comments

Comments
 (0)