Skip to content
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
8 changes: 4 additions & 4 deletions runtime/Swift/Sources/Antlr4/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import Foundation
///
/// - SeeAlso: `ATNDeserializationOptions.generateRuleBypassTransitions`
///
private var bypassAltsAtnCache = [String: ATN]()
private var bypassAltsAtnCache : ATN? = nil

///
/// mutex for bypassAltsAtnCache updates
Expand Down Expand Up @@ -417,15 +417,15 @@ open class Parser: Recognizer<ParserATNSimulator> {
let serializedAtn = getSerializedATN()

return bypassAltsAtnCacheMutex.synchronized {
if let cachedResult = bypassAltsAtnCache[serializedAtn] {
if let cachedResult = bypassAltsAtnCache {
return cachedResult
}

var opts = ATNDeserializationOptions()
opts.generateRuleBypassTransitions = true
let result = try! ATNDeserializer(opts).deserialize(serializedAtn)
bypassAltsAtnCache[serializedAtn] = result
return result
bypassAltsAtnCache = result
return bypassAltsAtnCache!
}
}

Expand Down
4 changes: 2 additions & 2 deletions runtime/Swift/Sources/Antlr4/Recognizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public protocol RecognizerProtocol {
func getGrammarFileName() -> String
func getParseInfo() -> ParseInfo?
func getRuleNames() -> [String]
func getSerializedATN() -> String
func getSerializedATN() -> [Int]
func getState() -> Int
func getTokenType(_ tokenName: String) -> Int
func getVocabulary() -> Vocabulary
Expand Down Expand Up @@ -95,7 +95,7 @@ open class Recognizer<ATNInterpreter: ATNSimulator>: RecognizerProtocol {
/// For interpreters, we don't know their serialized ATN despite having
/// created the interpreter from it.
///
open func getSerializedATN() -> String {
open func getSerializedATN() -> [Int] {
fatalError("there is no serialized ATN")
}

Expand Down
11 changes: 2 additions & 9 deletions runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@
/// can be found in the LICENSE.txt file in the project root.
///



///
///
/// - Sam Harwell
///

import Foundation

public class ATNDeserializer {
Expand All @@ -22,8 +15,8 @@ public class ATNDeserializer {
self.deserializationOptions = deserializationOptions ?? ATNDeserializationOptions()
}

public func deserialize(_ str: String) throws -> ATN {
let data = str.utf16.map { element in Int(element) }
public func deserialize(_ data: [Int]) throws -> ATN {
// let data = str.utf16.map { element in Int(element) }
var p = 0

let version = data[p]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class InterpreterDataReader {
self.ruleNames = ruleNames
self.channelNames = channelNames
self.modeNames = modeNames
let atnSerialized = String(atnText.map{Character(UnicodeScalar(UInt16($0.trimmingCharacters(in:.whitespaces))!)!)})
let atnSerialized = atnText.map{Int($0.trimmingCharacters(in:.whitespaces))!}
atn = try ATNDeserializer().deserialize(atnSerialized)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ Parser_(parser, funcs, atn, sempredFuncs, ctor, superClass) ::= <<
func getRuleNames() -> [String] { return <parser.name>.ruleNames }

override <accessLevelOpenOK(parser)>
func getSerializedATN() -> String { return <parser.name>._serializedATN }
func getSerializedATN() -> [Int] { return <parser.name>._serializedATN }

override <accessLevelOpenOK(parser)>
func getATN() -> ATN { return <parser.name>._ATN }
Expand Down Expand Up @@ -1029,7 +1029,7 @@ Lexer(lexer, atn, actionFuncs, sempredFuncs, superClass) ::= <<
func getRuleNames() -> [String] { return <lexer.name>.ruleNames }

override <accessLevelOpenOK(lexer)>
func getSerializedATN() -> String { return <lexer.name>._serializedATN }
func getSerializedATN() -> [Int] { return <lexer.name>._serializedATN }

override <accessLevelOpenOK(lexer)>
func getChannelNames() -> [String] { return <lexer.name>.channelNames }
Expand All @@ -1049,9 +1049,9 @@ Lexer(lexer, atn, actionFuncs, sempredFuncs, superClass) ::= <<
>>

SerializedATN(model) ::= <<
static let _serializedATN: String = """
<model.serialized; wrap={\\<\n>}>
"""
static let _serializedATN:[Int] = [
<model.serialized: {s | <s>}; separator=",", wrap>
]
>>

/** Using a type to init value map, try to init a type; if not in table
Expand Down
2 changes: 1 addition & 1 deletion tool/src/org/antlr/v4/codegen/target/SwiftTarget.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public String toString(Object o, String formatString, Locale locale) {

@Override
public boolean isATNSerializedAsInts() {
return false;
return true;
}

@Override
Expand Down