Skip to content

Commit a5df6f0

Browse files
authored
Refactor AST conversions and other cleanups (#142)
1 parent ff266c6 commit a5df6f0

File tree

16 files changed

+419
-394
lines changed

16 files changed

+419
-394
lines changed

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(for: String.self)
4444
_ = (vm, engine)
4545

4646
fatalError("Unsupported")

Sources/Prototypes/PEG/PEGTranspile.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import _MatchingEngine
1313

1414
extension PEG.VM {
1515
typealias MEProgram = _MatchingEngine.Program<Input>
16-
func transpile() -> MEProgram {
16+
func transpile() throws -> MEProgram {
1717
typealias Builder = MEProgram.Builder
1818
var builder = MEProgram.Builder()
1919

@@ -106,14 +106,14 @@ extension PEG.VM {
106106
}
107107
}
108108

109-
return builder.assemble()
109+
return try builder.assemble()
110110
}
111111
}
112112

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

Sources/_MatchingEngine/Engine/Builder.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ extension Program.Builder {
230230

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

263263
case .splitSaving:
264264
guard let fix2 = tok.second else {
265-
fatalError("unreachable")
265+
throw Unreachable("TODO: reason")
266266
}
267267
let saving = addressTokens[fix2.rawValue]!
268268
payload = .init(addr: addr, addr2: saving)
269269

270-
default: fatalError("unreachable")
270+
default: throw Unreachable("TODO: reason")
271271

272272
}
273273

Sources/_MatchingEngine/Regex/Parse/LexicalAnalysis.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1785,7 +1785,7 @@ extension Source {
17851785
if customCC {
17861786
return .char(char)
17871787
}
1788-
fatalError("unreachable")
1788+
throw Unreachable("TODO: reason")
17891789

17901790
// (sometimes) special metacharacters
17911791
case ".": return customCC ? .char(".") : .any

Sources/_MatchingEngine/Regex/Parse/Parse.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ extension Parser {
223223
continue
224224
}
225225

226-
fatalError("unreachable?")
226+
throw Unreachable("TODO: reason")
227227
}
228228
guard !result.isEmpty else {
229229
return .empty(.init(loc(_start)))
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
//
10+
//===----------------------------------------------------------------------===//
11+
12+
public struct Unsupported: Error, CustomStringConvertible {
13+
var message: String
14+
var file: String
15+
var line: Int
16+
17+
public var description: String { """
18+
Unsupported: '\(message)'
19+
\(file):\(line)
20+
"""
21+
}
22+
23+
public init(
24+
_ s: String,
25+
file: StaticString = #file,
26+
line: UInt = #line
27+
) {
28+
self.message = s
29+
self.file = file.description
30+
self.line = Int(asserting: line)
31+
}
32+
}
33+
34+
public struct Unreachable: Error, CustomStringConvertible {
35+
var message: String
36+
var file: String
37+
var line: Int
38+
39+
public var description: String { """
40+
Unreachable: '\(message)'
41+
\(file):\(line)
42+
"""
43+
}
44+
45+
public init(
46+
_ s: String,
47+
file: StaticString = #file,
48+
line: UInt = #line
49+
) {
50+
self.message = s
51+
self.file = file.description
52+
self.line = Int(asserting: line)
53+
}
54+
}

Sources/_MatchingEngine/Utility/Misc.swift

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,6 @@ extension FixedWidthInteger {
1515
}
1616
}
1717

18-
// TODO: Replace all fatal error unreachables with these calls.
19-
// We will likely want to convert them to unhandleable throws
20-
// or something similar.
21-
func unreachable(_ s: @autoclosure () -> String) -> Never {
22-
fatalError("unreachable \(s())")
23-
}
24-
func unreachable() -> Never {
25-
fatalError("unreachable")
26-
}
27-
2818
extension Substring {
2919
var string: String { String(self) }
3020
}

Sources/_StringProcessing/ByteCodeGen.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ extension Compiler {
55
var options: MatchingOptions
66
var builder = _MatchingEngine.Program<String>.Builder()
77

8-
mutating func finish() -> _MatchingEngine.Program<String> {
8+
mutating func finish(
9+
) throws -> _MatchingEngine.Program<String> {
910
builder.buildAccept()
10-
return builder.assemble()
11+
return try builder.assemble()
1112
}
1213
}
1314
}
@@ -90,7 +91,7 @@ extension Compiler.ByteCodeGen {
9091

9192
case .resetStartOfMatch:
9293
// FIXME: Figure out how to communicate this out
93-
throw unsupported(#"\K (reset/keep assertion)"#)
94+
throw Unsupported(#"\K (reset/keep assertion)"#)
9495

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

101102
case .textSegment:
102103
// This we should be able to do!
103-
throw unsupported(#"\y (text segment)"#)
104+
throw Unsupported(#"\y (text segment)"#)
104105

105106
case .notTextSegment:
106107
// This we should be able to do!
107-
throw unsupported(#"\Y (not text segment)"#)
108+
throw Unsupported(#"\Y (not text segment)"#)
108109

109110
case .startOfLine:
110111
builder.buildAssert { (input, pos, bounds) in
@@ -204,7 +205,7 @@ extension Compiler.ByteCodeGen {
204205
_ child: DSLTree.Node
205206
) throws {
206207
guard kind.forwards else {
207-
throw unsupported("backwards assertions")
208+
throw Unsupported("backwards assertions")
208209
}
209210

210211
let positive = kind.positive
@@ -259,7 +260,7 @@ extension Compiler.ByteCodeGen {
259260
switch kind {
260261
case .lookahead, .negativeLookahead,
261262
.lookbehind, .negativeLookbehind:
262-
fatalError("unreachable")
263+
throw Unreachable("TODO: reason")
263264

264265
case .capture, .namedCapture:
265266
let cap = builder.makeCapture()
@@ -299,7 +300,7 @@ extension Compiler.ByteCodeGen {
299300
// Ok
300301
break
301302
default:
302-
fatalError("unreachable?")
303+
throw Unreachable("TODO: reason")
303304
}
304305

305306
// Compiler and/or parser should enforce these invariants

Sources/_StringProcessing/Compiler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class Compiler {
3434
// TODO: Handle global options
3535
var codegen = ByteCodeGen(options: options)
3636
try codegen.emitNode(tree.root)
37-
let program = codegen.finish()
37+
let program = try codegen.finish()
3838
return RegexProgram(program: program)
3939
}
4040
}

0 commit comments

Comments
 (0)