Skip to content

Commit cbd2277

Browse files
committed
internalize capture structure
1 parent 120b42c commit cbd2277

File tree

5 files changed

+17
-27
lines changed

5 files changed

+17
-27
lines changed

Sources/PatternConverter/PatternConverter.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ struct PatternConverter: ParsableCommand {
3030
@Flag(help: "Whether to show canonical regex literal")
3131
var showCanonical: Bool = false
3232

33-
@Flag(help: "Whether to show capture structure")
34-
var showCaptureStructure: Bool = false
35-
3633
@Flag(help: "Whether to skip result builder DSL")
3734
var skipDSL: Bool = false
3835

@@ -71,13 +68,6 @@ struct PatternConverter: ParsableCommand {
7168
print()
7269
}
7370

74-
if showCaptureStructure {
75-
print("Capture structure:")
76-
print()
77-
print(ast.captureStructure)
78-
print()
79-
}
80-
8171
print()
8272
if !skipDSL {
8373
let render = ast.renderAsBuilderDSL(

Sources/_RegexParser/Regex/AST/AST.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ extension AST {
2626
public var hasCapture: Bool { root.hasCapture }
2727

2828
/// The capture structure of this AST tree.
29-
public var captureStructure: CaptureStructure {
29+
var captureStructure: CaptureStructure {
3030
root._captureList._captureStructure(nestOptionals: true)
3131
}
3232
}

Sources/_RegexParser/Regex/Parse/CaptureList.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ extension AST.Node {
111111
}
112112

113113
extension CaptureList {
114-
public func _captureStructure(nestOptionals: Bool) -> CaptureStructure {
114+
func _captureStructure(nestOptionals: Bool) -> CaptureStructure {
115115
if captures.isEmpty { return .empty }
116116
if captures.count == 1 {
117117
return captures.first!._captureStructure(nestOptionals: nestOptionals)
@@ -123,7 +123,7 @@ extension CaptureList {
123123
}
124124

125125
extension CaptureList.Capture {
126-
public func _captureStructure(nestOptionals: Bool) -> CaptureStructure {
126+
func _captureStructure(nestOptionals: Bool) -> CaptureStructure {
127127
if optionalDepth == 0 {
128128
if let ty = type {
129129
return .atom(name: name, type: .init(ty))

Sources/_RegexParser/Regex/Parse/CaptureStructure.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@
1010
//===----------------------------------------------------------------------===//
1111

1212
// A tree representing the type of some captures.
13-
public enum CaptureStructure: Equatable {
13+
enum CaptureStructure: Equatable {
1414
case atom(name: String? = nil, type: AnyType? = nil)
1515
indirect case optional(CaptureStructure)
1616
indirect case tuple([CaptureStructure])
1717

18-
public static func tuple(_ children: CaptureStructure...) -> Self {
18+
static func tuple(_ children: CaptureStructure...) -> Self {
1919
tuple(children)
2020
}
2121

22-
public static var empty: Self {
22+
static var empty: Self {
2323
.tuple([])
2424
}
2525
}
@@ -29,14 +29,14 @@ public enum CaptureStructure: Equatable {
2929
extension CaptureStructure {
3030
/// Returns a Boolean indicating whether the structure does not contain any
3131
/// captures.
32-
public var isEmpty: Bool {
32+
var isEmpty: Bool {
3333
if case .tuple(let elements) = self, elements.isEmpty {
3434
return true
3535
}
3636
return false
3737
}
3838

39-
public func type(withAtomType atomType: Any.Type) -> Any.Type {
39+
func type(withAtomType atomType: Any.Type) -> Any.Type {
4040
switch self {
4141
case .atom(_, type: nil):
4242
return atomType
@@ -51,13 +51,13 @@ extension CaptureStructure {
5151
}
5252
}
5353

54-
public typealias DefaultAtomType = Substring
54+
typealias DefaultAtomType = Substring
5555

56-
public var type: Any.Type {
56+
var type: Any.Type {
5757
type(withAtomType: DefaultAtomType.self)
5858
}
5959

60-
public var atomType: AnyType {
60+
var atomType: AnyType {
6161
switch self {
6262
case .atom(_, type: nil):
6363
return .init(Substring.self)
@@ -89,7 +89,7 @@ extension CaptureStructure {
8989
private typealias SerializationVersion = UInt16
9090
private static let currentSerializationVersion: SerializationVersion = 1
9191

92-
public static func serializationBufferSize(
92+
static func serializationBufferSize(
9393
forInputUTF8CodeUnitCount inputUTF8CodeUnitCount: Int
9494
) -> Int {
9595
MemoryLayout<SerializationVersion>.stride + inputUTF8CodeUnitCount + 1
@@ -110,7 +110,7 @@ extension CaptureStructure {
110110
///
111111
/// - Parameter buffer: A buffer whose byte count is at least the byte count
112112
/// of the regular expression string that produced this capture structure.
113-
public func encode(to buffer: UnsafeMutableRawBufferPointer) {
113+
func encode(to buffer: UnsafeMutableRawBufferPointer) {
114114
assert(!buffer.isEmpty, "Buffer must not be empty")
115115
assert(
116116
buffer.count >=
@@ -169,7 +169,7 @@ extension CaptureStructure {
169169

170170
/// Creates a capture structure by decoding a serialized representation from
171171
/// the given buffer.
172-
public init?(decoding buffer: UnsafeRawBufferPointer) {
172+
init?(decoding buffer: UnsafeRawBufferPointer) {
173173
var scopes: [[CaptureStructure]] = [[]]
174174
var currentScope: [CaptureStructure] {
175175
get { scopes[scopes.endIndex - 1] }
@@ -223,13 +223,13 @@ extension CaptureStructure {
223223
}
224224

225225
extension CaptureStructure: CustomStringConvertible {
226-
public var description: String {
226+
var description: String {
227227
var printer = PrettyPrinter()
228228
_print(&printer)
229229
return printer.finish()
230230
}
231231

232-
private func _print(_ printer: inout PrettyPrinter) {
232+
func _print(_ printer: inout PrettyPrinter) {
233233
switch self {
234234
case let .atom(name, type):
235235
let name = name ?? "<unnamed>"

Tests/RegexTests/CaptureTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
import XCTest
1313
@testable @_spi(RegexBuilder) import _StringProcessing
14-
import _RegexParser
14+
@testable import _RegexParser
1515

1616
extension StructuredCapture {
1717
func formatStringCapture(input: String) -> String {

0 commit comments

Comments
 (0)