Skip to content
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

WIP: mapOutput as dsl tree node #466

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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: 9 additions & 1 deletion Sources/_RegexParser/Regex/Parse/CaptureList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,15 @@ extension CaptureList.Capture: CustomStringConvertible {
typeStr = "Substring"
}
let suffix = String(repeating: "?", count: optionalDepth)
return typeStr + suffix

let prefix: String
if let name = name {
prefix = name + ": "
} else {
prefix = ""
}

return prefix + typeStr + suffix
}
}
extension CaptureList: CustomStringConvertible {
Expand Down
3 changes: 3 additions & 0 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,9 @@ extension Compiler.ByteCodeGen {
case let .matcher(_, f):
emitMatcher(f)

case let .mapOutput(retTy, fun, child):
fatalError()

case .transform:
throw Unreachable(
"Transforms only directly inside captures")
Expand Down
2 changes: 2 additions & 0 deletions Sources/_StringProcessing/ConsumerInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ extension DSLTree.Node {
fatalError("FIXME: Is this where we handle them?")
case .characterPredicate:
fatalError("FIXME: Is this where we handle them?")
case .mapOutput:
fatalError("FIXME: Is this where we handle them?")
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions Sources/_StringProcessing/PrintAsPattern.swift
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,9 @@ extension PrettyPrinter {
case .characterPredicate:
print("/* TODO: character predicates */")

case .mapOutput:
print("/* TODO: map output */")

case .absentFunction:
print("/* TODO: absent function */")
}
Expand Down
34 changes: 22 additions & 12 deletions Sources/_StringProcessing/Regex/DSLTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ extension DSLTree {

// TODO: Would this just boil down to a consumer?
case characterPredicate(_CharacterPredicateInterface)

case mapOutput(Any.Type, _MapOutputInterface, Node)
}
}

Expand Down Expand Up @@ -237,9 +239,11 @@ public typealias _MatcherInterface = (

// Character-set (post grapheme segmentation)
@_spi(RegexBuilder)
public typealias _CharacterPredicateInterface = (
(Character) -> Bool
)
public typealias _CharacterPredicateInterface = (Character) -> Bool

// Output mapping
@_spi(RegexBuilder)
public typealias _MapOutputInterface = (Any) -> Any

/*

Expand All @@ -257,20 +261,22 @@ extension DSLTree.Node {
public var children: [DSLTree.Node]? {
switch self {

case let .orderedChoice(v): return v
case let .orderedChoice(v): return v
case let .concatenation(v): return v

case let .convertedRegexLiteral(n, _):
// Treat this transparently
return n.children

case let .capture(_, _, n): return [n]
case let .nonCapturingGroup(_, n): return [n]
case let .transform(_, n): return [n]
case let .quantification(_, _, n): return [n]
case let .capture(_, _, n): return [n]
case let .nonCapturingGroup(_, n): return [n]
case let .transform(_, n): return [n]
case let .quantification(_, _, n): return [n]
case let .mapOutput(_, _, n): return [n]

case let .conditional(_, t, f): return [t,f]


case .trivia, .empty, .quotedLiteral, .regexLiteral,
.consumer, .matcher, .characterPredicate,
.customCharacterClass, .atom:
Expand Down Expand Up @@ -513,6 +519,9 @@ extension DSLTree.Node {
case .matcher:
break

case let .mapOutput(retTy, _, _):
fatalError("Add retTy's contents to capture list")

case .transform(_, let child):
child._addCaptures(to: &list, optionalNesting: nesting)

Expand Down Expand Up @@ -549,10 +558,11 @@ extension DSLTree {
// Treat this transparently
return _Tree(n).children

case let .capture(_, _, n): return [_Tree(n)]
case let .nonCapturingGroup(_, n): return [_Tree(n)]
case let .transform(_, n): return [_Tree(n)]
case let .quantification(_, _, n): return [_Tree(n)]
case let .capture(_, _, n): return [_Tree(n)]
case let .nonCapturingGroup(_, n): return [_Tree(n)]
case let .transform(_, n): return [_Tree(n)]
case let .quantification(_, _, n): return [_Tree(n)]
case let .mapOutput(_, _, n): return [_Tree(n)]

case let .conditional(_, t, f): return [_Tree(t), _Tree(f)]

Expand Down
14 changes: 14 additions & 0 deletions Sources/_StringProcessing/Regex/MapOutput.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@


@available(SwiftStdlib 5.7, *)
extension Regex {
public func mapOutput<NewOutput>(
_ f: @escaping (Output) -> NewOutput
) -> Regex<NewOutput> {
.init(node: .mapOutput(
NewOutput.self,
{ f($0 as! Output) },
self.root))
}
}

12 changes: 6 additions & 6 deletions Sources/_StringProcessing/Utility/TypeVerification.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@
@available(SwiftStdlib 5.7, *)
extension Regex {
internal func _verifyType() -> Bool {
guard Output.self != AnyRegexOutput.self else { return true }

var tupleElements: [Any.Type] = [Substring.self]
var labels = " "

for capture in program.tree.root._captureList.captures {
var captureType: Any.Type = capture.type ?? Substring.self
var i = capture.optionalDepth

while i != 0 {

for _ in 0..<capture.optionalDepth {
captureType = TypeConstruction.optionalType(of: captureType)
i -= 1
}

tupleElements.append(captureType)

if let name = capture.name {
Expand All @@ -47,7 +47,7 @@ extension Regex {
// to the tuple. In that case, don't pass a label string.
labels: labels.all { $0 == " " } ? nil : labels
)

return Output.self == createdType
}
}
163 changes: 113 additions & 50 deletions Tests/RegexBuilderTests/AnyRegexOutputTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@ import RegexBuilder

private let enablePrinting = false

func addNote(
_ in: (Substring, Substring, Substring, Substring, Substring)
) -> (Substring, Substring, note: Substring, Substring, Substring) {
return `in`
}
func removeName(
_ in: (Substring, Substring, Substring, Substring, Substring)
) -> (Substring, Substring, Substring, Substring, Substring) {
return `in`
}

extension RegexDSLTests {

func testContrivedAROExample() {
Expand Down Expand Up @@ -139,7 +150,7 @@ extension RegexDSLTests {
let aro = Regex<AnyRegexOutput>(re)

// FIXME: The below fatal errors
let casted = aro//try! XCTUnwrap(Regex(aro, as: Output.self))
let casted = try! XCTUnwrap(Regex(aro, as: Output.self))

// contains(captureNamed:)
checkContains(re, kind)
Expand All @@ -153,71 +164,123 @@ extension RegexDSLTests {
}

// Literals (mocked up via explicit `as` types)
check(try! Regex(#"""
let noCapBody = #"""
(?x)
\p{hexdigit}{4} -? \p{hexdigit}{4} -?
\p{hexdigit}{4} -? \p{hexdigit}{4}
"""#, as: Substring.self),
.none,
noCapOutput
)
check(try! Regex(#"""
"""#
let noCapType = Substring.self

let unnamedBody = #"""
(?x)
(\p{hexdigit}{4}) -? (\p{hexdigit}{4}) -?
(\p{hexdigit}{4}) -? (\p{hexdigit}{4})
"""#, as: (Substring, Substring, Substring, Substring, Substring).self),
.unnamed,
unnamedOutput
)
check(try! Regex(#"""
"""#
let unnamedType = (Substring, Substring, Substring, Substring, Substring).self

let salientBody = #"""
(?x)
(\p{hexdigit}{4}) -? (?<salient>\p{hexdigit}{4}) -?
(\p{hexdigit}{4}) -? (\p{hexdigit}{4})
"""#, as: (Substring, Substring, Substring, Substring, Substring).self),
.salient,
salientOutput
)
check(try! Regex(#"""
"""#
let salientType = (Substring, Substring, salient: Substring, Substring, Substring).self

let noteBody = #"""
(?x)
(\p{hexdigit}{4}) -? (?<note>\p{hexdigit}{4}) -?
(\p{hexdigit}{4}) -? (\p{hexdigit}{4})
"""#, as: (Substring, Substring, Substring, Substring, Substring).self),
"""#
let noteType = (Substring, Substring, note: Substring, Substring, Substring).self

let unknownBody = #"""
(?x)
(\p{hexdigit}{4}) -? (?<unknown>\p{hexdigit}{4}) -?
(\p{hexdigit}{4}) -? (\p{hexdigit}{4})
"""#
let unknownType = (Substring, Substring, unknown: Substring, Substring, Substring).self

// TODO: unknown body tests?


// Literals (mocked via exactly matching explicit types)
check(
try! Regex(noCapBody, as: noCapType),
.none,
noCapOutput
)
check(
try! Regex(unnamedBody, as: unnamedType),
.unnamed,
unnamedOutput
)
check(
try! Regex(salientBody, as: salientType),
.salient,
salientOutput
)
check(
try! Regex(noteBody, as: noteType),
.note,
noteOutput
)
// Unknown behaves same as unnamed
check(
try! Regex(unknownBody, as: unknownType),
.unnamed,
unnamedOutput
)

// TODO: Try regexes `as` different types to pick up different behavior

// TODO: A `mapOutput` variant that takes no-cap and produces captures
// by matching the other regexes inside the mapping

// Run-time strings (ARO)
check(try! Regex(#"""
(?x)
\p{hexdigit}{4} -? \p{hexdigit}{4} -?
\p{hexdigit}{4} -? \p{hexdigit}{4}
"""#),
check(
try! Regex(noCapBody),
.none,
noCapOutput)
check(try! Regex(#"""
(?x)
(\p{hexdigit}{4}) -? (\p{hexdigit}{4}) -?
(\p{hexdigit}{4}) -? (\p{hexdigit}{4})
"""#),
check(
try! Regex(unnamedBody),
.unnamed,
unnamedOutput
)
check(try! Regex(#"""
(?x)
(\p{hexdigit}{4}) -? (?<salient>\p{hexdigit}{4}) -?
(\p{hexdigit}{4}) -? (\p{hexdigit}{4})
"""#),
check(
try! Regex(salientBody),
.salient,
salientOutput
)
check(try! Regex(#"""
(?x)
(\p{hexdigit}{4}) -? (?<note>\p{hexdigit}{4}) -?
(\p{hexdigit}{4}) -? (\p{hexdigit}{4})
"""#),
check(
try! Regex(noteBody),
.note,
noteOutput
)
// Unknown behaves same as no names
check(
try! Regex(unknownBody),
.unnamed,
unnamedOutput
)

// // Use `mapOutput` to add or remove capture names
// check(
// try! Regex(unnamedBody).mapOutput(addNote),
// .note,
// noteOutput
// )
// check(
// try! Regex(salientBody).mapOutput(addNote),
// .note,
// noteOutput
// )
// check(try! Regex(#"""
// (?x)
// (\p{hexdigit}{4}) -? (?<salient>\p{hexdigit}{4}) -?
// (\p{hexdigit}{4}) -? (\p{hexdigit}{4})
// """#).mapOutput(removeName),
// .unnamed,
// unnamedOutput
// )

// Builders
check(
Expand All @@ -234,23 +297,23 @@ extension RegexDSLTests {
.none,
noCapOutput
)
let capDSL = Regex {
let doublet = Repeat(.hexDigit, count: 4)
Capture { doublet }
Optionally { "-" }
Capture { doublet }
Optionally { "-" }
Capture { doublet }
Optionally { "-" }
Capture { doublet }
}
check(
Regex {
let doublet = Repeat(.hexDigit, count: 4)
Capture { doublet }
Optionally { "-" }
Capture { doublet }
Optionally { "-" }
Capture { doublet }
Optionally { "-" }
Capture { doublet }
},
capDSL,
.unnamed,
unnamedOutput
)

// FIXME: `salient` and `note` builders using a semantically rich
// `mapOutput`
// TODO: add first-class capture names via `mapOutput` to DSL test

}
}