Skip to content

Commit

Permalink
Merge pull request #2115 from ewanmellor/swift-minor-tidups
Browse files Browse the repository at this point in the history
[Swift] Some minor tidyups.
  • Loading branch information
parrt authored Nov 29, 2017
2 parents 3536a84 + 6cc35ad commit ed37fa2
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 59 deletions.
13 changes: 6 additions & 7 deletions runtime/Swift/Sources/Antlr4/atn/ATN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand Down
30 changes: 5 additions & 25 deletions runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

}
28 changes: 13 additions & 15 deletions runtime/Swift/Sources/Antlr4/atn/LexerATNSimulator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
15 changes: 6 additions & 9 deletions runtime/Swift/Sources/Antlr4/dfa/DFA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

///
Expand All @@ -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 {
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions runtime/Swift/Sources/Antlr4/misc/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed37fa2

Please sign in to comment.