Skip to content

Commit 957bb96

Browse files
Merge branch 'apple:main' into main
2 parents 5101c72 + 8febede commit 957bb96

File tree

131 files changed

+176324
-7873
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

131 files changed

+176324
-7873
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.DS_Store
2+
13
# Xcode
24
#
35
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

Package.swift

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ let package = Package(
1616
.library(
1717
name: "_MatchingEngine",
1818
targets: ["_MatchingEngine"]),
19-
.library(
20-
name: "_Unicode",
21-
targets: ["_Unicode"]),
2219
.executable(
2320
name: "VariadicsGenerator",
2421
targets: ["VariadicsGenerator"])
@@ -31,31 +28,33 @@ let package = Package(
3128
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
3229
.target(
3330
name: "_MatchingEngine",
34-
dependencies: [/*"_Unicode"*/],
31+
dependencies: [],
3532
swiftSettings: [
3633
.unsafeFlags(["-enable-library-evolution"])
3734
]),
3835
.testTarget(
3936
name: "MatchingEngineTests",
40-
dependencies: ["_MatchingEngine"]),
37+
dependencies: [
38+
"_MatchingEngine", "_StringProcessing"]),
39+
.target(
40+
name: "_CUnicode",
41+
dependencies: []),
4142
.target(
4243
name: "_StringProcessing",
43-
dependencies: ["_MatchingEngine"],
44+
dependencies: ["_MatchingEngine", "_CUnicode"],
4445
swiftSettings: [
45-
.unsafeFlags(["-enable-library-evolution"])
46+
.unsafeFlags(["-enable-library-evolution"]),
47+
.unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"])
4648
]),
47-
.target(
48-
name: "_Unicode",
49-
dependencies: []),
5049
.testTarget(
5150
name: "RegexTests",
52-
dependencies: ["_StringProcessing"]),
51+
dependencies: ["_StringProcessing"],
52+
swiftSettings: [
53+
.unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"])
54+
]),
5355
.target(
5456
name: "Prototypes",
55-
dependencies: ["_MatchingEngine"]),
56-
.testTarget(
57-
name: "UnicodeTests",
58-
dependencies: ["_Unicode"]),
57+
dependencies: ["_MatchingEngine", "_StringProcessing"]),
5958

