Skip to content

Catch more unquantifiable elements #391

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 3 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion Sources/_RegexParser/Regex/AST/AST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ extension AST.Node {
switch self {
case .atom(let a):
return a.isQuantifiable
case .group, .conditional, .customCharacterClass, .absentFunction:
case .group(let g):
return g.isQuantifiable
case .conditional, .customCharacterClass, .absentFunction:
return true
case .alternation, .concatenation, .quantification, .quote, .trivia,
.empty:
Expand Down
2 changes: 2 additions & 0 deletions Sources/_RegexParser/Regex/AST/Atom.swift
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,8 @@ extension AST.Atom {
// TODO: Are callouts quantifiable?
case .escaped(let esc):
return esc.isQuantifiable
case .startOfLine, .endOfLine:
return false
default:
return true
}
Expand Down
15 changes: 15 additions & 0 deletions Sources/_RegexParser/Regex/AST/Group.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,18 @@ extension AST.Group {
}
}
}

extension AST.Group {
var isQuantifiable: Bool {
switch kind.value {
case .capture, .namedCapture, .balancedCapture, .nonCapture,
.nonCaptureReset, .atomicNonCapturing, .scriptRun, .atomicScriptRun:
return true

case .lookahead, .negativeLookahead, .nonAtomicLookahead,
.lookbehind, .negativeLookbehind, .nonAtomicLookbehind,
.changeMatchingOptions:
return false
}
}
}
9 changes: 5 additions & 4 deletions Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1756,10 +1756,6 @@ extension RegexTests {
charClass("a", "b")
))

parseTest(#"(?i:)?"#, zeroOrOne(of: changeMatchingOptions(
matchingOptions(adding: .caseInsensitive), empty()
)))

// Test multi-line comment handling.
parseTest(
"""
Expand Down Expand Up @@ -2569,6 +2565,11 @@ extension RegexTests {
diagnosticTest(#"\Z??"#, .notQuantifiable)
diagnosticTest(#"\G*?"#, .notQuantifiable)
diagnosticTest(#"\z+?"#, .notQuantifiable)
diagnosticTest(#"^*"#, .notQuantifiable)
diagnosticTest(#"$?"#, .notQuantifiable)
diagnosticTest(#"(?=a)+"#, .notQuantifiable)
diagnosticTest(#"(?i)*"#, .notQuantifiable)
diagnosticTest(#"(?i:)?"#, .notQuantifiable)
diagnosticTest(#"\K{1}"#, .unsupported(#"'\K'"#))
diagnosticTest(#"\y{2,5}"#, .notQuantifiable)
diagnosticTest(#"\Y{3,}"#, .notQuantifiable)
Expand Down