[Swift] Tidy-ups around addDFAEdge.

Do some tidy-ups around addDFAEdge, improving the handling of nils so that
we're not forcing or nil-checking so much.
This commit is contained in:
Ewan Mellor 2018-11-14 21:55:31 -08:00
parent 8f42439943
commit 0f1614dd99
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
2 changed files with 19 additions and 30 deletions

View File

@ -638,7 +638,7 @@ open class LexerATNSimulator: ATNSimulator {
}
final func addDFAEdge(_ from: DFAState,
private final func addDFAEdge(_ from: DFAState,
_ t: Int,
_ q: ATNConfigSet) -> DFAState {
///
@ -665,7 +665,7 @@ open class LexerATNSimulator: ATNSimulator {
return to
}
final func addDFAEdge(_ p: DFAState, _ t: Int, _ q: DFAState) {
private final func addDFAEdge(_ p: DFAState, _ t: Int, _ q: DFAState) {
if t < LexerATNSimulator.MIN_DFA_EDGE || t > LexerATNSimulator.MAX_DFA_EDGE {
// Only track edges within the DFA bounds
return

View File

@ -581,20 +581,19 @@ open class ParserATNSimulator: ATNSimulator {
///
func computeTargetState(_ dfa: DFA, _ previousD: DFAState, _ t: Int) throws -> DFAState {
let reach = try computeReachSet(previousD.configs, t, false)
if reach == nil {
guard let reach = try computeReachSet(previousD.configs, t, false) else {
addDFAEdge(dfa, previousD, t, ATNSimulator.ERROR)
return ATNSimulator.ERROR
}
// create new target state; we'll add to DFA after it's complete
var D: DFAState = DFAState(reach!)
let D = DFAState(reach)
let predictedAlt: Int = ParserATNSimulator.getUniqueAlt(reach!)
let predictedAlt = ParserATNSimulator.getUniqueAlt(reach)
if debug {
let altSubSets = PredictionMode.getConflictingAltSubsets(reach!)
print("SLL altSubSets=\(altSubSets), configs=\(reach!), predict=\(predictedAlt), allSubsetsConflict=\(PredictionMode.allSubsetsConflict(altSubSets)), conflictingAlts=\(getConflictingAlts(reach!))")
let altSubSets = PredictionMode.getConflictingAltSubsets(reach)
print("SLL altSubSets=\(altSubSets), configs=\(reach), predict=\(predictedAlt), allSubsetsConflict=\(PredictionMode.allSubsetsConflict(altSubSets)), conflictingAlts=\(getConflictingAlts(reach))")
}
if predictedAlt != ATN.INVALID_ALT_NUMBER {
@ -603,9 +602,9 @@ open class ParserATNSimulator: ATNSimulator {
D.configs.uniqueAlt = predictedAlt
D.prediction = predictedAlt
} else {
if PredictionMode.hasSLLConflictTerminatingPrediction(mode, reach!) {
if PredictionMode.hasSLLConflictTerminatingPrediction(mode, reach) {
// MORE THAN ONE VIABLE ALTERNATIVE
D.configs.conflictingAlts = getConflictingAlts(reach!)
D.configs.conflictingAlts = getConflictingAlts(reach)
D.requiresFullContext = true
// in SLL-only mode, we will stop at this state and return the minimum alt
D.isAcceptState = true
@ -621,8 +620,7 @@ open class ParserATNSimulator: ATNSimulator {
}
// all adds to dfa are done after we've created full D state
D = addDFAEdge(dfa, previousD, t, D)!
return D
return addDFAEdge(dfa, previousD, t, D)
}
final func predicateDFAState(_ dfaState: DFAState, _ decisionState: DecisionState) {
@ -1951,29 +1949,20 @@ open class ParserATNSimulator: ATNSimulator {
/// - parameter t: The input symbol
/// - parameter to: The target state for the edge
///
/// - returns: If `to` is `null`, this method returns `null`;
/// otherwise this method returns the result of calling _#addDFAState_
/// on `to`
/// - returns: the result of calling _#addDFAState_ on `to`
///
@discardableResult
final func addDFAEdge(_ dfa: DFA,
_ from: DFAState?,
private final func addDFAEdge(_ dfa: DFA,
_ from: DFAState,
_ t: Int,
_ to: DFAState?) -> DFAState? {
_ to: DFAState) -> DFAState {
var to = to
if debug {
print("EDGE \(from?.description ?? "nil")) -> \(to?.description ?? "nil") upon \(getTokenName(t))")
print("EDGE \(from) -> \(to) upon \(getTokenName(t))")
}
if to == nil {
return nil
}
to = addDFAState(dfa, to!) // used existing if possible not incoming
if from == nil || t < -1 || t > atn.maxTokenType {
return to
}
guard let from = from else {
to = addDFAState(dfa, to) // used existing if possible not incoming
if t < -1 || t > atn.maxTokenType {
return to
}
dfaStateMutex.synchronized {
@ -1982,7 +1971,7 @@ open class ParserATNSimulator: ATNSimulator {
from.edges = [DFAState?](repeating: nil, count: self.atn.maxTokenType + 1 + 1) //new DFAState[atn.maxTokenType+1+1];
}
from.edges![t + 1] = to! // connect
from.edges[t + 1] = to // connect
}
if debug {
@ -2007,7 +1996,7 @@ open class ParserATNSimulator: ATNSimulator {
/// state if `D` is already in the DFA, or `D` itself if the
/// state was not already present.
///
final func addDFAState(_ dfa: DFA, _ D: DFAState) -> DFAState {
private final func addDFAState(_ dfa: DFA, _ D: DFAState) -> DFAState {
if D == ATNSimulator.ERROR {
return D
}