[Swift] Some minor tidyups.

Minor tidyups in the Swift runtime.  No semantic change.
This commit is contained in:
Ewan Mellor 2017-11-09 15:05:26 -08:00
parent e77d690e36
commit e2f4cdc68d
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
5 changed files with 21 additions and 46 deletions

View File

@ -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<ATNState?> = Array<ATNState?>()
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<DecisionState> = Array<DecisionState>()
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<TokensStartState> = Array<TokensStartState>()
public final var modeToStartState = [TokensStartState]()
///
/// Used for runtime deserialization of ATNs from strings

View File

@ -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)
@ -548,42 +545,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
}

View File

@ -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

View File

@ -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 {
@ -151,8 +148,8 @@ public class DFA: CustomStringConvertible {
///
/// Return a list of all states in this DFA, ordered by state number.
///
public func getStates() -> Array<DFAState> {
var result: Array<DFAState> = Array<DFAState>(states.keys)
public func getStates() -> [DFAState] {
var result = [DFAState](states.keys)
result = result.sorted {
$0.stateNumber < $1.stateNumber

View File

@ -34,9 +34,9 @@ public class Utils {
}
public static func toMap(_ keys: [String]) -> Dictionary<String, Int> {
var m = Dictionary<String, Int>()
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