Merge pull request #2115 from ewanmellor/swift-minor-tidups

[Swift] Some minor tidyups.
This commit is contained in:
Terence Parr 2017-11-29 09:54:13 -08:00 committed by GitHub
commit ed37fa23e6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 59 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)
@ -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
}

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

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

@ -32,9 +32,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