From 8a292c0f4f8f7a4705330d0015762383dab00744 Mon Sep 17 00:00:00 2001 From: Ewan Mellor Date: Wed, 27 Sep 2017 11:54:15 -0700 Subject: [PATCH] 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. --- .../Sources/Antlr4/ANTLRErrorListener.swift | 12 ++-- .../Sources/Antlr4/BaseErrorListener.swift | 12 ++-- .../Sources/Antlr4/ConsoleErrorListener.swift | 12 ++-- runtime/Swift/Sources/Antlr4/Lexer.swift | 2 +- .../Sources/Antlr4/ProxyErrorListener.swift | 14 ++--- .../Swift/Sources/Antlr4/RuleContext.swift | 4 +- .../Swift/Sources/Antlr4/atn/ATNConfig.swift | 2 +- .../Sources/Antlr4/atn/ATNConfigSet.swift | 5 +- .../Sources/Antlr4/atn/ATNDeserializer.swift | 2 +- .../Antlr4/atn/PredictionContext.swift | 6 +- .../Sources/Antlr4/atn/SemanticContext.swift | 56 ++++++++----------- .../Sources/Antlr4/misc/ArrayWrapper.swift | 2 +- .../Swift/Sources/Antlr4/misc/BitSet.swift | 4 +- .../Swift/Sources/Antlr4/misc/HashMap.swift | 4 +- .../Sources/Antlr4/misc/MurmurHash.swift | 16 +++--- .../misc/extension/StringExtension.swift | 2 +- .../Tests/Antlr4Tests/VisitorTests.swift | 8 +-- 17 files changed, 76 insertions(+), 87 deletions(-) diff --git a/runtime/Swift/Sources/Antlr4/ANTLRErrorListener.swift b/runtime/Swift/Sources/Antlr4/ANTLRErrorListener.swift index c83ee58a3..56d1987f3 100644 --- a/runtime/Swift/Sources/Antlr4/ANTLRErrorListener.swift +++ b/runtime/Swift/Sources/Antlr4/ANTLRErrorListener.swift @@ -39,12 +39,12 @@ public protocol ANTLRErrorListener: class { /// the parser was able to recover in line without exiting the /// surrounding rule. /// - func syntaxError(_ recognizer: Recognizer, - _ offendingSymbol: AnyObject?, - _ line: Int, - _ charPositionInLine: Int, - _ msg: String, - _ e: AnyObject? + func syntaxError(_ recognizer: Recognizer, + _ offendingSymbol: AnyObject?, + _ line: Int, + _ charPositionInLine: Int, + _ msg: String, + _ e: AnyObject? ) /// diff --git a/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift b/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift index 75d3cad8c..9a9805b96 100644 --- a/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift +++ b/runtime/Swift/Sources/Antlr4/BaseErrorListener.swift @@ -17,12 +17,12 @@ open class BaseErrorListener: ANTLRErrorListener { public init() { } - open func syntaxError(_ recognizer: Recognizer, - _ offendingSymbol: AnyObject?, - _ line: Int, - _ charPositionInLine: Int, - _ msg: String, - _ e: AnyObject? + open func syntaxError(_ recognizer: Recognizer, + _ offendingSymbol: AnyObject?, + _ line: Int, + _ charPositionInLine: Int, + _ msg: String, + _ e: AnyObject? ) { } diff --git a/runtime/Swift/Sources/Antlr4/ConsoleErrorListener.swift b/runtime/Swift/Sources/Antlr4/ConsoleErrorListener.swift index 22a76c5f4..de6584c30 100644 --- a/runtime/Swift/Sources/Antlr4/ConsoleErrorListener.swift +++ b/runtime/Swift/Sources/Antlr4/ConsoleErrorListener.swift @@ -25,12 +25,12 @@ public class ConsoleErrorListener: BaseErrorListener { /// line __line__:__charPositionInLine__ __msg__ /// /// - override public func syntaxError(_ recognizer: Recognizer, - _ offendingSymbol: AnyObject?, - _ line: Int, - _ charPositionInLine: Int, - _ msg: String, - _ e: AnyObject? + override public func syntaxError(_ recognizer: Recognizer, + _ offendingSymbol: AnyObject?, + _ line: Int, + _ charPositionInLine: Int, + _ msg: String, + _ e: AnyObject? ) { if Parser.ConsoleError { errPrint("line \(line):\(charPositionInLine) \(msg)") diff --git a/runtime/Swift/Sources/Antlr4/Lexer.swift b/runtime/Swift/Sources/Antlr4/Lexer.swift index a05150db4..a324d321b 100644 --- a/runtime/Swift/Sources/Antlr4/Lexer.swift +++ b/runtime/Swift/Sources/Antlr4/Lexer.swift @@ -405,7 +405,7 @@ open class Lexer: Recognizer } } - open func notifyListeners(_ e: LexerNoViableAltException, recognizer: Recognizer) { + open func notifyListeners(_ e: LexerNoViableAltException, recognizer: Recognizer) { let text: String = _input!.getText(Interval.of(_tokenStartCharIndex, _input!.index())) let msg: String = "token recognition error at: '\(getErrorDisplay(text))'" diff --git a/runtime/Swift/Sources/Antlr4/ProxyErrorListener.swift b/runtime/Swift/Sources/Antlr4/ProxyErrorListener.swift index 481064ff5..5d1d7646c 100644 --- a/runtime/Swift/Sources/Antlr4/ProxyErrorListener.swift +++ b/runtime/Swift/Sources/Antlr4/ProxyErrorListener.swift @@ -20,13 +20,13 @@ public class ProxyErrorListener: ANTLRErrorListener { self.delegates = delegates } - public func syntaxError(_ recognizer: Recognizer, - _ offendingSymbol: AnyObject?, - _ line: Int, - _ charPositionInLine: Int, - _ msg: String, - _ e: AnyObject?) - { + public func syntaxError(_ recognizer: Recognizer, + _ offendingSymbol: AnyObject?, + _ line: Int, + _ charPositionInLine: Int, + _ msg: String, + _ e: AnyObject?) + { for listener: ANTLRErrorListener in delegates { listener.syntaxError(recognizer, offendingSymbol, line, charPositionInLine, msg, e) } diff --git a/runtime/Swift/Sources/Antlr4/RuleContext.swift b/runtime/Swift/Sources/Antlr4/RuleContext.swift index 18dafd571..a05b43257 100644 --- a/runtime/Swift/Sources/Antlr4/RuleContext.swift +++ b/runtime/Swift/Sources/Antlr4/RuleContext.swift @@ -233,7 +233,7 @@ open class RuleContext: RuleNode { return description } - public final func toString(_ recog: Recognizer) -> String { + public final func toString(_ recog: Recognizer) -> 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(_ recog: Recognizer?, _ stop: RuleContext) -> String { + open func toString(_ recog: Recognizer?, _ stop: RuleContext) -> String { let ruleNames: [String]? = recog != nil ? recog!.getRuleNames() : nil let ruleNamesList: Array? = ruleNames ?? nil return toString(ruleNamesList, stop) diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift b/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift index 1054ef342..75bff30e4 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNConfig.swift @@ -167,7 +167,7 @@ public class ATNConfig: Hashable, CustomStringConvertible { //return "MyClass \(string)" return toString(nil, true) } - public func toString(_ recog: Recognizer?, _ showAlt: Bool) -> String { + public func toString(_ recog: Recognizer?, _ showAlt: Bool) -> String { let buf: StringBuilder = StringBuilder() buf.append("(") buf.append(state) diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift index 5cac3bffb..803d9c369 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNConfigSet.swift @@ -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 { diff --git a/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift b/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift index 5d3d25015..7bc3f63fc 100644 --- a/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift +++ b/runtime/Swift/Sources/Antlr4/atn/ATNDeserializer.swift @@ -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) diff --git a/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift b/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift index c603b06c6..783a1ab5b 100644 --- a/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift +++ b/runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift @@ -712,17 +712,17 @@ public class PredictionContext: Hashable, CustomStringConvertible { } } - public func toString(_ recog: Recognizer) -> String { + public func toString(_ recog: Recognizer) -> String { return NSStringFromClass(PredictionContext.self) // return toString(recog, ParserRuleContext.EMPTY); } - public func toStrings(_ recognizer: Recognizer, _ currentState: Int) -> [String] { + public func toStrings(_ recognizer: Recognizer, _ currentState: Int) -> [String] { return toStrings(recognizer, PredictionContext.EMPTY, currentState) } // FROM SAM - public func toStrings(_ recognizer: Recognizer?, _ stop: PredictionContext, _ currentState: Int) -> [String] { + public func toStrings(_ recognizer: Recognizer?, _ stop: PredictionContext, _ currentState: Int) -> [String] { var result: Array = Array() var perm: Int = 0 outer: while true { diff --git a/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift b/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift index 413d517be..4ad0bd935 100644 --- a/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift +++ b/runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift @@ -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(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> Bool { + public func eval(_ parser: Recognizer, _ 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(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> SemanticContext? { + public func evalPrecedence(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> SemanticContext? { return self } public var hashValue: Int { @@ -90,7 +90,7 @@ public class SemanticContext: Hashable, CustomStringConvertible { } override - public func eval(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> Bool { + public func eval(_ parser: Recognizer, _ 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(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> Bool { + public func eval(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> Bool { return try parser.precpred(parserCallStack, precedence) } override - public func evalPrecedence(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> SemanticContext? { + public func evalPrecedence(_ parser: Recognizer, _ 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 = - 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(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> Bool { + public func eval(_ parser: Recognizer, _ 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(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> SemanticContext? { + public func evalPrecedence(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> SemanticContext? { var differs: Bool = false var operands: Array = Array() for context: SemanticContext in opnds { @@ -314,17 +313,17 @@ public class SemanticContext: Hashable, CustomStringConvertible { operands.insert(b) } - let precedencePredicates: Array = 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(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> Bool { + public func eval(_ parser: Recognizer, _ 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(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> SemanticContext? { + public func evalPrecedence(_ parser: Recognizer, _ parserCallStack: RuleContext) throws -> SemanticContext? { var differs: Bool = false var operands: Array = Array() for context: SemanticContext in opnds { @@ -443,21 +442,14 @@ public class SemanticContext: Hashable, CustomStringConvertible { return result } - private static func filterPrecedencePredicates( - _ collection: inout Set) -> - Array { - - let result = collection.filter { - $0 is PrecedencePredicate + private static func filterPrecedencePredicates(_ collection: inout Set) -> [PrecedencePredicate] { + let result = collection.flatMap { + $0 as? PrecedencePredicate } collection = Set(collection.filter { !($0 is PrecedencePredicate) }) - //if (result == nil) { - //return Array(); - //} - - return (result as! Array) + return result } } diff --git a/runtime/Swift/Sources/Antlr4/misc/ArrayWrapper.swift b/runtime/Swift/Sources/Antlr4/misc/ArrayWrapper.swift index 1ad30758b..5d5a68b88 100644 --- a/runtime/Swift/Sources/Antlr4/misc/ArrayWrapper.swift +++ b/runtime/Swift/Sources/Antlr4/misc/ArrayWrapper.swift @@ -69,7 +69,7 @@ public final class ArrayWrapper: ExpressibleByArrayLiteral, Hashabl } -public func == (lhs: ArrayWrapper, rhs: ArrayWrapper) -> Bool { +public func == (lhs: ArrayWrapper, rhs: ArrayWrapper) -> Bool { if lhs === rhs { return true } diff --git a/runtime/Swift/Sources/Antlr4/misc/BitSet.swift b/runtime/Swift/Sources/Antlr4/misc/BitSet.swift index d2c9b510f..5110ab712 100644 --- a/runtime/Swift/Sources/Antlr4/misc/BitSet.swift +++ b/runtime/Swift/Sources/Antlr4/misc/BitSet.swift @@ -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 diff --git a/runtime/Swift/Sources/Antlr4/misc/HashMap.swift b/runtime/Swift/Sources/Antlr4/misc/HashMap.swift index 89aced6a2..0157d8d56 100644 --- a/runtime/Swift/Sources/Antlr4/misc/HashMap.swift +++ b/runtime/Swift/Sources/Antlr4/misc/HashMap.swift @@ -41,7 +41,7 @@ final class Entry: CustomStringConvertible { var description: String { return "\(getKey())=\(getValue())" } } -func == (lhs: Entry, rhs: Entry) -> Bool { +func == (lhs: Entry, rhs: Entry) -> Bool { if lhs === rhs { return true } @@ -52,7 +52,7 @@ func == (lhs: Entry, rhs: Entry) -> Bool { } return false } -func == (lhs: Entry, rhs: Entry) -> Bool { +func == (lhs: Entry, rhs: Entry) -> Bool { if lhs === rhs { return true } diff --git a/runtime/Swift/Sources/Antlr4/misc/MurmurHash.swift b/runtime/Swift/Sources/Antlr4/misc/MurmurHash.swift index 09819e0d0..1bdbcd426 100644 --- a/runtime/Swift/Sources/Antlr4/misc/MurmurHash.swift +++ b/runtime/Swift/Sources/Antlr4/misc/MurmurHash.swift @@ -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) diff --git a/runtime/Swift/Sources/Antlr4/misc/extension/StringExtension.swift b/runtime/Swift/Sources/Antlr4/misc/extension/StringExtension.swift index c50ba7a1e..1928be7f9 100644 --- a/runtime/Swift/Sources/Antlr4/misc/extension/StringExtension.swift +++ b/runtime/Swift/Sources/Antlr4/misc/extension/StringExtension.swift @@ -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]) } } diff --git a/runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift b/runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift index e22488797..d7adb1cb6 100644 --- a/runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift +++ b/runtime/Swift/Tests/Antlr4Tests/VisitorTests.swift @@ -62,10 +62,10 @@ class VisitorTests: XCTestCase { var errors = [String]() - override func syntaxError(_ recognizer: Recognizer, - _ offendingSymbol: AnyObject?, - _ line: Int, _ charPositionInLine: Int, - _ msg: String, _ e: AnyObject?) { + override func syntaxError(_ recognizer: Recognizer, + _ offendingSymbol: AnyObject?, + _ line: Int, _ charPositionInLine: Int, + _ msg: String, _ e: AnyObject?) { errors.append("line \(line):\(charPositionInLine) \(msg)") } }