|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// |
| 10 | +//===----------------------------------------------------------------------===// |
| 11 | + |
| 12 | +public struct CaptureList { |
| 13 | + public var captures: [Capture] |
| 14 | + |
| 15 | + public init<S: Sequence>(_ s: S) where S.Element == Capture { |
| 16 | + captures = Array(s) |
| 17 | + } |
| 18 | + |
| 19 | + public mutating func append(_ c: Capture) { |
| 20 | + captures.append(c) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +extension CaptureList { |
| 25 | + public struct Capture { |
| 26 | + public var name: String? |
| 27 | + public var type: Any.Type? |
| 28 | + public var optionalDepth: Int |
| 29 | + |
| 30 | + public init( |
| 31 | + name: String? = nil, |
| 32 | + type: Any.Type? = nil, |
| 33 | + optionalDepth: Int |
| 34 | + ) { |
| 35 | + self.name = name |
| 36 | + self.type = type |
| 37 | + self.optionalDepth = optionalDepth |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// MARK: Generating from AST |
| 43 | + |
| 44 | +extension AST.Node { |
| 45 | + public func _addCaptures( |
| 46 | + to list: inout CaptureList, |
| 47 | + optionalNesting nesting: Int |
| 48 | + ) { |
| 49 | + let addOptional = nesting+1 |
| 50 | + switch self { |
| 51 | + case let .alternation(a): |
| 52 | + for child in a.children { |
| 53 | + child._addCaptures(to: &list, optionalNesting: addOptional) |
| 54 | + } |
| 55 | + |
| 56 | + case let .concatenation(c): |
| 57 | + for child in c.children { |
| 58 | + child._addCaptures(to: &list, optionalNesting: nesting) |
| 59 | + } |
| 60 | + |
| 61 | + case let .group(g): |
| 62 | + switch g.kind.value { |
| 63 | + case .capture: |
| 64 | + list.append(.init(optionalDepth: nesting)) |
| 65 | + |
| 66 | + case .namedCapture(let name): |
| 67 | + list.append(.init(name: name.value, optionalDepth: nesting)) |
| 68 | + |
| 69 | + case .balancedCapture(let b): |
| 70 | + list.append(.init(name: b.name?.value, optionalDepth: nesting)) |
| 71 | + |
| 72 | + default: break |
| 73 | + } |
| 74 | + g.child._addCaptures(to: &list, optionalNesting: nesting) |
| 75 | + |
| 76 | + case .conditional(let c): |
| 77 | + switch c.condition.kind { |
| 78 | + case .group(let g): |
| 79 | + AST.Node.group(g)._addCaptures(to: &list, optionalNesting: nesting) |
| 80 | + default: |
| 81 | + break |
| 82 | + } |
| 83 | + |
| 84 | + c.trueBranch._addCaptures(to: &list, optionalNesting: addOptional) |
| 85 | + c.falseBranch._addCaptures(to: &list, optionalNesting: addOptional) |
| 86 | + |
| 87 | + case .quantification(let q): |
| 88 | + var optNesting = nesting |
| 89 | + if q.amount.value.bounds.atLeast == 0 { |
| 90 | + optNesting += 1 |
| 91 | + } |
| 92 | + q.child._addCaptures(to: &list, optionalNesting: optNesting) |
| 93 | + |
| 94 | + case .absentFunction(let abs): |
| 95 | + switch abs.kind { |
| 96 | + case .expression(_, _, let child): |
| 97 | + child._addCaptures(to: &list, optionalNesting: nesting) |
| 98 | + case .clearer, .repeater, .stopper: |
| 99 | + break |
| 100 | + } |
| 101 | + |
| 102 | + case .quote, .trivia, .atom, .customCharacterClass, .empty: |
| 103 | + break |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + public var _captureList: CaptureList { |
| 108 | + var caps = CaptureList() |
| 109 | + self._addCaptures(to: &caps, optionalNesting: 0) |
| 110 | + return caps |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +extension AST { |
| 115 | + /// Get the capture list for this AST |
| 116 | + public var captureList: CaptureList { |
| 117 | + root._captureList |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +// MARK: Convenience for testing and inspection |
| 122 | + |
| 123 | +extension CaptureList.Capture: Equatable { |
| 124 | + public static func == (lhs: Self, rhs: Self) -> Bool { |
| 125 | + lhs.name == rhs.name && |
| 126 | + lhs.optionalDepth == rhs.optionalDepth && |
| 127 | + lhs.type == rhs.type |
| 128 | + } |
| 129 | +} |
| 130 | +extension CaptureList: Equatable {} |
| 131 | + |
| 132 | +extension CaptureList.Capture: CustomStringConvertible { |
| 133 | + public var description: String { |
| 134 | + let typeStr: String |
| 135 | + if let ty = type { |
| 136 | + typeStr = "\(ty)" |
| 137 | + } else { |
| 138 | + typeStr = "Substring" |
| 139 | + } |
| 140 | + let suffix = String(repeating: "?", count: optionalDepth) |
| 141 | + return typeStr + suffix |
| 142 | + } |
| 143 | +} |
| 144 | +extension CaptureList: CustomStringConvertible { |
| 145 | + public var description: String { |
| 146 | + "(" + captures.map(\.description).joined(separator: ", ") + ")" |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +extension CaptureList: ExpressibleByArrayLiteral { |
| 151 | + public init(arrayLiteral elements: Capture...) { |
| 152 | + self.init(elements) |
| 153 | + } |
| 154 | +} |
0 commit comments