Skip to content
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

Report errors in ANTLRFileStream. #2102

Merged
merged 1 commit into from
Nov 29, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <lexerName>(input)\n" +
"let tokens = CommonTokenStream(lex)\n" +
"<createParser>\n" +
Expand Down Expand Up @@ -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 = <lexerName>(input)\n" +
"let tokens = CommonTokenStream(lex)\n" +

Expand Down
22 changes: 5 additions & 17 deletions runtime/Swift/Sources/Antlr4/ANTLRFileStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

}
13 changes: 0 additions & 13 deletions runtime/Swift/Sources/Antlr4/misc/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
var m = Dictionary<String, Int>()
for (index,v) in keys.enumerated() {
Expand Down