MultiMap

///
///
public class MultiMap<K:Hashable, V> {
    private var mapping = [K: Array<V>]()
    public func map(_ key: K, _ value: V) {
        mapping[key, default: Array()].append(value)
    }
    public func getPairs() -> Array<(K, V)> {
        var pairs: Array<(K, V)> = Array<(K, V)>()
        for key: K in mapping.keys {
            for value: V in mapping[key]! {
                pairs.append((key, value))
            }
        }
        return pairs
    }
    public func get(_ key: K) -> Array<(V)>? {
        return mapping[key]
    }
    public func size() -> Int {
        return mapping.count
    }
}


BitSet

///
///
//
//  BitSet.swift
//  Antlr.swift
//
//  Created by janyou on 15/9/8.
//
import Foundation
///
/// This class implements a vector of bits that grows as needed. Each
/// component of the bit set has a `boolean` value. The
/// bits of a `BitSet` are indexed by nonnegative integers.
/// Individual indexed bits can be examined, set, or cleared. One
/// `BitSet` may be used to modify the contents of another
/// `BitSet` through logical AND, logical inclusive OR, and
/// logical exclusive OR operations.
///
/// By default, all bits in the set initially have the value
/// `false`.
///
/// Every bit set has a current size, which is the number of bits
/// of space currently in use by the bit set. Note that the size is
/// related to the implementation of a bit set, so it may change with
/// implementation. The length of a bit set relates to logical length
/// of a bit set and is defined independently of implementation.
///
/// A `BitSet` is not safe for multithreaded use without
/// external synchronization.
///
/// - note: Arthur van Hoff
/// - note: Michael McCloskey
/// - note: Martin Buchholz
/// - note: JDK1.0
///
public class BitSet: Hashable, CustomStringConvertible {
    ///
    /// BitSets are packed into arrays of "words."  Currently a word is
    /// a long, which consists of 64 bits, requiring 6 address bits.
    /// The choice of word size is determined purely by performance concerns.
    ///
    private static let ADDRESS_BITS_PER_WORD: Int = 6
    private static let BITS_PER_WORD: Int = 1 << ADDRESS_BITS_PER_WORD
    private static let BIT_INDEX_MASK: Int = BITS_PER_WORD - 1
    ///
    /// Used to shift left or right for a partial word mask
    ///
    private static let WORD_MASK: Int64 = Int64.max
    //0xfffffffffffffff//-1
    // 0xffffffffffffffffL;
    ///
    /// -  bits long[]
    ///
    /// The bits in this BitSet.  The ith bit is stored in bits[i/64] at
    /// bit position i % 64 (where bit position 0 refers to the least
    /// significant bit and 63 refers to the most significant bit).
    ///
    ///
    /// The internal field corresponding to the serialField "bits".
    ///
    fileprivate var words: [Int64]
    ///
    /// The number of words in the logical size of this BitSet.
    ///
    fileprivate var wordsInUse: Int = 0
    //transient
    ///
    /// Whether the size of "words" is user-specified.  If so, we assume
    /// the user knows what he's doing and try harder to preserve it.
    ///
    private var sizeIsSticky: Bool = false
    //transient
    ///
    /// use serialVersionUID from JDK 1.0.2 for interoperability
    ///
    private let serialVersionUID: Int64 = 7997698588986878753
    //L;
    ///
    /// Given a bit index, return word index containing it.
    ///
    private static func wordIndex(_ bitIndex: Int) -> Int {
        return bitIndex >> ADDRESS_BITS_PER_WORD
    }
    ///
    /// Every public method must preserve these invariants.
    ///
    fileprivate func checkInvariants() {
        assert((wordsInUse == 0 || words[wordsInUse - 1] != 0), "Expected: (wordsInUse==0||words[wordsInUse-1]!=0)")
        assert((wordsInUse >= 0 && wordsInUse <= words.count), "Expected: (wordsInUse>=0&&wordsInUse<=words.length)")
        // print("\(wordsInUse),\(words.count),\(words[wordsInUse])")
        assert((wordsInUse == words.count || words[wordsInUse] == 0), "Expected: (wordsInUse==words.count||words[wordsInUse]==0)")
    }
    ///
    /// Sets the field wordsInUse to the logical size in words of the bit set.
    /// WARNING:This method assumes that the number of words actually in use is
    /// less than or equal to the current value of wordsInUse!
    ///
    private func recalculateWordsInUse() {
        // Traverse the bitset until a used word is found
        var i: Int = wordsInUse - 1
        while i >= 0 {
            if words[i] != 0 {
                break
            }
            i -= 1
        }
        wordsInUse = i + 1 // The new logical size
    }
    ///
    /// Creates a new bit set. All bits are initially `false`.
    ///
    public init() {
        sizeIsSticky = false
        words = [Int64](repeating: Int64(0), count: BitSet.wordIndex(BitSet.BITS_PER_WORD - 1) + 1)
        //initWords(BitSet.BITS_PER_WORD);
    }
    ///
    /// Creates a bit set whose initial size is large enough to explicitly
    /// represent bits with indices in the range `0` through
    /// `nbits-1`. All bits are initially `false`.
    ///
    /// - parameter  nbits: the initial size of the bit set
    /// - throws: _ANTLRError.negativeArraySize_ if the specified initial size
    /// is negative
    ///
    public init(_ nbits: Int) throws {
        // nbits can't be negative; size 0 is OK
        // words = [BitSet.wordIndex(nbits-1) + 1];
        words = [Int64](repeating: Int64(0), count: BitSet.wordIndex(BitSet.BITS_PER_WORD - 1) + 1)
        sizeIsSticky = true
        if nbits < 0 {
            throw ANTLRError.negativeArraySize(msg: "nbits < 0:\(nbits) ")
        }
        // initWords(nbits);
    }
    private func initWords(_ nbits: Int) {
        // words =  [Int64](count: BitSet.wordIndex(BitSet.BITS_PER_WORD-1) + 1, repeatedValue: Int64(0));
        //  words = [BitSet.wordIndex(nbits-1) + 1];
    }
    ///
    /// Creates a bit set using words as the internal representation.
    /// The last word (if there is one) must be non-zero.
    ///
    private init(_ words: [Int64]) {
        self.words = words
        self.wordsInUse = words.count
        checkInvariants()
    }
    ///
    /// Returns a new long array containing all the bits in this bit set.
    ///
    /// More precisely, if
    /// `long[] longs = s.toLongArray();`
    /// then `longs.length == (s.length()+63)/64` and
    /// `s.get(n) == ((longs[n/64] & (1L<<(n%64))) != 0)`
    /// for all `n < 64 * longs.length`.
    ///
    /// - returns: a long array containing a little-endian representation
    /// of all the bits in this bit set
    ///
    public func toLongArray() -> [Int64] {
        return copyOf(words, wordsInUse)
    }
    private func copyOf(_ words: [Int64], _ newLength: Int) -> [Int64] {
        var newWords = [Int64](repeating: Int64(0), count: newLength)
        let length = min(words.count, newLength)
        newWords[0 ..< length] = words[0 ..< length]
        return newWords
    }
    ///
    /// Ensures that the BitSet can hold enough words.
    /// - parameter wordsRequired: the minimum acceptable number of words.
    ///
    private func ensureCapacity(_ wordsRequired: Int) {
        if words.count < wordsRequired {
            // Allocate larger of doubled size or required size
            let request: Int = max(2 * words.count, wordsRequired)
            words = copyOf(words, request)
            sizeIsSticky = false
        }
    }
    ///
    /// Ensures that the BitSet can accommodate a given wordIndex,
    /// temporarily violating the invariants.  The caller must
    /// restore the invariants before returning to the user,
    /// possibly using recalculateWordsInUse().
    /// - parameter wordIndex: the index to be accommodated.
    ///
    private func expandTo(_ wordIndex: Int) {
        let wordsRequired: Int = wordIndex + 1
        if wordsInUse < wordsRequired {
            ensureCapacity(wordsRequired)
            wordsInUse = wordsRequired
        }
    }
    ///
    /// Checks that fromIndex ... toIndex is a valid range of bit indices.
    ///
    private static func checkRange(_ fromIndex: Int, _ toIndex: Int) throws {
        if fromIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "fromIndex < 0: \(fromIndex)")
        }
        if toIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "toIndex < 0: \(toIndex)")
        }
        if fromIndex > toIndex {
            throw ANTLRError.indexOutOfBounds(msg: "fromInde: \(fromIndex) > toIndex: \(toIndex)")
        }
    }
    ///
    /// Sets the bit at the specified index to the complement of its
    /// current value.
    ///
    /// - parameter  bitIndex: the index of the bit to flip
    /// - throws: _ANTLRError.IndexOutOfBounds_ if the specified index is negative
    ///
    public func flip(_ bitIndex: Int) throws {
        if bitIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "bitIndex < 0: \(bitIndex)")
        }
        let index: Int = BitSet.wordIndex(bitIndex)
        expandTo(index)
        words[index] ^= (Int64(1) << Int64(bitIndex % 64))
        recalculateWordsInUse()
        checkInvariants()
    }
    ///
    /// Sets each bit from the specified `fromIndex` (inclusive) to the
    /// specified `toIndex` (exclusive) to the complement of its current
    /// value.
    ///
    /// - parameter  fromIndex: index of the first bit to flip
    /// - parameter  toIndex: index after the last bit to flip
    /// - throws: _ANTLRError.IndexOutOfBounds_ if `fromIndex` is negative,
    /// or `toIndex` is negative, or `fromIndex` is
    /// larger than `toIndex`
    ///
    public func flip(_ fromIndex: Int, _ toIndex: Int) throws {
        try BitSet.checkRange(fromIndex, toIndex)
        if fromIndex == toIndex {
            return
        }
        let startWordIndex: Int = BitSet.wordIndex(fromIndex)
        let endWordIndex: Int = BitSet.wordIndex(toIndex - 1)
        expandTo(endWordIndex)
        let firstWordMask: Int64 = BitSet.WORD_MASK << Int64(fromIndex % 64)
        let lastWordMask: Int64 = BitSet.WORD_MASK >>> Int64(-toIndex)
        //var lastWordMask : Int64  = WORD_MASK >>> Int64(-toIndex);
        if startWordIndex == endWordIndex {
            // Case 1: One word
            words[startWordIndex] ^= (firstWordMask & lastWordMask)
        } else {
            // Case 2: Multiple words
            // Handle first word
            words[startWordIndex] ^= firstWordMask
            // Handle intermediate words, if any
            let start = startWordIndex + 1
            for i in start..<endWordIndex {
                words[i] ^= BitSet.WORD_MASK
            }
            // Handle last word
            words[endWordIndex] ^= lastWordMask
        }
        recalculateWordsInUse()
        checkInvariants()
    }
    ///
    /// Sets the bit at the specified index to `true`.
    ///
    /// - parameter  bitIndex: a bit index
    /// - throws: _ANTLRError.IndexOutOfBounds_ if the specified index is negative
    ///
    public func set(_ bitIndex: Int) throws {
        if bitIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "bitIndex < 0: \(bitIndex)")
        }
        let index: Int = BitSet.wordIndex(bitIndex)
        expandTo(index)
        // print(words.count)
        words[index] |= (Int64(1) << Int64(bitIndex % 64))  // Restores invariants
        checkInvariants()
    }
    ///
    /// Sets the bit at the specified index to the specified value.
    ///
    /// - parameter  bitIndex: a bit index
    /// - parameter  value: a boolean value to set
    /// - throws: _ANTLRError.IndexOutOfBounds_ if the specified index is negative
    ///
    public func set(_ bitIndex: Int, _ value: Bool) throws {
        if value {
            try set(bitIndex)
        } else {
            try clear(bitIndex)
        }
    }
    ///
    /// Sets the bits from the specified `fromIndex` (inclusive) to the
    /// specified `toIndex` (exclusive) to `true`.
    ///
    /// - parameter  fromIndex: index of the first bit to be set
    /// - parameter  toIndex: index after the last bit to be set
    /// - throws: _ANTLRError.IndexOutOfBounds_ if `fromIndex` is negative,
    /// or `toIndex` is negative, or `fromIndex` is
    /// larger than `toIndex`
    ///
    public func set(_ fromIndex: Int, _ toIndex: Int) throws {
        try BitSet.checkRange(fromIndex, toIndex)
        if fromIndex == toIndex {
            return
        }
        // Increase capacity if necessary
        let startWordIndex: Int = BitSet.wordIndex(fromIndex)
        let endWordIndex: Int = BitSet.wordIndex(toIndex - 1)
        expandTo(endWordIndex)
        let firstWordMask: Int64 = BitSet.WORD_MASK << Int64(fromIndex % 64)
        let lastWordMask: Int64 = BitSet.WORD_MASK >>> Int64(-toIndex)
        //var lastWordMask : Int64  = WORD_MASK >>>Int64( -toIndex);
        if startWordIndex == endWordIndex {
            // Case 1: One word
            words[startWordIndex] |= (firstWordMask & lastWordMask)
        } else {
            // Case 2: Multiple words
            // Handle first word
            words[startWordIndex] |= firstWordMask
            // Handle intermediate words, if any
            let start = startWordIndex + 1
            for i in start..<endWordIndex {
                words[i] = BitSet.WORD_MASK
            }
            // Handle last word (restores invariants)
            words[endWordIndex] |= lastWordMask
        }
        checkInvariants()
    }
    ///
    /// Sets the bits from the specified `fromIndex` (inclusive) to the
    /// specified `toIndex` (exclusive) to the specified value.
    ///
    /// - parameter  fromIndex: index of the first bit to be set
    /// - parameter  toIndex: index after the last bit to be set
    /// - parameter  value: value to set the selected bits to
    /// - throws: _ANTLRError.IndexOutOfBounds_ if `fromIndex` is negative,
    /// or `toIndex` is negative, or `fromIndex` is
    /// larger than `toIndex`
    ///
    public func set(_ fromIndex: Int, _ toIndex: Int, _ value: Bool) throws {
        if value {
            try set(fromIndex, toIndex)
        } else {
            try clear(fromIndex, toIndex)
        }
    }
    ///
    /// Sets the bit specified by the index to `false`.
    ///
    /// - parameter  bitIndex: the index of the bit to be cleared
    /// - throws: _ANTLRError.IndexOutOfBounds_ if the specified index is negative
    /// -   JDK1.0
    ///
    public func clear(_ bitIndex: Int) throws {
        if bitIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "bitIndex < 0: \(bitIndex)")
        }
        let index: Int = BitSet.wordIndex(bitIndex)
        if index >= wordsInUse {
            return
        }
        let option = Int64(1) << Int64(bitIndex % 64)
        words[index] &= ~option
        recalculateWordsInUse()
        checkInvariants()
    }
    ///
    /// Sets the bits from the specified `fromIndex` (inclusive) to the
    /// specified `toIndex` (exclusive) to `false`.
    ///
    /// - parameter  fromIndex: index of the first bit to be cleared
    /// - parameter  toIndex: index after the last bit to be cleared
    /// - throws: _ANTLRError.IndexOutOfBounds_ if `fromIndex` is negative,
    /// or `toIndex` is negative, or `fromIndex` is
    /// larger than `toIndex`
    ///
    public func clear(_ fromIndex: Int,  _ toIndex: Int) throws {
        var toIndex = toIndex
        try BitSet.checkRange(fromIndex, toIndex)
        if fromIndex == toIndex {
            return
        }
        let startWordIndex: Int = BitSet.wordIndex(fromIndex)
        if startWordIndex >= wordsInUse {
            return
        }
        var endWordIndex: Int = BitSet.wordIndex(toIndex - 1)
        if endWordIndex >= wordsInUse {
            toIndex = length()
            endWordIndex = wordsInUse - 1
        }
        let firstWordMask: Int64 = BitSet.WORD_MASK << Int64(fromIndex % 64)
        // ar lastWordMask : Int64  = WORD_MASK >>> Int64((-toIndex);
        let lastWordMask: Int64 = BitSet.WORD_MASK >>> Int64(-toIndex)
        if startWordIndex == endWordIndex {
            // Case 1: One word
            words[startWordIndex] &= ~(firstWordMask & lastWordMask)
        } else {
            // Case 2: Multiple words
            // Handle first word
            words[startWordIndex] &= ~firstWordMask
            // Handle intermediate words, if any
            let start = startWordIndex + 1
            for i in start..<endWordIndex {
                words[i] = 0
            }
            // Handle last word
            words[endWordIndex] &= ~lastWordMask
        }
        recalculateWordsInUse()
        checkInvariants()
    }
    ///
    /// Sets all of the bits in this BitSet to `false`.
    ///
    public func clear() {
        while wordsInUse > 0 {
            wordsInUse -= 1
            words[wordsInUse] = 0
        }
    }
    ///
    /// Returns the value of the bit with the specified index. The value
    /// is `true` if the bit with the index `bitIndex`
    /// is currently set in this `BitSet`; otherwise, the result
    /// is `false`.
    ///
    /// - parameter  bitIndex:   the bit index
    /// - returns: the value of the bit with the specified index
    /// - throws: _ANTLRError.IndexOutOfBounds_ if the specified index is negative
    ///
    public func get(_ bitIndex: Int) throws -> Bool {
        if bitIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "bitIndex < 0: \(bitIndex)")
        }
        checkInvariants()
        let index: Int = BitSet.wordIndex(bitIndex)
        return (index < wordsInUse)
                && ((words[index] & ((Int64(1) << Int64(bitIndex % 64)))) != 0)
    }
    ///
    /// Returns a new `BitSet` composed of bits from this `BitSet`
    /// from `fromIndex` (inclusive) to `toIndex` (exclusive).
    ///
    /// - parameter  fromIndex: index of the first bit to include
    /// - parameter  toIndex: index after the last bit to include
    /// - returns: a new `BitSet` from a range of this `BitSet`
    /// - throws: _ANTLRError.IndexOutOfBounds_ if `fromIndex` is negative,
    /// or `toIndex` is negative, or `fromIndex` is
    /// larger than `toIndex`
    ///
    public func get(_ fromIndex: Int, _ toIndex: Int) throws -> BitSet {
        var toIndex = toIndex
        try  BitSet.checkRange(fromIndex, toIndex)
        checkInvariants()
        let len: Int = length()
        // If no set bits in range return empty bitset
        if len <= fromIndex || fromIndex == toIndex {
            return try  BitSet(0)
        }
        // An optimization
        if toIndex > len {
            toIndex = len
        }
        let result: BitSet = try BitSet(toIndex - fromIndex)
        let targetWords: Int = BitSet.wordIndex(toIndex - fromIndex - 1) + 1
        var sourceIndex: Int = BitSet.wordIndex(fromIndex)
        let wordAligned: Bool = (fromIndex & BitSet.BIT_INDEX_MASK) == 0
        // Process all words but the last word
        var i: Int = 0;
        while i < targetWords - 1 {
            let wordOption1: Int64 = (words[sourceIndex] >>> Int64(fromIndex))
            let wordOption2: Int64 = (words[sourceIndex + 1] << Int64(-fromIndex % 64))
            let wordOption = wordOption1 | wordOption2
            result.words[i] = wordAligned ? words[sourceIndex] : wordOption
            i += 1
            sourceIndex += 1
        }
        // Process the last word
        // var lastWordMask : Int64 = WORD_MASK >>> Int64(-toIndex);
        let lastWordMask: Int64 = BitSet.WORD_MASK >>> Int64(-toIndex)
        let toIndexTest = ((toIndex - 1) & BitSet.BIT_INDEX_MASK)
        let fromIndexTest = (fromIndex & BitSet.BIT_INDEX_MASK)
        let wordOption1: Int64 = (words[sourceIndex] >>> Int64(fromIndex))
        let wordOption2: Int64 = (words[sourceIndex + 1] & lastWordMask)
        let wordOption3: Int64 = (64 + Int64(-fromIndex % 64))
        let wordOption = wordOption1 | wordOption2 << wordOption3
        let wordOption4 = (words[sourceIndex] & lastWordMask)
        let wordOption5 = wordOption4 >>> Int64(fromIndex)
        result.words[targetWords - 1] =
                toIndexTest < fromIndexTest
                ? wordOption : wordOption5
        // Set wordsInUse correctly
        result.wordsInUse = targetWords
        result.recalculateWordsInUse()
        result.checkInvariants()
        return result
    }
    ///
    /// Equivalent to nextSetBit(0), but guaranteed not to throw an exception.
    ///
    public func firstSetBit() -> Int {
        return try! nextSetBit(0)
    }
    ///
    /// Returns the index of the first bit that is set to `true`
    /// that occurs on or after the specified starting index. If no such
    /// bit exists then `-1` is returned.
    ///
    /// To iterate over the `true` bits in a `BitSet`,
    /// use the following loop:
    ///
    /// `
    /// for (int i = bs.firstSetBit(); i >= 0; i = bs.nextSetBit(i+1)) {
    /// // operate on index i here
    /// `}
    ///
    /// - parameter  fromIndex: the index to start checking from (inclusive)
    /// - returns: the index of the next set bit, or `-1` if there
    /// is no such bit
    /// - throws: _ANTLRError.IndexOutOfBounds_ if the specified index is negative
    ///
    public func nextSetBit(_ fromIndex: Int) throws -> Int {
        if fromIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "fromIndex < 0: \(fromIndex)")
        }
        checkInvariants()
        var u: Int = BitSet.wordIndex(fromIndex)
        if u >= wordsInUse {
            return -1
        }
        var word: Int64 = words[u] & (BitSet.WORD_MASK << Int64(fromIndex % 64))
        while true {
            if word != 0 {
                let bit = (u * BitSet.BITS_PER_WORD) + word.trailingZeroBitCount
                return bit
            }
            u += 1
            if u == wordsInUse {
                return -1
            }
            word = words[u]
        }
    }
    ///
    /// Returns the index of the first bit that is set to `false`
    /// that occurs on or after the specified starting index.
    ///
    /// - parameter  fromIndex: the index to start checking from (inclusive)
    /// - returns: the index of the next clear bit
    /// - throws: _ANTLRError.IndexOutOfBounds if the specified index is negative
    ///
    public func nextClearBit(_ fromIndex: Int) throws -> Int {
        // Neither spec nor implementation handle bitsets of maximal length.
        // See 4816253.
        if fromIndex < 0 {
            throw ANTLRError.indexOutOfBounds(msg: "fromIndex < 0: \(fromIndex)")
        }
        checkInvariants()
        var u: Int = BitSet.wordIndex(fromIndex)
        if u >= wordsInUse {
            return fromIndex
        }
        var word: Int64 = ~words[u] & (BitSet.WORD_MASK << Int64(fromIndex % 64))
        while true {
            if word != 0 {
                return (u * BitSet.BITS_PER_WORD) + word.trailingZeroBitCount
            }
            u += 1
            if u == wordsInUse {
                return wordsInUse * BitSet.BITS_PER_WORD
            }
            word = ~words[u]
        }
    }
    ///
    /// Returns the index of the nearest bit that is set to `true`
    /// that occurs on or before the specified starting index.
    /// If no such bit exists, or if `-1` is given as the
    /// starting index, then `-1` is returned.
    ///
    /// To iterate over the `true` bits in a `BitSet`,
    /// use the following loop:
    ///
    /// `
    /// for (int i = bs.length(); (i = bs.previousSetBit(i-1)) >= 0; ) {
    /// // operate on index i here
    /// `}
    ///
    /// - parameter  fromIndex: the index to start checking from (inclusive)
    /// - returns: the index of the previous set bit, or `-1` if there
    /// is no such bit
    /// - throws: _ANTLRError.IndexOutOfBounds if the specified index is less
    /// than `-1`
    /// - note: 1.7
    ///
    public func previousSetBit(_ fromIndex: Int) throws -> Int {
        if fromIndex < 0 {
            if fromIndex == -1 {
                return -1
            }
            throw ANTLRError.indexOutOfBounds(msg: "fromIndex < -1: \(fromIndex)")
        }
        checkInvariants()
        var u: Int = BitSet.wordIndex(fromIndex)
        if u >= wordsInUse {
            return length() - 1
        }
        var word: Int64 = words[u] & (BitSet.WORD_MASK >>> Int64(-(fromIndex + 1)))
        while true {
            if word != 0 {
                return (u + 1) * BitSet.BITS_PER_WORD - 1 - word.leadingZeroBitCount
            }
            if u == 0 {
                return -1
            }
            u -= 1
            word = words[u]
        }
    }
    ///
    /// Returns the index of the nearest bit that is set to `false`
    /// that occurs on or before the specified starting index.
    /// If no such bit exists, or if `-1` is given as the
    /// starting index, then `-1` is returned.
    ///
    /// - parameter  fromIndex: the index to start checking from (inclusive)
    /// - returns: the index of the previous clear bit, or `-1` if there
    /// is no such bit
    /// - throws: _ANTLRError.IndexOutOfBounds if the specified index is less
    /// than `-1`
    /// - note: 1.7
    ///
    public func previousClearBit(_ fromIndex: Int) throws -> Int {
        if fromIndex < 0 {
            if fromIndex == -1 {
                return -1
            }
            throw ANTLRError.indexOutOfBounds(msg: "fromIndex < -1: \(fromIndex)")
        }
        checkInvariants()
        var u: Int = BitSet.wordIndex(fromIndex)
        if u >= wordsInUse {
            return fromIndex
        }
        var word: Int64 = ~words[u] & (BitSet.WORD_MASK >>> Int64(-(fromIndex + 1)))
        // var word : Int64 = ~words[u] & (WORD_MASK >>> -(fromIndex+1));
        while true {
            if word != 0 {
                return (u + 1) * BitSet.BITS_PER_WORD - 1 - word.leadingZeroBitCount
            }
            if u == 0 {
                return -1
            }
            u -= 1
            word = ~words[u]
        }
    }
    ///
    /// Returns the "logical size" of this `BitSet`: the index of
    /// the highest set bit in the `BitSet` plus one. Returns zero
    /// if the `BitSet` contains no set bits.
    ///
    /// - returns: the logical size of this `BitSet`
    ///
    public func length() -> Int {
        if wordsInUse == 0 {
            return 0
        }
        return BitSet.BITS_PER_WORD * (wordsInUse - 1) +
                (BitSet.BITS_PER_WORD - words[wordsInUse - 1].leadingZeroBitCount)
    }
    ///
    /// Returns true if this `BitSet` contains no bits that are set
    /// to `true`.
    ///
    /// - returns: boolean indicating whether this `BitSet` is empty
    ///
    public func isEmpty() -> Bool {
        return wordsInUse == 0
    }
    ///
    /// Returns true if the specified `BitSet` has any bits set to
    /// `true` that are also set to `true` in this `BitSet`.
    ///
    /// - parameter  set: `BitSet` to intersect with
    /// - returns: boolean indicating whether this `BitSet` intersects
    /// the specified `BitSet`
    ///
    public func intersects(_ set: BitSet) -> Bool {
        var i: Int = min(wordsInUse, set.wordsInUse) - 1
        while i >= 0 {
            if (words[i] & set.words[i]) != 0 {
                return true
            }
            i -= 1
        }
        return false
    }
    ///
    /// Returns the number of bits set to `true` in this `BitSet`.
    ///
    /// - returns: the number of bits set to `true` in this `BitSet`
    ///
    public func cardinality() -> Int {
        var sum: Int = 0
        for i in 0..<wordsInUse {
            sum += words[i].nonzeroBitCount
        }
        return sum
    }
    ///
    /// 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
    /// has the value `true` if and only if it both initially
    /// had the value `true` and the corresponding bit in the
    /// bit set argument also had the value `true`.
    ///
    /// - parameter set: a bit set
    ///
    public func and(_ set: BitSet) {
        if self == set {
            return
        }
        while wordsInUse > set.wordsInUse {
            wordsInUse -= 1
            words[wordsInUse] = 0
        }
        // Perform logical AND on words in common
        for i in 0..<wordsInUse {
            words[i] &= set.words[i]
        }
        recalculateWordsInUse()
        checkInvariants()
    }
    ///
    /// Performs a logical __OR__ of this bit set with the bit set
    /// argument. This bit set is modified so that a bit in it has the
    /// value `true` if and only if it either already had the
    /// value `true` or the corresponding bit in the bit set
    /// argument has the value `true`.
    ///
    /// - parameter set: a bit set
    ///
    public func or(_ set: BitSet) {
        if self == set {
            return
        }
        let wordsInCommon: Int = min(wordsInUse, set.wordsInUse)
        if wordsInUse < set.wordsInUse {
            ensureCapacity(set.wordsInUse)
            wordsInUse = set.wordsInUse
        }
        // Perform logical OR on words in common
        for i in 0..<wordsInCommon {
            words[i] |= set.words[i]
        }
        // Copy any remaining words
        if wordsInCommon < set.wordsInUse {
            words[wordsInCommon ..< wordsInUse] = set.words[wordsInCommon ..< wordsInUse]
        }
        // recalculateWordsInUse() is unnecessary
        checkInvariants()
    }
    ///
    /// Performs a logical __XOR__ of this bit set with the bit set
    /// argument. This bit set is modified so that a bit in it has the
    /// value `true` if and only if one of the following
    /// statements holds:
    ///
    /// * The bit initially has the value `true`, and the
    /// corresponding bit in the argument has the value `false`.
    /// * The bit initially has the value `false`, and the
    /// corresponding bit in the argument has the value `true`.
    ///
    /// - parameter  set: a bit set
    ///
    public func xor(_ set: BitSet) {
        let wordsInCommon: Int = min(wordsInUse, set.wordsInUse)
        if wordsInUse < set.wordsInUse {
            ensureCapacity(set.wordsInUse)
            wordsInUse = set.wordsInUse
        }
        // Perform logical XOR on words in common
        for i in 0..<wordsInCommon {
            words[i] ^= set.words[i]
        }
        // Copy any remaining words
        if wordsInCommon < set.wordsInUse {
            words[wordsInCommon ..< wordsInUse] = set.words[wordsInCommon ..< wordsInUse]
        }
        recalculateWordsInUse()
        checkInvariants()
    }
    ///
    /// Clears all of the bits in this `BitSet` whose corresponding
    /// bit is set in the specified `BitSet`.
    ///
    /// - parameter  set: the `BitSet` with which to mask this
    /// `BitSet`
    ///
    public func andNot(_ set: BitSet) {
        // Perform logical (a & !b) on words in common
        var i: Int = min(wordsInUse, set.wordsInUse) - 1
        while i >= 0 {
            words[i] &= ~set.words[i]
            i -= 1
        }
        recalculateWordsInUse()
        checkInvariants()
    }
    ///
    /// Returns the hash code value for this bit set. The hash code depends
    /// only on which bits are set within this `BitSet`.
    ///
    /// The hash code is defined to be the result of the following
    /// calculation:
    /// `
    /// public int hashCode() {
    /// long h = 1234;
    /// long[] words = toLongArray();
    /// for (int i = words.length; --i >= 0; )
    /// h ^= words[i] * (i + 1);
    /// return (int)((h >> 32) ^ h);
    /// `}
    /// Note that the hash code changes if the set of bits is altered.
    ///
    /// - returns: the hash code value for this bit set
    ///
    private var hashCode: Int {
        var h: Int64 = 1234
        var i: Int = wordsInUse
        i -= 1
        while i >= 0 {
             h ^= words[i] * Int64(i + 1)
             i -= 1
        }
        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
    /// `BitSet` to represent bit values.
    /// The maximum element in the set is the size - 1st element.
    ///
    /// - returns: the number of bits currently in this bit set
    ///
    public func size() -> Int {
        return words.count * BitSet.BITS_PER_WORD
    }
    ///
    /// Attempts to reduce internal storage used for the bits in this bit set.
    /// Calling this method may, but is not required to, affect the value
    /// returned by a subsequent call to the _#size()_ method.
    ///
    private func trimToSize() {
        if wordsInUse != words.count {
            words = copyOf(words, wordsInUse)
            checkInvariants()
        }
    }
    ///
    /// Returns a string representation of this bit set. For every index
    /// for which this `BitSet` contains a bit in the set
    /// state, the decimal representation of that index is included in
    /// the result. Such indices are listed in order from lowest to
    /// highest, separated by ", " (a comma and a space) and
    /// surrounded by braces, resulting in the usual mathematical
    /// notation for a set of integers.
    ///
    /// Example:
    ///
    /// `BitSet drPepper = new BitSet();`
    /// Now `drPepper.description` returns `"{}"`.
    ///
    /// `drPepper.set(2);`
    /// Now `drPepper.description` returns `"{2}"`.
    ///
    /// `drPepper.set(4);`
    /// `drPepper.set(10);`
    /// Now `drPepper.description` returns `"{2, 4, 10}"`.
    ///
    /// - returns: a string representation of this bit set
    ///
    public var description: String {
        checkInvariants()
        //let numBits: Int = (wordsInUse > 128) ?
        // cardinality() : wordsInUse * BitSet.BITS_PER_WORD
        var b = "{"
        var i = firstSetBit()
        if i != -1 {
            b += String(i)
            i = try! nextSetBit(i + 1)
            while i >= 0 {
                let endOfRun = try! nextClearBit(i)
                repeat {
                    b += ", \(i)"
                    i += 1
                } while i < endOfRun
                i = try! nextSetBit(i + 1)
            }
        }
        b += "}"
        return b
    }
}
public func ==(lhs: BitSet, rhs: BitSet) -> Bool {
    if lhs === rhs {
        return true
    }
    lhs.checkInvariants()
    rhs.checkInvariants()
    if lhs.wordsInUse != rhs.wordsInUse {
        return false
    }
    // Check words in use by both BitSets
    let length = lhs.wordsInUse
    for i in 0..<length {
        if lhs.words[i] != rhs.words[i] {
            return false
        }
    }
    return true
}


