Report errors in ANTLRFileStream.

Change the initializer to ANTLRFileStream so that it throws any errors that
occur while reading the file.  Previously, it was just dropping any errors on
the floor (inside Utils.readFile).

Remove Utils.readFile, it's not used anywhere else.
This commit is contained in:
Ewan Mellor 2017-11-01 00:16:02 -07:00
parent b4c34da1f0
commit e77d690e36
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
3 changed files with 7 additions and 32 deletions

View File

@ -281,7 +281,7 @@ public class BaseSwiftTest implements RuntimeTestSupport {
"\n" + "\n" +
"do {\n" + "do {\n" +
"let args = CommandLine.arguments\n" + "let args = CommandLine.arguments\n" +
"let input = ANTLRFileStream(args[1])\n" + "let input = try ANTLRFileStream(args[1])\n" +
"let lex = <lexerName>(input)\n" + "let lex = <lexerName>(input)\n" +
"let tokens = CommonTokenStream(lex)\n" + "let tokens = CommonTokenStream(lex)\n" +
"<createParser>\n" + "<createParser>\n" +
@ -327,7 +327,7 @@ public class BaseSwiftTest implements RuntimeTestSupport {
"setbuf(stdout, nil)\n" + "setbuf(stdout, nil)\n" +
"let args = CommandLine.arguments\n" + "let args = CommandLine.arguments\n" +
"let input = ANTLRFileStream(args[1])\n" + "let input = try ANTLRFileStream(args[1])\n" +
"let lex = <lexerName>(input)\n" + "let lex = <lexerName>(input)\n" +
"let tokens = CommonTokenStream(lex)\n" + "let tokens = CommonTokenStream(lex)\n" +

View File

@ -9,30 +9,18 @@
import Foundation import Foundation
public class ANTLRFileStream: ANTLRInputStream { public class ANTLRFileStream: ANTLRInputStream {
internal var fileName: String private let fileName: String
public convenience override init(_ fileName: String) { public init(_ fileName: String, _ encoding: String.Encoding? = nil) throws {
self.init(fileName, nil)
}
public init(_ fileName: String, _ encoding: String.Encoding?) {
self.fileName = fileName self.fileName = fileName
super.init() super.init()
load(fileName, encoding) let fileContents = try String(contentsOfFile: fileName, encoding: encoding ?? .utf8)
} data = Array(fileContents)
n = data.count
public func load(_ fileName: String, _ encoding: String.Encoding?) {
if encoding != nil {
data = Utils.readFile(fileName, encoding!)
} else {
data = Utils.readFile(fileName)
}
self.n = data.count
} }
override override
public func getSourceName() -> String { public func getSourceName() -> String {
return fileName return fileName
} }
} }

View File

@ -34,19 +34,6 @@ public class Utils {
} }
public static func readFile(_ path: String, _ encoding: String.Encoding = String.Encoding.utf8) -> [Character] {
var fileContents: String
do {
fileContents = try String(contentsOfFile: path, encoding: encoding)
} catch {
return [Character]()
}
return Array(fileContents.characters)
}
public static func toMap(_ keys: [String]) -> Dictionary<String, Int> { public static func toMap(_ keys: [String]) -> Dictionary<String, Int> {
var m = Dictionary<String, Int>() var m = Dictionary<String, Int>()
for (index,v) in keys.enumerated() { for (index,v) in keys.enumerated() {