Skip to content

Commit a2c3f68

Browse files
committed
Move MatchingEngine out of the _MatchingEngine module
1 parent aad7831 commit a2c3f68

30 files changed

+109
-115
lines changed

Package.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ let package = Package(
3434
]),
3535
.testTarget(
3636
name: "MatchingEngineTests",
37-
dependencies: ["_MatchingEngine"]),
37+
dependencies: [
38+
"_MatchingEngine", "_StringProcessing"]),
3839
.target(
3940
name: "_StringProcessing",
4041
dependencies: ["_MatchingEngine"],
@@ -50,7 +51,7 @@ let package = Package(
5051
]),
5152
.target(
5253
name: "Prototypes",
53-
dependencies: ["_MatchingEngine"]),
54+
dependencies: ["_MatchingEngine", "_StringProcessing"]),
5455

5556
// MARK: Scripts
5657
.executableTarget(

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 = try! program.transpile(for: String.self)
43+
let engine = try! program.transpile()
4444
_ = (vm, engine)
4545

4646
fatalError("Unsupported")

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: 10 additions & 10 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() throws -> 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
//
@@ -110,10 +111,9 @@ extension PEG.VM {
110111
}
111112
}
112113

113-
extension PEG.Program {
114-
public func transpile<Input: Collection>(
115-
for input: Input.Type = Input.self
116-
) throws -> Engine<Input> where Input.Element == Element {
117-
try 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
}

Sources/Prototypes/PEG/PEGVM.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 {
1515

Sources/Prototypes/PEG/Printing.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 PEGCore.Instruction: InstructionProtocol {
1515
var operandPC: InstructionAddress? { self.pc }

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ import _MatchingEngine
33
extension Compiler {
44
struct ByteCodeGen {
55
var options: MatchingOptions
6-
var builder = _MatchingEngine.Program<String>.Builder()
6+
var builder = Program.Builder()
77

88
mutating func finish(
9-
) throws -> _MatchingEngine.Program<String> {
9+
) throws -> Program {
1010
builder.buildAccept()
1111
return try builder.assemble()
1212
}

Sources/_StringProcessing/Compiler.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111

1212
import _MatchingEngine
1313

14-
struct RegexProgram {
15-
typealias Program = _MatchingEngine.Program<String>
16-
var program: Program
17-
}
18-
1914
class Compiler {
2015
let tree: DSLTree
2116

@@ -30,12 +25,12 @@ class Compiler {
3025
self.tree = tree
3126
}
3227

33-
__consuming func emit() throws -> RegexProgram {
28+
__consuming func emit() throws -> Program {
3429
// TODO: Handle global options
3530
var codegen = ByteCodeGen(options: options)
3631
try codegen.emitNode(tree.root)
3732
let program = try codegen.finish()
38-
return RegexProgram(program: program)
33+
return program
3934
}
4035
}
4136

Sources/_StringProcessing/ConsumerInterface.swift

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ extension DSLTree.Node {
1818
/// the front of an input range
1919
func generateConsumer(
2020
_ opts: MatchingOptions
21-
) throws -> Program<String>.ConsumeFunction? {
21+
) throws -> MEProgram<String>.ConsumeFunction? {
2222
switch self {
2323
case .atom(let a):
2424
return try a.generateConsumer(opts)
@@ -55,7 +55,7 @@ extension DSLTree.Atom {
5555
// unnecessary...
5656
func generateConsumer(
5757
_ opts: MatchingOptions
58-
) throws -> Program<String>.ConsumeFunction? {
58+
) throws -> MEProgram<String>.ConsumeFunction? {
5959
switch self {
6060

6161
case let .char(c):
@@ -112,7 +112,7 @@ extension AST.Atom {
112112

113113
func generateConsumer(
114114
_ opts: MatchingOptions
115-
) throws -> Program<String>.ConsumeFunction? {
115+
) throws -> MEProgram<String>.ConsumeFunction? {
116116
// TODO: Wean ourselves off of this type...
117117
if let cc = self.characterClass?.withMatchLevel(
118118
opts.matchLevel
@@ -171,7 +171,7 @@ extension AST.Atom {
171171
extension DSLTree.CustomCharacterClass.Member {
172172
func generateConsumer(
173173
_ opts: MatchingOptions
174-
) throws -> Program<String>.ConsumeFunction {
174+
) throws -> MEProgram<String>.ConsumeFunction {
175175
switch self {
176176
case let .atom(a):
177177
guard let c = try a.generateConsumer(opts) else {
@@ -256,7 +256,7 @@ extension DSLTree.CustomCharacterClass.Member {
256256
extension AST.CustomCharacterClass.Member {
257257
func generateConsumer(
258258
_ opts: MatchingOptions
259-
) throws -> Program<String>.ConsumeFunction {
259+
) throws -> MEProgram<String>.ConsumeFunction {
260260
switch self {
261261
case .custom(let ccc):
262262
return try ccc.generateConsumer(opts)
@@ -351,7 +351,7 @@ extension AST.CustomCharacterClass.Member {
351351
extension DSLTree.CustomCharacterClass {
352352
func generateConsumer(
353353
_ opts: MatchingOptions
354-
) throws -> Program<String>.ConsumeFunction {
354+
) throws -> MEProgram<String>.ConsumeFunction {
355355
// NOTE: Easy way to implement, obviously not performant
356356
let consumers = try members.map {
357357
try $0.generateConsumer(opts)
@@ -374,7 +374,7 @@ extension DSLTree.CustomCharacterClass {
374374
extension AST.CustomCharacterClass {
375375
func generateConsumer(
376376
_ opts: MatchingOptions
377-
) throws -> Program<String>.ConsumeFunction {
377+
) throws -> MEProgram<String>.ConsumeFunction {
378378
// NOTE: Easy way to implement, obviously not performant
379379
let consumers = try strippingTriviaShallow.members.map {
380380
try $0.generateConsumer(opts)
@@ -397,22 +397,22 @@ extension AST.CustomCharacterClass {
397397
// NOTE: Conveniences, though not most performant
398398
private func consumeScalarGC(
399399
_ gc: Unicode.GeneralCategory
400-
) -> Program<String>.ConsumeFunction {
400+
) -> MEProgram<String>.ConsumeFunction {
401401
consumeScalar { gc == $0.properties.generalCategory }
402402
}
403403
private func consumeScalarGCs(
404404
_ gcs: [Unicode.GeneralCategory]
405-
) -> Program<String>.ConsumeFunction {
405+
) -> MEProgram<String>.ConsumeFunction {
406406
consumeScalar { gcs.contains($0.properties.generalCategory) }
407407
}
408408
private func consumeScalarProp(
409409
_ p: @escaping (Unicode.Scalar.Properties) -> Bool
410-
) -> Program<String>.ConsumeFunction {
410+
) -> MEProgram<String>.ConsumeFunction {
411411
consumeScalar { p($0.properties) }
412412
}
413413
func consumeScalar(
414414
_ p: @escaping (Unicode.Scalar) -> Bool
415-
) -> Program<String>.ConsumeFunction {
415+
) -> MEProgram<String>.ConsumeFunction {
416416
{ input, bounds in
417417
// TODO: bounds check?
418418
let curIdx = bounds.lowerBound
@@ -427,11 +427,11 @@ func consumeScalar(
427427
extension AST.Atom.CharacterProperty {
428428
func generateConsumer(
429429
_ opts: MatchingOptions
430-
) throws -> Program<String>.ConsumeFunction {
430+
) throws -> MEProgram<String>.ConsumeFunction {
431431
// Handle inversion for us, albeit not efficiently
432432
func invert(
433-
_ p: @escaping Program<String>.ConsumeFunction
434-
) -> Program<String>.ConsumeFunction {
433+
_ p: @escaping MEProgram<String>.ConsumeFunction
434+
) -> MEProgram<String>.ConsumeFunction {
435435
return { input, bounds in
436436
if p(input, bounds) != nil { return nil }
437437
// TODO: semantic level
@@ -444,7 +444,7 @@ extension AST.Atom.CharacterProperty {
444444
// FIXME: Below is largely scalar based, for convenience,
445445
// but we want a comprehensive treatment to semantic mode
446446
// switching.
447-
let preInversion: Program<String>.ConsumeFunction =
447+
let preInversion: MEProgram<String>.ConsumeFunction =
448448
try {
449449
switch kind {
450450
// TODO: is this modeled differently?
@@ -498,7 +498,7 @@ extension Unicode.BinaryProperty {
498498
// FIXME: Semantic level, vet for precise defs
499499
func generateConsumer(
500500
_ opts: MatchingOptions
501-
) throws -> Program<String>.ConsumeFunction {
501+
) throws -> MEProgram<String>.ConsumeFunction {
502502
switch self {
503503

504504
case .asciiHexDigit:
@@ -664,7 +664,7 @@ extension Unicode.POSIXProperty {
664664
// FIXME: Semantic level, vet for precise defs
665665
func generateConsumer(
666666
_ opts: MatchingOptions
667-
) -> Program<String>.ConsumeFunction {
667+
) -> MEProgram<String>.ConsumeFunction {
668668
// FIXME: semantic levels, modes, etc
669669
switch self {
670670
case .alnum:
@@ -710,7 +710,7 @@ extension Unicode.ExtendedGeneralCategory {
710710
// FIXME: Semantic level
711711
func generateConsumer(
712712
_ opts: MatchingOptions
713-
) throws -> Program<String>.ConsumeFunction {
713+
) throws -> MEProgram<String>.ConsumeFunction {
714714
switch self {
715715
case .letter:
716716
return consumeScalarGCs([

Sources/_MatchingEngine/Engine/Consume.swift renamed to Sources/_StringProcessing/Engine/Consume.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extension Engine where Input == String {
3434
public func consume(
3535
_ input: Input,
3636
in range: Range<Input.Index>,
37-
matchMode: MatchMode = .prefix
37+
matchMode: MatchMode = .partialFromFront
3838
) -> (Input.Index, CaptureList)? {
3939
if enableTracing {
4040
print("Consume: \(input)")

Sources/_MatchingEngine/Engine/Engine.swift renamed to Sources/_StringProcessing/Engine/Engine.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// But, we can play around with this.
1414
public struct Engine<Input: BidirectionalCollection> where Input.Element: Hashable {
1515

16-
var program: Program<Input>
16+
var program: MEProgram<Input>
1717

1818
// TODO: Pre-allocated register banks
1919

@@ -25,7 +25,7 @@ public struct Engine<Input: BidirectionalCollection> where Input.Element: Hashab
2525
}
2626

2727
public init(
28-
_ program: Program<Input>,
28+
_ program: MEProgram<Input>,
2929
enableTracing: Bool? = nil
3030
) {
3131
var program = program

0 commit comments

Comments
 (0)