MurmurHash

 */
///
/// https://en.wikipedia.org/wiki/MurmurHash
///
/// - Author: Sam Harwell
///
public final class MurmurHash {
    private static let DEFAULT_SEED: UInt32 = 0
    private static let c1 = UInt32(0xCC9E2D51)
    private static let c2 = UInt32(0x1B873593)
    private static let r1 = UInt32(15)
    private static let r2 = UInt32(13)
    private static let m = UInt32(5)
    private static let n = UInt32(0xE6546B64)
    ///
    /// Initialize the hash using the default seed value.
    ///
    /// - Returns: the intermediate hash value
    ///
    public static func initialize() -> UInt32 {
        return initialize(DEFAULT_SEED)
    }
    ///
    /// Initialize the hash using the specified `seed`.
    ///
    /// - Parameter seed: the seed
    /// - Returns: the intermediate hash value
    ///
    public static func initialize(_ seed: UInt32) -> UInt32 {
        return seed
    }
    private static func calcK(_ value: UInt32) -> UInt32 {
        var k = value
        k = k &* c1
        k = (k << r1) | (k >> (32 - r1))
        k = k &* c2
        return k
     }
    ///
    /// Update the intermediate hash value for the next input `value`.
    ///
    /// - Parameter hash: the intermediate hash value
    /// - Parameter value: the value to add to the current hash
    /// - Returns: the updated intermediate hash value
    ///
    public static func update2(_ hashIn: UInt32, _ value: Int) -> UInt32 {
        return updateInternal(hashIn, UInt32(truncatingIfNeeded: value))
    }
    private static func updateInternal(_ hashIn: UInt32, _ value: UInt32) -> UInt32 {
        let k = calcK(value)
        var hash = hashIn
        hash = hash ^ k
        hash = (hash << r2) | (hash >> (32 - r2))
        hash = hash &* m &+ n
        // print("murmur update2 : \(hash)")
        return hash
    }
    ///
    /// Update the intermediate hash value for the next input `value`.
    ///
    /// - Parameter hash: the intermediate hash value
    /// - Parameter value: the value to add to the current hash
    /// - Returns: the updated intermediate hash value
    ///
    public static func update<T:Hashable>(_ hash: UInt32, _ value: T?) -> UInt32 {
        return update2(hash, value?.hashValue ?? 0)
    }
    ///
    /// Apply the final computation steps to the intermediate value `hash`
    /// to form the final result of the MurmurHash 3 hash function.
    ///
    /// - Parameter hash: the intermediate hash value
    /// - Parameter numberOfWords: the number of UInt32 values added to the hash
    /// - Returns: the final hash result
    ///
    public static func finish(_ hashin: UInt32, _ numberOfWords: Int) -> Int {
        return Int(finish(hashin, byteCount: (numberOfWords &* 4)))
    }
    private static func finish(_ hashin: UInt32, byteCount byteCountInt: Int) -> UInt32 {
        let byteCount = UInt32(truncatingIfNeeded: byteCountInt)
        var hash = hashin
        hash ^= byteCount
        hash ^= (hash >> 16)
        hash = hash &* 0x85EBCA6B
        hash ^= (hash >> 13)
        hash = hash &* 0xC2B2AE35
        hash ^= (hash >> 16)
        //print("murmur finish : \(hash)")
        return hash
    }
    ///
    /// Utility function to compute the hash code of an array using the
    /// MurmurHash algorithm.
    ///
    /// - Parameter <T>: the array element type
    /// - Parameter data: the array data
    /// - Parameter seed: the seed for the MurmurHash algorithm
    /// - Returns: the hash code of the data
    ///
    public static func hashCode<T:Hashable>(_ data: [T], _ seed: Int) -> Int {
        var hash = initialize(UInt32(truncatingIfNeeded: seed))
        for value in data {
            hash = update(hash, value)
        }
        return finish(hash, data.count)
    }
    ///
    /// Compute a hash for the given String and seed.  The String is encoded
    /// using UTF-8, then the bytes are interpreted as unsigned 32-bit
    /// little-endian values, giving UInt32 values for the update call.
    ///
    /// If the bytes do not evenly divide by 4, the final bytes are treated
    /// slightly differently (not doing the final rotate / multiply / add).
    ///
    /// This matches the treatment of byte sequences in publicly available
    /// test patterns (see MurmurHashTests.swift) and the example code on
    /// Wikipedia.
    ///
    public static func hashString(_ s: String, _ seed: UInt32) -> UInt32 {
        let bytes = Array(s.utf8)
        return hashBytesLittleEndian(bytes, seed)
    }
    private static func hashBytesLittleEndian(_ bytes: [UInt8], _ seed: UInt32) -> UInt32 {
        let byteCount = bytes.count
        var hash = seed
        for i in stride(from: 0, to: byteCount - 3, by: 4) {
            var word = UInt32(bytes[i])
            word |= UInt32(bytes[i + 1]) << 8
            word |= UInt32(bytes[i + 2]) << 16
            word |= UInt32(bytes[i + 3]) << 24
            hash = updateInternal(hash, word)
        }
        let remaining = byteCount & 3
        if remaining != 0 {
            var lastWord = UInt32(0)
            for r in 0 ..< remaining {
                lastWord |= UInt32(bytes[byteCount - 1 - r]) << (8 * (remaining - 1 - r))
            }
            let k = calcK(lastWord)
            hash ^= k
        }
        return finish(hash, byteCount: byteCount)
    }
    private init() {
    }
}


