Migrate the Swift runtime to Swift 4.

Remove a number of generic type constraints, since these can now
be inferred by the compiler.

Match the syntax change when passing a tuple into a function (adding
an extra set of parens).

Change filterPrecedencePredicates to avoid a now-illegal cast.

Match the renames truncatingBitPattern -> truncatingIfNeeded,
multiplyWithOverflow -> multipliedReportingOverflow, etc.  In
some cases the multiplyWithOverflow calls are replaced by
overflowing operators (e.g. &*) instead.
This commit is contained in:
Ewan Mellor 2017-09-27 11:54:15 -07:00
parent 365d4f40bb
commit 8a292c0f4f
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
17 changed files with 76 additions and 87 deletions

View File

@ -39,12 +39,12 @@ public protocol ANTLRErrorListener: class {
/// the parser was able to recover in line without exiting the
/// surrounding rule.
///
func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
func syntaxError<T>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
)
///

View File

@ -17,12 +17,12 @@ open class BaseErrorListener: ANTLRErrorListener {
public init() {
}
open func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
open func syntaxError<T>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
) {
}

View File

@ -25,12 +25,12 @@ public class ConsoleErrorListener: BaseErrorListener {
/// line __line__:__charPositionInLine__ __msg__
///
///
override public func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
override public func syntaxError<T>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?
) {
if Parser.ConsoleError {
errPrint("line \(line):\(charPositionInLine) \(msg)")

View File

@ -405,7 +405,7 @@ open class Lexer: Recognizer<LexerATNSimulator>
}
}
open func notifyListeners<T:ATNSimulator>(_ e: LexerNoViableAltException, recognizer: Recognizer<T>) {
open func notifyListeners<T>(_ e: LexerNoViableAltException, recognizer: Recognizer<T>) {
let text: String = _input!.getText(Interval.of(_tokenStartCharIndex, _input!.index()))
let msg: String = "token recognition error at: '\(getErrorDisplay(text))'"

View File

@ -20,13 +20,13 @@ public class ProxyErrorListener: ANTLRErrorListener {
self.delegates = delegates
}
public func syntaxError<T:ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?)
{
public func syntaxError<T>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int,
_ charPositionInLine: Int,
_ msg: String,
_ e: AnyObject?)
{
for listener: ANTLRErrorListener in delegates {
listener.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e)
}

View File

@ -233,7 +233,7 @@ open class RuleContext: RuleNode {
return description
}
public final func toString<T:ATNSimulator>(_ recog: Recognizer<T>) -> String {
public final func toString<T>(_ recog: Recognizer<T>) -> String {
return toString(recog, ParserRuleContext.EMPTY)
}
@ -242,7 +242,7 @@ open class RuleContext: RuleNode {
}
// recog null unless ParserRuleContext, in which case we use subclass toString(...)
open func toString<T:ATNSimulator>(_ recog: Recognizer<T>?, _ stop: RuleContext) -> String {
open func toString<T>(_ recog: Recognizer<T>?, _ stop: RuleContext) -> String {
let ruleNames: [String]? = recog != nil ? recog!.getRuleNames() : nil
let ruleNamesList: Array<String>? = ruleNames ?? nil
return toString(ruleNamesList, stop)

View File

@ -167,7 +167,7 @@ public class ATNConfig: Hashable, CustomStringConvertible {
//return "MyClass \(string)"
return toString(nil, true)
}
public func toString<T:ATNSimulator>(_ recog: Recognizer<T>?, _ showAlt: Bool) -> String {
public func toString<T>(_ recog: Recognizer<T>?, _ showAlt: Bool) -> String {
let buf: StringBuilder = StringBuilder()
buf.append("(")
buf.append(state)

View File

@ -238,12 +238,9 @@ public class ATNConfigSet: Hashable, CustomStringConvertible {
private var configsHashValue: Int {
var hashCode = 1
for item in configs {
hashCode = Int.multiplyWithOverflow(3, hashCode).0
hashCode = Int.addWithOverflow(hashCode, item.hashValue).0
hashCode = hashCode &* 3 &+ item.hashValue
}
return hashCode
}
public final var count: Int {

View File

@ -175,7 +175,7 @@ public class ATNDeserializer {
if let s = s as? BlockStartState {
let endStateNumber: Int = toInt(data[p])
p += 1
endStateNumbers.append(s, endStateNumber)
endStateNumbers.append((s, endStateNumber))
}
}
atn.addState(s)

View File

@ -712,17 +712,17 @@ public class PredictionContext: Hashable, CustomStringConvertible {
}
}
public func toString<T:ATNSimulator>(_ recog: Recognizer<T>) -> String {
public func toString<T>(_ recog: Recognizer<T>) -> String {
return NSStringFromClass(PredictionContext.self)
// return toString(recog, ParserRuleContext.EMPTY);
}
public func toStrings<T:ATNSimulator>(_ recognizer: Recognizer<T>, _ currentState: Int) -> [String] {
public func toStrings<T>(_ recognizer: Recognizer<T>, _ currentState: Int) -> [String] {
return toStrings(recognizer, PredictionContext.EMPTY, currentState)
}
// FROM SAM
public func toStrings<T:ATNSimulator>(_ recognizer: Recognizer<T>?, _ stop: PredictionContext, _ currentState: Int) -> [String] {
public func toStrings<T>(_ recognizer: Recognizer<T>?, _ stop: PredictionContext, _ currentState: Int) -> [String] {
var result: Array<String> = Array<String>()
var perm: Int = 0
outer: while true {

View File

@ -37,7 +37,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
/// prediction, so we passed in the outer context here in case of context
/// dependent predicate evaluation.
///
public func eval<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
public func eval<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
RuntimeException(#function + " must be overridden")
return false
}
@ -58,7 +58,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
/// * A non-`null` _org.antlr.v4.runtime.atn.SemanticContext_: the new simplified
/// semantic context after precedence predicates are evaluated.
///
public func evalPrecedence<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
public func evalPrecedence<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
return self
}
public var hashValue: Int {
@ -90,7 +90,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
}
override
public func eval<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
public func eval<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
let localctx: RuleContext? = isCtxDependent ? parserCallStack : nil
return try parser.sempred(localctx, ruleIndex, predIndex)
}
@ -126,12 +126,12 @@ public class SemanticContext: Hashable, CustomStringConvertible {
}
override
public func eval<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
public func eval<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
return try parser.precpred(parserCallStack, precedence)
}
override
public func evalPrecedence<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
public func evalPrecedence<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
if try parser.precpred(parserCallStack, precedence) {
return SemanticContext.NONE
} else {
@ -198,18 +198,17 @@ public class SemanticContext: Hashable, CustomStringConvertible {
operands.insert(b)
}
let precedencePredicates: Array<PrecedencePredicate> =
SemanticContext.filterPrecedencePredicates(&operands)
let precedencePredicates = SemanticContext.filterPrecedencePredicates(&operands)
if !precedencePredicates.isEmpty {
// interested in the transition with the lowest precedence
let reduced: PrecedencePredicate = precedencePredicates.sorted {
let reduced = precedencePredicates.sorted {
$0.precedence < $1.precedence
}.first! //Collections.min(precedencePredicates);
operands.insert(reduced)
}
operands.insert(reduced[0])
}
opnds = Array(operands) //.toArray(new, SemanticContext[operands.size()]);
opnds = Array(operands)
}
override
@ -234,7 +233,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
/// unordered.
///
override
public func eval<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
public func eval<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
for opnd: SemanticContext in opnds {
if try !opnd.eval(parser, parserCallStack) {
return false
@ -244,7 +243,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
}
override
public func evalPrecedence<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
public func evalPrecedence<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
var differs: Bool = false
var operands: Array<SemanticContext> = Array<SemanticContext>()
for context: SemanticContext in opnds {
@ -314,17 +313,17 @@ public class SemanticContext: Hashable, CustomStringConvertible {
operands.insert(b)
}
let precedencePredicates: Array<PrecedencePredicate> = SemanticContext.filterPrecedencePredicates(&operands)
let precedencePredicates = SemanticContext.filterPrecedencePredicates(&operands)
if !precedencePredicates.isEmpty {
// interested in the transition with the highest precedence
let reduced: PrecedencePredicate = precedencePredicates.sorted {
let reduced = precedencePredicates.sorted {
$0.precedence > $1.precedence
}.first!
//var reduced : PrecedencePredicate = Collections.max(precedencePredicates);
operands.insert(reduced)
}
operands.insert(reduced[0])
}
self.opnds = Array(operands) //operands.toArray(new, SemanticContext[operands.size()]);
self.opnds = Array(operands)
}
override
@ -347,7 +346,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
/// unordered.
///
override
public func eval<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
public func eval<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> Bool {
for opnd: SemanticContext in opnds {
if try opnd.eval(parser, parserCallStack) {
return true
@ -357,7 +356,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
}
override
public func evalPrecedence<T:ATNSimulator>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
public func evalPrecedence<T>(_ parser: Recognizer<T>, _ parserCallStack: RuleContext) throws -> SemanticContext? {
var differs: Bool = false
var operands: Array<SemanticContext> = Array<SemanticContext>()
for context: SemanticContext in opnds {
@ -443,21 +442,14 @@ public class SemanticContext: Hashable, CustomStringConvertible {
return result
}
private static func filterPrecedencePredicates(
_ collection: inout Set<SemanticContext>) ->
Array<PrecedencePredicate> {
let result = collection.filter {
$0 is PrecedencePredicate
private static func filterPrecedencePredicates(_ collection: inout Set<SemanticContext>) -> [PrecedencePredicate] {
let result = collection.flatMap {
$0 as? PrecedencePredicate
}
collection = Set<SemanticContext>(collection.filter {
!($0 is PrecedencePredicate)
})
//if (result == nil) {
//return Array<PrecedencePredicate>();
//}
return (result as! Array<PrecedencePredicate>)
return result
}
}

View File

@ -69,7 +69,7 @@ public final class ArrayWrapper<T: Hashable>: ExpressibleByArrayLiteral, Hashabl
}
public func == <Element: Equatable>(lhs: ArrayWrapper<Element>, rhs: ArrayWrapper<Element>) -> Bool {
public func == <Element>(lhs: ArrayWrapper<Element>, rhs: ArrayWrapper<Element>) -> Bool {
if lhs === rhs {
return true
}

View File

@ -648,12 +648,12 @@ public class BitSet: Hashable, CustomStringConvertible {
return 64
}
var n: Int32 = 63
y = Int32(truncatingBitPattern: i)
y = Int32(truncatingIfNeeded: i)
if y != 0 {
n = n - 32
x = y
} else {
x = Int32(truncatingBitPattern: i >>> 32)
x = Int32(truncatingIfNeeded: i >>> 32)
}
y = x << 16

View File

@ -41,7 +41,7 @@ final class Entry<K: Hashable,V>: CustomStringConvertible {
var description: String { return "\(getKey())=\(getValue())" }
}
func == <K: Hashable, V: Equatable>(lhs: Entry<K,V>, rhs: Entry<K,V>) -> Bool {
func == <K, V: Equatable>(lhs: Entry<K,V>, rhs: Entry<K,V>) -> Bool {
if lhs === rhs {
return true
}
@ -52,7 +52,7 @@ func == <K: Hashable, V: Equatable>(lhs: Entry<K,V>, rhs: Entry<K,V>) -> Bool {
}
return false
}
func == <K: Hashable, V: Equatable>(lhs: Entry<K,V?>, rhs: Entry<K,V?>) -> Bool {
func == <K, V: Equatable>(lhs: Entry<K,V?>, rhs: Entry<K,V?>) -> Bool {
if lhs === rhs {
return true
}

View File

@ -49,19 +49,19 @@ public final class MurmurHash {
let m: Int32 = 5
let n: Int32 = -430675100//0xE6546B64;
var k: Int32 = Int32(truncatingBitPattern: value)
k = Int32.multiplyWithOverflow(k, c1).0
var k: Int32 = Int32(truncatingIfNeeded: value)
k = k.multipliedReportingOverflow(by: c1).partialValue
// (k,_) = UInt32.multiplyWithOverflow(k, c1) ;//( k * c1);
//TODO: CHECKE >>>
k = (k << r1) | (k >>> (Int32(32) - r1)) //k = (k << r1) | (k >>> (32 - r1));
//k = UInt32 (truncatingBitPattern:Int64(Int64(k) * Int64(c2)));//( k * c2);
//(k,_) = UInt32.multiplyWithOverflow(k, c2)
k = Int32.multiplyWithOverflow(k, c2).0
k = k.multipliedReportingOverflow(by: c2).partialValue
var hash = Int32(hashIn)
hash = hash ^ k
hash = (hash << r2) | (hash >>> (Int32(32) - r2))//hash = (hash << r2) | (hash >>> (32 - r2));
(hash, _) = Int32.multiplyWithOverflow(hash, m)
(hash, _) = Int32.addWithOverflow(hash, n)
hash = hash.multipliedReportingOverflow(by: m).partialValue
hash = hash.addingReportingOverflow(n).partialValue
//hash = hash * m + n;
// print("murmur update2 : \(hash)")
return Int(hash)
@ -90,12 +90,12 @@ public final class MurmurHash {
public static func finish(_ hashin: Int, _ numberOfWordsIn: Int) -> Int {
var hash = Int32(hashin)
let numberOfWords = Int32(numberOfWordsIn)
hash = hash ^ Int32.multiplyWithOverflow(numberOfWords, Int32(4)).0 //(numberOfWords * UInt32(4));
hash = hash ^ numberOfWords.multipliedReportingOverflow(by: 4).partialValue //(numberOfWords * UInt32(4));
hash = hash ^ (hash >>> Int32(16)) //hash = hash ^ (hash >>> 16);
(hash, _) = Int32.multiplyWithOverflow(hash, Int32(-2048144789))//hash * UInt32(0x85EBCA6B);
hash = hash.multipliedReportingOverflow(by: -2048144789).partialValue //hash * UInt32(0x85EBCA6B);
hash = hash ^ (hash >>> Int32(13))//hash = hash ^ (hash >>> 13);
//hash = UInt32(truncatingBitPattern: UInt64(hash) * UInt64(0xC2B2AE35)) ;
(hash, _) = Int32.multiplyWithOverflow(hash, Int32(-1028477387))
hash = hash.multipliedReportingOverflow(by: -1028477387).partialValue
hash = hash ^ (hash >>> Int32(16))// hash = hash ^ (hash >>> 16);
//print("murmur finish : \(hash)")
return Int(hash)

View File

@ -61,7 +61,7 @@ extension String {
let start = characters.index(startIndex, offsetBy: integerRange.lowerBound)
let end = characters.index(startIndex, offsetBy: integerRange.upperBound)
let range = start ..< end
return self[range]
return String(self[range])
}
}

View File

@ -62,10 +62,10 @@ class VisitorTests: XCTestCase {
var errors = [String]()
override func syntaxError<T : ATNSimulator>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int, _ charPositionInLine: Int,
_ msg: String, _ e: AnyObject?) {
override func syntaxError<T>(_ recognizer: Recognizer<T>,
_ offendingSymbol: AnyObject?,
_ line: Int, _ charPositionInLine: Int,
_ msg: String, _ e: AnyObject?) {
errors.append("line \(line):\(charPositionInLine) \(msg)")
}
}