Skip to content

Option APIs for RegexProtocol #171

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 1 commit into from
Feb 21, 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
10 changes: 10 additions & 0 deletions Sources/_MatchingEngine/Regex/AST/MatchingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ extension AST {
}
}

extension AST.MatchingOptionSequence {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, when we pay off some technical debt, DSLTree will have fewer and fewer AST constructs in it.

In general, we have 3 things, the AST's representation, the API's representation, and the model type.

Literal/AST lowers-to model
API lowers-to model

It's possible in some situations for all 3 to be the same type, or for the model to also be made public in API.

public init(adding: [AST.MatchingOption]) {
self.init(caretLoc: nil, adding: adding, minusLoc: nil, removing: [])
}

public init(removing: [AST.MatchingOption]) {
self.init(caretLoc: nil, adding: [], minusLoc: nil, removing: removing)
}
}

extension AST.MatchingOption: _ASTPrintable {
public var _dumpBase: String { "\(kind)" }
}
Expand Down
35 changes: 20 additions & 15 deletions Sources/_StringProcessing/MatchingOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,19 @@ extension MatchingOptions {
contains(.init(kind))
}

mutating func add(_ opt: Option) {
// If opt is in one of the mutually exclusive groups, clear out the
// group before inserting.
if Self.semanticMatchingLevels.contains(opt.representation) {
remove(.semanticMatchingLevels)
}
if Self.textSegmentOptions.contains(opt.representation) {
remove(.textSegmentOptions)
}

insert(opt.representation)
}

/// Applies the changes described by `sequence` to this set of options.
mutating func apply(_ sequence: AST.MatchingOptionSequence) {
// Replace entirely if the sequence includes a caret, e.g. `(?^is)`.
Expand All @@ -197,28 +210,17 @@ extension MatchingOptions {
}

for opt in sequence.adding {
guard let opt = Option(opt.kind)?.representation else {
guard let opt = Option(opt.kind) else {
continue
}

// If opt is in one of the mutually exclusive groups, clear out the
// group before inserting.
if Self.semanticMatchingLevels.contains(opt) {
remove(.semanticMatchingLevels)
}
if Self.textSegmentOptions.contains(opt) {
remove(.textSegmentOptions)
}

insert(opt)
add(opt)
}

for opt in sequence.removing {
guard let opt = Option(opt.kind)?.representation else {
guard let opt = Option(opt.kind) else {
continue
}

remove(opt)
remove(opt.representation)
}
}
}
Expand All @@ -229,6 +231,9 @@ extension MatchingOptions.Representation {
self.rawValue = 1 << kind.rawValue
}

// Case insensitivity
static var caseInsensitive: Self { .init(.caseInsensitive) }

// Text segmentation options
static var textSegmentGraphemeMode: Self { .init(.textSegmentGraphemeMode) }
static var textSegmentWordMode: Self { .init(.textSegmentWordMode) }
Expand Down
26 changes: 26 additions & 0 deletions Sources/_StringProcessing/RegexDSL/Options.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

import _MatchingEngine

extension RegexProtocol {
public func caseSensitive(_ isCaseSensitive: Bool) -> Regex<Match> {
// The API is "case sensitive = true or false", so as to avoid the
// double negatives inherent in setting "case insensitive" to a Boolean
// value. The internal version of this option, on the other hand, is
// `.caseInsensitive`, derived from the `(?i)` regex literal option.
let sequence = isCaseSensitive
? AST.MatchingOptionSequence(removing: [.init(.caseInsensitive, location: .fake)])
: AST.MatchingOptionSequence(adding: [.init(.caseInsensitive, location: .fake)])
return Regex(node: .group(.changeMatchingOptions(sequence, isIsolated: false), regex.root))
}
}

52 changes: 45 additions & 7 deletions Tests/RegexTests/RegexDSLTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,51 @@ class RegexDSLTests: XCTestCase {
}
}

func testOptions() throws {
try _testDSLCaptures(
("abc", "abc"),
("ABC", "ABC"),
("abcabc", "abcabc"),
("abcABCaBc", "abcABCaBc"),
captureType: Substring.self, ==) {
oneOrMore {
"abc"
}.caseSensitive(false)
}

// Multiple options on one component wrap successively, but do not
// override - equivalent to each option attached to a wrapping `Regex`.
try _testDSLCaptures(
("abc", "abc"),
("ABC", "ABC"),
("abcabc", "abcabc"),
("abcABCaBc", "abcABCaBc"),
captureType: Substring.self, ==) {
oneOrMore {
"abc"
}
.caseSensitive(false)
.caseSensitive(true)
}

// An option on an outer component doesn't override an option set on an
// inner component.
try _testDSLCaptures(
("abc", "abc"),
("ABC", "ABC"),
("ABCde", "ABCde"),
("ABCDE", nil),
("abcabc", "abcabc"),
("abcdeABCdeaBcde", "abcdeABCdeaBcde"),
captureType: Substring.self, ==) {
oneOrMore {
"abc".caseSensitive(false)
optionally("de")
}
.caseSensitive(true)
}
}

func testQuantificationBehavior() throws {
try _testDSLCaptures(
("abc1def2", ("abc1def2", "2")),
Expand Down Expand Up @@ -525,13 +570,6 @@ extension Unicode.Scalar {

// MARK: Extra == functions

// (Substring, [(Substring, Substring, [Substring])])
typealias S_AS = (Substring, [(Substring, Substring, [Substring])])

func ==(lhs: S_AS, rhs: S_AS) -> Bool {
lhs.0 == rhs.0 && lhs.1.elementsEqual(rhs.1, by: ==)
}

func == <T0: Equatable, T1: Equatable, T2: Equatable, T3: Equatable, T4: Equatable, T5: Equatable, T6: Equatable>(
l: (T0, T1, T2, T3, T4, T5, T6), r: (T0, T1, T2, T3, T4, T5, T6)
) -> Bool {
Expand Down