Stack

 */
//
//  Stack.swift
//  antlr.swift
//
//  Created by janyou on 15/9/8.
//
import Foundation
public struct Stack<T> {
    var items = [T]()
    public mutating func push(_ item: T) {
        items.append(item)
    }
    public mutating func pop() -> T {
        return items.removeLast()
    }
    public mutating func clear() {
        return items.removeAll()
    }
    public func peek() -> T? {
        return items.last
    }
    public var isEmpty: Bool {
        return items.isEmpty
    }
}


CommonUtil

 */
//
//  CommonUtil.swift
//   antlr.swift
//
//  Created by janyou on 15/9/4.
//
import Foundation
func errPrint(_ msg: String) {
    fputs(msg + "\n", stderr)
}
public func +(lhs: String, rhs: Int) -> String {
    return lhs + String(rhs)
}
public func +(lhs: Int, rhs: String) -> String {
    return String(lhs) + rhs
}
public func +(lhs: String, rhs: Token) -> String {
    return lhs + rhs.description
}
public func +(lhs: Token, rhs: String) -> String {
    return lhs.description + rhs
}
infix operator >>> : BitwiseShiftPrecedence
func >>>(lhs: Int32, rhs: Int32) -> Int32 {
    return lhs &>> rhs
}
func >>>(lhs: Int64, rhs: Int64) -> Int64 {
    return lhs &>> rhs
}
func >>>(lhs: Int, rhs: Int) -> Int {
    return lhs &>> rhs
}
func intChar2String(_ i: Int) -> String {
    return String(Character(integerLiteral: i))
}
func log(_ message: String = "", file: String = #file, function: String = #function, lineNum: Int = #line) {
    // #if DEBUG
    print("FILE: \(URL(fileURLWithPath: file).pathComponents.last!),FUNC: \(function), LINE: \(lineNum) MESSAGE: \(message)")
    //   #else
    // do nothing
    //   #endif
}
func toInt(_ c: Character) -> Int {
    return c.unicodeValue
}
func toInt32(_ data: [Character], _ offset: Int) -> Int {
    return data[offset].unicodeValue | (data[offset + 1].unicodeValue << 16)
}
func toLong(_ data: [Character], _ offset: Int) -> Int64 {
    let mask: Int64 = 0x00000000FFFFFFFF
    let lowOrder: Int64 = Int64(toInt32(data, offset)) & mask
    return lowOrder | Int64(toInt32(data, offset + 2) << 32)
}
func toUUID(_ data: [Character], _ offset: Int) -> UUID {
    let leastSigBits: Int64 = toLong(data, offset)
    let mostSigBits: Int64 = toLong(data, offset + 4)
    return UUID(mostSigBits: mostSigBits, leastSigBits: leastSigBits)
}


