Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions runtime/Swift/Sources/Antlr4/BufferedTokenStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,7 @@ public class BufferedTokenStream: TokenStream {
var filteredTokens = [Token]()
for i in start...stop {
let t = tokens[i]
if let types = types, !types.contains(t.getType()) {
}
else {
if types?.contains(t.getType()) ?? true {
filteredTokens.append(t)
}
}
Expand Down
4 changes: 2 additions & 2 deletions runtime/Swift/Sources/Antlr4/FailedPredicateException.swift
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public class FailedPredicateException: RecognitionException {


private static func formatMessage(_ predicate: String?, _ message: String?) -> String {
if message != nil {
return message!
if let message = message {
return message
}

let predstr = predicate ?? "<unknown>"
Expand Down
6 changes: 1 addition & 5 deletions runtime/Swift/Sources/Antlr4/LexerInterpreter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,6 @@ public class LexerInterpreter: Lexer {

override
public func getVocabulary() -> Vocabulary {
if vocabulary != nil {
return vocabulary!
}

return super.getVocabulary()
return vocabulary ?? super.getVocabulary()
}
}
4 changes: 1 addition & 3 deletions runtime/Swift/Sources/Antlr4/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ open class Parser: Recognizer<ParserATNSimulator> {

/// reset the parser's state
public func reset() throws {
if (getInputStream() != nil) {
try getInputStream()!.seek(0)
}
try getInputStream()?.seek(0)
_errHandler.reset(self)
_ctx = nil
_syntaxErrors = 0
Expand Down
8 changes: 4 additions & 4 deletions runtime/Swift/Sources/Antlr4/atn/LexerATNSimulator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -419,15 +419,15 @@ open class LexerATNSimulator: ATNSimulator {

if config.state is RuleStopState {
if LexerATNSimulator.debug {
if recog != nil {
print("closure at \(recog!.getRuleNames()[config.state.ruleIndex!]) rule stop \(config)\n")
if let recog = recog {
print("closure at \(recog.getRuleNames()[config.state.ruleIndex!]) rule stop \(config)\n")
} else {
print("closure at rule stop \(config)\n")
}
}

if config.context == nil || config.context!.hasEmptyPath() {
if config.context == nil || config.context!.isEmpty() {
if config.context?.hasEmptyPath() ?? true {
if config.context?.isEmpty() ?? true {
try configs.add(config)
return true
} else {
Expand Down
4 changes: 2 additions & 2 deletions runtime/Swift/Sources/Antlr4/atn/LexerActionExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ public class LexerActionExecutor: Hashable {
/// of `lexerActionExecutor` and `lexerAction`.
///
public static func append(_ lexerActionExecutor: LexerActionExecutor?, _ lexerAction: LexerAction) -> LexerActionExecutor {
if lexerActionExecutor == nil {
guard let lexerActionExecutor = lexerActionExecutor else {
return LexerActionExecutor([lexerAction])
}

//var lexerActions : [LexerAction] = lexerActionExecutor.lexerActions, //lexerActionExecutor.lexerActions.length + 1);
var lexerActions: [LexerAction] = lexerActionExecutor!.lexerActions
var lexerActions: [LexerAction] = lexerActionExecutor.lexerActions
lexerActions.append(lexerAction)
//lexerActions[lexerActions.length - 1] = lexerAction;
return LexerActionExecutor(lexerActions)
Expand Down
61 changes: 19 additions & 42 deletions runtime/Swift/Sources/Antlr4/atn/PredictionContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,18 @@ public class PredictionContext: Hashable, CustomStringConvertible {

if let mergeCache = mergeCache {
var previous = mergeCache.get(a, b)
if previous != nil {
return previous!
if let previous = previous {
return previous
}
previous = mergeCache.get(b, a)
if previous != nil {
return previous!
if let previous = previous {
return previous
}
}


if let rootMerge = mergeRoot(a, b, rootIsWildcard) {
if mergeCache != nil {
mergeCache!.put(a, b, rootMerge)
}
mergeCache?.put(a, b, rootMerge)
return rootMerge
}

Expand All @@ -243,9 +241,7 @@ public class PredictionContext: Hashable, CustomStringConvertible {
// of those graphs. dup a, a' points at merged array
// new joined parent so create new singleton pointing to it, a'
let a_ = SingletonPredictionContext.create(parent, a.returnState);
if mergeCache != nil {
mergeCache!.put(a, b, a_)
}
mergeCache?.put(a, b, a_)
return a_
} else {
// a != b payloads differ
Expand All @@ -256,7 +252,7 @@ public class PredictionContext: Hashable, CustomStringConvertible {
// ax + bx = [a,b]x
singleParent = a.parent
}
if singleParent != nil {
if let singleParent = singleParent {
// parents are same
// sort payloads and use same parent
var payloads = [a.returnState, b.returnState]
Expand All @@ -266,9 +262,7 @@ public class PredictionContext: Hashable, CustomStringConvertible {
}
let parents = [singleParent, singleParent]
let a_ = ArrayPredictionContext(parents, payloads)
if mergeCache != nil {
mergeCache!.put(a, b, a_)
}
mergeCache?.put(a, b, a_)
return a_
}
// parents differ and can't merge them. Just pack together
Expand All @@ -286,9 +280,7 @@ public class PredictionContext: Hashable, CustomStringConvertible {
// print("parent is null")
}
let a_ = ArrayPredictionContext(parents, payloads)
if mergeCache != nil {
mergeCache!.put(a, b, a_)
}
mergeCache?.put(a, b, a_)
return a_
}
}
Expand Down Expand Up @@ -388,15 +380,8 @@ public class PredictionContext: Hashable, CustomStringConvertible {
_ rootIsWildcard: Bool,
_ mergeCache: inout DoubleKeyMap<PredictionContext, PredictionContext, PredictionContext>?) -> PredictionContext {

if mergeCache != nil {
var previous = mergeCache!.get(a, b)
if previous != nil {
return previous!
}
previous = mergeCache!.get(b, a)
if previous != nil {
return previous!
}
if let previous = mergeCache?.get(a, b) ?? mergeCache?.get(b, a) {
return previous
}

// merge sorted payloads a + b => M
Expand Down Expand Up @@ -425,7 +410,7 @@ public class PredictionContext: Hashable, CustomStringConvertible {
let payload = aReturnStates[i]
// $+$ = $
let both$ = ((payload == EMPTY_RETURN_STATE) && a_parent == nil && b_parent == nil)
let ax_ax = (a_parent != nil && b_parent != nil && a_parent! == b_parent!)
let ax_ax = (a_parent != nil && b_parent != nil && a_parent == b_parent)

if both$ || ax_ax {
mergedParents[k] = a_parent // choose left
Expand Down Expand Up @@ -474,9 +459,7 @@ public class PredictionContext: Hashable, CustomStringConvertible {
if k == 1 {
// for just one merged element, return singleton top
let a_ = SingletonPredictionContext.create(mergedParents[0], mergedReturnStates[0])
if mergeCache != nil {
mergeCache!.put(a, b, a_)
}
mergeCache?.put(a, b, a_)
//print("merge array 1 \(a_)")
return a_
}
Expand All @@ -489,25 +472,19 @@ public class PredictionContext: Hashable, CustomStringConvertible {
// if we created same array as a or b, return that instead
// TODO: track whether this is possible above during merge sort for speed
if M == a {
if mergeCache != nil {
mergeCache!.put(a, b, a)
}
mergeCache?.put(a, b, a)
return a
}
if M == b {
if mergeCache != nil {
mergeCache!.put(a, b, b)
}
mergeCache?.put(a, b, b)
return b
}

//modify by janyou
//combineCommonParents(&mergedParents)
M.combineCommonParents()

if mergeCache != nil {
mergeCache!.put(a, b, M)
}
mergeCache?.put(a, b, M)
// print("merge array 4 \(M)")
return M
}
Expand Down Expand Up @@ -596,12 +573,12 @@ public class PredictionContext: Hashable, CustomStringConvertible {
var parents = [PredictionContext?](repeating: nil, count: context.size())
let length = parents.count
for i in 0..<length {
if context.getParent(i) == nil {
guard let p = context.getParent(i) else {
return context
}

let parent = getCachedContext(context.getParent(i)!, contextCache, &visited)
if changed || parent !== context.getParent(i) {
let parent = getCachedContext(p, contextCache, &visited)
if changed || parent !== p {
if !changed {
parents = [PredictionContext?](repeating: nil, count: context.size())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class SingletonPredictionContext: PredictionContext {
self.returnState = returnState


super.init(parent != nil ? PredictionContext.calculateHashCode(parent!, returnState) : PredictionContext.calculateEmptyHashCode())
super.init(parent.map { PredictionContext.calculateHashCode($0, returnState) } ?? PredictionContext.calculateEmptyHashCode())
}

public static func create(_ parent: PredictionContext?, _ returnState: Int) -> SingletonPredictionContext {
Expand Down
5 changes: 1 addition & 4 deletions runtime/Swift/Sources/Antlr4/misc/DoubleKeyMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,7 @@ public struct DoubleKeyMap<Key1: Hashable, Key2: Hashable, Value> {
}

public func get(_ k1: Key1, _ k2: Key2) -> Value? {
if let data2 = data[k1] {
return data2[k2]
}
return nil
return data[k1]?[k2]
}

public func get(_ k1: Key1) -> [Key2: Value]? {
Expand Down
2 changes: 1 addition & 1 deletion runtime/Swift/Sources/Antlr4/misc/MurmurHash.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public final class MurmurHash {
/// - Returns: the updated intermediate hash value
///
public static func update<T:Hashable>(_ hash: UInt32, _ value: T?) -> UInt32 {
return update2(hash, value != nil ? value!.hashValue : 0)
return update2(hash, value?.hashValue ?? 0)
}

///
Expand Down
14 changes: 5 additions & 9 deletions runtime/Swift/Sources/Antlr4/tree/Trees.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public class Trees {
/// parse trees and extract data appropriately.
///
public static func toStringTree(_ t: Tree, _ recog: Parser?) -> String {
let ruleNames: [String]? = recog != nil ? recog!.getRuleNames() : nil
let ruleNamesList: Array<String>? = ruleNames ?? nil
let ruleNamesList: [String]? = recog?.getRuleNames()
return toStringTree(t, ruleNamesList)
}

Expand All @@ -81,16 +80,14 @@ public class Trees {
}

public static func getNodeText(_ t: Tree, _ recog: Parser?) -> String {
let ruleNames: [String]? = recog != nil ? recog!.getRuleNames() : nil
let ruleNamesList: Array<String>? = ruleNames ?? nil
return getNodeText(t, ruleNamesList)
return getNodeText(t, recog?.getRuleNames())
}

public static func getNodeText(_ t: Tree, _ ruleNames: Array<String>?) -> String {
if ruleNames != nil {
if let ruleNames = ruleNames {
if let ruleNode = t as? RuleNode {
let ruleIndex: Int = ruleNode.getRuleContext().getRuleIndex()
let ruleName: String = ruleNames![ruleIndex]
let ruleName: String = ruleNames[ruleIndex]
let altNumber = (t as! RuleContext).getAltNumber()
if altNumber != ATN.INVALID_ALT_NUMBER {
return "\(ruleName):\(altNumber)"
Expand All @@ -109,8 +106,7 @@ public class Trees {
}
// no recog for rule names
let payload: AnyObject = t.getPayload()
if payload is Token {
let token = payload as! Token
if let token = payload as? Token {
return token.getText()!
}
return "\(t.getPayload())"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,7 @@ public class ParseTreeMatch: CustomStringConvertible {
/// is returned.
///
public func getAll(_ label: String) -> Array<ParseTree> {
guard let nodes = labels.get(label) else {
return Array<ParseTree>()
}

return nodes
return labels.get(label) ?? []
}

///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ public class ParseTreePatternMatcher {
let tokenTagToken: TokenTagToken = t2.getSymbol() as! TokenTagToken
// track label->list-of-nodes for both token name and label (if any)
labels.map(tokenTagToken.getTokenName(), tree)
if tokenTagToken.getLabel() != nil {
labels.map(tokenTagToken.getLabel()!, tree)
if let label = tokenTagToken.getLabel() {
labels.map(label, tree)
}
} else {
if t1.getText() == t2.getText() {
Expand Down
4 changes: 2 additions & 2 deletions runtime/Swift/Sources/Antlr4/tree/pattern/TokenTagToken.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ public class TokenTagToken: CommonToken {
///
override
public func getText() -> String {
if label != nil {
return "<" + label! + ":" + tokenName + ">"
if let label = label {
return "<" + label + ":" + tokenName + ">"
}

return "<" + tokenName + ">"
Expand Down