Skip to content

Move MatchingEngine out of _MatchingEngine 😐 #143

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 1 commit into from
Feb 12, 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
5 changes: 3 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ let package = Package(
]),
.testTarget(
name: "MatchingEngineTests",
dependencies: ["_MatchingEngine"]),
dependencies: [
"_MatchingEngine", "_StringProcessing"]),
.target(
name: "_StringProcessing",
dependencies: ["_MatchingEngine"],
Expand All @@ -50,7 +51,7 @@ let package = Package(
]),
.target(
name: "Prototypes",
dependencies: ["_MatchingEngine"]),
dependencies: ["_MatchingEngine", "_StringProcessing"]),

// MARK: Scripts
.executableTarget(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Exercises/Participants/PEGParticipant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private func graphemeBreakPropertyData(forLine line: String) -> GraphemeBreakEnt
let program = PEG.Program(start: "Entry", environment: ["Entry": entry])

let vm = program.compile(for: String.self)
let engine = try! program.transpile(for: String.self)
let engine = try! program.transpile()
_ = (vm, engine)

fatalError("Unsupported")
Expand Down
2 changes: 1 addition & 1 deletion Sources/Prototypes/PEG/PEGCode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _MatchingEngine
import _StringProcessing

extension PEG.VM {
struct Code {
Expand Down
2 changes: 1 addition & 1 deletion Sources/Prototypes/PEG/PEGCompile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _MatchingEngine
import _StringProcessing

extension PEG.VM {
typealias InIndex = Input.Index
Expand Down
2 changes: 1 addition & 1 deletion Sources/Prototypes/PEG/PEGCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _MatchingEngine
import _StringProcessing
let emitComments = true

struct PEGCore<
Expand Down
20 changes: 10 additions & 10 deletions Sources/Prototypes/PEG/PEGTranspile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@
//===----------------------------------------------------------------------===//

import _MatchingEngine
import _StringProcessing

extension PEG.VM {
typealias MEProgram = _MatchingEngine.Program<Input>
func transpile() throws -> MEProgram {
typealias Builder = MEProgram.Builder
var builder = MEProgram.Builder()
extension PEG.VM where Input == String {
typealias MEProg = MEProgram<String>
func transpile() throws -> MEProg {
typealias Builder = MEProg.Builder
var builder = MEProg.Builder()

// Address token info
//
Expand Down Expand Up @@ -110,10 +111,9 @@ extension PEG.VM {
}
}

extension PEG.Program {
public func transpile<Input: Collection>(
for input: Input.Type = Input.self
) throws -> Engine<Input> where Input.Element == Element {
try Engine(compile(for: input).vm.transpile())
extension PEG.Program where Element == Character {
public func transpile(
) throws -> Engine<String> {
try Engine(compile(for: String.self).vm.transpile())
}
}
2 changes: 1 addition & 1 deletion Sources/Prototypes/PEG/PEGVM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _MatchingEngine
import _StringProcessing

extension PEG {

Expand Down
2 changes: 1 addition & 1 deletion Sources/Prototypes/PEG/Printing.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//
//===----------------------------------------------------------------------===//

import _MatchingEngine
import _StringProcessing

extension PEGCore.Instruction: InstructionProtocol {
var operandPC: InstructionAddress? { self.pc }
Expand Down
4 changes: 2 additions & 2 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import _MatchingEngine
extension Compiler {
struct ByteCodeGen {
var options: MatchingOptions
var builder = _MatchingEngine.Program<String>.Builder()
var builder = Program.Builder()

mutating func finish(
) throws -> _MatchingEngine.Program<String> {
) throws -> Program {
builder.buildAccept()
return try builder.assemble()
}
Expand Down
9 changes: 2 additions & 7 deletions Sources/_StringProcessing/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

import _MatchingEngine

struct RegexProgram {
typealias Program = _MatchingEngine.Program<String>
var program: Program
}

class Compiler {
let tree: DSLTree

Expand All @@ -30,12 +25,12 @@ class Compiler {
self.tree = tree
}

__consuming func emit() throws -> RegexProgram {
__consuming func emit() throws -> Program {
// TODO: Handle global options
var codegen = ByteCodeGen(options: options)
try codegen.emitNode(tree.root)
let program = try codegen.finish()
return RegexProgram(program: program)
return program
}
}

Expand Down
36 changes: 18 additions & 18 deletions Sources/_StringProcessing/ConsumerInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ extension DSLTree.Node {
/// the front of an input range
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction? {
) throws -> MEProgram<String>.ConsumeFunction? {
switch self {
case .atom(let a):
return try a.generateConsumer(opts)
Expand Down Expand Up @@ -55,7 +55,7 @@ extension DSLTree.Atom {
// unnecessary...
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction? {
) throws -> MEProgram<String>.ConsumeFunction? {
switch self {

case let .char(c):
Expand Down Expand Up @@ -112,7 +112,7 @@ extension AST.Atom {

func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction? {
) throws -> MEProgram<String>.ConsumeFunction? {
// TODO: Wean ourselves off of this type...
if let cc = self.characterClass?.withMatchLevel(
opts.matchLevel
Expand Down Expand Up @@ -171,7 +171,7 @@ extension AST.Atom {
extension DSLTree.CustomCharacterClass.Member {
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction {
) throws -> MEProgram<String>.ConsumeFunction {
switch self {
case let .atom(a):
guard let c = try a.generateConsumer(opts) else {
Expand Down Expand Up @@ -256,7 +256,7 @@ extension DSLTree.CustomCharacterClass.Member {
extension AST.CustomCharacterClass.Member {
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction {
) throws -> MEProgram<String>.ConsumeFunction {
switch self {
case .custom(let ccc):
return try ccc.generateConsumer(opts)
Expand Down Expand Up @@ -351,7 +351,7 @@ extension AST.CustomCharacterClass.Member {
extension DSLTree.CustomCharacterClass {
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction {
) throws -> MEProgram<String>.ConsumeFunction {
// NOTE: Easy way to implement, obviously not performant
let consumers = try members.map {
try $0.generateConsumer(opts)
Expand All @@ -374,7 +374,7 @@ extension DSLTree.CustomCharacterClass {
extension AST.CustomCharacterClass {
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction {
) throws -> MEProgram<String>.ConsumeFunction {
// NOTE: Easy way to implement, obviously not performant
let consumers = try strippingTriviaShallow.members.map {
try $0.generateConsumer(opts)
Expand All @@ -397,22 +397,22 @@ extension AST.CustomCharacterClass {
// NOTE: Conveniences, though not most performant
private func consumeScalarGC(
_ gc: Unicode.GeneralCategory
) -> Program<String>.ConsumeFunction {
) -> MEProgram<String>.ConsumeFunction {
consumeScalar { gc == $0.properties.generalCategory }
}
private func consumeScalarGCs(
_ gcs: [Unicode.GeneralCategory]
) -> Program<String>.ConsumeFunction {
) -> MEProgram<String>.ConsumeFunction {
consumeScalar { gcs.contains($0.properties.generalCategory) }
}
private func consumeScalarProp(
_ p: @escaping (Unicode.Scalar.Properties) -> Bool
) -> Program<String>.ConsumeFunction {
) -> MEProgram<String>.ConsumeFunction {
consumeScalar { p($0.properties) }
}
func consumeScalar(
_ p: @escaping (Unicode.Scalar) -> Bool
) -> Program<String>.ConsumeFunction {
) -> MEProgram<String>.ConsumeFunction {
{ input, bounds in
// TODO: bounds check?
let curIdx = bounds.lowerBound
Expand All @@ -427,11 +427,11 @@ func consumeScalar(
extension AST.Atom.CharacterProperty {
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction {
) throws -> MEProgram<String>.ConsumeFunction {
// Handle inversion for us, albeit not efficiently
func invert(
_ p: @escaping Program<String>.ConsumeFunction
) -> Program<String>.ConsumeFunction {
_ p: @escaping MEProgram<String>.ConsumeFunction
) -> MEProgram<String>.ConsumeFunction {
return { input, bounds in
if p(input, bounds) != nil { return nil }
// TODO: semantic level
Expand All @@ -444,7 +444,7 @@ extension AST.Atom.CharacterProperty {
// FIXME: Below is largely scalar based, for convenience,
// but we want a comprehensive treatment to semantic mode
// switching.
let preInversion: Program<String>.ConsumeFunction =
let preInversion: MEProgram<String>.ConsumeFunction =
try {
switch kind {
// TODO: is this modeled differently?
Expand Down Expand Up @@ -498,7 +498,7 @@ extension Unicode.BinaryProperty {
// FIXME: Semantic level, vet for precise defs
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction {
) throws -> MEProgram<String>.ConsumeFunction {
switch self {

case .asciiHexDigit:
Expand Down Expand Up @@ -664,7 +664,7 @@ extension Unicode.POSIXProperty {
// FIXME: Semantic level, vet for precise defs
func generateConsumer(
_ opts: MatchingOptions
) -> Program<String>.ConsumeFunction {
) -> MEProgram<String>.ConsumeFunction {
// FIXME: semantic levels, modes, etc
switch self {
case .alnum:
Expand Down Expand Up @@ -710,7 +710,7 @@ extension Unicode.ExtendedGeneralCategory {
// FIXME: Semantic level
func generateConsumer(
_ opts: MatchingOptions
) throws -> Program<String>.ConsumeFunction {
) throws -> MEProgram<String>.ConsumeFunction {
switch self {
case .letter:
return consumeScalarGCs([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension Engine where Input == String {
public func consume(
_ input: Input,
in range: Range<Input.Index>,
matchMode: MatchMode = .prefix
matchMode: MatchMode = .partialFromFront
) -> (Input.Index, CaptureList)? {
if enableTracing {
print("Consume: \(input)")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// But, we can play around with this.
public struct Engine<Input: BidirectionalCollection> where Input.Element: Hashable {

var program: Program<Input>
var program: MEProgram<Input>

// TODO: Pre-allocated register banks

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

public init(
_ program: Program<Input>,
_ program: MEProgram<Input>,
enableTracing: Bool? = nil
) {
var program = program
Expand Down
Loading