Merge pull request #2410 from ewanmellor/swift-tidyups

Swift tidyups
This commit is contained in:
Terence Parr 2018-11-16 08:47:42 -08:00 committed by GitHub
commit 46a5f29fc8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 147 additions and 274 deletions

View File

@ -36,7 +36,7 @@ public class CommonTokenFactory: TokenFactory {
/// The default value is `false` to avoid the performance and memory
/// overhead of copying text for every token unless explicitly requested.
///
internal final var copyText: Bool
internal let copyText: Bool
///
/// Constructs a _org.antlr.v4.runtime.CommonTokenFactory_ with the specified value for

View File

@ -12,9 +12,9 @@
/// prediction.
///
public class FailedPredicateException: RecognitionException {
private final var ruleIndex: Int
private final var predicateIndex: Int
private final var predicate: String?
private let ruleIndex: Int
private let predicateIndex: Int
private let predicate: String?
public init(_ recognizer: Parser, _ predicate: String? = nil, _ message: String? = nil) {
let s = recognizer.getInterpreter().atn.states[recognizer.getState()]!

View File

@ -6,17 +6,17 @@
public class LexerInterpreter: Lexer {
internal final var grammarFileName: String
internal final var atn: ATN
internal let grammarFileName: String
internal let atn: ATN
internal final var ruleNames: [String]
internal final var channelNames: [String]
internal final var modeNames: [String]
internal let ruleNames: [String]
internal let channelNames: [String]
internal let modeNames: [String]
private final var vocabulary: Vocabulary?
private let vocabulary: Vocabulary?
internal final var _decisionToDFA: [DFA]
internal final var _sharedContextCache = PredictionContextCache()
internal let _sharedContextCache = PredictionContextCache()
public init(_ grammarFileName: String, _ vocabulary: Vocabulary, _ ruleNames: Array<String>, _ channelNames: Array<String>, _ modeNames: Array<String>, _ atn: ATN, _ input: CharStream) throws {

View File

@ -9,12 +9,12 @@ public class LexerNoViableAltException: RecognitionException, CustomStringConver
///
/// Matching attempted at what input index?
///
private final var startIndex: Int
private let startIndex: Int
///
/// Which configurations did we try at input.index() that couldn't match input.LA(1)?
///
private final var deadEndConfigs: ATNConfigSet
private let deadEndConfigs: ATNConfigSet
public init(_ lexer: Lexer?,
_ input: CharStream,

View File

@ -17,7 +17,7 @@ public class ListTokenSource: TokenSource {
///
/// The wrapped collection of _org.antlr.v4.runtime.Token_ objects to return.
///
internal final var tokens: [Token]
internal let tokens: [Token]
///
/// The name of the input source. If this value is `null`, a call to
@ -25,7 +25,7 @@ public class ListTokenSource: TokenSource {
/// the next token in _#tokens_ (or the previous token if the end of
/// the input has been reached).
///
private final var sourceName: String?
private let sourceName: String?
///
/// The index into _#tokens_ of token to return by the next call to

View File

@ -13,14 +13,14 @@
public class NoViableAltException: RecognitionException {
/// Which configurations did we try at input.index() that couldn't match input.LT(1)?
private final var deadEndConfigs: ATNConfigSet?
private let deadEndConfigs: ATNConfigSet?
/// The token object at the start index; the input stream might
/// not be buffering tokens so get a reference to it. (At the
/// time the error occurred, of course the stream needs to keep a
/// buffer all of the tokens but later we might not have access to those.)
///
private final var startToken: Token
private let startToken: Token
public convenience init(_ recognizer: Parser) {
// LL(1) error

View File

@ -20,21 +20,20 @@
///
public class ParserInterpreter: Parser {
internal final var grammarFileName: String
internal final var atn: ATN
internal let grammarFileName: String
internal let atn: ATN
/// This identifies StarLoopEntryState's that begin the (...)*
/// precedence loops of left recursive rules.
///
internal final var statesNeedingLeftRecursionContext: BitSet
internal let statesNeedingLeftRecursionContext: BitSet
internal final var decisionToDFA: [DFA]
// not shared like it is for generated parsers
internal final var sharedContextCache: PredictionContextCache =
PredictionContextCache()
internal let sharedContextCache = PredictionContextCache()
internal final var ruleNames: [String]
internal let ruleNames: [String]
private final var vocabulary: Vocabulary
private let vocabulary: Vocabulary
/// Tracks LR rules for adjusting the contexts
internal final var _parentContextStack: Array<(ParserRuleContext?, Int)> =

View File

@ -25,11 +25,11 @@ public class Vocabulary: Hashable {
public static let EMPTY_VOCABULARY: Vocabulary = Vocabulary(EMPTY_NAMES, EMPTY_NAMES, EMPTY_NAMES)
private final var literalNames: [String?]
private let literalNames: [String?]
private final var symbolicNames: [String?]
private let symbolicNames: [String?]
private final var displayNames: [String?]
private let displayNames: [String?]
///
/// Constructs a new instance of _org.antlr.v4.runtime.Vocabulary_ from the specified

View File

@ -8,29 +8,29 @@
public class ATN {
public static let INVALID_ALT_NUMBER = 0
public final var states = [ATNState?]()
public private(set) 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 = [DecisionState]()
public private(set) final var decisionToState = [DecisionState]()
///
/// Maps from rule index to starting state number.
///
public final var ruleToStartState: [RuleStartState]!
public internal(set) final var ruleToStartState: [RuleStartState]!
///
/// Maps from rule index to stop state number.
///
public final var ruleToStopState: [RuleStopState]!
public internal(set) final var ruleToStopState: [RuleStopState]!
///
/// 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.
@ -43,15 +43,15 @@ public class ATN {
/// type if the `ATNDeserializationOptions.generateRuleBypassTransitions`
/// deserialization option was specified; otherwise, this is `null`.
///
public final var ruleToTokenType: [Int]!
public internal(set) final var ruleToTokenType: [Int]!
///
/// For lexer ATNs, this is an array of _org.antlr.v4.runtime.atn.LexerAction_ objects which may
/// be referenced by action transitions in the ATN.
///
public final var lexerActions: [LexerAction]!
public internal(set) final var lexerActions: [LexerAction]!
public final var modeToStartState = [TokensStartState]()
public internal(set) final var modeToStartState = [TokensStartState]()
///
/// Used for runtime deserialization of ATNs from strings

View File

@ -37,7 +37,7 @@ public class ATNConfig: Hashable, CustomStringConvertible {
/// with this config. We track only those contexts pushed during
/// execution of the ATN simulator.
///
public final var context: PredictionContext?
public internal(set) final var context: PredictionContext?
///
/// We cannot execute predicates dependent upon local context unless
@ -62,31 +62,14 @@ public class ATNConfig: Hashable, CustomStringConvertible {
/// _org.antlr.v4.runtime.atn.ATNConfigSet#add(org.antlr.v4.runtime.atn.ATNConfig, DoubleKeyMap)_ method are
/// __completely__ unaffected by the change.
///
public final var reachesIntoOuterContext: Int = 0
//=0 intital by janyou
public internal(set) final var reachesIntoOuterContext: Int = 0
public final let semanticContext: SemanticContext
public init(_ old: ATNConfig) {
// dup
self.state = old.state
self.alt = old.alt
self.context = old.context
self.semanticContext = old.semanticContext
self.reachesIntoOuterContext = old.reachesIntoOuterContext
}
public convenience init(_ state: ATNState,
_ alt: Int,
_ context: PredictionContext?) {
self.init(state, alt, context, SemanticContext.NONE)
}
public init(_ state: ATNState,
_ alt: Int,
_ context: PredictionContext?,
_ semanticContext: SemanticContext) {
_ semanticContext: SemanticContext = SemanticContext.NONE) {
self.state = state
self.alt = alt
self.context = context

View File

@ -10,7 +10,7 @@
/// info about the set, with support for combining similar configurations using a
/// graph-structured stack.
///
public class ATNConfigSet: Hashable, CustomStringConvertible {
public final class ATNConfigSet: Hashable, CustomStringConvertible {
///
/// The reason that we need this is because we don't want the hash map to use
/// the standard hash code and equals. We need all configurations with the same
@ -27,22 +27,22 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
/// fields; in particular, conflictingAlts is set after
/// we've made this readonly.
///
internal final var readonly = false
private var readonly = false
///
/// All configs but hashed by (s, i, _, pi) not including context. Wiped out
/// when we go readonly as this set becomes a DFA state.
///
public final var configLookup: LookupDictionary
private var configLookup: LookupDictionary
///
/// Track the elements as they are added to the set; supports get(i)
///
public final var configs = [ATNConfig]()
public private(set) var configs = [ATNConfig]()
// TODO: these fields make me pretty uncomfortable but nice to pack up info together, saves recomputation
// TODO: can we track conflicts as they are added to save scanning configs later?
public final var uniqueAlt = 0
public internal(set) var uniqueAlt = 0
//TODO no default
///
/// Currently this is only used when we detect SLL conflict; this does
@ -50,13 +50,13 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
/// I should also point out that this seems to include predicated alternatives
/// that have predicates that evaluate to false. Computed in computeTargetState().
///
internal final var conflictingAlts: BitSet?
internal var conflictingAlts: BitSet?
// Used in parser and lexer. In lexer, it indicates we hit a pred
// while computing a closure operation. Don't make a DFA state from this.
public final var hasSemanticContext = false
public internal(set) var hasSemanticContext = false
//TODO no default
public final var dipsIntoOuterContext = false
public internal(set) var dipsIntoOuterContext = false
//TODO no default
///
@ -64,27 +64,18 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
/// LL prediction. It will be used to determine how to merge $. With SLL
/// it's a wildcard whereas it is not for LL context merge.
///
public final var fullCtx: Bool
public let fullCtx: Bool
private var cachedHashCode = -1
public init(_ fullCtx: Bool = true) {
configLookup = LookupDictionary()
public init(_ fullCtx: Bool = true, isOrdered: Bool = false) {
configLookup = isOrdered ? LookupDictionary(type: LookupDictionaryType.ordered) : LookupDictionary()
self.fullCtx = fullCtx
}
public convenience init(_ old: ATNConfigSet) {
self.init(old.fullCtx)
try! addAll(old)
self.uniqueAlt = old.uniqueAlt
self.conflictingAlts = old.conflictingAlts
self.hasSemanticContext = old.hasSemanticContext
self.dipsIntoOuterContext = old.dipsIntoOuterContext
}
//override
@discardableResult
public final func add(_ config: ATNConfig) throws -> Bool {
public func add(_ config: ATNConfig) throws -> Bool {
var mergeCache : DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext>? = nil
return try add(config, &mergeCache)
}
@ -100,7 +91,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
/// _#hasSemanticContext_ when necessary.
///
@discardableResult
public final func add(
public func add(
_ config: ATNConfig,
_ mergeCache: inout DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext>?) throws -> Bool {
if readonly {
@ -140,7 +131,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return true
}
public final func getOrAdd(_ config: ATNConfig) -> ATNConfig {
public func getOrAdd(_ config: ATNConfig) -> ATNConfig {
return configLookup.getOrAdd(config)
}
@ -149,11 +140,11 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
///
/// Return a List holding list of configs
///
public final func elements() -> [ATNConfig] {
public func elements() -> [ATNConfig] {
return configs
}
public final func getStates() -> Set<ATNState> {
public func getStates() -> Set<ATNState> {
var states = Set<ATNState>(minimumCapacity: configs.count)
for config in configs {
states.insert(config.state)
@ -169,7 +160,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
///
/// - since: 4.3
///
public final func getAlts() -> BitSet {
public func getAlts() -> BitSet {
let alts = BitSet()
for config in configs {
try! alts.set(config.alt)
@ -177,7 +168,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return alts
}
public final func getPredicates() -> [SemanticContext] {
public func getPredicates() -> [SemanticContext] {
var preds = [SemanticContext]()
for config in configs {
if config.semanticContext != SemanticContext.NONE {
@ -187,11 +178,11 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return preds
}
public final func get(_ i: Int) -> ATNConfig {
public func get(_ i: Int) -> ATNConfig {
return configs[i]
}
public final func optimizeConfigs(_ interpreter: ATNSimulator) throws {
public func optimizeConfigs(_ interpreter: ATNSimulator) throws {
if readonly {
throw ANTLRError.illegalState(msg: "This set is readonly")
}
@ -205,7 +196,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
}
@discardableResult
public final func addAll(_ coll: ATNConfigSet) throws -> Bool {
public func addAll(_ coll: ATNConfigSet) throws -> Bool {
for c in coll.configs {
try add(c)
}
@ -232,27 +223,26 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return hashCode
}
public final var count: Int {
public var count: Int {
return configs.count
}
public final func size() -> Int {
public func size() -> Int {
return configs.count
}
public final func isEmpty() -> Bool {
public func isEmpty() -> Bool {
return configs.isEmpty
}
public final func contains(_ o: ATNConfig) -> Bool {
public func contains(_ o: ATNConfig) -> Bool {
return configLookup.contains(o)
}
public final func clear() throws {
public func clear() throws {
if readonly {
throw ANTLRError.illegalState(msg: "This set is readonly")
}
@ -261,11 +251,11 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
configLookup.removeAll()
}
public final func isReadonly() -> Bool {
public func isReadonly() -> Bool {
return readonly
}
public final func setReadonly(_ readonly: Bool) {
public func setReadonly(_ readonly: Bool) {
self.readonly = readonly
configLookup.removeAll()
@ -294,14 +284,14 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
/// public <T> func toArray(a : [T]) -> [T] {
/// return configLookup.toArray(a);
///
private final func configHash(_ stateNumber: Int,_ context: PredictionContext?) -> Int{
private func configHash(_ stateNumber: Int,_ context: PredictionContext?) -> Int{
var hashCode = MurmurHash.initialize(7)
hashCode = MurmurHash.update(hashCode, stateNumber)
hashCode = MurmurHash.update(hashCode, context)
return MurmurHash.finish(hashCode, 2)
}
public final func getConflictingAltSubsets() -> [BitSet] {
public func getConflictingAltSubsets() -> [BitSet] {
let length = configs.count
var configToAlts = [Int: BitSet]()
@ -340,7 +330,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
}
//for DFAState
public final func getAltSet() -> Set<Int>? {
public func getAltSet() -> Set<Int>? {
if configs.isEmpty {
return nil
}
@ -352,7 +342,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
}
//for DiagnosticErrorListener
public final func getAltBitSet() -> BitSet {
public func getAltBitSet() -> BitSet {
let result = BitSet()
for config in configs {
try! result.set(config.alt)
@ -361,7 +351,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
}
//LexerATNSimulator
public final var firstConfigWithRuleStopState: ATNConfig? {
public var firstConfigWithRuleStopState: ATNConfig? {
for config in configs {
if config.state is RuleStopState {
return config
@ -373,7 +363,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
//ParserATNSimulator
public final func getUniqueAlt() -> Int {
public func getUniqueAlt() -> Int {
var alt = ATN.INVALID_ALT_NUMBER
for config in configs {
if alt == ATN.INVALID_ALT_NUMBER {
@ -385,7 +375,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return alt
}
public final func removeAllConfigsNotInRuleStopState(_ mergeCache: inout DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext>?,_ lookToEndOfRule: Bool,_ atn: ATN) -> ATNConfigSet {
public func removeAllConfigsNotInRuleStopState(_ mergeCache: inout DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext>?,_ lookToEndOfRule: Bool,_ atn: ATN) -> ATNConfigSet {
if PredictionMode.allConfigsInRuleStopStates(self) {
return self
}
@ -409,7 +399,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return result
}
public final func applyPrecedenceFilter(_ mergeCache: inout DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext>?,_ parser: Parser,_ _outerContext: ParserRuleContext!) throws -> ATNConfigSet {
public func applyPrecedenceFilter(_ mergeCache: inout DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext>?,_ parser: Parser,_ _outerContext: ParserRuleContext!) throws -> ATNConfigSet {
let configSet = ATNConfigSet(fullCtx)
var statesFromAlt1 = [Int: PredictionContext]()
@ -484,7 +474,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return (nPredAlts == 0 ? nil : altToPred)
}
public final func getAltThatFinishedDecisionEntryRule() -> Int {
public func getAltThatFinishedDecisionEntryRule() -> Int {
let alts = IntervalSet()
for config in configs {
if config.getOuterContextDepth() > 0 ||
@ -509,7 +499,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
/// Assumption: the input stream has been restored to the starting point
/// prediction, which is where predicates need to evaluate.
///
public final func splitAccordingToSemanticValidity(
public func splitAccordingToSemanticValidity(
_ outerContext: ParserRuleContext,
_ evalSemanticContext: (SemanticContext, ParserRuleContext, Int, Bool) throws -> Bool) rethrows -> (ATNConfigSet, ATNConfigSet) {
let succeeded = ATNConfigSet(fullCtx)
@ -529,7 +519,7 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return (succeeded, failed)
}
public final func dupConfigsWithoutSemanticPredicates() -> ATNConfigSet {
public func dupConfigsWithoutSemanticPredicates() -> ATNConfigSet {
let dup = ATNConfigSet()
for config in configs {
let c = ATNConfig(config, SemanticContext.NONE)
@ -538,11 +528,11 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
return dup
}
public final var hasConfigInRuleStopState: Bool {
public var hasConfigInRuleStopState: Bool {
return configs.contains(where: { $0.state is RuleStopState })
}
public final var allConfigsInRuleStopStates: Bool {
public var allConfigsInRuleStopStates: Bool {
return !configs.contains(where: { !($0.state is RuleStopState) })
}
}

View File

@ -66,8 +66,6 @@
///
///
public class ATNState: Hashable, CustomStringConvertible {
public static let INITIAL_NUM_TRANSITIONS: Int = 4
// constants for serialization
public static let INVALID_TYPE: Int = 0
public static let BASIC: Int = 1
@ -107,23 +105,22 @@ public class ATNState: Hashable, CustomStringConvertible {
///
public final var atn: ATN? = nil
public final var stateNumber: Int = INVALID_STATE_NUMBER
public internal(set) final var stateNumber: Int = INVALID_STATE_NUMBER
public final var ruleIndex: Int?
public internal(set) final var ruleIndex: Int?
// at runtime, we don't have Rule objects
public final var epsilonOnlyTransitions: Bool = false
public private(set) final var epsilonOnlyTransitions: Bool = false
///
/// Track the transitions emanating from this ATN state.
///
internal final var transitions: Array<Transition> = Array<Transition>()
//Array<Transition>(INITIAL_NUM_TRANSITIONS);
internal private(set) final var transitions = [Transition]()
///
/// Used to cache lookahead during parsing, not used during construction
///
public final var nextTokenWithinRule: IntervalSet?
public internal(set) final var nextTokenWithinRule: IntervalSet?
public var hashValue: Int {

View File

@ -11,7 +11,7 @@ public class ArrayPredictionContext: PredictionContext {
/// from _#EMPTY_ and non-empty. We merge _#EMPTY_ by using null parent and
/// returnState == _#EMPTY_RETURN_STATE_.
///
public final var parents: [PredictionContext?]
public private(set) final var parents: [PredictionContext?]
///
/// Sorted for merge, no duplicates; if present,

View File

@ -23,7 +23,7 @@ public class DecisionInfo: CustomStringConvertible {
///
/// The decision number, which is an index into _org.antlr.v4.runtime.atn.ATN#decisionToState_.
///
public final var decision: Int
public private(set) final var decision: Int
///
/// The total number of times _org.antlr.v4.runtime.atn.ParserATNSimulator#adaptivePredict_ was

View File

@ -69,7 +69,7 @@ open class LexerATNSimulator: ATNSimulator {
///
public var charPositionInLine = 0
public final var decisionToDFA: [DFA]
public private(set) final var decisionToDFA: [DFA]
internal var mode = Lexer.DEFAULT_MODE
@ -286,7 +286,7 @@ open class LexerATNSimulator: ATNSimulator {
///
internal func computeTargetState(_ input: CharStream, _ s: DFAState, _ t: Int) throws -> DFAState {
let reach = OrderedATNConfigSet()
let reach = ATNConfigSet(true, isOrdered: true)
// if we don't find an existing DFA state
// Fill reach starting from closure, following t transitions
@ -404,7 +404,7 @@ open class LexerATNSimulator: ATNSimulator {
final func computeStartState(_ input: CharStream,
_ p: ATNState) throws -> ATNConfigSet {
let initialContext = PredictionContext.EMPTY
let configs = OrderedATNConfigSet()
let configs = ATNConfigSet(true, isOrdered: true)
let length = p.getNumberOfTransitions()
for i in 0..<length {
let target = p.transition(i).target
@ -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

@ -15,7 +15,7 @@
///
public final class LexerModeAction: LexerAction, CustomStringConvertible {
fileprivate final var mode: Int
fileprivate let mode: Int
///
/// Constructs a new `mode` action with the specified mode value.

View File

@ -15,7 +15,7 @@
///
public final class LexerPushModeAction: LexerAction, CustomStringConvertible {
fileprivate final var mode: Int
fileprivate let mode: Int
///
/// Constructs a new `pushMode` action with the specified mode value.

View File

@ -14,7 +14,7 @@
///
public class LexerTypeAction: LexerAction, CustomStringConvertible {
fileprivate final var type: Int
fileprivate let type: Int
///
/// Constructs a new `type` action with the specified token type value.

View File

@ -34,22 +34,11 @@ public class LookupATNConfig: Hashable {
}
public func ==(lhs: LookupATNConfig, rhs: LookupATNConfig) -> Bool {
if lhs.config === rhs.config {
return true
}
if (lhs is OrderedATNConfig) && (rhs is OrderedATNConfig) {
return lhs.config == rhs.config
}
let same: Bool =
lhs.config.state.stateNumber == rhs.config.state.stateNumber &&
return lhs.config.state.stateNumber == rhs.config.state.stateNumber &&
lhs.config.alt == rhs.config.alt &&
lhs.config.semanticContext == rhs.config.semanticContext
return same
}

View File

@ -1,34 +0,0 @@
///
/// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/// Use of this file is governed by the BSD 3-clause license that
/// can be found in the LICENSE.txt file in the project root.
///
//
// OrderedATNConfig.swift
// objc2swiftwithswith
//
// Created by janyou on 15/9/14.
//
import Foundation
public class OrderedATNConfig: LookupATNConfig {
override
public var hashValue: Int {
return config.hashValue
}
}
//useless
public func ==(lhs: OrderedATNConfig, rhs: OrderedATNConfig) -> Bool {
if lhs.config === rhs.config {
return true
}
return lhs.config == rhs.config
}

View File

@ -1,22 +0,0 @@
///
/// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
/// Use of this file is governed by the BSD 3-clause license that
/// can be found in the LICENSE.txt file in the project root.
///
///
///
/// - Sam Harwell
///
public class OrderedATNConfigSet: ATNConfigSet {
public init() {
super.init(true)
self.configLookup = LookupDictionary(type: LookupDictionaryType.ordered)
}
}

View File

@ -253,7 +253,7 @@ open class ParserATNSimulator: ATNSimulator {
internal final unowned let parser: Parser
public final var decisionToDFA: [DFA]
public private(set) final var decisionToDFA: [DFA]
///
/// SLL, LL, or LL + exact ambig detection?
@ -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
}

View File

@ -12,7 +12,7 @@
///
public final class PrecedencePredicateTransition: AbstractPredicateTransition, CustomStringConvertible {
public final var precedence: Int
public let precedence: Int
public init(_ target: ATNState, _ precedence: Int) {

View File

@ -12,26 +12,15 @@ public final class RuleTransition: Transition {
///
/// Ptr to the rule definition object for this rule ref
///
public final var ruleIndex: Int
public let ruleIndex: Int
// no Rule object at runtime
public final var precedence: Int
public let precedence: Int
///
/// What node to begin computations following ref to rule
///
public final var followState: ATNState
///
/// - Use
/// _#RuleTransition(org.antlr.v4.runtime.atn.RuleStartState, int, int, org.antlr.v4.runtime.atn.ATNState)_ instead.
///
//@Deprecated
public convenience init(_ ruleStart: RuleStartState,
_ ruleIndex: Int,
_ followState: ATNState) {
self.init(ruleStart, ruleIndex, 0, followState)
}
public let followState: ATNState
public init(_ ruleStart: RuleStartState,
_ ruleIndex: Int,

View File

@ -11,7 +11,7 @@
///
public class SetTransition: Transition, CustomStringConvertible {
public final var set: IntervalSet
public let set: IntervalSet
// TODO (sam): should we really allow null here?
public init(_ target: ATNState, _ set: IntervalSet) {

View File

@ -73,7 +73,7 @@ public class Transition {
/// The target of this transition.
///
public final var target: ATNState
public internal(set) final var target: ATNState
init(_ target: ATNState) {

View File

@ -13,7 +13,7 @@ public class DFA: CustomStringConvertible {
public var s0: DFAState?
public final var decision: Int
public let decision: Int
///
/// From which ATN state did we create this DFA?

View File

@ -31,27 +31,27 @@
/// meaning that state was reached via a different set of rule invocations.
///
public class DFAState: Hashable, CustomStringConvertible {
public var stateNumber = -1
public final class DFAState: Hashable, CustomStringConvertible {
public internal(set) var stateNumber = -1
public var configs = ATNConfigSet()
public internal(set) var configs: ATNConfigSet
///
/// `edges[symbol]` points to target of symbol. Shift up by 1 so (-1)
/// _org.antlr.v4.runtime.Token#EOF_ maps to `edges[0]`.
///
public var edges: [DFAState?]!
public internal(set) var edges: [DFAState?]!
public var isAcceptState = false
public internal(set) var isAcceptState = false
///
/// if accept state, what ttype do we match or alt do we predict?
/// This is set to _org.antlr.v4.runtime.atn.ATN#INVALID_ALT_NUMBER_ when _#predicates_`!=null` or
/// _#requiresFullContext_.
///
public var prediction = 0
public internal(set) var prediction = 0
public var lexerActionExecutor: LexerActionExecutor!
public internal(set) var lexerActionExecutor: LexerActionExecutor?
///
/// Indicates that this state was created during SLL prediction that
@ -59,7 +59,7 @@ public class DFAState: Hashable, CustomStringConvertible {
/// _org.antlr.v4.runtime.atn.ParserATNSimulator#execATN_ invocations immediately jumped doing
/// full context prediction if this field is true.
///
public var requiresFullContext = false
public internal(set) var requiresFullContext = false
///
/// During SLL parsing, this is a list of predicates associated with the
@ -75,16 +75,16 @@ public class DFAState: Hashable, CustomStringConvertible {
/// This list is computed by _org.antlr.v4.runtime.atn.ParserATNSimulator#predicateDFAState_.
///
public var predicates: [PredPrediction]?
public internal(set) var predicates: [PredPrediction]?
///
/// Map a predicate to a predicted alternative.
///
public class PredPrediction: CustomStringConvertible {
public final var pred: SemanticContext
public final class PredPrediction: CustomStringConvertible {
public let pred: SemanticContext
// never null; at least SemanticContext.NONE
public final var alt: Int
public let alt: Int
public init(_ pred: SemanticContext, _ alt: Int) {
self.alt = alt
@ -96,13 +96,6 @@ public class DFAState: Hashable, CustomStringConvertible {
}
}
public init() {
}
public init(_ stateNumber: Int) {
self.stateNumber = stateNumber
}
public init(_ configs: ATNConfigSet) {
self.configs = configs
}
@ -122,19 +115,6 @@ public class DFAState: Hashable, CustomStringConvertible {
return MurmurHash.finish(hash, 1)
}
///
/// Two _org.antlr.v4.runtime.dfa.DFAState_ instances are equal if their ATN configuration sets
/// are the same. This method is used to see if a state already exists.
///
/// Because the number of alternatives and number of ATN configurations are
/// finite, there is a finite number of DFA states that can be processed.
/// This is necessary to show that the algorithm terminates.
///
/// Cannot test the DFA state numbers here because in
/// _org.antlr.v4.runtime.atn.ParserATNSimulator#addDFAState_ we need to know if any other state
/// exists that has this exact set of ATN configurations. The
/// _#stateNumber_ is irrelevant.
///
public var description: String {
var buf = "\(stateNumber):\(configs)"
if isAcceptState {
@ -150,6 +130,19 @@ public class DFAState: Hashable, CustomStringConvertible {
}
}
///
/// Two _org.antlr.v4.runtime.dfa.DFAState_ instances are equal if their ATN configuration sets
/// are the same. This method is used to see if a state already exists.
///
/// Because the number of alternatives and number of ATN configurations are
/// finite, there is a finite number of DFA states that can be processed.
/// This is necessary to show that the algorithm terminates.
///
/// Cannot test the DFA state numbers here because in
/// _org.antlr.v4.runtime.atn.ParserATNSimulator#addDFAState_ we need to know if any other state
/// exists that has this exact set of ATN configurations. The
/// _#stateNumber_ is irrelevant.
///
public func ==(lhs: DFAState, rhs: DFAState) -> Bool {
if lhs === rhs {
return true

View File

@ -15,16 +15,16 @@ public class RuleTagToken: Token, CustomStringConvertible {
///
/// This is the backing field for _#getRuleName_.
///
private final var ruleName: String
private let ruleName: String
///
/// The token type for the current token. This is the token type assigned to
/// the bypass alternative for the rule during ATN deserialization.
///
private final var bypassTokenType: Int
private let bypassTokenType: Int
///
/// This is the backing field for _#getLabel_.
///
private final var label: String?
private let label: String?
public var visited = false