Skip to content

Implement the (?n) option #377

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 2 commits into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion Sources/_RegexParser/Regex/AST/MatchingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ extension AST {
case caseInsensitive // i
case allowDuplicateGroupNames // J
case multiline // m
case noAutoCapture // n
case namedCapturesOnly // n
case singleLine // s
case reluctantByDefault // U
case extended // x
Expand Down
6 changes: 5 additions & 1 deletion Sources/_RegexParser/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ extension Source {
case "i": return advanceAndReturn(.caseInsensitive)
case "J": return advanceAndReturn(.allowDuplicateGroupNames)
case "m": return advanceAndReturn(.multiline)
case "n": return advanceAndReturn(.noAutoCapture)
case "n": return advanceAndReturn(.namedCapturesOnly)
case "s": return advanceAndReturn(.singleLine)
case "U": return advanceAndReturn(.reluctantByDefault)
case "x":
Expand Down Expand Up @@ -914,6 +914,10 @@ extension Source {
}
// TODO: (name:)

// If (?n) is set, a bare (...) group is non-capturing.
if context.syntax.contains(.namedCapturesOnly) {
return .nonCapture
}
return .capture
}
}
Expand Down
33 changes: 22 additions & 11 deletions Sources/_RegexParser/Regex/Parse/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -287,23 +287,34 @@ extension Parser {
private mutating func applySyntaxOptions(
of opts: AST.MatchingOptionSequence
) {
// We skip this for multi-line, as extended syntax is always enabled there.
if context.syntax.contains(.multilineExtendedSyntax) { return }
func mapOption(_ option: SyntaxOptions,
_ pred: (AST.MatchingOption) -> Bool) {
if opts.resetsCurrentOptions {
context.syntax.remove(option)
}
if opts.adding.contains(where: pred) {
context.syntax.insert(option)
}
if opts.removing.contains(where: pred) {
context.syntax.remove(option)
}
}
func mapOption(_ option: SyntaxOptions, _ kind: AST.MatchingOption.Kind) {
mapOption(option, { $0.kind == kind })
}

// (?n)
mapOption(.namedCapturesOnly, .namedCapturesOnly)

// Check if we're introducing or removing extended syntax.
// (?x), (?xx)
// We skip this for multi-line, as extended syntax is always enabled there.
// TODO: PCRE differentiates between (?x) and (?xx) where only the latter
// handles non-semantic whitespace in a custom character class. Other
// engines such as Oniguruma, Java, and ICU do this under (?x). Therefore,
// treat (?x) and (?xx) as the same option here. If we ever get a strict
// PCRE mode, we will need to change this to handle that.
if opts.resetsCurrentOptions {
context.syntax.remove(.extendedSyntax)
}
if opts.adding.contains(where: \.isAnyExtended) {
context.syntax.insert(.extendedSyntax)
}
if opts.removing.contains(where: \.isAnyExtended) {
context.syntax.remove(.extendedSyntax)
if !context.syntax.contains(.multilineExtendedSyntax) {
mapOption(.extendedSyntax, \.isAnyExtended)
}
}

Expand Down
3 changes: 3 additions & 0 deletions Sources/_RegexParser/Regex/Parse/SyntaxOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public struct SyntaxOptions: OptionSet {
return [Self(1 << 6), .extendedSyntax]
}

/// `(?n)`
public static var namedCapturesOnly: Self { Self(1 << 7) }

/*

/// `<digit>*` == `[[:digit:]]*` == `\d*`
Expand Down
6 changes: 3 additions & 3 deletions Sources/_StringProcessing/MatchingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ extension MatchingOptions {
case caseInsensitive
case allowDuplicateGroupNames
case multiline
case noAutoCapture
case namedCapturesOnly
case singleLine
case reluctantByDefault

Expand Down Expand Up @@ -174,8 +174,8 @@ extension MatchingOptions {
self = .allowDuplicateGroupNames
case .multiline:
self = .multiline
case .noAutoCapture:
self = .noAutoCapture
case .namedCapturesOnly:
self = .namedCapturesOnly
case .singleLine:
self = .singleLine
case .reluctantByDefault:
Expand Down
9 changes: 8 additions & 1 deletion Tests/RegexTests/ParseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -906,7 +906,7 @@ extension RegexTests {
))

let allOptions: [AST.MatchingOption.Kind] = [
.caseInsensitive, .allowDuplicateGroupNames, .multiline, .noAutoCapture,
.caseInsensitive, .allowDuplicateGroupNames, .multiline, .namedCapturesOnly,
.singleLine, .reluctantByDefault, .extraExtended, .extended,
.unicodeWordBoundaries, .asciiOnlyDigit, .asciiOnlyPOSIXProps,
.asciiOnlySpace, .asciiOnlyWord, .textSegmentGraphemeMode,
Expand Down Expand Up @@ -973,6 +973,13 @@ extension RegexTests {
"d"
)), captures: [.cap])

parseTest("(?n)(?^:())(?<x>)()", concat(
changeMatchingOptions(matchingOptions(adding: .namedCapturesOnly)),
changeMatchingOptions(unsetMatchingOptions(), capture(empty())),
namedCapture("x", empty()),
nonCapture(empty())
), captures: [.cap, .named("x")])

// MARK: References

// \1 ... \9 are always backreferences.
Expand Down