Mutex

import Foundation
///
/// Using class so it can be shared even if
/// it appears to be a field in a class.
///
class Mutex {
    ///
    /// The mutex instance.
    ///
    private let semaphore = DispatchSemaphore(value: 1)
    ///
    /// Running the supplied closure synchronously.
    ///
    /// - Parameter closure: the closure to run
    /// - Returns: the value returned by the closure
    /// - Throws: the exception populated by the closure run
    ///
    func synchronized<R>(closure: () throws -> R) rethrows -> R {
        semaphore.wait()
        defer {
            semaphore.signal()
        }
        return try closure()
    }
}


StringExtension

///
///
import Foundation
extension String {
    func lastIndex(of target: String) -> String.Index? {
        if target.isEmpty {
            return nil
        }
        var result: String.Index? = nil
        var substring = self[...]
        while true {
            guard let targetRange = substring.range(of: target) else {
                return result
            }
            result = targetRange.lowerBound
            let nextChar = substring.index(after: targetRange.lowerBound)
            substring = self[nextChar...]
        }
    }
    subscript(integerRange: Range<Int>) -> String {
        let start = index(startIndex, offsetBy: integerRange.lowerBound)
        let end = index(startIndex, offsetBy: integerRange.upperBound)
        let range = start ..< end
        return String(self[range])
    }
}
// Implement Substring.hasPrefix, which is not currently in the Linux stdlib.
// https://bugs.swift.org/browse/SR-5627
#if os(Linux)
extension Substring {
    func hasPrefix(_ prefix: String) -> Bool {
        return String(self).hasPrefix(prefix)
    }
}
#endif


UUIDExtension

///
///
import Foundation
extension UUID {
    public init(mostSigBits: Int64, leastSigBits: Int64) {
        let bytes = UnsafeMutablePointer<UInt8>.allocate(capacity: 16)
        defer {
            bytes.deallocate()
        }
        bytes.withMemoryRebound(to: Int64.self, capacity: 2) {
            $0.pointee = leastSigBits
            $0.advanced(by: 1).pointee = mostSigBits
        }
        let u = NSUUID(uuidBytes: bytes)
        self.init(uuidString: u.uuidString)!
    }
}


IntStreamExtension

