 import Foundation
 
 internal protocol Token {
-    func match(_ string: String, matchRange: NSRange) -> NSRange
+    func match(_ string: String, matchRange: Range<String.Index>) -> Range<String.Index>?
     func isNewLine() -> Bool
 }
 

    }
    
    func accept(_ token: T) -> String? {
        let range = token.match(stringStream, matchRange: matchRange)
            
        if range.location != NSNotFound {
            
            if token.isNewLine() {
                line += 1
                column = 1
            } else {
                column += range.length
            }
            // Increment the range for matching
            matchRange.location += range.length
            matchRange.length   -= range.length
            
    // FIXME: This is temporary until the the following known issue is fixed. https://github.com/apple/swift-corelibs-foundation/blob/master/Docs/Issues.md#known-issues
#if os(Linux) || os(FreeBSD)
            return stringStream.bridge().substring(with: range)
#else
            return (stringStream as NSString).substring(with: range)
#endif
        }
        return nil
    }
    
    func expect(_ token: T) -> Bool {
        return token.match(stringStream, matchRange: matchRange).location != NSNotFound
    }
}

    
    var line = 0
    var column = 0
    
    fileprivate var stringStream: String
    fileprivate var matchRange: Range<String.Index>
    
    init(string: String) {
        self.stringStream = string
        self.matchRange = string.startIndex..<string.endIndex
        
        if self.stringStream.characters.count > 0 {
            line = 1
