[Swift] Make ATNDeserializationOptions a simple struct.

The previous code was ported from Java, which resulted in 70 lines of code
to hold two bools.  Use a struct instead.
This commit is contained in:
Ewan Mellor 2017-11-30 00:36:04 -08:00
parent f8e63d53c5
commit b4f3a619fd
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
4 changed files with 12 additions and 79 deletions

View File

@ -12,7 +12,7 @@ import Foundation
/// This field maps from the serialized ATN string to the deserialized _org.antlr.v4.runtime.atn.ATN_ with
/// bypass alternatives.
///
/// - SeeAlso: org.antlr.v4.runtime.atn.ATNDeserializationOptions#isGenerateRuleBypassTransitions()
/// - SeeAlso: `ATNDeserializationOptions.generateRuleBypassTransitions`
///
private var bypassAltsAtnCache = [String: ATN]()
@ -421,9 +421,9 @@ open class Parser: Recognizer<ParserATNSimulator> {
return cachedResult
}
let deserializationOptions = ATNDeserializationOptions()
try! deserializationOptions.setGenerateRuleBypassTransitions(true)
let result = try! ATNDeserializer(deserializationOptions).deserialize(Array(serializedAtn))
var opts = ATNDeserializationOptions()
opts.generateRuleBypassTransitions = true
let result = try! ATNDeserializer(opts).deserialize(Array(serializedAtn))
bypassAltsAtnCache[serializedAtn] = result
return result
}

View File

@ -40,8 +40,7 @@ public class ATN {
///
/// For lexer ATNs, this maps the rule index to the resulting token type.
/// For parser ATNs, this maps the rule index to the generated bypass token
/// type if the
/// _org.antlr.v4.runtime.atn.ATNDeserializationOptions#isGenerateRuleBypassTransitions_
/// type if the `ATNDeserializationOptions.generateRuleBypassTransitions`
/// deserialization option was specified; otherwise, this is `null`.
///
public final var ruleToTokenType: [Int]!

View File

@ -4,73 +4,7 @@
/// can be found in the LICENSE.txt file in the project root.
///
///
///
/// - Sam Harwell
///
public class ATNDeserializationOptions {
static let defaultOptions: ATNDeserializationOptions = {
let defaultOptions = ATNDeserializationOptions()
defaultOptions.makeReadOnly()
return defaultOptions
}()
private var readOnly: Bool = false
private var verifyATN: Bool
private var generateRuleBypassTransitions: Bool
public init() {
self.verifyATN = true
self.generateRuleBypassTransitions = false
}
public init(_ options: ATNDeserializationOptions) {
self.verifyATN = options.verifyATN
self.generateRuleBypassTransitions = options.generateRuleBypassTransitions
}
public static func getDefaultOptions() -> ATNDeserializationOptions {
return defaultOptions
}
public final func isReadOnly() -> Bool {
return readOnly
}
public final func makeReadOnly() {
readOnly = true
}
public final func isVerifyATN() -> Bool {
return verifyATN
}
public final func setVerifyATN(_ verifyATN: Bool) throws {
try throwIfReadOnly()
self.verifyATN = verifyATN
}
public final func isGenerateRuleBypassTransitions() -> Bool {
return generateRuleBypassTransitions
}
public final func setGenerateRuleBypassTransitions(_ generateRuleBypassTransitions: Bool) throws {
try throwIfReadOnly()
self.generateRuleBypassTransitions = generateRuleBypassTransitions
}
internal func throwIfReadOnly() throws {
if isReadOnly() {
throw ANTLRError.illegalState(msg: "This object is readonly")
}
}
public struct ATNDeserializationOptions {
public var verifyATN = true
public var generateRuleBypassTransitions = false
}

View File

@ -60,7 +60,7 @@ public class ATNDeserializer {
private let deserializationOptions: ATNDeserializationOptions
public init(_ deserializationOptions: ATNDeserializationOptions? = nil) {
self.deserializationOptions = deserializationOptions ?? ATNDeserializationOptions.getDefaultOptions()
self.deserializationOptions = deserializationOptions ?? ATNDeserializationOptions()
}
///
@ -667,13 +667,13 @@ public class ATNDeserializer {
private func finalizeATN(_ atn: ATN) throws {
markPrecedenceDecisions(atn)
if deserializationOptions.isVerifyATN() {
if deserializationOptions.verifyATN {
try verifyATN(atn)
}
if deserializationOptions.isGenerateRuleBypassTransitions() && atn.grammarType == ATNType.parser {
if deserializationOptions.generateRuleBypassTransitions && atn.grammarType == ATNType.parser {
try generateRuleBypassTransitions(atn)
if deserializationOptions.isVerifyATN() {
if deserializationOptions.verifyATN {
// reverify after modification
try verifyATN(atn)
}