Skip to content

[Integration] main (3683457) -> swift/main #557

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 29 commits into from
Jul 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
23d9f44
Retract ~= for Regex.
stephentyrone Jun 30, 2022
0f1d185
Forgot to remove ~= tests as well.
stephentyrone Jun 30, 2022
9cb337f
Use word breaking SPI for \b
Azoy Jun 30, 2022
b7bfd16
Merge pull request #526 from apple/remove-tilde-equals
stephentyrone Jul 1, 2022
1b2fbfc
Fix Repeat off-by-one error
hamishknight Jul 1, 2022
08e12b9
Upgrade a couple of assertions to preconditions
hamishknight Jul 1, 2022
ed842cb
Merge pull request #535 from hamishknight/one-more
hamishknight Jul 1, 2022
7cad6cb
Throw error when Output is wrong type
Azoy Jul 5, 2022
6ce46cf
Fix duplicate symbols static-linking failure by renaming symbols
kateinoigakukun Jun 18, 2022
7b7e35d
Merge pull request #544 from hamishknight/sym-city
hamishknight Jul 5, 2022
8f57563
Cache word indices
Azoy Jul 5, 2022
bfc3335
Merge pull request #541 from Azoy/errors-what-errors
Azoy Jul 5, 2022
b45111d
Fix crash in generateConsumer
Azoy Jul 5, 2022
f6df268
If def macOS tests until further notice
Azoy Jul 6, 2022
88239aa
Merge pull request #530 from Azoy/words
Azoy Jul 6, 2022
410dbe6
Merge pull request #546 from Azoy/fix-emitany
Azoy Jul 6, 2022
548b737
Mark '\O' unsupported
hamishknight Jul 6, 2022
3458383
Remove CharacterClass.anyUnicodeScalar
hamishknight Jul 6, 2022
d44a176
Rename `anyGrapheme` -> `anyGraphemeCluster`
hamishknight Jul 6, 2022
f4d66be
Merge pull request #540 from hamishknight/scalar-support
hamishknight Jul 6, 2022
99002fa
Fix `_testDSLCaptures`
hamishknight Jul 6, 2022
970bd0e
Merge pull request #554 from hamishknight/ohno
hamishknight Jul 6, 2022
285f9e3
Remove DSLTree.Node.regexLiteral
hamishknight Jul 5, 2022
bae0284
Use `nil` instead of `.some(nil)` for failed captures
hamishknight Jul 5, 2022
ed69b80
Statically enforce type equivalence for `_testDSLCaptures`
hamishknight Jul 5, 2022
b24389d
Flatten optional nesting for regex literal captures
hamishknight Jul 5, 2022
3081c13
Remove `nestOptionals` param from `_captureStructure`
hamishknight Jul 5, 2022
3683457
Merge pull request #545 from hamishknight/flatpak
hamishknight Jul 6, 2022
da2500e
Merge branch 'main' into main-merge
hamishknight Jul 6, 2022
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
6 changes: 1 addition & 5 deletions Sources/RegexBuilder/CharacterClass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,10 @@ extension RegexComponent where Self == CharacterClass {
.init(DSLTree.CustomCharacterClass(members: [.atom(.any)]))
}