///
///
//
//  IntStreamExtension.swift
//  Antlr.swift
//
//  Created by janyou on 15/9/3.
//
import Foundation
extension IntStream {
    ///
    /// The value returned by _#LA LA()_ when the end of the stream is
    /// reached.
    ///
    public static var EOF: Int {
        return -1
    }
    ///
    /// The value returned by _#getSourceName_ when the actual name of the
    /// underlying source is not known.
    ///
    public static var UNKNOWN_SOURCE_NAME: String {
        return "<unknown>"
    }
}


TokenExtension

///
///
//
//  TokenExtension.swift
//  Antlr.swift
//
//  Created by janyou on 15/9/4.
//
import Foundation
extension Token {
    static public var INVALID_TYPE: Int {
        return 0
    }
    ///
    /// During lookahead operations, this "token" signifies we hit rule end ATN state
    /// and did not follow it despite needing to.
    ///
    static public var EPSILON: Int {
        return -2
    }
    static public var MIN_USER_TOKEN_TYPE: Int {
        return 1
    }
    static public var EOF: Int {
        return -1
    }

    ///
    /// All tokens go to the parser (unless skip() is called in that rule)
    /// on a particular "channel".  The parser tunes to a particular channel
    /// so that whitespace etc... can go to the parser on a "hidden" channel.
    ///
    static public var DEFAULT_CHANNEL: Int {
        return 0
    }

    ///
    /// Anything on different channel than DEFAULT_CHANNEL is not parsed
    /// by parser.
    ///
    static public var HIDDEN_CHANNEL: Int {
        return 1
    }

    ///
    /// This is the minimum constant value which can be assigned to a
    /// user-defined token channel.
    ///
    ///
    /// The non-negative numbers less than _#MIN_USER_CHANNEL_VALUE_ are
    /// assigned to the predefined channels _#DEFAULT_CHANNEL_ and
    /// _#HIDDEN_CHANNEL_.
    ///
    /// - seealso: org.antlr.v4.runtime.Token#getChannel()
    ///
    static public var MIN_USER_CHANNEL_VALUE: Int {
        return 2
    }
}


ArrayExtension

///
    mutating func concat(_ addArray: [Element]) -> [Element] {
        return self + addArray
    }
    mutating func removeObject<T:Equatable>(_ object: T) {
        var index: Int?
        for (idx, objectToCompare) in self.enumerated() {
            if let to = objectToCompare as? T {
                if object == to {
                    index = idx
                }
            }
        }
        if index != nil {
            self.remove(at: index!)
        }
    }
    ///
    /// Removes the last element from self and returns it.
    ///
    /// :returns: The removed element
    ///
    mutating func pop() -> Element {
        return removeLast()
    }
    ///
    /// Same as append.
    ///
    /// :param: newElement Element to append
    ///
    mutating func push(_ newElement: Element) {
        return append(newElement)
    }
    func all(_ test: (Element) -> Bool) -> Bool {
        for item in self {
            if !test(item) {
                return false
            }
        }
        return true
    }
    ///
    /// Checks if test returns true for all the elements in self
    ///
    /// :param: test Function to call for each element
    /// :returns: True if test returns true for all the elements in self
    ///
    func every(_ test: (Element) -> Bool) -> Bool {
        for item in self {
            if !test(item) {
                return false
            }
        }
        return true
    }
    ///
    /// Checks if test returns true for any element of self.
    ///
    /// :param: test Function to call for each element
    /// :returns: true if test returns true for any element of self
    ///
    func any(_ test: (Element) -> Bool) -> Bool {
        for item in self {
            if test(item) {
                return true
            }
        }
        return false
    }
    ///
    /// slice array
    /// :param: index slice index
    /// :param: isClose is close array
    /// :param: first First array
    /// :param: second Second array
    ///
    //func slice(startIndex startIndex:Int, endIndex:Int) -> Slice<Element> {
    func slice(startIndex: Int, endIndex: Int) -> ArraySlice<Element> {
        return self[startIndex ... endIndex]
    }
    // func slice(index:Int,isClose:Bool = false) ->(first:Slice<Element> ,second:Slice<Element>){
    func slice(_ index: Int, isClose: Bool = false) -> (first:ArraySlice<Element>, second:ArraySlice<Element>) {
        var first = self[0 ... index]
        var second = self[index ..< count]
        if isClose {
            first = second + first
            second = []
        }
        return (first, second)
    }
}


CharacterExtension

///
///
//
//  CharacterEextension.swift
//  Antlr.swift
//
//  Created by janyou on 15/9/4.
//
import Foundation
extension Character {
    //"1" -> 1 "2"  -> 2
    var integerValue: Int {
        return Int(String(self)) ?? 0
    }
    public init(integerLiteral value: IntegerLiteralType) {
        self = Character(UnicodeScalar(value)!)
    }
    var utf8Value: UInt8 {
        for s in String(self).utf8 {
            return s
        }
        return 0
    }
    var utf16Value: UInt16 {
        for s in String(self).utf16 {
            return s
        }
        return 0
    }
    //char ->  int
    var unicodeValue: Int {
        return Int(String(self).unicodeScalars.first?.value ?? 0)
    }
    public static var MAX_VALUE: Int {
        let c: Character = "\u{10FFFF}"
        return c.unicodeValue
    }
    public static var MIN_VALUE: Int {
        let c: Character = "\u{0000}"
        return c.unicodeValue
    }
    public static func isJavaIdentifierStart(_ char: Int) -> Bool {
        let ch = Character(integerLiteral: char)
        return ch == "_" || ch == "$" || ("a" <= ch && ch <= "z")
                || ("A" <= ch && ch <= "Z")
    }
    public static func isJavaIdentifierPart(_ char: Int) -> Bool {
        let ch = Character(integerLiteral: char)
        return isJavaIdentifierStart(char) || ("0" <= ch && ch <= "9")
    }
    public static func toCodePoint(_ high: Int, _ low: Int) -> Int {
        let MIN_SUPPLEMENTARY_CODE_POINT = 65536 // 0x010000
        let MIN_HIGH_SURROGATE = 0xd800 //"\u{dbff}"  //"\u{DBFF}"  //"\u{DBFF}"
        let MIN_LOW_SURROGATE = 0xdc00 //"\u{dc00}" //"\u{DC00}"
        return ((high << 10) + low) + (MIN_SUPPLEMENTARY_CODE_POINT
                - (MIN_HIGH_SURROGATE << 10)
                - MIN_LOW_SURROGATE)
    }
}


ANTLRError

///
///
//
//  ANTLRError.swift
//  antlr.swift
//
//  Created by janyou on 15/9/4.
//
import Foundation
public enum ANTLRError: Error {
    case unsupportedOperation(msg:String)
    case indexOutOfBounds(msg:String)
    case illegalState(msg:String)
    case illegalArgument(msg:String)
    case negativeArraySize(msg:String)
}


ANTLRException

///
///
//
//  ANTLRException.swift
//  antlr.swift
//
//  Created by janyou on 15/9/8.
//
import Foundation
public enum ANTLRException: Error {
    case parseCancellation(e: RecognitionException)
    case recognition(e: RecognitionException)
}


LinuxMain

#if os(Linux)
import XCTest
XCTMain([
    // Antlr4Tests
    testCase(TokenStreamTests.allTests),
    testCase(TokenStreamRewriterTests.allTests),
    testCase(VisitorTests.allTests)
])
#endif


TokenStreamRewriterTests

import XCTest
import Antlr4
class TokenStreamRewriterTests: XCTestCase {

    static let allTests = [
        ("testPreservesOrderOfContiguousInserts", testPreservesOrderOfContiguousInserts),
        ("testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder2", testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder2),
        ("testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder", testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder),
        ("testInsertBeforeTokenThenDeleteThatToken", testInsertBeforeTokenThenDeleteThatToken),
        ("testLeaveAloneDisjointInsert2", testLeaveAloneDisjointInsert2),
        ("testLeaveAloneDisjointInsert", testLeaveAloneDisjointInsert),
        ("testDropPrevCoveredInsert", testDropPrevCoveredInsert),
        ("testDropIdenticalReplace", testDropIdenticalReplace),
        ("testOverlappingReplace4", testOverlappingReplace4),
        ("testOverlappingReplace3", testOverlappingReplace3),
        ("testOverlappingReplace2", testOverlappingReplace2),
        ("testOverlappingReplace", testOverlappingReplace),
        ("testDisjointInserts", testDisjointInserts),
        ("testCombineInsertOnLeftWithDelete", testCombineInsertOnLeftWithDelete),
        ("testCombineInsertOnLeftWithReplace", testCombineInsertOnLeftWithReplace),
        ("testCombine3Inserts", testCombine3Inserts),
        ("testCombineInserts", testCombineInserts),
        ("testReplaceSingleMiddleThenOverlappingSuperset", testReplaceSingleMiddleThenOverlappingSuperset),
        ("testReplaceThenReplaceLowerIndexedSuperset", testReplaceThenReplaceLowerIndexedSuperset),
        ("testReplaceThenReplaceSuperset", testReplaceThenReplaceSuperset),
        ("testReplaceSubsetThenFetch", testReplaceSubsetThenFetch),
        ("testReplaceAll", testReplaceAll),
        ("testReplaceRangeThenInsertAfterRightEdge", testReplaceRangeThenInsertAfterRightEdge),
        ("testReplaceRangeThenInsertAtRightEdge", testReplaceRangeThenInsertAtRightEdge),
        ("testReplaceThenInsertAtLeftEdge", testReplaceThenInsertAtLeftEdge),
        ("testReplaceThenInsertAfterLastIndex", testReplaceThenInsertAfterLastIndex),
        ("testInsertThenReplaceLastIndex", testInsertThenReplaceLastIndex),
        ("testReplaceThenInsertBeforeLastIndex", testReplaceThenInsertBeforeLastIndex),
        ("test2InsertThenReplaceIndex0", test2InsertThenReplaceIndex0),
        ("test2InsertMiddleIndex", test2InsertMiddleIndex),
        ("testInsertThenReplaceSameIndex", testInsertThenReplaceSameIndex),
        ("testInsertInPriorReplace", testInsertInPriorReplace),
        ("testReplaceThenDeleteMiddleIndex", testReplaceThenDeleteMiddleIndex),
        ("test2ReplaceMiddleIndex1InsertBefore", test2ReplaceMiddleIndex1InsertBefore),
        ("test2ReplaceMiddleIndex", test2ReplaceMiddleIndex),
        ("testToStringStartStop2", testToStringStartStop2),
        ("testToStringStartStop", testToStringStartStop),
        ("testReplaceMiddleIndex", testReplaceMiddleIndex),
        ("testReplaceLastIndex", testReplaceLastIndex),
        ("testReplaceIndex0", testReplaceIndex0),
        ("test2InsertBeforeAfterMiddleIndex", test2InsertBeforeAfterMiddleIndex),
        ("testInsertAfterLastIndex", testInsertAfterLastIndex),
        ("testInsertBeforeIndex0", testInsertBeforeIndex0)
    ]

