[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 - clang-3.7
- os: osx - os: osx
compiler: clang compiler: clang
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=cpp - TARGET=cpp
- GROUP=LEXER - GROUP=LEXER
stage: extended-test stage: extended-test
- os: osx - os: osx
compiler: clang compiler: clang
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=cpp - TARGET=cpp
- GROUP=PARSER - GROUP=PARSER
stage: extended-test stage: extended-test
- os: osx - os: osx
compiler: clang compiler: clang
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=cpp - TARGET=cpp
- GROUP=RECURSION - GROUP=RECURSION
stage: extended-test stage: extended-test
- os: osx - os: osx
compiler: clang compiler: clang
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=swift - TARGET=swift
- GROUP=LEXER - GROUP=LEXER
stage: main-test stage: main-test
- os: osx - os: osx
compiler: clang compiler: clang
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=swift - TARGET=swift
- GROUP=PARSER - GROUP=PARSER
stage: main-test stage: main-test
- os: osx - os: osx
compiler: clang compiler: clang
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=swift - TARGET=swift
- GROUP=RECURSION - GROUP=RECURSION
@ -122,19 +122,19 @@ matrix:
- GROUP=ALL - GROUP=ALL
stage: extended-test stage: extended-test
- os: osx - os: osx
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=dotnet - TARGET=dotnet
- GROUP=LEXER - GROUP=LEXER
stage: extended-test stage: extended-test
- os: osx - os: osx
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=dotnet - TARGET=dotnet
- GROUP=PARSER - GROUP=PARSER
stage: extended-test stage: extended-test
- os: osx - os: osx
osx_image: xcode10.1 osx_image: xcode10.2
env: env:
- TARGET=dotnet - TARGET=dotnet
- GROUP=RECURSION - GROUP=RECURSION

View File

@ -6,7 +6,7 @@ set -euo pipefail
# here since environment variables doesn't pass # here since environment variables doesn't pass
# across scripts # across scripts
if [ $TRAVIS_OS_NAME == "linux" ]; then 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 SWIFT_HOME=$(pwd)/swift/$SWIFT_VERSION-RELEASE-ubuntu16.04/usr/bin/
export PATH=$SWIFT_HOME:$PATH export PATH=$SWIFT_HOME:$PATH

View File

@ -133,7 +133,7 @@ open func emit() -> Token {
private func handleAcceptPositionForIdentifier() -> Bool { private func handleAcceptPositionForIdentifier() -> Bool {
let tokenText = getText() let tokenText = getText()
var identifierLength = 0 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 identifierLength += 1
} }
@ -147,8 +147,8 @@ private func handleAcceptPositionForIdentifier() -> Bool {
} }
private func handleAcceptPositionForKeyword(_ keyword:String) -> Bool { private func handleAcceptPositionForKeyword(_ keyword:String) -> Bool {
if getInputStream()!.index() > _tokenStartCharIndex + keyword.characters.count { if getInputStream()!.index() > _tokenStartCharIndex + keyword.count {
let offset = keyword.characters.count - 1 let offset = keyword.count - 1
(getInterpreter() as! PositionAdjustingLexerATNSimulator).resetAcceptPosition(getInputStream()!, _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset) (getInterpreter() as! PositionAdjustingLexerATNSimulator).resetAcceptPosition(getInputStream()!, _tokenStartCharIndex + offset, _tokenStartLine, _tokenStartCharPositionInLine + offset)
return true return true
} }

View File

@ -343,6 +343,8 @@ fileprivate struct UInt8StreamIterator: IteratorProtocol {
return nil return nil
case .opening, .open, .reading: case .opening, .open, .reading:
break break
@unknown default:
fatalError()
} }
let count = stream.read(&buffer, maxLength: buffer.count) let count = stream.read(&buffer, maxLength: buffer.count)

View File

@ -155,11 +155,9 @@ public class Vocabulary: Hashable {
return String(tokenType) return String(tokenType)
} }
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
return Unmanaged.passUnretained(self).toOpaque().hashValue hasher.combine(ObjectIdentifier(self))
// return unsafeAddress(of: self).hashValue
} }
} }
public func ==(lhs: Vocabulary, rhs: Vocabulary) -> Bool { public func ==(lhs: Vocabulary, rhs: Vocabulary) -> Bool {

View File

@ -126,20 +126,11 @@ public class ATNConfig: Hashable, CustomStringConvertible {
} }
} }
/// public func hash(into hasher: inout Hasher) {
/// An ATN configuration is equal to another if both have hasher.combine(state.stateNumber)
/// the same state, they predict the same alternative, and hasher.combine(alt)
/// syntactic/semantic contexts are the same. hasher.combine(context)
/// hasher.combine(semanticContext)
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 var description: String { 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 { public func ==(lhs: ATNConfig, rhs: ATNConfig) -> Bool {
if lhs === rhs { if lhs === rhs {

View File

@ -203,16 +203,16 @@ public final class ATNConfigSet: Hashable, CustomStringConvertible {
return false return false
} }
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
if isReadonly() { if isReadonly() {
if cachedHashCode == -1 { if cachedHashCode == -1 {
cachedHashCode = configsHashValue//configs.hashValue ; cachedHashCode = configsHashValue
} }
hasher.combine(cachedHashCode)
return cachedHashCode }
else {
hasher.combine(configsHashValue)
} }
return configsHashValue // configs.hashValue;
} }
private var configsHashValue: Int { private var configsHashValue: Int {

View File

@ -78,8 +78,8 @@ public class ATNDeserializer {
/// ///
internal func isFeatureSupported(_ feature: UUID, _ actualUuid: UUID) -> Bool { internal func isFeatureSupported(_ feature: UUID, _ actualUuid: UUID) -> Bool {
let supported = ATNDeserializer.SUPPORTED_UUIDS let supported = ATNDeserializer.SUPPORTED_UUIDS
guard let featureIndex = supported.index(of: feature), guard let featureIndex = supported.firstIndex(of: feature),
let actualIndex = supported.index(of: actualUuid) else { let actualIndex = supported.firstIndex(of: actualUuid) else {
return false return false
} }
return actualIndex >= featureIndex return actualIndex >= featureIndex

View File

@ -123,11 +123,10 @@ public class ATNState: Hashable, CustomStringConvertible {
public internal(set) final var nextTokenWithinRule: IntervalSet? public internal(set) final var nextTokenWithinRule: IntervalSet?
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
return stateNumber hasher.combine(stateNumber)
} }
public func isNonGreedyExitState() -> Bool { public func isNonGreedyExitState() -> Bool {
return false return false
} }

View File

@ -72,22 +72,14 @@ public class LexerATNConfig: ATNConfig {
return passedThroughNonGreedyDecision return passedThroughNonGreedyDecision
} }
override public override func hash(into hasher: inout Hasher) {
/*public func hashCode() -> Int { hasher.combine(state.stateNumber)
hasher.combine(alt)
}*/ hasher.combine(context)
public var hashValue: Int { hasher.combine(semanticContext)
var hashCode = MurmurHash.initialize(7) hasher.combine(passedThroughNonGreedyDecision)
hashCode = MurmurHash.update(hashCode, state.stateNumber) hasher.combine(lexerActionExecutor)
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)
} }
} }
//useless //useless

View File

@ -56,7 +56,7 @@ public class LexerAction: Hashable {
fatalError(#function + " must be overridden") fatalError(#function + " must be overridden")
} }
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
fatalError(#function + " must be overridden") fatalError(#function + " must be overridden")
} }

View File

@ -176,11 +176,9 @@ public class LexerActionExecutor: Hashable {
} }
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
return self.hashCode hasher.combine(hashCode)
} }
} }
public func ==(lhs: LexerActionExecutor, rhs: LexerActionExecutor) -> Bool { public func ==(lhs: LexerActionExecutor, rhs: LexerActionExecutor) -> Bool {

View File

@ -63,12 +63,9 @@ public final class LexerChannelAction: LexerAction, CustomStringConvertible {
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(getActionType())
var hash = MurmurHash.initialize() hasher.combine(channel)
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, channel)
return MurmurHash.finish(hash, 2)
} }
public var description: String { public var description: String {

View File

@ -92,24 +92,17 @@ public final class LexerCustomAction: LexerAction {
try lexer.action(nil, ruleIndex, actionIndex) try lexer.action(nil, ruleIndex, actionIndex)
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(ruleIndex)
var hash = MurmurHash.initialize() hasher.combine(actionIndex)
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, ruleIndex)
hash = MurmurHash.update(hash, actionIndex)
return MurmurHash.finish(hash, 3)
} }
} }
public func ==(lhs: LexerCustomAction, rhs: LexerCustomAction) -> Bool { public func ==(lhs: LexerCustomAction, rhs: LexerCustomAction) -> Bool {
if lhs === rhs { if lhs === rhs {
return true return true
} }
return lhs.ruleIndex == rhs.ruleIndex return lhs.ruleIndex == rhs.ruleIndex
&& lhs.actionIndex == rhs.actionIndex && lhs.actionIndex == rhs.actionIndex
} }

