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
14 changes: 6 additions & 8 deletions runtime/Swift/Sources/Antlr4/BufferedTokenStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ public class BufferedTokenStream: TokenStream {
}

public func get(_ i: Int) throws -> Token {
if i < 0 || i >= tokens.count {
guard tokens.indices.contains(i) else {
throw ANTLRError.indexOutOfBounds(msg: "token index \(i) out of range 0 ..< \(tokens.count)")
}
return tokens[i]
Expand Down Expand Up @@ -284,8 +284,8 @@ public class BufferedTokenStream: TokenStream {
///
public func getTokens(_ start: Int, _ stop: Int, _ types: Set<Int>?) throws -> [Token]? {
try lazyInit()
if start < 0 || start >= tokens.count ||
stop < 0 || stop >= tokens.count {
guard tokens.indices.contains(start),
tokens.indices.contains(stop) else {
throw ANTLRError.indexOutOfBounds(msg: "start \(start) or stop \(stop) not in 0 ..< \(tokens.count)")

}
Expand All @@ -309,9 +309,7 @@ public class BufferedTokenStream: TokenStream {
}

public func getTokens(_ start: Int, _ stop: Int, _ ttype: Int) throws -> [Token]? {
var s = Set<Int>()
s.insert(ttype)
return try getTokens(start, stop, s)
return try getTokens(start, stop, [ttype])
}

///
Expand Down Expand Up @@ -378,7 +376,7 @@ public class BufferedTokenStream: TokenStream {
///
public func getHiddenTokensToRight(_ tokenIndex: Int, _ channel: Int = -1) throws -> [Token]? {
try lazyInit()
if tokenIndex < 0 || tokenIndex >= tokens.count {
guard tokens.indices.contains(tokenIndex) else {
throw ANTLRError.indexOutOfBounds(msg: "\(tokenIndex) not in 0 ..< \(tokens.count)")
}

Expand All @@ -403,7 +401,7 @@ public class BufferedTokenStream: TokenStream {
///
public func getHiddenTokensToLeft(_ tokenIndex: Int, _ channel: Int = -1) throws -> [Token]? {
try lazyInit()
if tokenIndex < 0 || tokenIndex >= tokens.count {
guard tokens.indices.contains(tokenIndex) else {
throw ANTLRError.indexOutOfBounds(msg: "\(tokenIndex) not in 0 ..< \(tokens.count)")
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/Swift/Sources/Antlr4/DiagnosticErrorListener.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public class DiagnosticErrorListener: BaseErrorListener {
let ruleIndex: Int = dfa.atnStartState.ruleIndex!

let ruleNames: [String] = recognizer.getRuleNames()
if ruleIndex < 0 || ruleIndex >= ruleNames.count {
guard ruleNames.indices.contains(ruleIndex) else {
return String(decision)
}

Expand Down
2 changes: 1 addition & 1 deletion runtime/Swift/Sources/Antlr4/atn/ATN.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public class ATN {
/// number `stateNumber`
///
public func getExpectedTokens(_ stateNumber: Int, _ context: RuleContext) throws -> IntervalSet {
if stateNumber < 0 || stateNumber >= states.count {
guard states.indices.contains(stateNumber) else {
throw ANTLRError.illegalArgument(msg: "Invalid state number.")
}

Expand Down
26 changes: 2 additions & 24 deletions runtime/Swift/Sources/Antlr4/atn/SemanticContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
return self
}

if operands.isEmpty {
// all elements were true, so the AND context is true
return SemanticContext.NONE
}

var result = operands[0]
let length = operands.count
for i in 1..<length {
result = SemanticContext.and(result, operands[i])
}

return result
return operands.reduce(SemanticContext.NONE, SemanticContext.and)
}

override
Expand Down Expand Up @@ -354,18 +343,7 @@ public class SemanticContext: Hashable, CustomStringConvertible {
return self
}

if operands.isEmpty {
// all elements were false, so the OR context is false
return nil
}

var result = operands[0]
let length = operands.count
for i in 1..<length {
result = SemanticContext.or(result, operands[i])
}

return result
return operands.reduce(nil, SemanticContext.or)
}

override
Expand Down
95 changes: 6 additions & 89 deletions runtime/Swift/Sources/Antlr4/misc/BitSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ public class BitSet: Hashable, CustomStringConvertible {

while true {
if word != 0 {
let bit = (u * BitSet.BITS_PER_WORD) + BitSet.numberOfTrailingZeros(word)
let bit = (u * BitSet.BITS_PER_WORD) + word.trailingZeroBitCount
return bit
}
u += 1
Expand All @@ -644,44 +644,6 @@ public class BitSet: Hashable, CustomStringConvertible {
}
}

public static func numberOfTrailingZeros(_ i: Int64) -> Int {
// HD, Figure 5-14
var x: Int32, y: Int32
if i == 0 {
return 64
}
var n: Int32 = 63
y = Int32(truncatingIfNeeded: i)
if y != 0 {
n = n - 32
x = y
} else {
x = Int32(truncatingIfNeeded: i >>> 32)
}

y = x << 16
if y != 0 {
n = n - 16
x = y
}
y = x << 8
if y != 0 {
n = n - 8
x = y
}
y = x << 4
if y != 0 {
n = n - 4
x = y
}
y = x << 2
if y != 0 {
n = n - 2
x = y
}
return Int(n - ((x << 1) >>> 31))
}

///
/// Returns the index of the first bit that is set to `false`
/// that occurs on or after the specified starting index.
Expand All @@ -708,7 +670,7 @@ public class BitSet: Hashable, CustomStringConvertible {

while true {
if word != 0 {
return (u * BitSet.BITS_PER_WORD) + BitSet.numberOfTrailingZeros(word)
return (u * BitSet.BITS_PER_WORD) + word.trailingZeroBitCount
}
u += 1
if u == wordsInUse {
Expand Down Expand Up @@ -759,7 +721,7 @@ public class BitSet: Hashable, CustomStringConvertible {
var word: Int64 = words[u] & (BitSet.WORD_MASK >>> Int64(-(fromIndex + 1)))
while true {
if word != 0 {
return (u + 1) * BitSet.BITS_PER_WORD - 1 - BitSet.numberOfLeadingZeros(word)
return (u + 1) * BitSet.BITS_PER_WORD - 1 - word.leadingZeroBitCount
}
if u == 0 {
return -1
Expand Down Expand Up @@ -803,7 +765,7 @@ public class BitSet: Hashable, CustomStringConvertible {

while true {
if word != 0 {
return (u + 1) * BitSet.BITS_PER_WORD - 1 - BitSet.numberOfLeadingZeros(word)
return (u + 1) * BitSet.BITS_PER_WORD - 1 - word.leadingZeroBitCount
}
if u == 0 {
return -1
Expand All @@ -812,38 +774,6 @@ public class BitSet: Hashable, CustomStringConvertible {
word = ~words[u]
}
}

public static func numberOfLeadingZeros(_ i: Int64) -> Int {
// HD, Figure 5-6
if i == 0 {
return 64
}
var n: Int32 = 1
var x = Int32(i >>> 32)
if x == 0 {
n += 32
x = Int32(i)
}
if x >>> 16 == 0 {
n += 16
x <<= 16
}
if x >>> 24 == 0 {
n += 8
x <<= 8
}
if x >>> 28 == 0 {
n += 4
x <<= 4
}
if x >>> 30 == 0 {
n += 2
x <<= 2
}
n -= x >>> 31

return Int(n)
}
///
/// Returns the "logical size" of this `BitSet`: the index of
/// the highest set bit in the `BitSet` plus one. Returns zero
Expand All @@ -857,7 +787,7 @@ public class BitSet: Hashable, CustomStringConvertible {
}

return BitSet.BITS_PER_WORD * (wordsInUse - 1) +
(BitSet.BITS_PER_WORD - BitSet.numberOfLeadingZeros(words[wordsInUse - 1]))
(BitSet.BITS_PER_WORD - words[wordsInUse - 1].leadingZeroBitCount)
}

///
Expand Down Expand Up @@ -897,24 +827,11 @@ public class BitSet: Hashable, CustomStringConvertible {
public func cardinality() -> Int {
var sum: Int = 0
for i in 0..<wordsInUse {
sum += BitSet.bitCount(words[i])
sum += words[i].nonzeroBitCount
}
return sum
}

public static func bitCount(_ i: Int64) -> Int {
var i = i
// HD, Figure 5-14
i = i - ((i >>> 1) & 0x5555555555555555)
i = (i & 0x3333333333333333) + ((i >>> 2) & 0x3333333333333333)
i = (i + (i >>> 4)) & 0x0f0f0f0f0f0f0f0f
i = i + (i >>> 8)
i = i + (i >>> 16)
i = i + (i >>> 32)

return Int(i) & 0x7f
}

///
/// Performs a logical __AND__ of this target bit set with the
/// argument bit set. This bit set is modified so that each bit in it
Expand Down
5 changes: 1 addition & 4 deletions runtime/Swift/Sources/Antlr4/misc/IntervalSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -613,10 +613,7 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible {
for interval in intervals {
let a = interval.a
let b = interval.b

for v in a...b {
values.append(v)
}
values.append(contentsOf: a...b)
}
return values
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/Swift/Sources/Antlr4/misc/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class Utils {
}

public static func bitLeftShift(_ n: Int) -> Int64 {
return (Int64(1) << Int64(n % 64))
return (Int64(1) &<< n)
}


Expand Down
32 changes: 3 additions & 29 deletions runtime/Swift/Sources/Antlr4/misc/utils/CommonUtil.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,15 @@ public func +(lhs: Token, rhs: String) -> String {
infix operator >>> : BitwiseShiftPrecedence

func >>>(lhs: Int32, rhs: Int32) -> Int32 {
let left = UInt32(bitPattern: lhs)
let right = UInt32(bitPattern: rhs) % 32

return Int32(bitPattern: left >> right)
return lhs &>> rhs
}

func >>>(lhs: Int64, rhs: Int64) -> Int64 {
let left = UInt64(bitPattern: lhs)
let right = UInt64(bitPattern: rhs) % 64

return Int64(bitPattern: left >> right)
return lhs &>> rhs
}

func >>>(lhs: Int, rhs: Int) -> Int {
let numberOfBits: UInt = MemoryLayout<UInt>.size == MemoryLayout<UInt64>.size ? 64 : 32

let left = UInt(bitPattern: lhs)
let right = UInt(bitPattern: rhs) % numberOfBits

return Int(bitPattern: left >> right)
return lhs &>> rhs
}

func intChar2String(_ i: Int) -> String {
Expand Down Expand Up @@ -89,18 +78,3 @@ func toUUID(_ data: [Character], _ offset: Int) -> UUID {
let mostSigBits: Int64 = toLong(data, offset + 4)
return UUID(mostSigBits: mostSigBits, leastSigBits: leastSigBits)
}

func ==<T:Equatable>(_ lhs: [T?], _ rhs: [T?]) -> Bool {

if lhs.count != rhs.count {
return false
}

for i in 0..<lhs.count {
if lhs[i] != rhs[i] {
return false
}
}

return true
}
3 changes: 1 addition & 2 deletions runtime/Swift/Sources/Antlr4/tree/Trees.swift
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ public class Trees {
}

public static func descendants(_ t: ParseTree) -> Array<ParseTree> {
var nodes: Array<ParseTree> = Array<ParseTree>()
nodes.append(t)
var nodes: Array<ParseTree> = [t]

let n: Int = t.getChildCount()
for i in 0..<n {
Expand Down