diff --git a/runtime/Swift/Sources/Antlr4/ANTLRInputStream.swift b/runtime/Swift/Sources/Antlr4/ANTLRInputStream.swift index 4267ce132..31c32e107 100644 --- a/runtime/Swift/Sources/Antlr4/ANTLRInputStream.swift +++ b/runtime/Swift/Sources/Antlr4/ANTLRInputStream.swift @@ -2,18 +2,17 @@ /// Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. /// Use of this file is governed by the BSD 3-clause license that /// can be found in the LICENSE.txt file in the project root. +/// + +/// /// Vacuum all input from a _java.io.Reader_/_java.io.InputStream_ and then treat it /// like a `char[]` buffer. Can also pass in a _String_ or /// `char[]` to use. /// /// If you need encoding, pass in stream/reader with correct encoding. -/// - +/// public class ANTLRInputStream: CharStream { - public static let READ_BUFFER_SIZE: Int = 1024 - public static let INITIAL_BUFFER_SIZE: Int = 1024 - - /// + /// /// The data being scanned /// internal var data: [Character] @@ -24,9 +23,9 @@ public class ANTLRInputStream: CharStream { internal var n: Int /// - /// 0..n-1 index into string of next char + /// 0...n-1 index into string of next char /// - internal var p: Int = 0 + internal var p = 0 /// /// What is name or source of this char stream? @@ -62,7 +61,7 @@ public class ANTLRInputStream: CharStream { if p >= n { assert(LA(1) == ANTLRInputStream.EOF, "Expected: LA(1)==IntStream.EOF") - throw ANTLRError.illegalState(msg: "annot consume EOF") + throw ANTLRError.illegalState(msg: "cannot consume EOF") } @@ -99,7 +98,7 @@ public class ANTLRInputStream: CharStream { } /// - /// Return the current input symbol index 0..n where n indicates the + /// Return the current input symbol index 0...n where n indicates the /// last symbol has been read. The index is the index of char to /// be returned from LA(1). /// @@ -136,29 +135,21 @@ public class ANTLRInputStream: CharStream { // seek forward, consume until p hits index or n (whichever comes first) index = min(index, n) while p < index { - try consume() + try consume() } } public func getText(_ interval: Interval) -> String { - let start: Int = interval.a - var stop: Int = interval.b - if stop >= n { - stop = n - 1 - } - let count = stop - start + 1; + let start = interval.a if start >= n { return "" } - - return String(data[start ..< (start + count)]) + let stop = min(n, interval.b + 1) + return String(data[start ..< stop]) } public func getSourceName() -> String { - guard let name = name , !name.isEmpty else { - return ANTLRInputStream.UNKNOWN_SOURCE_NAME - } - return name + return name ?? ANTLRInputStream.UNKNOWN_SOURCE_NAME } public func toString() -> String { diff --git a/runtime/Swift/Sources/Antlr4/BufferedTokenStream.swift b/runtime/Swift/Sources/Antlr4/BufferedTokenStream.swift index 82e0fb05a..72cb18fda 100644 --- a/runtime/Swift/Sources/Antlr4/BufferedTokenStream.swift +++ b/runtime/Swift/Sources/Antlr4/BufferedTokenStream.swift @@ -17,7 +17,7 @@ /// channel, such as _org.antlr.v4.runtime.Token#DEFAULT_CHANNEL_ or /// _org.antlr.v4.runtime.Token#HIDDEN_CHANNEL_, use a filtering token stream such a /// _org.antlr.v4.runtime.CommonTokenStream_. -/// +/// public class BufferedTokenStream: TokenStream { /// @@ -30,8 +30,7 @@ public class BufferedTokenStream: TokenStream { /// considered a complete view of the input once _#fetchedEOF_ is set /// to `true`. /// - internal var tokens: Array = Array() - // Array(100 + internal var tokens = [Token]() /// /// The index into _#tokens_ of the current token (next token to @@ -44,7 +43,7 @@ public class BufferedTokenStream: TokenStream { /// see the documentation of _org.antlr.v4.runtime.IntStream_ for a description of /// Initializing Methods. /// - internal var p: Int = -1 + internal var p = -1 /// /// Indicates whether the _org.antlr.v4.runtime.Token#EOF_ token has been fetched from @@ -58,10 +57,10 @@ public class BufferedTokenStream: TokenStream { /// * _#fetch_: The check to prevent adding multiple EOF symbols into /// _#tokens_ is trivial with this field. /// - internal var fetchedEOF: Bool = false + internal var fetchedEOF = false + public init(_ tokenSource: TokenSource) { - self.tokenSource = tokenSource } @@ -135,10 +134,10 @@ public class BufferedTokenStream: TokenStream { @discardableResult internal func sync(_ i: Int) throws -> Bool { assert(i >= 0, "Expected: i>=0") - let n: Int = i - tokens.count + 1 // how many more elements we need? + let n = i - tokens.count + 1 // how many more elements we need? //print("sync("+i+") needs "+n); if n > 0 { - let fetched: Int = try fetch(n) + let fetched = try fetch(n) return fetched >= n } @@ -156,12 +155,12 @@ public class BufferedTokenStream: TokenStream { } for i in 0.. Token { if i < 0 || i >= tokens.count { - let index = tokens.count - 1 - throw ANTLRError.indexOutOfBounds(msg: "token index \(i) out of range 0..\(index)") + throw ANTLRError.indexOutOfBounds(msg: "token index \(i) out of range 0 ..< \(tokens.count)") } return tokens[i] } /// - /// Get all tokens from start..stop inclusively + /// Get all tokens from start...stop inclusively /// public func get(_ start: Int,_ stop: Int) throws -> Array? { var stop = stop @@ -188,12 +186,12 @@ public class BufferedTokenStream: TokenStream { return nil } try lazyInit() - var subset: Array = Array() + var subset = [Token]() if stop >= tokens.count { stop = tokens.count - 1 } for i in start...stop { - let t: Token = tokens[i] + let t = tokens[i] if t.getType() == BufferedTokenStream.EOF { break } @@ -223,14 +221,13 @@ public class BufferedTokenStream: TokenStream { return try LB(-k) } - let i: Int = p + k - 1 + let i = p + k - 1 try sync(i) if i >= tokens.count { // return EOF token // EOF must be last token - return tokens[tokens.count - 1] + return tokens.last! } -// if ( i>range ) range = i; return tokens[i] } @@ -289,7 +286,7 @@ public class BufferedTokenStream: TokenStream { try lazyInit() if start < 0 || start >= tokens.count || stop < 0 || stop >= tokens.count { - throw ANTLRError.indexOutOfBounds(msg: "start \(start) or stop \(stop) not in 0...\(tokens.count - 1)") + throw ANTLRError.indexOutOfBounds(msg: "start \(start) or stop \(stop) not in 0 ..< \(tokens.count)") } if start > stop { @@ -330,7 +327,7 @@ public class BufferedTokenStream: TokenStream { return size() - 1 } - var token: Token = tokens[i] + var token = tokens[i] while token.getChannel() != channel { if token.getType() == BufferedTokenStream.EOF { return i @@ -363,7 +360,7 @@ public class BufferedTokenStream: TokenStream { } while i >= 0 { - let token: Token = tokens[i] + let token = tokens[i] if token.getType() == BufferedTokenStream.EOF || token.getChannel() == channel { return i } @@ -379,45 +376,35 @@ public class BufferedTokenStream: TokenStream { /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL or /// EOF. If channel is -1, find any non default channel token. /// - public func getHiddenTokensToRight(_ tokenIndex: Int, _ channel: Int) throws -> Array? { + public func getHiddenTokensToRight(_ tokenIndex: Int, _ channel: Int = -1) throws -> [Token]? { try lazyInit() if tokenIndex < 0 || tokenIndex >= tokens.count { - throw ANTLRError.indexOutOfBounds(msg: "\(tokenIndex) not in 0..\(tokens.count - 1)") - + throw ANTLRError.indexOutOfBounds(msg: "\(tokenIndex) not in 0 ..< \(tokens.count)") } - let nextOnChannel: Int = - try nextTokenOnChannel(tokenIndex + 1, Lexer.DEFAULT_TOKEN_CHANNEL) - var to: Int - let from: Int = tokenIndex + 1 + let nextOnChannel = try nextTokenOnChannel(tokenIndex + 1, Lexer.DEFAULT_TOKEN_CHANNEL) + let from = tokenIndex + 1 + let to: Int // if none onchannel to right, nextOnChannel=-1 so set to = last token if nextOnChannel == -1 { to = size() - 1 - } else { + } + else { to = nextOnChannel } return filterForChannel(from, to, channel) } - /// - /// Collect all hidden tokens (any off-default channel) to the right of - /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL - /// or EOF. - /// - public func getHiddenTokensToRight(_ tokenIndex: Int) throws -> Array? { - return try getHiddenTokensToRight(tokenIndex, -1) - } - - /// + /// /// Collect all tokens on specified channel to the left of /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. /// If channel is -1, find any non default channel token. /// - public func getHiddenTokensToLeft(_ tokenIndex: Int, _ channel: Int) throws -> Array? { + public func getHiddenTokensToLeft(_ tokenIndex: Int, _ channel: Int = -1) throws -> [Token]? { try lazyInit() if tokenIndex < 0 || tokenIndex >= tokens.count { - throw ANTLRError.indexOutOfBounds(msg: "\(tokenIndex) not in 0..\(tokens.count - 1)") + throw ANTLRError.indexOutOfBounds(msg: "\(tokenIndex) not in 0 ..< \(tokens.count)") } if tokenIndex == 0 { @@ -425,26 +412,16 @@ public class BufferedTokenStream: TokenStream { return nil } - let prevOnChannel: Int = - try previousTokenOnChannel(tokenIndex - 1, Lexer.DEFAULT_TOKEN_CHANNEL) + let prevOnChannel = try previousTokenOnChannel(tokenIndex - 1, Lexer.DEFAULT_TOKEN_CHANNEL) if prevOnChannel == tokenIndex - 1 { return nil } // if none onchannel to left, prevOnChannel=-1 then from=0 - let from: Int = prevOnChannel + 1 - let to: Int = tokenIndex - 1 - + let from = prevOnChannel + 1 + let to = tokenIndex - 1 return filterForChannel(from, to, channel) } - /// - /// Collect all hidden tokens (any off-default channel) to the left of - /// the current token up until we see a token on DEFAULT_TOKEN_CHANNEL. - /// - public func getHiddenTokensToLeft(_ tokenIndex: Int) throws -> [Token]? { - return try getHiddenTokensToLeft(tokenIndex, -1) - } - internal func filterForChannel(_ from: Int, _ to: Int, _ channel: Int) -> [Token]? { var hidden = [Token]() for t in tokens[from...to] { @@ -478,17 +455,13 @@ public class BufferedTokenStream: TokenStream { public func getText(_ interval: Interval) throws -> String { let start = interval.a - var stop = interval.b - if start < 0 || stop < 0 { + if start < 0 { return "" } try fill() - if stop >= tokens.count { - stop = tokens.count - 1 - } - + let stop = min(tokens.count, interval.b + 1) var buf = "" - for t in tokens[start...stop] { + for t in tokens[start ..< stop] { if t.getType() == BufferedTokenStream.EOF { break } diff --git a/runtime/Swift/Sources/Antlr4/ListTokenSource.swift b/runtime/Swift/Sources/Antlr4/ListTokenSource.swift index 681e7aa9d..f082452cc 100644 --- a/runtime/Swift/Sources/Antlr4/ListTokenSource.swift +++ b/runtime/Swift/Sources/Antlr4/ListTokenSource.swift @@ -79,10 +79,10 @@ public class ListTokenSource: TokenSource { else if let eofToken = eofToken { return eofToken.getCharPositionInLine() } - else if tokens.count > 0 { + else if !tokens.isEmpty { // have to calculate the result from the line/column of the previous // token, along with the text of the token. - let lastToken = tokens[tokens.count - 1] + let lastToken = tokens.last! if let tokenText = lastToken.getText() { if let lastNewLine = tokenText.lastIndex(of: "\n") { @@ -93,10 +93,11 @@ public class ListTokenSource: TokenSource { lastToken.getStopIndex() - lastToken.getStartIndex() + 1) } - - // only reach this if tokens is empty, meaning EOF occurs at the first - // position in the input - return 0 + else { + // only reach this if tokens is empty, meaning EOF occurs at the first + // position in the input + return 0 + } } public func nextToken() -> Token { @@ -130,33 +131,32 @@ public class ListTokenSource: TokenSource { public func getLine() -> Int { if i < tokens.count { return tokens[i].getLine() - } else { - if let eofToken = eofToken { - return eofToken.getLine() - } else { - if tokens.count > 0 { - // have to calculate the result from the line/column of the previous - // token, along with the text of the token. - let lastToken = tokens[tokens.count - 1] - var line = lastToken.getLine() + } + else if let eofToken = eofToken { + return eofToken.getLine() + } + else if !tokens.isEmpty { + // have to calculate the result from the line/column of the previous + // token, along with the text of the token. + let lastToken = tokens.last! + var line = lastToken.getLine() - if let tokenText = lastToken.getText() { - for c in tokenText { - if c == "\n" { - line += 1 - } - } + if let tokenText = lastToken.getText() { + for c in tokenText { + if c == "\n" { + line += 1 } - - // if no text is available, assume the token did not contain any newline characters. - return line } } - } - // only reach this if tokens is empty, meaning EOF occurs at the first - // position in the input - return 1 + // if no text is available, assume the token did not contain any newline characters. + return line + } + else { + // only reach this if tokens is empty, meaning EOF occurs at the first + // position in the input + return 1 + } } public func getInputStream() -> CharStream? { @@ -166,8 +166,8 @@ public class ListTokenSource: TokenSource { else if let eofToken = eofToken { return eofToken.getInputStream() } - else if tokens.count > 0 { - return tokens[tokens.count - 1].getInputStream() + else if !tokens.isEmpty { + return tokens.last!.getInputStream() } // no input stream information is available diff --git a/runtime/Swift/Sources/Antlr4/UnbufferedTokenStream.swift b/runtime/Swift/Sources/Antlr4/UnbufferedTokenStream.swift index e69affed7..3f3dcc0cb 100644 --- a/runtime/Swift/Sources/Antlr4/UnbufferedTokenStream.swift +++ b/runtime/Swift/Sources/Antlr4/UnbufferedTokenStream.swift @@ -12,22 +12,22 @@ public class UnbufferedTokenStream: TokenStream { /// we keep adding to buffer. Otherwise, _#consume consume()_ resets so /// we start filling at index 0 again. /// - internal var tokens: [Token] + internal var tokens = [Token]() /// - /// The number of tokens currently in _#tokens tokens_. + /// The number of tokens currently in `self.tokens`. /// - /// This is not the buffer capacity, that's `tokens.length`. + /// This is not the buffer capacity, that's `self.tokens.count`. /// - internal var n: Int + internal var n = 0 /// - /// 0..n-1 index into _#tokens tokens_ of next token. + /// `0...n-1` index into `self.tokens` of next token. /// /// The `LT(1)` token is `tokens[p]`. If `p == n`, we are /// out of buffered tokens. /// - internal var p: Int = 0 + internal var p = 0 /// /// Count up with _#mark mark()_ and down with @@ -35,7 +35,7 @@ public class UnbufferedTokenStream: TokenStream { /// `numMarkers` reaches 0 and we reset the buffer. Copy /// `tokens[p]..tokens[n-1]` to `tokens[0]..tokens[(n-1)-p]`. /// - internal var numMarkers: Int = 0 + internal var numMarkers = 0 /// /// This is the `LT(-1)` token for the current position. @@ -56,24 +56,18 @@ public class UnbufferedTokenStream: TokenStream { /// This value is used to set the token indexes if the stream provides tokens /// that implement _org.antlr.v4.runtime.WritableToken_. /// - internal var currentTokenIndex: Int = 0 + internal var currentTokenIndex = 0 - public convenience init(_ tokenSource: TokenSource) throws { - try self.init(tokenSource, 256) - } - //TODO: bufferSize don't be use - public init(_ tokenSource: TokenSource, _ bufferSize: Int) throws { + + public init(_ tokenSource: TokenSource) throws { self.tokenSource = tokenSource - //tokens = [Token](count: bufferSize, repeatedValue: Token) ; - tokens = [Token]() - n = 0 try fill(1) // prime the pump } public func get(_ i: Int) throws -> Token { // get absolute index - let bufferStartIndex: Int = getBufferStartIndex() + let bufferStartIndex = getBufferStartIndex() if i < bufferStartIndex || i >= bufferStartIndex + n { throw ANTLRError.indexOutOfBounds(msg: "get(\(i)) outside buffer: \(bufferStartIndex)..\(bufferStartIndex + n)") } @@ -103,7 +97,7 @@ public class UnbufferedTokenStream: TokenStream { public func LA(_ i: Int) throws -> Int { - return try LT(i)!.getType() + return try LT(i)!.getType() } @@ -184,8 +178,8 @@ public class UnbufferedTokenStream: TokenStream { //tokens = Arrays.copyOf(tokens, tokens.length * 2); } - if t is WritableToken { - (t as! WritableToken).setTokenIndex(getBufferStartIndex() + n) + if let wt = t as? WritableToken { + wt.setTokenIndex(getBufferStartIndex() + n) } tokens[n] = t @@ -205,14 +199,14 @@ public class UnbufferedTokenStream: TokenStream { lastTokenBufferStart = lastToken } - let mark: Int = -numMarkers - 1 + let mark = -numMarkers - 1 numMarkers += 1 return mark } public func release(_ marker: Int) throws { - let expectedMark: Int = -numMarkers + let expectedMark = -numMarkers if marker != expectedMark { throw ANTLRError.illegalState(msg: "release() called with an invalid marker.") } @@ -224,7 +218,6 @@ public class UnbufferedTokenStream: TokenStream { // Copy tokens[p]..tokens[n-1] to tokens[0]..tokens[(n-1)-p], reset ptrs // p is last valid token; move nothing if p==n as we have no valid char tokens = Array(tokens[p ... n - 1]) - //System.arraycopy(tokens, p, tokens, 0, n - p); // shift n-p tokens from p to 0 n = n - p p = 0 } @@ -251,16 +244,14 @@ public class UnbufferedTokenStream: TokenStream { index = min(index, getBufferStartIndex() + n - 1) } - let bufferStartIndex: Int = getBufferStartIndex() - let i: Int = index - bufferStartIndex + let bufferStartIndex = getBufferStartIndex() + let i = index - bufferStartIndex if i < 0 { throw ANTLRError.illegalState(msg: "cannot seek to negative index \(index)") - } else { - if i >= n { - throw ANTLRError.unsupportedOperation(msg: "seek to index outside buffer: \(index) not in \(bufferStartIndex)..\(bufferStartIndex + n)") - - } + } + else if i >= n { + throw ANTLRError.unsupportedOperation(msg: "seek to index outside buffer: \(index) not in \(bufferStartIndex)..<\(bufferStartIndex + n)") } p = i @@ -290,7 +281,7 @@ public class UnbufferedTokenStream: TokenStream { let start = interval.a let stop = interval.b if start < bufferStartIndex || stop > bufferStopIndex { - throw ANTLRError.unsupportedOperation(msg: "interval \(interval) not in token buffer window: \(bufferStartIndex)..bufferStopIndex)") + throw ANTLRError.unsupportedOperation(msg: "interval \(interval) not in token buffer window: \(bufferStartIndex)...\(bufferStopIndex)") } let a = start - bufferStartIndex