    func testInsertBeforeIndex0() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "0")
        let result = try tokens.getText()
        let expecting = "0abc"
        XCTAssertEqual(expecting, result)
    }
    func testInsertAfterLastIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertAfter(2, "x")
        let result = try tokens.getText()
        let expecting = "abcx"
        XCTAssertEqual(expecting, result)
    }
    func test2InsertBeforeAfterMiddleIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(1, "x")
        tokens.insertAfter(1, "x")
        let result = try tokens.getText()
        let expecting = "axbxc"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceIndex0() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(0, "x")
        let result = try tokens.getText()
        let expecting = "xbc"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceLastIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, "x")
        let result = try tokens.getText()
        let expecting = "abx"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceMiddleIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(1, "x")
        let result = try tokens.getText()
        let expecting = "axc"
        XCTAssertEqual(expecting, result)
    }
    func testToStringStartStop() throws {
        // Tokens: 0123456789
        // Input:  x = 3 * 0
        let input = ANTLRInputStream("x = 3 * 0;")
        let lexer = LexerB(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        // replace 3 * 0 with 0
        try tokens.replace(4, 8, "0")
        try stream.fill()
        var result = try tokens.getTokenStream().getText()
        var expecting = "x = 3 * 0;"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText()
        expecting = "x = 0;"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText(Interval.of(0, 9))
        expecting = "x = 0;"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText(Interval.of(4, 8))
        expecting = "0"
        XCTAssertEqual(expecting, result)
    }
    func testToStringStartStop2() throws {
        // Tokens: 012345678901234567
        // Input:  x = 3 * 0 + 2 * 0;
        let input = ANTLRInputStream("x = 3 * 0 + 2 * 0;")
        let lexer = LexerB(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        var result = try tokens.getTokenStream().getText()
        var expecting = "x = 3 * 0 + 2 * 0;"
        XCTAssertEqual(expecting, result)
        // replace 3 * 0 with 0
        try tokens.replace(4, 8, "0")
        try stream.fill()
        result = try tokens.getText()
        expecting = "x = 0 + 2 * 0;"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText(Interval.of(0, 17))
        expecting = "x = 0 + 2 * 0;"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText(Interval.of(4, 8))
        expecting = "0"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText(Interval.of(0, 8))
        expecting = "x = 0"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText(Interval.of(12, 16))
        expecting = "2 * 0"
        XCTAssertEqual(expecting, result)
        tokens.insertAfter(17, "// comment")
        result = try tokens.getText(Interval.of(12, 18))
        expecting = "2 * 0;// comment"
        XCTAssertEqual(expecting, result)
        result = try tokens.getText(Interval.of(0, 8))
        try stream.fill()
        // try again after insert at end
        expecting = "x = 0"
        XCTAssertEqual(expecting, result)
    }
    func test2ReplaceMiddleIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(1, "x")
        try tokens.replace(1, "y")
        let result = try tokens.getText()
        let expecting = "ayc"
        XCTAssertEqual(expecting, result)
    }
    func test2ReplaceMiddleIndex1InsertBefore() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "_")
        try tokens.replace(1, "x")
        try tokens.replace(1, "y")
        let result = try tokens.getText()
        let expecting = "_ayc"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceThenDeleteMiddleIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(1, "x")
        try tokens.delete(1)
        let result = try tokens.getText()
        let expecting = "ac"
        XCTAssertEqual(expecting, result)
    }
    func testInsertInPriorReplace() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(0, 2, "x")
        tokens.insertBefore(1, "0")
        do {
            _ = try tokens.getText()
            XCTFail("Expected exception not thrown.")
        } catch ANTLRError.illegalArgument(let msg) {
            XCTAssertEqual(expecting, msg)
        }
    }
    func testInsertThenReplaceSameIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "0")
        try tokens.replace(0, "x")
        let result = try tokens.getText()
        let expecting = "0xbc"
        XCTAssertEqual(expecting, result)
    }
    func test2InsertMiddleIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(1, "x")
        tokens.insertBefore(1, "y")
        let result = try tokens.getText()
        let expecting = "ayxbc"
        XCTAssertEqual(expecting, result)
    }
    func test2InsertThenReplaceIndex0() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "x")
        tokens.insertBefore(0, "y")
        try tokens.replace(0, "z")
        let result = try tokens.getText()
        let expecting = "yxzbc"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceThenInsertBeforeLastIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, "x")
        tokens.insertBefore(2, "y")
        let result = try tokens.getText()
        let expecting = "abyx"
        XCTAssertEqual(expecting, result)
    }
    func testInsertThenReplaceLastIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(2, "y")
        try tokens.replace(2, "x")
        let result = try tokens.getText()
        let expecting = "abyx"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceThenInsertAfterLastIndex() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, "x")
        tokens.insertAfter(2, "y")
        let result = try tokens.getText()
        let expecting = "abxy"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceThenInsertAtLeftEdge() throws {
        let input = ANTLRInputStream("abcccba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 4, "x")
        tokens.insertBefore(2, "y")
        let result = try tokens.getText()
        let expecting = "abyxba"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceRangeThenInsertAtRightEdge() throws {
        let input = ANTLRInputStream("abcccba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 4, "x")
        tokens.insertBefore(4, "y")
        do {
            _ = try tokens.getText()
            XCTFail("Expected exception not thrown.")
        } catch ANTLRError.illegalArgument(let msg) {
            XCTAssertEqual(expecting, msg)
        }
    }
    func testReplaceRangeThenInsertAfterRightEdge() throws {
        let input = ANTLRInputStream("abcccba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 4, "x")
        tokens.insertAfter(4, "y")
        let result = try tokens.getText()
        let expecting = "abxyba"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceAll() throws {
        let input = ANTLRInputStream("abcccba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(0, 6, "x")
        let result = try tokens.getText()
        let expecting = "x"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceSubsetThenFetch() throws {
        let input = ANTLRInputStream("abcccba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 4, "xyz")
        let result = try tokens.getText(Interval.of(0, 6))
        let expecting = "abxyzba"
        XCTAssertEqual(expecting, result)
    }
    func testReplaceThenReplaceSuperset() throws {
        let input = ANTLRInputStream("abcccba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 4, "xyz")
        try tokens.replace(3, 5, "foo")
        do {
            _ = try tokens.getText()
            XCTFail("Expected exception not thrown.")
        } catch ANTLRError.illegalArgument(let msg) {
            XCTAssertEqual(expecting, msg)
        }
    }
    func testReplaceThenReplaceLowerIndexedSuperset() throws {
        let input = ANTLRInputStream("abcccba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 4, "xyz")
        try tokens.replace(1, 3, "foo")
        do {
            _ = try tokens.getText()
            XCTFail("Expected exception not thrown.")
        } catch ANTLRError.illegalArgument(let msg) {
            XCTAssertEqual(expecting, msg)
        }
    }
    func testReplaceSingleMiddleThenOverlappingSuperset() throws {
        let input = ANTLRInputStream("abcba")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 2, "xyz")
        try tokens.replace(0, 3, "foo")
        let result = try tokens.getText()
        let expecting = "fooa"
        XCTAssertEqual(expecting, result)
    }
    func testCombineInserts() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "x")
        tokens.insertBefore(0, "y")
        let result = try tokens.getText()
        let expecting = "yxabc"
        XCTAssertEqual(expecting, result)
    }
    func testCombine3Inserts() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(1, "x")
        tokens.insertBefore(0, "y")
        tokens.insertBefore(1, "z")
        let result = try tokens.getText()
        let expecting = "yazxbc"
        XCTAssertEqual(expecting, result)
    }
    func testCombineInsertOnLeftWithReplace() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        // combine with left edge of rewrite
        try tokens.replace(0, 2, "foo")
        tokens.insertBefore(0, "z")
        try stream.fill()
        let result = try tokens.getText()
        let expecting = "zfoo"
        XCTAssertEqual(expecting, result)
    }
    func testCombineInsertOnLeftWithDelete() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        // combine with left edge of rewrite
        try tokens.delete(0, 2)
        tokens.insertBefore(0, "z")
        try stream.fill()
        let result = try tokens.getText()
        let expecting = "z"
        // make sure combo is not znull
        try stream.fill()
        XCTAssertEqual(expecting, result)
    }
    func testDisjointInserts() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(1, "x")
        tokens.insertBefore(2, "y")
        tokens.insertBefore(0, "z")
        try stream.fill()
        let result = try tokens.getText()
        let expecting = "zaxbyc"
        XCTAssertEqual(expecting, result)
    }
    func testOverlappingReplace() throws {
        let input = ANTLRInputStream("abcc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(1, 2, "foo")
        try tokens.replace(0, 3, "bar")
        try stream.fill()
        // wipes prior nested replace
        let result = try tokens.getText()
        let expecting = "bar"
        XCTAssertEqual(expecting, result)
    }
    func testOverlappingReplace2() throws {
        let input = ANTLRInputStream("abcc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(0, 3, "bar")
        try tokens.replace(1, 2, "foo")
        try stream.fill()
        // cannot split earlier replace
        do {
            _ = try tokens.getText()
            XCTFail("Expected exception not thrown.")
        } catch ANTLRError.illegalArgument(let msg) {
            XCTAssertEqual(expecting, msg)
        }
    }
    func testOverlappingReplace3() throws {
        let input = ANTLRInputStream("abcc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(1, 2, "foo")
        try tokens.replace(0, 2, "bar")
        try stream.fill()
        // wipes prior nested replace
        let result = try tokens.getText()
        let expecting = "barc"
        XCTAssertEqual(expecting, result)
    }
    func testOverlappingReplace4() throws {
        let input = ANTLRInputStream("abcc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(1, 2, "foo")
        try tokens.replace(1, 3, "bar")
        try stream.fill()
        // wipes prior nested replace
        let result = try tokens.getText()
        let expecting = "abar"
        XCTAssertEqual(expecting, result)
    }
    func testDropIdenticalReplace() throws {
        let input = ANTLRInputStream("abcc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(1, 2, "foo")
        try tokens.replace(1, 2, "foo")
        try stream.fill()
        // drop previous, identical
        let result = try tokens.getText()
        let expecting = "afooc"
        XCTAssertEqual(expecting, result)
    }
    func testDropPrevCoveredInsert() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(1, "foo")
        try tokens.replace(1, 2, "foo")
        try stream.fill()
        // kill prev insert
        let result = try tokens.getText()
        let expecting = "afoofoo"
        XCTAssertEqual(expecting, result)
    }
    func testLeaveAloneDisjointInsert() throws {
        let input = ANTLRInputStream("abcc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(1, "x")
        try tokens.replace(2, 3, "foo")
        let result = try tokens.getText()
        let expecting = "axbfoo"
        XCTAssertEqual(expecting, result)
    }
    func testLeaveAloneDisjointInsert2() throws {
        let input = ANTLRInputStream("abcc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        try tokens.replace(2, 3, "foo")
        tokens.insertBefore(1, "x")
        let result = try tokens.getText()
        let expecting = "axbfoo"
        XCTAssertEqual(expecting, result)
    }
    func testInsertBeforeTokenThenDeleteThatToken() throws {
        let input = ANTLRInputStream("abc")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(2, "y")
        try tokens.delete(2)
        let result = try tokens.getText()
        let expecting = "aby"
        XCTAssertEqual(expecting, result)
    }
    func testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder() throws {
        let input = ANTLRInputStream("aa")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "<b>")
        tokens.insertAfter(0, "</b>")
        tokens.insertBefore(1, "<b>")
        tokens.insertAfter(1, "</b>")
        let result = try tokens.getText()
        let expecting = "<b>a</b><b>a</b>" // fails with <b>a<b></b>a</b>"
        XCTAssertEqual(expecting, result)
    }
    func testDistinguishBetweenInsertAfterAndInsertBeforeToPreserverOrder2() throws {
        let input = ANTLRInputStream("aa")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "<p>")
        tokens.insertBefore(0, "<b>")
        tokens.insertAfter(0, "</p>")
        tokens.insertAfter(0, "</b>")
        tokens.insertBefore(1, "<b>")
        tokens.insertAfter(1, "</b>")
        let result = try tokens.getText()
        let expecting = "<b><p>a</p></b><b>a</b>"
        XCTAssertEqual(expecting, result)
    }
    func testPreservesOrderOfContiguousInserts() throws {
        let input = ANTLRInputStream("ab")
        let lexer = LexerA(input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let tokens = TokenStreamRewriter(stream)
        tokens.insertBefore(0, "<p>")
        tokens.insertBefore(0, "<b>")
        tokens.insertBefore(0, "<div>")
        tokens.insertAfter(0, "</p>")
        tokens.insertAfter(0, "</b>")
        tokens.insertAfter(0, "</div>")
        tokens.insertBefore(1, "!")
        let result = try tokens.getText()
        let expecting = "<div><b><p>a</p></b></div>!b"
        XCTAssertEqual(expecting, result)
    }
}


ParseTreePatternMatcherTests

import Foundation
import XCTest
import Antlr4
class ParseTreePatternMatcherTests: XCTestCase {
    func testSplit() throws {
        try doSplitTest("", [TextChunk("")])
        try doSplitTest("Foo", [TextChunk("Foo")])
        try doSplitTest("<ID> = <e:expr> ;",
                        [TagChunk("ID"), TextChunk(" = "), TagChunk("e", "expr"), TextChunk(" ;")])
        try doSplitTest("\\<ID\\> = <e:expr> ;",
                        [TextChunk("<ID> = "), TagChunk("e", "expr"), TextChunk(" ;")])
    }
}
private func doSplitTest(_ input: String, _ expected: [Chunk]) throws {
    let matcher = try makeMatcher()
    XCTAssertEqual(try matcher.split(input), expected)
}
private func makeMatcher() throws -> ParseTreePatternMatcher {
    // The lexer and parser here aren't actually used.  They're just here
    // so that ParseTreePatternMatcher can be constructed, but in this file
    // we're currently only testing methods that don't depend on them.
    let lexer = Lexer()
    let ts = BufferedTokenStream(lexer)
    let parser = try Parser(ts)
    return ParseTreePatternMatcher(lexer, parser)
}


ANTLRInputStreamTests

import Foundation
import XCTest
import Antlr4
class ANTLRInputStreamTests: XCTestCase {
    func testASCIICharactersString() {
        let inputStream = ANTLRInputStream("Cat")
        XCTAssertEqual(inputStream.LA(1), 0x0043)
        XCTAssertEqual(inputStream.LA(2), 0x0061)
        XCTAssertEqual(inputStream.LA(3), 0x0074)
    }
    func testBasicMultilingualPlaneCharactersString() {
        // Three Japanese hiragana characters.
        let inputStream = ANTLRInputStream("\u{3053}\u{306D}\u{3053}")
        XCTAssertEqual(inputStream.LA(1), 0x3053)
        XCTAssertEqual(inputStream.LA(2), 0x306D)
        XCTAssertEqual(inputStream.LA(3), 0x3053)
    }
    func testSupplementaryMultilingualPlaneCharactersString() {
        // Three "Cat", "Cat Face", and "Grinning Cat with Smiling Eyes" emojis
        let inputStream = ANTLRInputStream("\u{1F408}\u{1F431}\u{1F638}")
        XCTAssertEqual(inputStream.LA(1), 0x1F408)
        XCTAssertEqual(inputStream.LA(2), 0x1F431)
        XCTAssertEqual(inputStream.LA(3), 0x1F638)
    }
    func testGraphemeCharactersString() {
        // One "Family (Man, Woman, Girl, Boy)" emoji
        let inputStream = ANTLRInputStream("\u{1F468}\u{200D}\u{1F469}\u{200D}\u{1F467}\u{200D}\u{1F466}")
        XCTAssertEqual(inputStream.LA(1), 0x1F468)
        XCTAssertEqual(inputStream.LA(2), 0x200D)
        XCTAssertEqual(inputStream.LA(3), 0x1F469)
        XCTAssertEqual(inputStream.LA(4), 0x200D)
        XCTAssertEqual(inputStream.LA(5), 0x1F467)
        XCTAssertEqual(inputStream.LA(6), 0x200D)
        XCTAssertEqual(inputStream.LA(7), 0x1F466)
    }
}


TokenStreamTests

import XCTest
import Antlr4
class TokenStreamTests: XCTestCase {

    static let allTests = [
        ("testBufferedTokenStreamClearFetchEOFWithNewSource", testBufferedTokenStreamClearFetchEOFWithNewSource)
    ]
    /// Test fetchEOF reset after setTokenSource
    func testBufferedTokenStreamClearFetchEOFWithNewSource() throws {
        let inputStream1 = ANTLRInputStream("A")
        let tokenStream = CommonTokenStream(VisitorBasicLexer(inputStream1))
        try tokenStream.fill();
        XCTAssertEqual(2, tokenStream.size())
        XCTAssertEqual(VisitorBasicLexer.A, try tokenStream.get(0).getType())
        XCTAssertEqual(Lexer.EOF, try tokenStream.get(1).getType())
        let inputStream2 = ANTLRInputStream("AA");
        tokenStream.setTokenSource(VisitorBasicLexer(inputStream2));
        try tokenStream.fill();
        XCTAssertEqual(3, tokenStream.size())
        XCTAssertEqual(VisitorBasicLexer.A, try tokenStream.get(0).getType())
        XCTAssertEqual(VisitorBasicLexer.A, try tokenStream.get(1).getType())
        XCTAssertEqual(Lexer.EOF, try tokenStream.get(2).getType())
    }
}


VisitorTests

import XCTest
import Antlr4
class VisitorTests: XCTestCase {
    static let allTests = [
        ("testCalculatorVisitor", testCalculatorVisitor),
        ("testShouldNotVisitTerminal", testShouldNotVisitTerminal),
        ("testShouldNotVisitEOF", testShouldNotVisitEOF),
        ("testVisitErrorNode", testVisitErrorNode),
        ("testVisitTerminalNode", testVisitTerminalNode)
    ]

    ///
    /// This test verifies the basic behavior of visitors, with an emphasis on
    ///
    func testVisitTerminalNode() throws {
        let lexer = VisitorBasicLexer(ANTLRInputStream("A"))
        let parser = try VisitorBasicParser(CommonTokenStream(lexer))
        let context = try parser.s()
        XCTAssertEqual("(s A <EOF>)", context.toStringTree(parser))
        class Visitor: VisitorBasicBaseVisitor<String> {
            override func visitTerminal(_ node: TerminalNode) -> String? {
                return "\(node.getSymbol()!)\n"
            }
            override func defaultResult() -> String? {
                return ""
            }
            override func aggregateResult(_ aggregate: String?, _ nextResult: String?) -> String? {
                return aggregate! + nextResult!
            }
        }
        let visitor = Visitor()
        let result = visitor.visit(context)
        let expected =
    ///
    func testVisitErrorNode() throws {
        let lexer = VisitorBasicLexer(ANTLRInputStream(""))
        let parser = try VisitorBasicParser(CommonTokenStream(lexer))
        class ErrorListener: BaseErrorListener {
            override init() {
                super.init()
            }
            var errors = [String]()
            override func syntaxError<T>(_ recognizer: Recognizer<T>,
                                         _ offendingSymbol: AnyObject?,
                                         _ line: Int, _ charPositionInLine: Int,
                                         _ msg: String, _ e: AnyObject?) {
                errors.append("line \(line):\(charPositionInLine) \(msg)")
            }
        }
        parser.removeErrorListeners()
        let errorListener = ErrorListener()
        parser.addErrorListener(errorListener)
        let context = try parser.s()
        let errors = errorListener.errors
        XCTAssertEqual("(s <missing 'A'> <EOF>)", context.toStringTree(parser))
        XCTAssertEqual(1, errors.count)
        XCTAssertEqual("line 1:0 missing 'A' at '<EOF>'", errors[0])
        class Visitor: VisitorBasicBaseVisitor<String> {
            override func visitErrorNode(_ node: ErrorNode) -> String? {
                return "Error encountered: \(node.getSymbol()!)"
            }
            override func defaultResult() -> String? {
                return ""
            }
            override func aggregateResult(_ aggregate: String?, _ nextResult: String?) -> String? {
                return aggregate! + nextResult!
            }
        }
        let visitor = Visitor()
        let result = visitor.visit(context)
    ///
    func testShouldNotVisitEOF() throws {
        let input = "A"
        let lexer = VisitorBasicLexer(ANTLRInputStream(input))
        let parser = try VisitorBasicParser(CommonTokenStream(lexer))
        let context = try parser.s()
        XCTAssertEqual("(s A <EOF>)", context.toStringTree(parser))
        class Visitor: VisitorBasicBaseVisitor<String> {
            override func visitTerminal(_ node: TerminalNode) -> String? {
                return "\(node.getSymbol()!)\n"
            }
            override func shouldVisitNextChild(_ node: RuleNode, _ currentResult: String?) -> Bool {
                return currentResult == nil || currentResult!.isEmpty
            }
        }
        let visitor = Visitor()
        let result = visitor.visit(context)
    /// visiting a tree.
    ///
    func testShouldNotVisitTerminal() throws {
        let input = "A"
        let lexer = VisitorBasicLexer(ANTLRInputStream(input))
        let parser = try VisitorBasicParser(CommonTokenStream(lexer))
        let context = try parser.s()
        XCTAssertEqual("(s A <EOF>)", context.toStringTree(parser))
        class Visitor: VisitorBasicBaseVisitor<String> {
            override func visitTerminal(_ node: TerminalNode) -> String? {
                XCTFail()
                return nil
            }
            override func defaultResult() -> String? {
                return "default result"
            }
            override func shouldVisitNextChild(_ node: RuleNode, _ currentResult: String?) -> Bool {
                return false
            }
        }
        let visitor = Visitor()
        let result = visitor.visit(context)
        let expected = "default result"
        XCTAssertEqual(expected, result)
    }
    ///
    /// This test verifies that the visitor correctly dispatches calls for labeled outer alternatives.
    ///
    func testCalculatorVisitor() throws {
        let input = "2 + 8 / 2"
        let lexer = VisitorCalcLexer(ANTLRInputStream(input))
        let parser = try VisitorCalcParser(CommonTokenStream(lexer))
        let context = try parser.s()
        XCTAssertEqual("(s (expr (expr 2) + (expr (expr 8) / (expr 2))) <EOF>)", context.toStringTree(parser))
        class Visitor: VisitorCalcBaseVisitor<Int> {
            override func visitS(_ ctx: VisitorCalcParser.SContext) -> Int? {
                return visit(ctx.expr()!)
            }
            override func visitNumber(_ ctx: VisitorCalcParser.NumberContext) -> Int? {
                return Int((ctx.INT()?.getText())!)
            }
            override func visitMultiply(_ ctx: VisitorCalcParser.MultiplyContext) -> Int? {
                let left = visit(ctx.expr(0)!)!
                let right = visit(ctx.expr(1)!)!
                if ctx.MUL() != nil {
                    return left * right
                }
                else {
                    return left / right
                }
            }
            override func visitAdd(_ ctx: VisitorCalcParser.AddContext) -> Int? {
                let left = visit(ctx.expr(0)!)!
                let right = visit(ctx.expr(1)!)!
                if ctx.ADD() != nil {
                    return left + right
                }
                else {
                    return left - right
                }
            }
        }
        let visitor = Visitor()
        let result = visitor.visit(context)
        let expected = 6
        XCTAssertEqual(expected, result!)
    }
}


ThreadingTests

import XCTest
import Antlr4
class ThreadingTests: XCTestCase {
    static let allTests = [
        ("testParallelExecution", testParallelExecution),
    ]
    ///
    /// This test verifies parallel execution of the parser
    ///
    func testParallelExecution() throws {
        let input = [
            "2 * 8 - 4",
            "2 + 8 / 4",
            "2 - 8 - 4",
            "2 * 8 * 4",
            "2 / 8 / 4",
            "2 + 8 + 4",
            "890",
        ]
        let exp = expectation(description: "Waiting on async-task")
        exp.expectedFulfillmentCount = 100
        for i in 1...100 {
            DispatchQueue.global().async {
                let lexer = ThreadingLexer(ANTLRInputStream(input[i % 7]))
                let tokenStream = CommonTokenStream(lexer)
                let parser = try? ThreadingParser(tokenStream)
                let _ = try? parser?.s()
                exp.fulfill()
            }
        }
        waitForExpectations(timeout: 30.0) { (_) in
            print("Completed")
        }
    }
}


MurmurHashTests

/// The test patterns below are by Ian Boyd and have been released into the
/// public domain.
/// https://stackoverflow.com/questions/14747343/murmurhash3-test-vectors
import XCTest
import Antlr4
class MurmurHashTests: XCTestCase {
    func testMurmurHash() {
        doMurmurHashTest("", 0, 0) //empty string with zero seed should give zero
        doMurmurHashTest("", 1, 0x514E28B7)
        doMurmurHashTest("", 0xffffffff, 0x81F16F39) //make sure seed value is handled unsigned
        doMurmurHashTest("\0\0\0\0", 0, 0x2362F9DE) //make sure we handle embedded nulls
        doMurmurHashTest("aaaa", 0x9747b28c, 0x5A97808A) //one full chunk
        doMurmurHashTest("aaa", 0x9747b28c, 0x283E0130) //three characters
        doMurmurHashTest("aa", 0x9747b28c, 0x5D211726) //two characters
        doMurmurHashTest("a", 0x9747b28c, 0x7FA09EA6) //one character
        //Endian order within the chunks
        doMurmurHashTest("abcd", 0x9747b28c, 0xF0478627) //one full chunk
        doMurmurHashTest("abc", 0x9747b28c, 0xC84A62DD)
        doMurmurHashTest("ab", 0x9747b28c, 0x74875592)
        doMurmurHashTest("a", 0x9747b28c, 0x7FA09EA6)
        doMurmurHashTest("Hello, world!", 0x9747b28c, 0x24884CBA)
        //Make sure you handle UTF-8 high characters. A bcrypt implementation messed this up
        doMurmurHashTest("ππππππππ", 0x9747b28c, 0xD58063C1) //U+03C0: Greek Small Letter Pi
        //String of 256 characters.
        doMurmurHashTest(String(repeating: "a", count: 256), 0x9747b28c, 0x37405BDC)
        doMurmurHashTest("abc", 0, 0xB3DD93FA)
        doMurmurHashTest("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq", 0, 0xEE925B90)
        doMurmurHashTest("The quick brown fox jumps over the lazy dog", 0x9747b28c, 0x2FA826CD)
    }
}
private func doMurmurHashTest(_ input: String, _ seed: UInt32, _ expected: UInt32) {
    XCTAssertEqual(MurmurHash.hashString(input, seed), expected)
}


StringExtensionTests

class StringExtensionTests: XCTestCase {
    func testLastIndex() {
        doLastIndexTest("", "", nil)
        doLastIndexTest("a", "", nil)
        doLastIndexTest("a", "a", 0)
        doLastIndexTest("aba", "a", 2)
        doLastIndexTest("aba", "b", 1)
        doLastIndexTest("abc", "d", nil)
    }
}
private func doLastIndexTest(_ str: String, _ target: String, _ expectedOffset: Int?) {
    let expectedIdx: String.Index?
    if let expectedOffset = expectedOffset {
        expectedIdx = str.index(str.startIndex, offsetBy: expectedOffset)
    }
    else {
        expectedIdx = nil
    }
    XCTAssertEqual(str.lastIndex(of: target), expectedIdx)
}


InterpreterDataTests

import XCTest
import Antlr4
class InterpreterDataTests: XCTestCase {
    // https://stackoverflow.com/a/57713176
    let sourceDir = URL(fileURLWithPath:#file).deletingLastPathComponent()
    func testLexerA() throws {
        let input = ANTLRInputStream("abc")
        let interpPath = sourceDir.appendingPathComponent("gen/LexerA.interp").path
        let data = try InterpreterDataReader(interpPath)
        let lexer = try data.createLexer(input:input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let result = try stream.getText()
        let expecting = "abc"
        XCTAssertEqual(expecting, result)
    }
    func testLexerB() throws {
        let input = ANTLRInputStream("x = 3 * 0 + 2 * 0;")
        let interpPath = sourceDir.appendingPathComponent("gen/LexerB.interp").path
        let data = try InterpreterDataReader(interpPath)
        let lexer = try data.createLexer(input:input)
        let stream = CommonTokenStream(lexer)
        try stream.fill()
        let result = try stream.getText()
        let expecting = "x = 3 * 0 + 2 * 0;"
        XCTAssertEqual(expecting, result)
    }
    func testCalculator() throws {
        let input = "2 + 8 / 2"
        let lexerInterpPath = sourceDir.appendingPathComponent("gen/VisitorCalcLexer.interp").path
        let lexerInterpData = try InterpreterDataReader(lexerInterpPath)
        let lexer = try lexerInterpData.createLexer(input:ANTLRInputStream(input))
        let parserInterpPath = sourceDir.appendingPathComponent("gen/VisitorCalc.interp").path
        let parserInterpData = try InterpreterDataReader(parserInterpPath)
        let parser = try parserInterpData.createParser(input:CommonTokenStream(lexer))
        let context = try parser.parse(parser.getRuleIndex("s"))
        XCTAssertEqual("(s (expr (expr 2) + (expr (expr 8) / (expr 2))) <EOF>)", context.toStringTree(parser))
    }
}


RuntimeMetaDataTests

import Foundation
import XCTest
import Antlr4
class RuntimeMetaDataTests: XCTestCase {
    func testGetMajorMinorVersion() {
        doGetMajorMinorVersionTest("", "")
        doGetMajorMinorVersionTest("4", "4")
        doGetMajorMinorVersionTest("4.", "4.")
        doGetMajorMinorVersionTest("4.7", "4.7")
        doGetMajorMinorVersionTest("4.7.1", "4.7")
        doGetMajorMinorVersionTest("4.7.2", "4.7")
        doGetMajorMinorVersionTest("4.8", "4.8")
        doGetMajorMinorVersionTest("4.9", "4.9")
        doGetMajorMinorVersionTest("4.9.1", "4.9")
        doGetMajorMinorVersionTest("4.9.2", "4.9")
        doGetMajorMinorVersionTest("4.9.3", "4.9")
        doGetMajorMinorVersionTest("4-SNAPSHOT", "4")
        doGetMajorMinorVersionTest("4.-SNAPSHOT", "4.")
        doGetMajorMinorVersionTest("4.7-SNAPSHOT", "4.7")
        doGetMajorMinorVersionTest("4.7.1-SNAPSHOT", "4.7")
        doGetMajorMinorVersionTest("4.7.2-SNAPSHOT", "4.7")
        doGetMajorMinorVersionTest("4.9.1-SNAPSHOT", "4.9")
        doGetMajorMinorVersionTest("4.9.2-SNAPSHOT", "4.9")
        doGetMajorMinorVersionTest("4.9.3-SNAPSHOT", "4.9")
        doGetMajorMinorVersionTest("4.10-SNAPSHOT", "4.10")
        doGetMajorMinorVersionTest("4.10.1", "4.10")
        doGetMajorMinorVersionTest("4.11.0", "4.11")
        doGetMajorMinorVersionTest("4.11.1", "4.11")
        doGetMajorMinorVersionTest("4.11.0-SNAPSHOT", "4.11")
        doGetMajorMinorVersionTest("4.11.1-SNAPSHOT", "4.11")
        doGetMajorMinorVersionTest("4.12.0-SNAPSHOT", "4.12")
        doGetMajorMinorVersionTest("4.12.0", "4.12")
        doGetMajorMinorVersionTest("4.13.0-SNAPSHOT", "4.13")
        doGetMajorMinorVersionTest("4.13.0", "4.13")
    }
}
private func doGetMajorMinorVersionTest(_ input: String, _ expected: String) {
    XCTAssertEqual(RuntimeMetaData.getMajorMinorVersion(input), expected)
}


M

import L
public let SixtyFour: Int = ThirtyTwo * 2


N

import M
public let OneTwentyEight = SixtyFour * 2


L

public let ThirtyTwo: Int = 32


main

dump("SwiftOnly")

#if SWIFTONLY
dump("SWIFTONLY defined")
#else
fatalError("SWIFTONLY NOT defined")
#endif


plugin

import main

public func importing() -> Int  { main.exported() + 1 }


main

public func exported() -> Int { 32 }
print(exported())


SubB

import SubA


SwiftMain

import Foundation
    dump("Hello World!");
#if FOO
    dump("FOO defined");
#else
    fatalError("FOO not defined");
#endif
#if BAR
    dump("BAR defined");
#else
    fatalError("BAR not defined");
#endif
#if CCOND
    fatalError("CCOND defined");
#else
    dump("CCOND not defined");
#endif
#if SWIFTCOND
    dump("SWIFTCOND defined");
#else
    fatalError("SWIFTCOND not defined");
#endif
    return 0;
  }
}


main

import SwiftMixedLib

print(add(a: 1, b: 2))


lib

public func add(a: Int, b: Int) -> Int {
    a + b
}


L

public let ThirtyTwo: Int = 32


E

func f() {
}


#if os(iOS)
import UIKit
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
        }
    }
}
#endif


foo

func foo() {
}


s

infix operator •: AdditionPrecedence
func • (a: Int, b: Int) -> Int {
  return a * b
}
func g(a: Int) -> Int{
  return a + 1
}
let i = g(a: 1 • 2)


import Swift

var s: [Int] = [1, 2, 3, 4]

func f() {
  for i in 0..<4 {
    print(s[i])
  }
}

f()