From e77d690e36af83f4efbff742a6687ef666896173 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Wed, 1 Nov 2017 00:16:02 -0700 Subject: [PATCH] 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. --- .../v4/test/runtime/swift/BaseSwiftTest.java | 4 ++-- .../Sources/Antlr4/ANTLRFileStream.swift | 22 +++++-------------- runtime/Swift/Sources/Antlr4/misc/Utils.swift | 13 ----------- 3 files changed, 7 insertions(+), 32 deletions(-) diff --git a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java index 90dc052454..c8abbdc8df 100644 --- a/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java +++ b/runtime-testsuite/test/org/antlr/v4/test/runtime/swift/BaseSwiftTest.java @@ -281,7 +281,7 @@ private void writeParserTestFile(String parserName, "\n" + "do {\n" + "let args = CommandLine.arguments\n" + - "let input = ANTLRFileStream(args[1])\n" + + "let input = try ANTLRFileStream(args[1])\n" + "let lex = (input)\n" + "let tokens = CommonTokenStream(lex)\n" + "\n" + @@ -327,7 +327,7 @@ private void writeLexerTestFile(String lexerName, boolean showDFA) { "setbuf(stdout, nil)\n" + "let args = CommandLine.arguments\n" + - "let input = ANTLRFileStream(args[1])\n" + + "let input = try ANTLRFileStream(args[1])\n" + "let lex = (input)\n" + "let tokens = CommonTokenStream(lex)\n" + diff --git a/runtime/Swift/Sources/Antlr4/ANTLRFileStream.swift b/runtime/Swift/Sources/Antlr4/ANTLRFileStream.swift index 9ed7ac9efd..96d4162df9 100644 --- a/runtime/Swift/Sources/Antlr4/ANTLRFileStream.swift +++ b/runtime/Swift/Sources/Antlr4/ANTLRFileStream.swift @@ -9,30 +9,18 @@ import Foundation public class ANTLRFileStream: ANTLRInputStream { - internal var fileName: String + private let fileName: String - public convenience override init(_ fileName: String) { - self.init(fileName, nil) - } - - public init(_ fileName: String, _ encoding: String.Encoding?) { + public init(_ fileName: String, _ encoding: String.Encoding? = nil) throws { self.fileName = fileName super.init() - load(fileName, encoding) - } - - 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 + let fileContents = try String(contentsOfFile: fileName, encoding: encoding ?? .utf8) + data = Array(fileContents) + n = data.count } override public func getSourceName() -> String { return fileName } - } diff --git a/runtime/Swift/Sources/Antlr4/misc/Utils.swift b/runtime/Swift/Sources/Antlr4/misc/Utils.swift index c025becdc1..f8c2aef9f8 100644 --- a/runtime/Swift/Sources/Antlr4/misc/Utils.swift +++ b/runtime/Swift/Sources/Antlr4/misc/Utils.swift @@ -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 { var m = Dictionary() for (index,v) in keys.enumerated() {