View File

@ -96,24 +96,17 @@ public final class LexerIndexedCustomAction: LexerAction {
} }
public override var hashValue: Int { public override func hash(into hasher: inout Hasher) {
var hash = MurmurHash.initialize() hasher.combine(offset)
hash = MurmurHash.update(hash, offset) hasher.combine(action)
hash = MurmurHash.update(hash, action)
return MurmurHash.finish(hash, 2)
} }
} }
public func ==(lhs: LexerIndexedCustomAction, rhs: LexerIndexedCustomAction) -> Bool { public func ==(lhs: LexerIndexedCustomAction, rhs: LexerIndexedCustomAction) -> Bool {
if lhs === rhs { if lhs === rhs {
return true return true
} }
return lhs.offset == rhs.offset return lhs.offset == rhs.offset
&& lhs.action == rhs.action && lhs.action == rhs.action
} }

View File

@ -62,25 +62,20 @@ public final class LexerModeAction: LexerAction, CustomStringConvertible {
public func execute(_ lexer: Lexer) { public func execute(_ lexer: Lexer) {
lexer.mode(mode) lexer.mode(mode)
} }
override
public var hashValue: Int { public override func hash(into hasher: inout Hasher) {
var hash = MurmurHash.initialize() hasher.combine(mode)
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, mode)
return MurmurHash.finish(hash, 2)
} }
public var description: String { public var description: String {
return "mode(\(mode))" return "mode(\(mode))"
} }
} }
public func ==(lhs: LexerModeAction, rhs: LexerModeAction) -> Bool { public func ==(lhs: LexerModeAction, rhs: LexerModeAction) -> Bool {
if lhs === rhs { if lhs === rhs {
return true return true
} }
return lhs.mode == rhs.mode return lhs.mode == rhs.mode
} }

