Skip to content

Simplify capture representations #360

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Sources/PatternConverter/PatternConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ struct PatternConverter: ParsableCommand {
@Flag(help: "Whether to show canonical regex literal")
var showCanonical: Bool = false

@Flag(help: "Whether to show capture structure")
var showCaptureStructure: Bool = false

@Flag(help: "Whether to skip result builder DSL")
var skipDSL: Bool = false

Expand Down Expand Up @@ -71,13 +68,6 @@ struct PatternConverter: ParsableCommand {
print()
}

if showCaptureStructure {
print("Capture structure:")
print()
print(ast.captureStructure)
print()
}

print()
if !skipDSL {
let render = ast.renderAsBuilderDSL(
Expand Down
6 changes: 0 additions & 6 deletions Sources/_RegexParser/Regex/AST/AST.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ public struct AST: Hashable {
extension AST {
/// Whether this AST tree has nested somewhere inside it a capture.
public var hasCapture: Bool { root.hasCapture }

/// The capture structure of this AST tree.
public var captureStructure: CaptureStructure {
var constructor = CaptureStructure.Constructor(.flatten)
return root._captureStructure(&constructor)
}
}

extension AST {
Expand Down
154 changes: 154 additions & 0 deletions Sources/_RegexParser/Regex/Parse/CaptureList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
//===----------------------------------------------------------------------===//
//
// 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 init(
name: String? = nil,
type: Any.Type? = nil,
optionalDepth: Int
) {
self.name = name
self.type = type
self.optionalDepth = optionalDepth
}
}
}

// MARK: Generating from AST

extension AST.Node {
public func _addCaptures(
to list: inout CaptureList,
optionalNesting nesting: Int
) {
let addOptional = nesting+1
switch self {
case let .alternation(a):
for child in a.children {
child._addCaptures(to: &list, optionalNesting: addOptional)
}

case let .concatenation(c):
for child in c.children {
child._addCaptures(to: &list, optionalNesting: nesting)
}

case let .group(g):
switch g.kind.value {
case .capture:
list.append(.init(optionalDepth: nesting))

case .namedCapture(let name):
list.append(.init(name: name.value, optionalDepth: nesting))

case .balancedCapture(let b):
list.append(.init(name: b.name?.value, optionalDepth: nesting))

default: break
}
g.child._addCaptures(to: &list, optionalNesting: nesting)

case .conditional(let c):
switch c.condition.kind {
case .group(let g):
AST.Node.group(g)._addCaptures(to: &list, optionalNesting: nesting)
default:
break
}

c.trueBranch._addCaptures(to: &list, optionalNesting: addOptional)
c.falseBranch._addCaptures(to: &list, optionalNesting: addOptional)

case .quantification(let q):
var optNesting = nesting
if q.amount.value.bounds.atLeast == 0 {
optNesting += 1
}
q.child._addCaptures(to: &list, optionalNesting: optNesting)

case .absentFunction(let abs):
switch abs.kind {
case .expression(_, _, let child):
child._addCaptures(to: &list, optionalNesting: nesting)
case .clearer, .repeater, .stopper:
break
}

case .quote, .trivia, .atom, .customCharacterClass, .empty:
break
}
}

public var _captureList: CaptureList {
var caps = CaptureList()
self._addCaptures(to: &caps, optionalNesting: 0)
return caps
}
}

extension AST {
/// Get the capture list for this AST
public var captureList: CaptureList {
root._captureList
}
}

// 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
}
}
extension CaptureList: Equatable {}

extension CaptureList.Capture: CustomStringConvertible {
public var description: String {
let typeStr: String
if let ty = type {
typeStr = "\(ty)"
} else {
typeStr = "Substring"
}
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)
}
}
Loading