File: CaptureList.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (214 lines) | stat: -rw-r--r-- 6,788 bytes parent folder | download
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

public struct CaptureList {
  public var captures: [Capture]

  public init<S: Sequence>(_ s: S) where S.Element == Capture {
    captures = Array(s)
  }

  public mutating func append(_ c: Capture) {
    captures.append(c)
  }
}

extension CaptureList {
  public struct Capture {
    public var name: String?
    public var type: Any.Type
    public var optionalDepth: Int
    public var location: SourceLocation
    public var visibleInTypedOutput: Bool

    public init(
      name: String? = nil,
      type: Any.Type = Substring.self,
      optionalDepth: Int,
      visibleInTypedOutput: Bool,
      _ location: SourceLocation
    ) {
      self.name = name
      self.type = type
      self.optionalDepth = optionalDepth
      self.visibleInTypedOutput = visibleInTypedOutput
      self.location = location
    }
  }
}

extension CaptureList {
  /// Retrieve the capture index of a given named capture, or `nil` if there is
  /// no such capture.
  public func indexOfCapture(named name: String) -> Int? {
    // Named references are guaranteed to be unique for literal ASTs by Sema.
    // The DSL tree does not use named references.
    captures.indices.first(where: { captures[$0].name == name })
  }

  /// Whether the capture list has a given named capture.
  public func hasCapture(named name: String) -> Bool {
    indexOfCapture(named: name) != nil
  }
}

extension CaptureList {
  public struct Builder {
    public var captures = CaptureList()

    public init() {}

    public struct OptionalNesting {
      // We maintain two depths, inner and outer. These allow e.g the nesting
      // of a regex literal in a DSL, where outside of the scope of the literal,
      // nesting is allowed, but inside the literal at most one extra layer of
      // optionality may be added.
      public var outerDepth: Int
      public var canNest: Bool
      public var innerDepth: Int

      internal init(outerDepth: Int, canNest: Bool) {
        self.outerDepth = outerDepth
        self.canNest = canNest
        self.innerDepth = 0
      }

      public init(canNest: Bool) {
        self.init(outerDepth: 0, canNest: canNest)
      }

      public var depth: Int { outerDepth + innerDepth }

      public var disablingNesting: Self {
        // If we are currently able to nest, store the current depth as the
        // outer depth, and disable nesting for an inner scope.
        guard canNest else { return self }
        return .init(outerDepth: depth, canNest: false)
      }

      public var addingOptional: Self {
        var result = self
        result.innerDepth = canNest ? innerDepth + 1 : 1
        return result
      }
    }
  }
}

// MARK: Generating from AST

extension CaptureList.Builder {
  public mutating func addCaptures(
    of node: AST.Node,
    optionalNesting nesting: OptionalNesting,
    visibleInTypedOutput: Bool
  ) {
    switch node {
    case let .alternation(a):
      for child in a.children {
        addCaptures(of: child, optionalNesting: nesting.addingOptional, visibleInTypedOutput: visibleInTypedOutput)
      }

    case let .concatenation(c):
      for child in c.children {
        addCaptures(of: child, optionalNesting: nesting, visibleInTypedOutput: visibleInTypedOutput)
      }

    case let .group(g):
      switch g.kind.value {
      case .capture:
        captures.append(.init(optionalDepth: nesting.depth, visibleInTypedOutput: visibleInTypedOutput, g.location))

      case .namedCapture(let name):
        captures.append(.init(
          name: name.value, optionalDepth: nesting.depth, visibleInTypedOutput: visibleInTypedOutput, g.location))

      case .balancedCapture(let b):
        captures.append(.init(
          name: b.name?.value, optionalDepth: nesting.depth, visibleInTypedOutput: visibleInTypedOutput, g.location))

      default: break
      }
      addCaptures(of: g.child, optionalNesting: nesting, visibleInTypedOutput: visibleInTypedOutput)

    case .conditional(let c):
      switch c.condition.kind {
      case .group(let g):
        addCaptures(of: .group(g), optionalNesting: nesting, visibleInTypedOutput: visibleInTypedOutput)
      default:
        break
      }

      addCaptures(of: c.trueBranch, optionalNesting: nesting.addingOptional, visibleInTypedOutput: visibleInTypedOutput)
      addCaptures(of: c.falseBranch, optionalNesting: nesting.addingOptional, visibleInTypedOutput: visibleInTypedOutput)

    case .quantification(let q):
      var optNesting = nesting
      if q.amount.value.bounds.atLeast == 0 {
        optNesting = optNesting.addingOptional
      }
      addCaptures(of: q.child, optionalNesting: optNesting, visibleInTypedOutput: visibleInTypedOutput)

    case .absentFunction(let abs):
      switch abs.kind {
      case .expression(_, _, let child):
        addCaptures(of: child, optionalNesting: nesting, visibleInTypedOutput: visibleInTypedOutput)
      case .clearer, .repeater, .stopper:
        break
      }

    case .quote, .trivia, .atom, .customCharacterClass, .empty, .interpolation:
      break
    }
  }
  public static func build(_ ast: AST) -> CaptureList {
    var builder = Self()
    builder.captures.append(.init(optionalDepth: 0, visibleInTypedOutput: true, .fake))
    builder.addCaptures(of: ast.root, optionalNesting: .init(canNest: false), visibleInTypedOutput: true)
    return builder.captures
  }
}

extension AST {
  /// The capture list (including the whole match) of this AST.
  public var captureList: CaptureList { .Builder.build(self) }
}

// MARK: Convenience for testing and inspection

extension CaptureList.Capture: Equatable {
  public static func == (lhs: Self, rhs: Self) -> Bool {
    lhs.name == rhs.name &&
    lhs.optionalDepth == rhs.optionalDepth &&
    lhs.type == rhs.type &&
    lhs.location == rhs.location
  }
}
extension CaptureList: Equatable {}

extension CaptureList.Capture: CustomStringConvertible {
  public var description: String {
    let typeStr = String(describing: type)
    let suffix = String(repeating: "?", count: optionalDepth)
    return typeStr + suffix
  }
}
extension CaptureList: CustomStringConvertible {
  public var description: String {
    "(" + captures.map(\.description).joined(separator: ", ") + ")"
  }
}

extension CaptureList: ExpressibleByArrayLiteral {
  public init(arrayLiteral elements: Capture...) {
    self.init(elements)
  }
}