View File

@ -56,23 +56,15 @@ public final class LexerMoreAction: LexerAction, CustomStringConvertible {
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(ObjectIdentifier(self))
var hash = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
return MurmurHash.finish(hash, 1)
} }
public var description: String { public var description: String {
return "more" return "more"
} }
} }
public func ==(lhs: LexerMoreAction, rhs: LexerMoreAction) -> Bool { public func ==(lhs: LexerMoreAction, rhs: LexerMoreAction) -> Bool {
return lhs === rhs return lhs === rhs
} }

View File

@ -57,21 +57,15 @@ public final class LexerPopModeAction: LexerAction, CustomStringConvertible {
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(ObjectIdentifier(self))
var hash = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
return MurmurHash.finish(hash, 1)
} }
public var description: String { public var description: String {
return "popMode" return "popMode"
} }
} }
public func ==(lhs: LexerPopModeAction, rhs: LexerPopModeAction) -> Bool { public func ==(lhs: LexerPopModeAction, rhs: LexerPopModeAction) -> Bool {
return lhs === rhs return lhs === rhs
} }

View File

@ -63,15 +63,10 @@ public final class LexerPushModeAction: LexerAction, CustomStringConvertible {
lexer.pushMode(mode) lexer.pushMode(mode)
} }
public override func hash(into hasher: inout Hasher) {
override hasher.combine(mode)
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 var description: String { public var description: String {
return "pushMode(\(mode))" return "pushMode(\(mode))"
} }
@ -79,10 +74,8 @@ public final class LexerPushModeAction: LexerAction, CustomStringConvertible {
public func ==(lhs: LexerPushModeAction, rhs: LexerPushModeAction) -> Bool { public func ==(lhs: LexerPushModeAction, rhs: LexerPushModeAction) -> Bool {
if lhs === rhs { if lhs === rhs {
return true return true
} }
return lhs.mode == rhs.mode return lhs.mode == rhs.mode
} }

View File

@ -56,19 +56,15 @@ public final class LexerSkipAction: LexerAction, CustomStringConvertible {
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(ObjectIdentifier(self))
var hash = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
return MurmurHash.finish(hash, 1)
} }
public var description: String { public var description: String {
return "skip" return "skip"
} }
} }
public func ==(lhs: LexerSkipAction, rhs: LexerSkipAction) -> Bool { public func ==(lhs: LexerSkipAction, rhs: LexerSkipAction) -> Bool {
return lhs === rhs return lhs === rhs
} }

View File

@ -62,24 +62,18 @@ public class LexerTypeAction: LexerAction, CustomStringConvertible {
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(type)
var hash = MurmurHash.initialize()
hash = MurmurHash.update(hash, getActionType().rawValue)
hash = MurmurHash.update(hash, type)
return MurmurHash.finish(hash, 2)
} }
public var description: String { public var description: String {
return "type(\(type))" return "type(\(type))"
} }
} }
public func ==(lhs: LexerTypeAction, rhs: LexerTypeAction) -> Bool { public func ==(lhs: LexerTypeAction, rhs: LexerTypeAction) -> Bool {
if lhs === rhs { if lhs === rhs {
return true return true
} }
return lhs.type == rhs.type return lhs.type == rhs.type
} }