public static var anyGrapheme: CharacterClass {
public static var anyGraphemeCluster: CharacterClass {
.init(unconverted: .anyGrapheme)
}

public static var anyUnicodeScalar: CharacterClass {
.init(unconverted: .anyUnicodeScalar)
}

public static var whitespace: CharacterClass {
.init(unconverted: .whitespace)
}
Expand Down
44 changes: 22 additions & 22 deletions Sources/RegexBuilder/Variadics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -784,7 +784,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == Substring {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -795,7 +795,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == Substring {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -913,7 +913,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?), Component.RegexOutput == (W, C1) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -923,7 +923,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?), Component.RegexOutput == (W, C1) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1039,7 +1039,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?), Component.RegexOutput == (W, C1, C2) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1049,7 +1049,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?), Component.RegexOutput == (W, C1, C2) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1165,7 +1165,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?), Component.RegexOutput == (W, C1, C2, C3) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1175,7 +1175,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?), Component.RegexOutput == (W, C1, C2, C3) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1291,7 +1291,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?), Component.RegexOutput == (W, C1, C2, C3, C4) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1301,7 +1301,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?), Component.RegexOutput == (W, C1, C2, C3, C4) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1417,7 +1417,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?), Component.RegexOutput == (W, C1, C2, C3, C4, C5) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1427,7 +1427,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?), Component.RegexOutput == (W, C1, C2, C3, C4, C5) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1543,7 +1543,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1553,7 +1553,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1669,7 +1669,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1679,7 +1679,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1795,7 +1795,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7, C8) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1805,7 +1805,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7, C8) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -1921,7 +1921,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7, C8, C9) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -1931,7 +1931,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7, C8, C9) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down Expand Up @@ -2047,7 +2047,7 @@ extension Repeat {
_ component: Component,
count: Int
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?, C10?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -2057,7 +2057,7 @@ extension Repeat {
count: Int,
@RegexComponentBuilder _ component: () -> Component
) where RegexOutput == (Substring, C1?, C2?, C3?, C4?, C5?, C6?, C7?, C8?, C9?, C10?), Component.RegexOutput == (W, C1, C2, C3, C4, C5, C6, C7, C8, C9, C10) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/VariadicsGenerator/VariadicsGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ struct VariadicsGenerator: ParsableCommand {
_ component: Component,
count: Int
) \(params.whereClauseForInit) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component))
}
Expand All @@ -516,7 +516,7 @@ struct VariadicsGenerator: ParsableCommand {
count: Int,
@\(concatBuilderName) _ component: () -> Component
) \(params.whereClauseForInit) {
assert(count > 0, "Must specify a positive count")
precondition(count >= 0, "Must specify a positive count")
let factory = makeFactory()
self.init(factory.exactly(count, component()))
}
Expand Down
93 changes: 68 additions & 25 deletions Sources/_RegexParser/Regex/Parse/CaptureList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,63 +57,105 @@ extension CaptureList {
}
}

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 AST.Node {
public func _addCaptures(
to list: inout CaptureList,
optionalNesting nesting: Int
extension CaptureList.Builder {
public mutating func addCaptures(
of node: AST.Node, optionalNesting nesting: OptionalNesting
) {
let addOptional = nesting+1
switch self {
switch node {
case let .alternation(a):
for child in a.children {
child._addCaptures(to: &list, optionalNesting: addOptional)
addCaptures(of: child, optionalNesting: nesting.addingOptional)
}

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

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

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

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

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

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

c.trueBranch._addCaptures(to: &list, optionalNesting: addOptional)
c.falseBranch._addCaptures(to: &list, optionalNesting: addOptional)
addCaptures(of: c.trueBranch, optionalNesting: nesting.addingOptional)
addCaptures(of: c.falseBranch, optionalNesting: nesting.addingOptional)

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

case .absentFunction(let abs):
switch abs.kind {
case .expression(_, _, let child):
child._addCaptures(to: &list, optionalNesting: nesting)
addCaptures(of: child, optionalNesting: nesting)
case .clearer, .repeater, .stopper:
break
}
Expand All @@ -122,16 +164,17 @@ extension AST.Node {
break
}
}
public static func build(_ ast: AST) -> CaptureList {
var builder = Self()
builder.captures.append(.init(optionalDepth: 0, .fake))
builder.addCaptures(of: ast.root, optionalNesting: .init(canNest: false))
return builder.captures
}
}

extension AST {
/// The capture list (including the whole match) of this AST.
public var captureList: CaptureList {
var caps = CaptureList()
caps.append(.init(optionalDepth: 0, .fake))
root._addCaptures(to: &caps, optionalNesting: 0)
return caps
}
public var captureList: CaptureList { .Builder.build(self) }
}

// MARK: Convenience for testing and inspection
Expand Down
Loading