diff --git a/runtime/Swift/Sources/Antlr4/atn/ATN.swift b/runtime/Swift/Sources/Antlr4/atn/ATN.swift index 7c1b0595b..be7565ade 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATN.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATN.swift @@ -6,17 +6,16 @@ public class ATN { - public static let INVALID_ALT_NUMBER: Int = 0 + public static let INVALID_ALT_NUMBER = 0 - - public final var states: Array = Array() + public final var states = [ATNState?]() /// /// Each subrule/rule is a decision point and we must track them so we /// can go back later and build DFA predictors for them. This includes /// all the rules, subrules, optional blocks, ()+, ()* etc... /// - public final var decisionToState: Array = Array() + public final var decisionToState = [DecisionState]() /// /// Maps from rule index to starting state number. @@ -35,12 +34,12 @@ public class ATN { /// /// The type of the ATN. /// - public let grammarType: ATNType! + public let grammarType: ATNType! /// /// The maximum value for any symbol recognized by a transition in the ATN. /// - public let maxTokenType: Int + public let maxTokenType: Int /// /// For lexer ATNs, this maps the rule index to the resulting token type. @@ -57,7 +56,7 @@ public class ATN { /// public final var lexerActions: [LexerAction]! - public final var modeToStartState: Array = Array() + public final var modeToStartState = [TokensStartState]() /// /// Used for runtime deserialization of ATNs from strings diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift index b1c682533..78901e6ac 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift @@ -68,13 +68,10 @@ public class ATNConfigSet: Hashable, CustomStringConvertible { private var cachedHashCode = -1 - public init(_ fullCtx: Bool) { + public init(_ fullCtx: Bool = true) { configLookup = LookupDictionary() self.fullCtx = fullCtx } - public convenience init() { - self.init(true) - } public convenience init(_ old: ATNConfigSet) { self.init(old.fullCtx) @@ -542,42 +539,25 @@ public class ATNConfigSet: Hashable, CustomStringConvertible { } public final var hasConfigInRuleStopState: Bool { - for config in configs { - if config.state is RuleStopState { - return true - } - } - - return false + return configs.contains(where: { $0.state is RuleStopState }) } public final var allConfigsInRuleStopStates: Bool { - for config in configs { - if !(config.state is RuleStopState) { - return false - } - } - - return true + return !configs.contains(where: { !($0.state is RuleStopState) }) } } public func ==(lhs: ATNConfigSet, rhs: ATNConfigSet) -> Bool { - if lhs === rhs { return true } - let same: Bool = - lhs.configs == rhs.configs && // includes stack context + return + lhs.configs == rhs.configs && // includes stack context lhs.fullCtx == rhs.fullCtx && lhs.uniqueAlt == rhs.uniqueAlt && lhs.conflictingAlts == rhs.conflictingAlts && lhs.hasSemanticContext == rhs.hasSemanticContext && lhs.dipsIntoOuterContext == rhs.dipsIntoOuterContext - - - return same - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerATNSimulator.swift b/runtime/Swift/Sources/Antlr4/atn/LexerATNSimulator.swift index 7f541ecc4..7f84feba4 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerATNSimulator.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerATNSimulator.swift @@ -15,8 +15,7 @@ open class LexerATNSimulator: ATNSimulator { public let dfa_debug = false public static let MIN_DFA_EDGE = 0 - public static let MAX_DFA_EDGE = 127 - // forces unicode to stay in ATN + public static let MAX_DFA_EDGE = 127 // forces unicode to stay in ATN /// /// When we hit an accept state in either the DFA or the ATN, we @@ -115,24 +114,23 @@ open class LexerATNSimulator: ATNSimulator { open func match(_ input: CharStream, _ mode: Int) throws -> Int { LexerATNSimulator.match_calls += 1 + self.mode = mode var mark = input.mark() - do { - self.startIndex = input.index() - self.prevAccept.reset() - var dfa = decisionToDFA[mode] - defer { - try! input.release(mark) - } - - if dfa.s0 == nil { - return try matchATN(input) - } else { - return try execATN(input, dfa.s0!) - } + defer { + try! input.release(mark) } + self.startIndex = input.index() + self.prevAccept.reset() + let dfa = decisionToDFA[mode] + if let s0 = dfa.s0 { + return try execATN(input, s0) + } + else { + return try matchATN(input) + } } override diff --git a/runtime/Swift/Sources/Antlr4/dfa/DFA.swift b/runtime/Swift/Sources/Antlr4/dfa/DFA.swift index 80d6c83fa..6686f8db1 100644 --- a/runtime/Swift/Sources/Antlr4/dfa/DFA.swift +++ b/runtime/Swift/Sources/Antlr4/dfa/DFA.swift @@ -88,14 +88,11 @@ public class DFA: CustomStringConvertible { } - // s0.edges is never null for a precedence DFA - // if (precedence < 0 || precedence >= s0!.edges!.count) { - if precedence < 0 || s0 == nil || - s0!.edges == nil || precedence >= s0!.edges!.count { + guard let s0 = s0, let edges = s0.edges, precedence >= 0, precedence < edges.count else { return nil } - return s0!.edges![precedence] + return edges[precedence] } /// @@ -111,12 +108,12 @@ public class DFA: CustomStringConvertible { public final func setPrecedenceStartState(_ precedence: Int, _ startState: DFAState) throws { if !isPrecedenceDfa() { throw ANTLRError.illegalState(msg: "Only precedence DFAs may contain a precedence start state.") - } - guard let s0 = s0,let edges = s0.edges , precedence >= 0 else { + guard let s0 = s0, let edges = s0.edges, precedence >= 0 else { return } + // synchronization on s0 here is ok. when the DFA is turned into a // precedence DFA, s0 will be initialized once and not updated again dfaStateMutex.synchronized { @@ -133,8 +130,8 @@ public class DFA: CustomStringConvertible { /// /// Return a list of all states in this DFA, ordered by state number. /// - public func getStates() -> Array { - var result: Array = Array(states.keys) + public func getStates() -> [DFAState] { + var result = [DFAState](states.keys) result = result.sorted { $0.stateNumber < $1.stateNumber diff --git a/runtime/Swift/Sources/Antlr4/misc/Utils.swift b/runtime/Swift/Sources/Antlr4/misc/Utils.swift index 080795ca4..8fa339d6b 100644 --- a/runtime/Swift/Sources/Antlr4/misc/Utils.swift +++ b/runtime/Swift/Sources/Antlr4/misc/Utils.swift @@ -32,9 +32,9 @@ public class Utils { } - public static func toMap(_ keys: [String]) -> Dictionary { - var m = Dictionary() - for (index,v) in keys.enumerated() { + public static func toMap(_ keys: [String]) -> [String: Int] { + var m = [String: Int]() + for (index, v) in keys.enumerated() { m[v] = index } return m