From 509eeefa4afa8c04e6f29bf18f95ab5aed022a6f Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Thu, 28 Mar 2019 15:24:27 -0700 Subject: [PATCH] [Swift] Migrate Swift runtime to Swift 5. This switches from using the deprecated hashValue to hash(into:). It also switches from using index to firstIndex (matching the change in the standard library). In the test template, we switch to using String directly instead of String.characters. This also switches all the Travis macOS tests to use the Xcode 10.2 / Mojave image and changes the Linux Swift tests to download Swift 5.0.1. --- .travis.yml | 18 +++++----- .travis/run-tests-swift.sh | 2 +- .../v4/test/runtime/templates/Swift.test.stg | 6 ++-- .../Sources/Antlr4/UnbufferedCharStream.swift | 2 ++ .../Sources/Antlr4/VocabularySingle.swift | 6 ++-- .../Swift/Sources/Antlr4/atn/ATNConfig.swift | 24 ++++++------- .../Sources/Antlr4/atn/ATNConfigSet.swift | 12 +++---- .../Sources/Antlr4/atn/ATNDeserializer.swift | 4 +-- .../Swift/Sources/Antlr4/atn/ATNState.swift | 5 ++- .../Sources/Antlr4/atn/LexerATNConfig.swift | 22 ++++-------- .../Sources/Antlr4/atn/LexerAction.swift | 2 +- .../Antlr4/atn/LexerActionExecutor.swift | 6 ++-- .../Antlr4/atn/LexerChannelAction.swift | 9 ++--- .../Antlr4/atn/LexerCustomAction.swift | 13 ++----- .../Antlr4/atn/LexerIndexedCustomAction.swift | 13 ++----- .../Sources/Antlr4/atn/LexerModeAction.swift | 13 +++---- .../Sources/Antlr4/atn/LexerMoreAction.swift | 12 ++----- .../Antlr4/atn/LexerPopModeAction.swift | 12 ++----- .../Antlr4/atn/LexerPushModeAction.swift | 13 ++----- .../Sources/Antlr4/atn/LexerSkipAction.swift | 10 ++---- .../Sources/Antlr4/atn/LexerTypeAction.swift | 12 ++----- .../Sources/Antlr4/atn/LookupATNConfig.swift | 13 +++---- .../Antlr4/atn/PredictionContext.swift | 4 +-- .../Sources/Antlr4/atn/SemanticContext.swift | 36 ++++++------------- .../Swift/Sources/Antlr4/dfa/DFAState.swift | 6 ++-- .../Swift/Sources/Antlr4/misc/BitSet.swift | 6 +++- .../Swift/Sources/Antlr4/misc/Interval.swift | 11 +++--- .../Sources/Antlr4/misc/IntervalSet.swift | 20 +++-------- 28 files changed, 107 insertions(+), 205 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0220f4ca2..cd774ceae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -74,42 +74,42 @@ matrix: - clang-3.7 - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=cpp - GROUP=LEXER stage: extended-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=cpp - GROUP=PARSER stage: extended-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=cpp - GROUP=RECURSION stage: extended-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=swift - GROUP=LEXER stage: main-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=swift - GROUP=PARSER stage: main-test - os: osx compiler: clang - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=swift - GROUP=RECURSION @@ -122,19 +122,19 @@ matrix: - GROUP=ALL stage: extended-test - os: osx - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=dotnet - GROUP=LEXER stage: extended-test - os: osx - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=dotnet - GROUP=PARSER stage: extended-test - os: osx - osx_image: xcode10.1 + osx_image: xcode10.2 env: - TARGET=dotnet - GROUP=RECURSION diff --git a/.travis/run-tests-swift.sh b/.travis/run-tests-swift.sh index 0e297c503..677f356b2 100755 --- a/.travis/run-tests-swift.sh +++ b/.travis/run-tests-swift.sh @@ -6,7 +6,7 @@ set -euo pipefail # here since environment variables doesn't pass # across scripts if [ $TRAVIS_OS_NAME == "linux" ]; then - export SWIFT_VERSION=swift-4.2.1 + export SWIFT_VERSION=swift-5.0.1 export SWIFT_HOME=$(pwd)/swift/$SWIFT_VERSION-RELEASE-ubuntu16.04/usr/bin/ export PATH=$SWIFT_HOME:$PATH diff --git a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg index d45168517..3cc31d141 100755 --- a/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg +++ b/runtime-testsuite/resources/org/antlr/v4/test/runtime/templates/Swift.test.stg @@ -133,7 +133,7 @@ open func emit() -> Token { private func handleAcceptPositionForIdentifier() -> Bool { let tokenText = getText() var identifierLength = 0 - while ((identifierLength \< tokenText.characters.count) && isIdentifierChar(tokenText[tokenText.characters.index(tokenText.startIndex, offsetBy: identifierLength)])) { + while ((identifierLength \< tokenText.count) && isIdentifierChar(tokenText[tokenText.index(tokenText.startIndex, offsetBy: identifierLength)])) { identifierLength += 1 } @@ -147,8 +147,8 @@ private func handleAcceptPositionForIdentifier() -> Bool { } private func handleAcceptPositionForKeyword(_ keyword:String) -> Bool { - if getInputStream()!.index() > _tokenStartCharIndex + keyword.characters.count { - let offset = keyword.characters.count - 1 + if getInputStream()!.index() > _tokenStartCharIndex + keyword.count { + let offset = keyword.count - 1 (getInterpreter() as! PositionAdjustingLexerATNSimulator).resetAcceptPosition(getInputStream()!, _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset) return true } diff --git a/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift b/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift index 7ef18facb..6b2841d2a 100644 --- a/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift +++ b/runtime/Swift/Sources/Antlr4/UnbufferedCharStream.swift @@ -343,6 +343,8 @@ fileprivate struct UInt8StreamIterator: IteratorProtocol { return nil case .opening, .open, .reading: break + @unknown default: + fatalError() } let count = stream.read(&buffer, maxLength: buffer.count) diff --git a/runtime/Swift/Sources/Antlr4/VocabularySingle.swift b/runtime/Swift/Sources/Antlr4/VocabularySingle.swift index 663ff5372..37cd3b23b 100644 --- a/runtime/Swift/Sources/Antlr4/VocabularySingle.swift +++ b/runtime/Swift/Sources/Antlr4/VocabularySingle.swift @@ -155,11 +155,9 @@ public class Vocabulary: Hashable { return String(tokenType) } - public var hashValue: Int { - return Unmanaged.passUnretained(self).toOpaque().hashValue -// return unsafeAddress(of: self).hashValue + public func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } - } public func ==(lhs: Vocabulary, rhs: Vocabulary) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift b/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift index b1069a126..cfbe8b615 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift @@ -126,20 +126,11 @@ public class ATNConfig: Hashable, CustomStringConvertible { } } - /// - /// An ATN configuration is equal to another if both have - /// the same state, they predict the same alternative, and - /// syntactic/semantic contexts are the same. - /// - - public var hashValue: Int { - var hashCode = MurmurHash.initialize(7) - hashCode = MurmurHash.update(hashCode, state.stateNumber) - hashCode = MurmurHash.update(hashCode, alt) - hashCode = MurmurHash.update(hashCode, context) - hashCode = MurmurHash.update(hashCode, semanticContext) - return MurmurHash.finish(hashCode, 4) - + public func hash(into hasher: inout Hasher) { + hasher.combine(state.stateNumber) + hasher.combine(alt) + hasher.combine(context) + hasher.combine(semanticContext) } public var description: String { @@ -166,6 +157,11 @@ public class ATNConfig: Hashable, CustomStringConvertible { } } +/// +/// An ATN configuration is equal to another if both have +/// the same state, they predict the same alternative, and +/// syntactic/semantic contexts are the same. +/// public func ==(lhs: ATNConfig, rhs: ATNConfig) -> Bool { if lhs === rhs { diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift index d1f6a1bb3..7da94377d 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift @@ -203,16 +203,16 @@ public final class ATNConfigSet: Hashable, CustomStringConvertible { return false } - public var hashValue: Int { + public func hash(into hasher: inout Hasher) { if isReadonly() { if cachedHashCode == -1 { - cachedHashCode = configsHashValue//configs.hashValue ; + cachedHashCode = configsHashValue } - - return cachedHashCode + hasher.combine(cachedHashCode) + } + else { + hasher.combine(configsHashValue) } - - return configsHashValue // configs.hashValue; } private var configsHashValue: Int { diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift b/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift index 65b02efc6..ce8349bc8 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift @@ -78,8 +78,8 @@ public class ATNDeserializer { /// internal func isFeatureSupported(_ feature: UUID, _ actualUuid: UUID) -> Bool { let supported = ATNDeserializer.SUPPORTED_UUIDS - guard let featureIndex = supported.index(of: feature), - let actualIndex = supported.index(of: actualUuid) else { + guard let featureIndex = supported.firstIndex(of: feature), + let actualIndex = supported.firstIndex(of: actualUuid) else { return false } return actualIndex >= featureIndex diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNState.swift b/runtime/Swift/Sources/Antlr4/atn/ATNState.swift index 02481d381..24f2dfab3 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNState.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNState.swift @@ -123,11 +123,10 @@ public class ATNState: Hashable, CustomStringConvertible { public internal(set) final var nextTokenWithinRule: IntervalSet? - public var hashValue: Int { - return stateNumber + public func hash(into hasher: inout Hasher) { + hasher.combine(stateNumber) } - public func isNonGreedyExitState() -> Bool { return false } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift b/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift index 3cacb48cb..1c4a0bb13 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerATNConfig.swift @@ -72,22 +72,14 @@ public class LexerATNConfig: ATNConfig { return passedThroughNonGreedyDecision } - override - /*public func hashCode() -> Int { - - }*/ - public var hashValue: Int { - var hashCode = MurmurHash.initialize(7) - hashCode = MurmurHash.update(hashCode, state.stateNumber) - hashCode = MurmurHash.update(hashCode, alt) - hashCode = MurmurHash.update(hashCode, context) - hashCode = MurmurHash.update(hashCode, semanticContext) - hashCode = MurmurHash.update(hashCode, passedThroughNonGreedyDecision ? 1 : 0) - hashCode = MurmurHash.update(hashCode, lexerActionExecutor) - return MurmurHash.finish(hashCode, 6) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(state.stateNumber) + hasher.combine(alt) + hasher.combine(context) + hasher.combine(semanticContext) + hasher.combine(passedThroughNonGreedyDecision) + hasher.combine(lexerActionExecutor) } - } //useless diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift index 99d604da5..5920e405b 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerAction.swift @@ -56,7 +56,7 @@ public class LexerAction: Hashable { fatalError(#function + " must be overridden") } - public var hashValue: Int { + public func hash(into hasher: inout Hasher) { fatalError(#function + " must be overridden") } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift b/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift index e33e92e9b..365f41a58 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift @@ -176,11 +176,9 @@ public class LexerActionExecutor: Hashable { } - public var hashValue: Int { - return self.hashCode + public func hash(into hasher: inout Hasher) { + hasher.combine(hashCode) } - - } public func ==(lhs: LexerActionExecutor, rhs: LexerActionExecutor) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift index 4d099f28c..a8fd17db6 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerChannelAction.swift @@ -63,12 +63,9 @@ public final class LexerChannelAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, channel) - return MurmurHash.finish(hash, 2) + public override func hash(into hasher: inout Hasher) { + hasher.combine(getActionType()) + hasher.combine(channel) } public var description: String { diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift index 3fe954236..fc6741ec2 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerCustomAction.swift @@ -92,24 +92,17 @@ public final class LexerCustomAction: LexerAction { try lexer.action(nil, ruleIndex, actionIndex) } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, ruleIndex) - hash = MurmurHash.update(hash, actionIndex) - return MurmurHash.finish(hash, 3) + public override func hash(into hasher: inout Hasher) { + hasher.combine(ruleIndex) + hasher.combine(actionIndex) } - } public func ==(lhs: LexerCustomAction, rhs: LexerCustomAction) -> Bool { - if lhs === rhs { return true } - return lhs.ruleIndex == rhs.ruleIndex && lhs.actionIndex == rhs.actionIndex } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift index 501b2e637..b9db97289 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerIndexedCustomAction.swift @@ -96,24 +96,17 @@ public final class LexerIndexedCustomAction: LexerAction { } - public override var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, offset) - hash = MurmurHash.update(hash, action) - return MurmurHash.finish(hash, 2) + public override func hash(into hasher: inout Hasher) { + hasher.combine(offset) + hasher.combine(action) } - - } public func ==(lhs: LexerIndexedCustomAction, rhs: LexerIndexedCustomAction) -> Bool { - if lhs === rhs { return true } - return lhs.offset == rhs.offset && lhs.action == rhs.action - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift index bdcaf6fdc..fca51619a 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerModeAction.swift @@ -62,25 +62,20 @@ public final class LexerModeAction: LexerAction, CustomStringConvertible { public func execute(_ lexer: Lexer) { lexer.mode(mode) } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, mode) - return MurmurHash.finish(hash, 2) + + public override func hash(into hasher: inout Hasher) { + hasher.combine(mode) } + public var description: String { return "mode(\(mode))" } } public func ==(lhs: LexerModeAction, rhs: LexerModeAction) -> Bool { - if lhs === rhs { return true } - return lhs.mode == rhs.mode - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift index bb9f197f3..c84f54ffd 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerMoreAction.swift @@ -56,23 +56,15 @@ public final class LexerMoreAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - return MurmurHash.finish(hash, 1) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } - public var description: String { return "more" } } public func ==(lhs: LexerMoreAction, rhs: LexerMoreAction) -> Bool { - return lhs === rhs - - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift index f35e78304..68fbe3fa6 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerPopModeAction.swift @@ -57,21 +57,15 @@ public final class LexerPopModeAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - return MurmurHash.finish(hash, 1) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } + public var description: String { return "popMode" } } public func ==(lhs: LexerPopModeAction, rhs: LexerPopModeAction) -> Bool { - return lhs === rhs - - } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift index fb432c497..ffac047aa 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerPushModeAction.swift @@ -63,15 +63,10 @@ public final class LexerPushModeAction: LexerAction, CustomStringConvertible { lexer.pushMode(mode) } - - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, mode) - return MurmurHash.finish(hash, 2) - + public override func hash(into hasher: inout Hasher) { + hasher.combine(mode) } + public var description: String { return "pushMode(\(mode))" } @@ -79,10 +74,8 @@ public final class LexerPushModeAction: LexerAction, CustomStringConvertible { public func ==(lhs: LexerPushModeAction, rhs: LexerPushModeAction) -> Bool { - if lhs === rhs { return true } - return lhs.mode == rhs.mode } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift index bbdd06d2f..db8b1d8e7 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerSkipAction.swift @@ -56,19 +56,15 @@ public final class LexerSkipAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - return MurmurHash.finish(hash, 1) + public override func hash(into hasher: inout Hasher) { + hasher.combine(ObjectIdentifier(self)) } + public var description: String { return "skip" } - } public func ==(lhs: LexerSkipAction, rhs: LexerSkipAction) -> Bool { - return lhs === rhs } diff --git a/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift b/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift index c08649ea3..8c7157be2 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LexerTypeAction.swift @@ -62,24 +62,18 @@ public class LexerTypeAction: LexerAction, CustomStringConvertible { } - override - public var hashValue: Int { - var hash = MurmurHash.initialize() - hash = MurmurHash.update(hash, getActionType().rawValue) - hash = MurmurHash.update(hash, type) - return MurmurHash.finish(hash, 2) + public override func hash(into hasher: inout Hasher) { + hasher.combine(type) } + public var description: String { return "type(\(type))" } - } public func ==(lhs: LexerTypeAction, rhs: LexerTypeAction) -> Bool { - if lhs === rhs { return true } - return lhs.type == rhs.type } diff --git a/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift b/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift index aef419b1d..d3a04a1cc 100644 --- a/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift +++ b/runtime/Swift/Sources/Antlr4/atn/LookupATNConfig.swift @@ -20,17 +20,12 @@ public class LookupATNConfig: Hashable { // dup config = old } - public var hashValue: Int { - - var hashCode: Int = 7 - hashCode = 31 * hashCode + config.state.stateNumber - hashCode = 31 * hashCode + config.alt - hashCode = 31 * hashCode + config.semanticContext.hashValue - return hashCode + public func hash(into hasher: inout Hasher) { + hasher.combine(config.state.stateNumber) + hasher.combine(config.alt) + hasher.combine(config.semanticContext) } - - } public func ==(lhs: LookupATNConfig, rhs: LookupATNConfig) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift b/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift index 376088905..78b0614b5 100644 --- a/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift +++ b/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift @@ -105,8 +105,8 @@ public class PredictionContext: Hashable, CustomStringConvertible { return getReturnState(size() - 1) == PredictionContext.EMPTY_RETURN_STATE } - public final var hashValue: Int { - return cachedHashCode + public func hash(into hasher: inout Hasher) { + hasher.combine(cachedHashCode) } static func calculateEmptyHashCode() -> Int { diff --git a/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift b/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift index 118f549f5..dffe24a1f 100644 --- a/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift +++ b/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift @@ -61,7 +61,7 @@ public class SemanticContext: Hashable, CustomStringConvertible { return self } - public var hashValue: Int { + public func hash(into hasher: inout Hasher) { fatalError(#function + " must be overridden") } @@ -94,16 +94,12 @@ public class SemanticContext: Hashable, CustomStringConvertible { return try parser.sempred(localctx, ruleIndex, predIndex) } - override - public var hashValue: Int { - var hashCode = MurmurHash.initialize() - hashCode = MurmurHash.update(hashCode, ruleIndex) - hashCode = MurmurHash.update(hashCode, predIndex) - hashCode = MurmurHash.update(hashCode, isCtxDependent ? 1 : 0) - return MurmurHash.finish(hashCode, 3) + public override func hash(into hasher: inout Hasher) { + hasher.combine(ruleIndex) + hasher.combine(predIndex) + hasher.combine(isCtxDependent) } - override public var description: String { return "{\(ruleIndex):\(predIndex)}?" @@ -138,11 +134,8 @@ public class SemanticContext: Hashable, CustomStringConvertible { } - override - public var hashValue: Int { - var hashCode: Int = 1 - hashCode = 31 * hashCode + precedence - return hashCode + public override func hash(into hasher: inout Hasher) { + hasher.combine(precedence) } override @@ -214,12 +207,8 @@ public class SemanticContext: Hashable, CustomStringConvertible { } - override - public var hashValue: Int { - //MurmurHash.hashCode(opnds, AND.class.hashCode()); - let seed = 1554547125 - //NSStringFromClass(AND.self).hashValue - return MurmurHash.hashCode(opnds, seed) + public override func hash(into hasher: inout Hasher) { + hasher.combine(opnds) } /// @@ -323,11 +312,8 @@ public class SemanticContext: Hashable, CustomStringConvertible { return opnds } - - override - public var hashValue: Int { - - return MurmurHash.hashCode(opnds, NSStringFromClass(OR.self).hashValue) + public override func hash(into hasher: inout Hasher) { + hasher.combine(opnds) } /// diff --git a/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift b/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift index d44dd34ec..8a705872c 100644 --- a/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift +++ b/runtime/Swift/Sources/Antlr4/dfa/DFAState.swift @@ -109,10 +109,8 @@ public final class DFAState: Hashable, CustomStringConvertible { } - public var hashValue: Int { - var hash = MurmurHash.initialize(7) - hash = MurmurHash.update(hash, configs.hashValue) - return MurmurHash.finish(hash, 1) + public func hash(into hasher: inout Hasher) { + hasher.combine(configs) } public var description: String { diff --git a/runtime/Swift/Sources/Antlr4/misc/BitSet.swift b/runtime/Swift/Sources/Antlr4/misc/BitSet.swift index ef03b8aa7..5e611efc2 100644 --- a/runtime/Swift/Sources/Antlr4/misc/BitSet.swift +++ b/runtime/Swift/Sources/Antlr4/misc/BitSet.swift @@ -1053,7 +1053,7 @@ public class BitSet: Hashable, CustomStringConvertible { /// /// - returns: the hash code value for this bit set /// - public var hashValue: Int { + private var hashCode: Int { var h: Int64 = 1234 var i: Int = wordsInUse i -= 1 @@ -1065,6 +1065,10 @@ public class BitSet: Hashable, CustomStringConvertible { return Int(Int32((h >> 32) ^ h)) } + public func hash(into hasher: inout Hasher) { + hasher.combine(hashCode) + } + /// /// Returns the number of bits of space actually in use by this /// `BitSet` to represent bit values. diff --git a/runtime/Swift/Sources/Antlr4/misc/Interval.swift b/runtime/Swift/Sources/Antlr4/misc/Interval.swift index e3b4ad49f..f03ad9b08 100644 --- a/runtime/Swift/Sources/Antlr4/misc/Interval.swift +++ b/runtime/Swift/Sources/Antlr4/misc/Interval.swift @@ -62,13 +62,12 @@ public class Interval: Hashable { } - public var hashValue: Int { - var hash: Int = 23 - hash = hash * 31 + a - hash = hash * 31 + b - return hash + public func hash(into hasher: inout Hasher) { + hasher.combine(a) + hasher.combine(b) } - /// + + /// /// Does this start completely before other? Disjoint /// public func startsBeforeDisjoint(_ other: Interval) -> Bool { diff --git a/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift b/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift index 04c590db0..522952250 100644 --- a/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift +++ b/runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift @@ -470,25 +470,13 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible { return intervals } - - public func hashCode() -> Int { - var hash = MurmurHash.initialize() - for I: Interval in intervals { - hash = MurmurHash.update(hash, I.a) - hash = MurmurHash.update(hash, I.b) + public func hash(into hasher: inout Hasher) { + for interval in intervals { + hasher.combine(interval.a) + hasher.combine(interval.b) } - - return MurmurHash.finish(hash, intervals.count * 2) } - public var hashValue: Int { - var hash = MurmurHash.initialize() - for I: Interval in intervals { - hash = MurmurHash.update(hash, I.a) - hash = MurmurHash.update(hash, I.b) - } - return MurmurHash.finish(hash, intervals.count * 2) - } /// /// Are two IntervalSets equal? Because all intervals are sorted /// and disjoint, equals is a simple linear walk over both lists