View File

@ -20,17 +20,12 @@ public class LookupATNConfig: Hashable {
// dup // dup
config = old 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 { 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 return getReturnState(size() - 1) == PredictionContext.EMPTY_RETURN_STATE
} }
public final var hashValue: Int { public func hash(into hasher: inout Hasher) {
return cachedHashCode hasher.combine(cachedHashCode)
} }
static func calculateEmptyHashCode() -> Int { static func calculateEmptyHashCode() -> Int {

View File

@ -61,7 +61,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
return self return self
} }
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
fatalError(#function + " must be overridden") fatalError(#function + " must be overridden")
} }
@ -94,16 +94,12 @@ public class SemanticContext: Hashable, CustomStringConvertible {
return try parser.sempred(localctx, ruleIndex, predIndex) return try parser.sempred(localctx, ruleIndex, predIndex)
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(ruleIndex)
var hashCode = MurmurHash.initialize() hasher.combine(predIndex)
hashCode = MurmurHash.update(hashCode, ruleIndex) hasher.combine(isCtxDependent)
hashCode = MurmurHash.update(hashCode, predIndex)
hashCode = MurmurHash.update(hashCode, isCtxDependent ? 1 : 0)
return MurmurHash.finish(hashCode, 3)
} }
override override
public var description: String { public var description: String {
return "{\(ruleIndex):\(predIndex)}?" return "{\(ruleIndex):\(predIndex)}?"
@ -138,11 +134,8 @@ public class SemanticContext: Hashable, CustomStringConvertible {
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(precedence)
var hashCode: Int = 1
hashCode = 31 * hashCode + precedence
return hashCode
} }
override override
@ -214,12 +207,8 @@ public class SemanticContext: Hashable, CustomStringConvertible {
} }
override public override func hash(into hasher: inout Hasher) {
public var hashValue: Int { hasher.combine(opnds)
//MurmurHash.hashCode(opnds, AND.class.hashCode());
let seed = 1554547125
//NSStringFromClass(AND.self).hashValue
return MurmurHash.hashCode(opnds, seed)
} }
/// ///
@ -323,11 +312,8 @@ public class SemanticContext: Hashable, CustomStringConvertible {
return opnds return opnds
} }
public override func hash(into hasher: inout Hasher) {
override hasher.combine(opnds)
public var hashValue: Int {
return MurmurHash.hashCode(opnds, NSStringFromClass(OR.self).hashValue)
} }
/// ///

View File

@ -109,10 +109,8 @@ public final class DFAState: Hashable, CustomStringConvertible {
} }
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
var hash = MurmurHash.initialize(7) hasher.combine(configs)
hash = MurmurHash.update(hash, configs.hashValue)
return MurmurHash.finish(hash, 1)
} }
public var description: String { public var description: String {

View File

@ -1053,7 +1053,7 @@ public class BitSet: Hashable, CustomStringConvertible {
/// ///
/// - returns: the hash code value for this bit set /// - returns: the hash code value for this bit set
/// ///
public var hashValue: Int { private var hashCode: Int {
var h: Int64 = 1234 var h: Int64 = 1234
var i: Int = wordsInUse var i: Int = wordsInUse
i -= 1 i -= 1
@ -1065,6 +1065,10 @@ public class BitSet: Hashable, CustomStringConvertible {
return Int(Int32((h >> 32) ^ h)) 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 /// Returns the number of bits of space actually in use by this
/// `BitSet` to represent bit values. /// `BitSet` to represent bit values.

View File

@ -62,13 +62,12 @@ public class Interval: Hashable {
} }
public var hashValue: Int { public func hash(into hasher: inout Hasher) {
var hash: Int = 23 hasher.combine(a)
hash = hash * 31 + a hasher.combine(b)
hash = hash * 31 + b
return hash
} }
///
///
/// Does this start completely before other? Disjoint /// Does this start completely before other? Disjoint
/// ///
public func startsBeforeDisjoint(_ other: Interval) -> Bool { public func startsBeforeDisjoint(_ other: Interval) -> Bool {

View File

@ -470,25 +470,13 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible {
return intervals return intervals
} }
public func hash(into hasher: inout Hasher) {
public func hashCode() -> Int { for interval in intervals {
var hash = MurmurHash.initialize() hasher.combine(interval.a)
for I: Interval in intervals { hasher.combine(interval.b)
hash = MurmurHash.update(hash, I.a)
hash = MurmurHash.update(hash, I.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 /// Are two IntervalSets equal? Because all intervals are sorted
/// and disjoint, equals is a simple linear walk over both lists /// and disjoint, equals is a simple linear walk over both lists