1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
@_implementationOnly import _RegexParser
/// A type that represents the current state of regex matching options, with
/// stack-based scoping.
struct MatchingOptions {
fileprivate var stack: [Representation]
fileprivate func _invariantCheck() {
assert(!stack.isEmpty, "Unbalanced call to endScope")
// Must contain exactly one of each mutually exclusive group
assert(stack.last!.intersection(.textSegmentOptions).rawValue.nonzeroBitCount == 1)
assert(stack.last!.intersection(.semanticMatchingLevels).rawValue.nonzeroBitCount == 1)
// Must contain at most one quantifier behavior
assert(stack.last!.intersection(.repetitionBehaviors).rawValue.nonzeroBitCount <= 1)
}
}
// MARK: Compilation API
extension MatchingOptions {
/// Creates an instance with the default options.
init() {
self.stack = [.default]
_invariantCheck()
}
/// Starts a new scope with the current options.
mutating func beginScope() {
stack.append(stack.last!)
_invariantCheck()
}
/// Ends the current scope.
mutating func endScope() {
_ = stack.removeLast()
_invariantCheck()
}
/// Updates the options in the current scope with the changes described by
/// `sequence`.
mutating func apply(_ sequence: AST.MatchingOptionSequence) {
stack[stack.count - 1].apply(sequence)
_invariantCheck()
}
// @testable
/// Returns true if the options at the top of `stack` are equal to those
/// for `other`.
func _equal(to other: MatchingOptions) -> Bool {
stack.last == other.stack.last
}
}
// MARK: Matching behavior API
extension MatchingOptions {
var isCaseInsensitive: Bool {
stack.last!.contains(.caseInsensitive)
}
var isReluctantByDefault: Bool {
stack.last!.contains(.reluctantByDefault)
}
var defaultQuantificationKind: AST.Quantification.Kind {
if stack.last!.contains(.possessiveByDefault) {
return .possessive
} else if stack.last!.contains(.reluctantByDefault) {
return .reluctant
} else {
return .eager
}
}
var dotMatchesNewline: Bool {
stack.last!.contains(.singleLine)
}
var anchorsMatchNewlines: Bool {
stack.last!.contains(.multiline)
}
var usesASCIIWord: Bool {
stack.last!.contains(.asciiOnlyWord)
|| stack.last!.contains(.asciiOnlyPOSIXProps)
}
var usesASCIIDigits: Bool {
stack.last!.contains(.asciiOnlyDigit)
|| stack.last!.contains(.asciiOnlyPOSIXProps)
}
var usesASCIISpaces: Bool {
stack.last!.contains(.asciiOnlySpace)
|| stack.last!.contains(.asciiOnlyPOSIXProps)
}
var usesSimpleUnicodeBoundaries: Bool {
!stack.last!.contains(.unicodeWordBoundaries)
}
var usesExtendedWhitespace: Bool {
stack.last!.contains(.extended)
|| stack.last!.contains(.extraExtended)
}
enum SemanticLevel {
case graphemeCluster
case unicodeScalar
}
var semanticLevel: SemanticLevel {
stack.last!.contains(.graphemeClusterSemantics)
? .graphemeCluster
: .unicodeScalar
}
var usesNSRECompatibleDot: Bool {
stack.last!.contains(.nsreCompatibleDot)
}
}
// MARK: - Implementation
extension MatchingOptions {
/// An option that changes the behavior of a regular expression.
fileprivate enum Option: Int {
// PCRE options
case caseInsensitive
case allowDuplicateGroupNames
case multiline
case namedCapturesOnly
case singleLine
case reluctantByDefault
// ICU options
case unicodeWordBoundaries
// NSRegularExpression compatibility options
// Not available via regex literal flags
case transparentBounds
case withoutAnchoringBounds
case nsreCompatibleDot
// Oniguruma options
case asciiOnlyDigit
case asciiOnlyPOSIXProps
case asciiOnlySpace
case asciiOnlyWord
// Oniguruma text segment options (these are mutually exclusive and cannot
// be unset, only flipped between)
case textSegmentGraphemeMode
case textSegmentWordMode
// Swift semantic matching level
case graphemeClusterSemantics
case unicodeScalarSemantics
case byteSemantics
// Swift-only default possessive quantifier
case possessiveByDefault
// Whitespace options
case extended
case extraExtended
init?(_ astKind: AST.MatchingOption.Kind) {
switch astKind {
case .caseInsensitive:
self = .caseInsensitive
case .allowDuplicateGroupNames:
self = .allowDuplicateGroupNames
case .multiline:
self = .multiline
case .namedCapturesOnly:
self = .namedCapturesOnly
case .singleLine:
self = .singleLine
case .reluctantByDefault:
self = .reluctantByDefault
case .unicodeWordBoundaries:
self = .unicodeWordBoundaries
case .asciiOnlyDigit:
self = .asciiOnlyDigit
case .asciiOnlyPOSIXProps:
self = .asciiOnlyPOSIXProps
case .asciiOnlySpace:
self = .asciiOnlySpace
case .asciiOnlyWord:
self = .asciiOnlyWord
case .textSegmentGraphemeMode:
self = .textSegmentGraphemeMode
case .textSegmentWordMode:
self = .textSegmentWordMode
case .graphemeClusterSemantics:
self = .graphemeClusterSemantics
case .unicodeScalarSemantics:
self = .unicodeScalarSemantics
case .byteSemantics:
self = .byteSemantics
case .possessiveByDefault:
self = .possessiveByDefault
case .nsreCompatibleDot:
self = .nsreCompatibleDot
case .extended:
self = .extended
case .extraExtended:
self = .extraExtended
#if RESILIENT_LIBRARIES
@unknown default:
fatalError()
#endif
}
}
fileprivate var representation: Representation {
return .init(self)
}
}
}
extension MatchingOptions {
/// A set of matching options.
fileprivate struct Representation: OptionSet, RawRepresentable {
var rawValue: UInt32
/// Returns `true` if the option denoted by `kind` is a member of this set.
func contains(_ kind: Option) -> Bool {
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)
}
if Self.repetitionBehaviors.contains(opt.representation) {
remove(.repetitionBehaviors)
}
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)`.
if sequence.caretLoc != nil {
self = .default
}
for opt in sequence.adding {
guard let opt = Option(opt.kind) else {
continue
}
add(opt)
}
for opt in sequence.removing {
guard let opt = Option(opt.kind) else {
continue
}
if Self.repetitionBehaviors.contains(opt.representation) {
remove(.repetitionBehaviors)
}
remove(opt.representation)
}
}
}
}
extension MatchingOptions.Representation {
fileprivate init(_ kind: MatchingOptions.Option) {
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) }
/// Options that comprise the mutually exclusive test segmentation group.
static var textSegmentOptions: Self {
[.textSegmentGraphemeMode, .textSegmentWordMode]
}
// Semantic matching level options
static var graphemeClusterSemantics: Self { .init(.graphemeClusterSemantics) }
static var unicodeScalarSemantics: Self { .init(.unicodeScalarSemantics) }
static var byteSemantics: Self { .init(.byteSemantics) }
/// Options that comprise the mutually exclusive semantic matching level
/// group.
static var semanticMatchingLevels: Self {
[.graphemeClusterSemantics, .unicodeScalarSemantics, .byteSemantics]
}
// Quantification behavior options
static var reluctantByDefault: Self { .init(.reluctantByDefault) }
static var possessiveByDefault: Self { .init(.possessiveByDefault) }
static var repetitionBehaviors: Self {
[.reluctantByDefault, .possessiveByDefault]
}
// Uses level 2 Unicode word boundaries
static var unicodeWordBoundaries: Self { .init(.unicodeWordBoundaries) }
/// The default set of options.
static var `default`: Self {
[.graphemeClusterSemantics, .textSegmentGraphemeMode, .unicodeWordBoundaries]
}
}
extension AST.Quantification.Kind {
func applying(_ options: MatchingOptions) -> Self {
if options.isReluctantByDefault && self != .possessive {
return self == .eager ? .reluctant : .eager
}
return self
}
}
|