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
|
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
extension AST {
public struct Group: Hashable {
public let kind: Located<Kind>
public let child: AST.Node
public let location: SourceLocation
public init(
_ kind: Located<Kind>, _ child: AST.Node, _ r: SourceLocation
) {
self.kind = kind
self.child = child
self.location = r
}
public enum Kind: Hashable {
// (...)
case capture
// (?<name>...) (?'name'...) (?P<name>...)
case namedCapture(Located<String>)
// (?<name-priorName>) (?'name-priorName')
case balancedCapture(BalancedCapture)
// (?:...)
case nonCapture
// (?|...)
case nonCaptureReset
// (?>...)
case atomicNonCapturing
// (?=...)
case lookahead
// (?!...)
case negativeLookahead
// (?*...)
case nonAtomicLookahead
// (?<=...)
case lookbehind
// (?<!...)
case negativeLookbehind
// (?<*...)
case nonAtomicLookbehind
// (*sr:...)
case scriptRun
// (*asr:...)
case atomicScriptRun
// (?iJmnsUxxxDPSWy{..}-iJmnsUxxxDPSW:)
case changeMatchingOptions(MatchingOptionSequence)
// NOTE: Comments appear to be groups, but are not parsed
// the same. They parse more like quotes, so are not
// listed here.
}
}
}
extension AST.Group.Kind {
/// Whether the group is a capturing group.
public var isCapturing: Bool {
switch self {
case .capture, .namedCapture, .balancedCapture: return true
default: return false
}
}
/// The name of the group.
///
/// If the group doesn't have a name, this value is `nil`.
public var name: String? {
switch self {
case .namedCapture(let name): return name.value
case .balancedCapture(let b): return b.name?.value
default: return nil
}
}
}
extension AST.Group.Kind {
/// The direction of a lookaround assertion
/// and an indication of whether the assertion is positive or negative.
///
/// If the group isn't a lookaheand or lookbehind assertion,
/// this value is `nil`.
public var lookaroundKind: (forwards: Bool, positive: Bool)? {
switch self {
case .lookahead: return (true, true)
case .negativeLookahead: return (true, false)
case .lookbehind: return (false, true)
case .negativeLookbehind: return (false, false)
default: return nil
}
}
}
extension AST.Group {
public struct BalancedCapture: Hashable {
/// The name of the group, or nil if the group has no name.
public var name: AST.Located<String>?
/// The location of the `-` in the group.
public var dash: SourceLocation
/// The name of the prior group that the balancing group references.
public var priorName: AST.Located<String>
public init(
name: AST.Located<String>?, dash: SourceLocation,
priorName: AST.Located<String>
) {
self.name = name
self.dash = dash
self.priorName = priorName
}
}
}
extension AST.Group {
var isQuantifiable: Bool {
switch kind.value {
case .capture, .namedCapture, .balancedCapture, .nonCapture,
.nonCaptureReset, .atomicNonCapturing, .scriptRun, .atomicScriptRun,
.changeMatchingOptions:
return true
case .lookahead, .negativeLookahead, .nonAtomicLookahead,
.lookbehind, .negativeLookbehind, .nonAtomicLookbehind:
return false
}
}
}
|