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 340 341 342 343 344 345 346 347
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
/// A regex abstract syntax tree.
///
/// This is a top-level type that stores the root node.
public struct AST: Hashable {
public var root: AST.Node
public var globalOptions: GlobalMatchingOptionSequence?
public var diags: Diagnostics
public init(
_ root: AST.Node, globalOptions: GlobalMatchingOptionSequence?,
diags: Diagnostics
) {
self.root = root
self.globalOptions = globalOptions
self.diags = diags
}
}
extension AST {
/// Whether this AST tree contains at least one capture nested inside of it.
public var hasCapture: Bool { root.hasCapture }
/// Whether this AST tree is either syntactically or semantically invalid.
public var isInvalid: Bool { diags.hasAnyError }
/// If the AST is invalid, throws an error. Otherwise, returns self.
@discardableResult
public func ensureValid() throws -> AST {
try diags.throwAnyError()
return self
}
}
extension AST {
/// A node in the regex AST.
public indirect enum Node:
Hashable, _TreeNode //, _ASTPrintable ASTValue, ASTAction
{
/// ... | ... | ...
case alternation(Alternation)
/// ... ...
case concatenation(Concatenation)
/// (...)
case group(Group)
/// (?(cond) true-branch | false-branch)
case conditional(Conditional)
case quantification(Quantification)
/// \Q...\E
case quote(Quote)
/// Comments, non-semantic whitespace, etc
case trivia(Trivia)
/// Intepolation `<{...}>`, currently reserved for future use.
case interpolation(Interpolation)
case atom(Atom)
case customCharacterClass(CustomCharacterClass)
case absentFunction(AbsentFunction)
case empty(Empty)
}
}
extension AST.Node {
// :-(
//
// Existential-based programming is highly prone to silent
// errors, but it does enable us to avoid having to switch
// over `self` _everywhere_ we want to do anything.
var _associatedValue: _ASTNode {
switch self {
case let .alternation(v): return v
case let .concatenation(v): return v
case let .group(v): return v
case let .conditional(v): return v
case let .quantification(v): return v
case let .quote(v): return v
case let .trivia(v): return v
case let .interpolation(v): return v
case let .atom(v): return v
case let .customCharacterClass(v): return v
case let .empty(v): return v
case let .absentFunction(v): return v
}
}
public func `as`<T: _ASTNode>(_ t: T.Type = T.self) -> T? {
_associatedValue as? T
}
/// The child nodes of this node.
///
/// If the node isn't a parent node, this value is `nil`.
public var children: [AST.Node]? {
return (_associatedValue as? _ASTParent)?.children
}
public var location: SourceLocation {
_associatedValue.location
}
/// Whether this node is trivia or non-semantic, like comments.
public var isTrivia: Bool {
switch self {
case .trivia: return true
default: return false
}
}
/// Whether this node contains at least one capture nested inside of it.
public var hasCapture: Bool {
switch self {
case .group(let g) where g.kind.value.isCapturing:
return true
default:
break
}
return self.children?.any(\.hasCapture) ?? false
}
/// Whether this node may be used as the operand of a quantifier such as
/// `?`, `+` or `*`.
public var isQuantifiable: Bool {
switch self {
case .atom(let a):
return a.isQuantifiable
case .group(let g):
return g.isQuantifiable
case .conditional, .customCharacterClass, .absentFunction:
return true
case .alternation, .concatenation, .quantification, .quote, .trivia,
.empty, .interpolation:
return false
}
}
}
// MARK: - AST types
extension AST {
public struct Alternation: Hashable, _ASTNode {
public let children: [AST.Node]
public let pipes: [SourceLocation]
public init(_ mems: [AST.Node], pipes: [SourceLocation]) {
// An alternation must have at least two branches (though the branches
// may be empty AST nodes), and n - 1 pipes.
precondition(mems.count >= 2)
precondition(pipes.count == mems.count - 1)
self.children = mems
self.pipes = pipes
}
public var location: SourceLocation {
.init(children.first!.location.start ..< children.last!.location.end)
}
}
public struct Concatenation: Hashable, _ASTNode {
public let children: [AST.Node]
public let location: SourceLocation
public init(_ mems: [AST.Node], _ location: SourceLocation) {
self.children = mems
self.location = location
}
}
public struct Quote: Hashable, _ASTNode {
public let literal: String
public let location: SourceLocation
public init(_ s: String, _ location: SourceLocation) {
self.literal = s
self.location = location
}
}
public struct Trivia: Hashable, _ASTNode {
public let contents: String
public let location: SourceLocation
public init(_ s: String, _ location: SourceLocation) {
self.contents = s
self.location = location
}
init(_ v: Located<String>) {
self.contents = v.value
self.location = v.location
}
}
public struct Interpolation: Hashable, _ASTNode {
public let contents: String
public let location: SourceLocation
public init(_ contents: String, _ location: SourceLocation) {
self.contents = contents
self.location = location
}
}
public struct Empty: Hashable, _ASTNode {
public let location: SourceLocation
public init(_ location: SourceLocation) {
self.location = location
}
}
/// An Oniguruma absent function.
///
/// This is used to model a pattern which should
/// not be matched against across varying scopes.
public struct AbsentFunction: Hashable, _ASTNode {
public enum Start: Hashable {
/// `(?~|`
case withPipe
/// `(?~`
case withoutPipe
}
public enum Kind: Hashable {
/// An absent repeater `(?~absent)`. This is equivalent to `(?~|absent|.*)`
/// and therefore matches as long as the pattern `absent` is not matched.
case repeater(AST.Node)
/// An absent expression `(?~|absent|expr)`, which defines an `absent`
/// pattern which must not be matched against while the pattern `expr` is
/// matched.
case expression(absentee: AST.Node, pipe: SourceLocation, expr: AST.Node)
/// An absent stopper `(?~|absent)`, which prevents matching against
/// `absent` until the end of the regex, or until it is cleared.
case stopper(AST.Node)
/// An absent clearer `(?~|)` which cancels the effect of an absent
/// stopper.
case clearer
}
/// The location of `(?~` or `(?~|`
public var start: SourceLocation
public var kind: Kind
public var location: SourceLocation
public init(
_ kind: Kind, start: SourceLocation, location: SourceLocation
) {
self.kind = kind
self.start = start
self.location = location
}
}
public struct Reference: Hashable {
public enum Kind: Hashable {
// \n \gn \g{n} \g<n> \g'n' (?n) (?(n)...
// Oniguruma: \k<n>, \k'n'
case absolute(AST.Atom.Number)
// \g{-n} \g<+n> \g'+n' \g<-n> \g'-n' (?+n) (?-n)
// (?(+n)... (?(-n)...
// Oniguruma: \k<-n> \k<+n> \k'-n' \k'+n'
case relative(AST.Atom.Number)
// \k<name> \k'name' \g{name} \k{name} (?P=name)
// \g<name> \g'name' (?&name) (?P>name)
// (?(<name>)... (?('name')... (?(name)...
case named(String)
/// (?R), (?(R)..., which are equivalent to (?0), (?(0)...
static func recurseWholePattern(_ loc: SourceLocation) -> Kind {
.absolute(.init(0, at: loc))
}
/// Whether this is a reference that recurses the whole pattern, rather
/// than a group.
public var recursesWholePattern: Bool {
switch self {
case .absolute(let a):
return a.value == 0
default:
return false
}
}
}
public var kind: Kind
/// An additional specifier supported by Oniguruma that specifies what
/// recursion level the group being referenced belongs to.
public var recursionLevel: AST.Atom.Number?
/// The location of the inner numeric or textual reference, e.g the location
/// of '-2' in '\g{-2}'. Note this includes the recursion level for e.g
/// '\k<a+2>'.
public var innerLoc: SourceLocation
public init(_ kind: Kind, recursionLevel: AST.Atom.Number? = nil,
innerLoc: SourceLocation) {
self.kind = kind
self.recursionLevel = recursionLevel
self.innerLoc = innerLoc
}
/// Whether this is a reference that recurses the whole pattern, rather than
/// a group.
public var recursesWholePattern: Bool { kind.recursesWholePattern }
}
/// A set of global matching options in a regular expression literal.
public struct GlobalMatchingOptionSequence: Hashable {
public var options: [AST.GlobalMatchingOption]
public init?(_ options: [AST.GlobalMatchingOption]) {
guard !options.isEmpty else { return nil }
self.options = options
}
public var location: SourceLocation {
options.first!.location.union(with: options.last!.location)
}
}
}
|