[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.
This commit is contained in:
Ewan Mellor 2019-03-28 15:24:27 -07:00
parent 54daca92f7
commit 509eeefa4a
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
28 changed files with 107 additions and 205 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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")
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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)
}
///

View File

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

View File

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

View File

@ -62,12 +62,11 @@ 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
///

View File

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