6059
// MARK: Scripts
6160
.executableTarget(
@@ -74,7 +73,10 @@ let package = Package(
7473
// MARK: Exercises
7574
.target(
7675
name: "Exercises",
77-
dependencies: ["_MatchingEngine", "Prototypes", "_StringProcessing"]),
76+
dependencies: ["_MatchingEngine", "Prototypes", "_StringProcessing"],
77+
swiftSettings: [
78+
.unsafeFlags(["-Xfrontend", "-enable-experimental-pairwise-build-block"])
79+
]),
7880
.testTarget(
7981
name: "ExercisesTests",
8082
dependencies: ["Exercises"]),

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
# MatchingEngine
1+
# Declarative String Processing for Swift
22

33
An early experimental general-purpose pattern matching engine for Swift.
44

55
See [Declarative String Processing Overview][decl-string]
66

77
[decl-string]: Documentation/DeclarativeStringProcessing.md
88

9+
## Requirements
10+
11+
- [Swift Trunk Development Snapshot](https://www.swift.org/download/#snapshots) DEVELOPMENT-SNAPSHOT-2022-02-03 or later.

Sources/Exercises/Participants/PEGParticipant.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ private func graphemeBreakPropertyData(forLine line: String) -> GraphemeBreakEnt
4040
let program = PEG.Program(start: "Entry", environment: ["Entry": entry])
4141

4242
let vm = program.compile(for: String.self)
43-
let engine = program.transpile(for: String.self)
43+
let engine = try! program.transpile()
4444
_ = (vm, engine)
4545

4646
fatalError("Unsupported")

Sources/Exercises/Participants/RegexParticipant.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ struct RegexLiteralParticipant: Participant {
4646
// MARK: - Regex literal
4747

4848
private func extractFromCaptures(
49-
_ match: Tuple4<Substring, Substring, Substring?, Substring>
49+
_ match: (Substring, Substring, Substring?, Substring)
5050
) -> GraphemeBreakEntry? {
5151
guard let lowerScalar = Unicode.Scalar(hex: match.1),
5252
let upperScalar = match.2.map(Unicode.Scalar.init(hex:)) ?? lowerScalar,
@@ -61,7 +61,7 @@ private func extractFromCaptures(
6161
private func graphemeBreakPropertyData<RP: RegexProtocol>(
6262
forLine line: String,
6363
using regex: RP
64-
) -> GraphemeBreakEntry? where RP.Match == Tuple4<Substring, Substring, Substring?, Substring> {
64+
) -> GraphemeBreakEntry? where RP.Match == (Substring, Substring, Substring?, Substring) {
6565
line.match(regex).map(\.match).flatMap(extractFromCaptures)
6666
}
6767

@@ -71,7 +71,7 @@ private func graphemeBreakPropertyDataLiteral(
7171
return graphemeBreakPropertyData(
7272
forLine: line,
7373
using: r(#"([0-9A-F]+)(?:\.\.([0-9A-F]+))?\s+;\s+(\w+).*"#,
74-
matching: Tuple4<Substring, Substring, Substring?, Substring>.self))
74+
matching: (Substring, Substring, Substring?, Substring).self))
7575
}
7676

7777
// MARK: - Builder DSL
@@ -80,18 +80,18 @@ private func graphemeBreakPropertyData(
8080
forLine line: String
8181
) -> GraphemeBreakEntry? {
8282
line.match {
83-
OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:))
84-
Optionally {
83+
tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
84+
optionally {
8585
".."
86-
OneOrMore(.hexDigit).tryCapture(Unicode.Scalar.init(hex:))
86+
tryCapture(oneOrMore(.hexDigit)) { Unicode.Scalar(hex: $0) }
8787
}
88-
OneOrMore(.whitespace)
88+
oneOrMore(.whitespace)
8989
";"
90-
OneOrMore(.whitespace)
91-
OneOrMore(.word).tryCapture(Unicode.GraphemeBreakProperty.init)
92-
Repeat(.any)
90+
oneOrMore(.whitespace)
91+
tryCapture(oneOrMore(.word)) { Unicode.GraphemeBreakProperty($0) }
92+
zeroOrMore(.any)
9393
}.map {
94-
let (_, lower, upper, property) = $0.match.tuple
94+
let (_, lower, upper, property) = $0.match
9595
return GraphemeBreakEntry(lower...(upper ?? lower), property)
9696
}
9797
}

Sources/PatternConverter/PatternConverter.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ 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+
36+
@Flag(help: "Whether to skip result builder DSL")
37+
var skipDSL: Bool = false
38+
3339
@Option(help: "Limit (from top-down) the conversion levels")
3440
var topDownConversionLimit: Int?
3541

@@ -65,12 +71,21 @@ struct PatternConverter: ParsableCommand {
6571
print()
6672
}
6773

74+
if showCaptureStructure {
75+
print("Capture structure:")
76+
print()
77+
print(ast.captureStructure)
78+
print()
79+
}
80+
6881
print()
69-
let render = ast.renderAsPattern(
70-
maxTopDownLevels: topDownConversionLimit,
71-
minBottomUpLevels: bottomUpConversionLimit
72-
)
73-
print(render)
82+
if !skipDSL {
83+
let render = ast.renderAsBuilderDSL(
84+
maxTopDownLevels: topDownConversionLimit,
85+
minBottomUpLevels: bottomUpConversionLimit
86+
)
87+
print(render)
88+
}
7489

7590
return
7691
}

Sources/Prototypes/PEG/PEGCode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
import _MatchingEngine
12+
import _StringProcessing
1313

1414
extension PEG.VM {
1515
struct Code {

Sources/Prototypes/PEG/PEGCompile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
import _MatchingEngine
12+
import _StringProcessing
1313

1414
extension PEG.VM {
1515
typealias InIndex = Input.Index

Sources/Prototypes/PEG/PEGCore.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
//
1010
//===----------------------------------------------------------------------===//
1111

12-
import _MatchingEngine
12+
import _StringProcessing
1313
let emitComments = true
1414

1515
struct PEGCore<

Sources/Prototypes/PEG/PEGTranspile.swift

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

1212
import _MatchingEngine
13+
import _StringProcessing
1314

14-
extension PEG.VM {
15-
typealias MEProgram = _MatchingEngine.Program<Input>
16-
func transpile() -> MEProgram {
17-
typealias Builder = MEProgram.Builder
18-
var builder = MEProgram.Builder()
15+
extension PEG.VM where Input == String {
16+
typealias MEProg = MEProgram<String>
17+
func transpile() throws -> MEProg {
18+
typealias Builder = MEProg.Builder
19+
var builder = MEProg.Builder()
1920

2021
// Address token info
2122
//
@@ -106,14 +107,13 @@ extension PEG.VM {
106107
}
107108
}
108109

109-
return builder.assemble()
110+
return try builder.assemble()
110111
}
111112
}
112113

113-
extension PEG.Program {
114-
public func transpile<Input: Collection>(
115-
for input: Input.Type = Input.self
116-
) -> Engine<Input> where Input.Element == Element {
117-
Engine(compile(for: input).vm.transpile())
114+
extension PEG.Program where Element == Character {
115+
public func transpile(
116+
) throws -> Engine<String> {
117+
try Engine(compile(for: String.self).vm.transpile())
118118
}
119119
}

0 commit comments

Comments
 (0)