Skip to content

Cleanups and refactorings #142

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

fatalError("Unsupported")
Expand Down
8 changes: 4 additions & 4 deletions Sources/Prototypes/PEG/PEGTranspile.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import _MatchingEngine

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

Expand Down Expand Up @@ -106,14 +106,14 @@ extension PEG.VM {
}
}

return builder.assemble()
return try builder.assemble()
}
}

extension PEG.Program {
public func transpile<Input: Collection>(
for input: Input.Type = Input.self
) -> Engine<Input> where Input.Element == Element {
Engine(compile(for: input).vm.transpile())
) throws -> Engine<Input> where Input.Element == Element {
try Engine(compile(for: input).vm.transpile())
}
}
6 changes: 3 additions & 3 deletions Sources/_MatchingEngine/Engine/Builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ extension Program.Builder {

// TODO: Mutating because of fail address fixup, drop when
// that's removed
public mutating func assemble() -> Program {
public mutating func assemble() throws -> Program {
// TODO: This will add a fail instruction at the end every
// time it's assembled. Better to do to the local instruction
// list copy, but that complicates logic. It's possible we
Expand Down Expand Up @@ -262,12 +262,12 @@ extension Program.Builder {

case .splitSaving:
guard let fix2 = tok.second else {
fatalError("unreachable")
throw Unreachable("TODO: reason")
}
let saving = addressTokens[fix2.rawValue]!
payload = .init(addr: addr, addr2: saving)

default: fatalError("unreachable")
default: throw Unreachable("TODO: reason")

}

Expand Down
2 changes: 1 addition & 1 deletion Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1785,7 +1785,7 @@ extension Source {
if customCC {
return .char(char)
}
fatalError("unreachable")
throw Unreachable("TODO: reason")

// (sometimes) special metacharacters
case ".": return customCC ? .char(".") : .any
Expand Down
2 changes: 1 addition & 1 deletion Sources/_MatchingEngine/Regex/Parse/Parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ extension Parser {
continue
}

fatalError("unreachable?")
throw Unreachable("TODO: reason")
}
guard !result.isEmpty else {
return .empty(.init(loc(_start)))
Expand Down
54 changes: 54 additions & 0 deletions Sources/_MatchingEngine/Utility/Errors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
//===----------------------------------------------------------------------===//

public struct Unsupported: Error, CustomStringConvertible {
var message: String
var file: String
var line: Int

public var description: String { """
Unsupported: '\(message)'
\(file):\(line)
"""
}

public init(
_ s: String,
file: StaticString = #file,
line: UInt = #line
) {
self.message = s
self.file = file.description
self.line = Int(asserting: line)
}
}

public struct Unreachable: Error, CustomStringConvertible {
var message: String
var file: String
var line: Int

public var description: String { """
Unreachable: '\(message)'
\(file):\(line)
"""
}

public init(
_ s: String,
file: StaticString = #file,
line: UInt = #line
) {
self.message = s
self.file = file.description
self.line = Int(asserting: line)
}
}
10 changes: 0 additions & 10 deletions Sources/_MatchingEngine/Utility/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,6 @@ extension FixedWidthInteger {
}
}

// TODO: Replace all fatal error unreachables with these calls.
// We will likely want to convert them to unhandleable throws
// or something similar.
func unreachable(_ s: @autoclosure () -> String) -> Never {
fatalError("unreachable \(s())")
}
func unreachable() -> Never {
fatalError("unreachable")
}

extension Substring {
var string: String { String(self) }
}
Expand Down
17 changes: 9 additions & 8 deletions Sources/_StringProcessing/ByteCodeGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ extension Compiler {
var options: MatchingOptions
var builder = _MatchingEngine.Program<String>.Builder()

mutating func finish() -> _MatchingEngine.Program<String> {
mutating func finish(
) throws -> _MatchingEngine.Program<String> {
builder.buildAccept()
return builder.assemble()
return try builder.assemble()
}
}
}
Expand Down Expand Up @@ -90,7 +91,7 @@ extension Compiler.ByteCodeGen {

case .resetStartOfMatch:
// FIXME: Figure out how to communicate this out
throw unsupported(#"\K (reset/keep assertion)"#)
throw Unsupported(#"\K (reset/keep assertion)"#)

case .firstMatchingPositionInSubject:
// TODO: We can probably build a nice model with API here
Expand All @@ -100,11 +101,11 @@ extension Compiler.ByteCodeGen {

case .textSegment:
// This we should be able to do!
throw unsupported(#"\y (text segment)"#)
throw Unsupported(#"\y (text segment)"#)

case .notTextSegment:
// This we should be able to do!
throw unsupported(#"\Y (not text segment)"#)
throw Unsupported(#"\Y (not text segment)"#)

case .startOfLine:
builder.buildAssert { (input, pos, bounds) in
Expand Down Expand Up @@ -204,7 +205,7 @@ extension Compiler.ByteCodeGen {
_ child: DSLTree.Node
) throws {
guard kind.forwards else {
throw unsupported("backwards assertions")
throw Unsupported("backwards assertions")
}

let positive = kind.positive
Expand Down Expand Up @@ -259,7 +260,7 @@ extension Compiler.ByteCodeGen {
switch kind {
case .lookahead, .negativeLookahead,
.lookbehind, .negativeLookbehind:
fatalError("unreachable")
throw Unreachable("TODO: reason")

case .capture, .namedCapture:
let cap = builder.makeCapture()
Expand Down Expand Up @@ -299,7 +300,7 @@ extension Compiler.ByteCodeGen {
// Ok
break
default:
fatalError("unreachable?")
throw Unreachable("TODO: reason")
}

// Compiler and/or parser should enforce these invariants
Expand Down
2 changes: 1 addition & 1 deletion Sources/_StringProcessing/Compiler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Compiler {
// TODO: Handle global options
var codegen = ByteCodeGen(options: options)
try codegen.emitNode(tree.root)
let program = codegen.finish()
let program = try codegen.finish()
return RegexProgram(program: program)
}
}
Expand Down
Loading