Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Swift] Minor tidyups in the stream code. #2156

Merged
merged 1 commit into from
Nov 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 14 additions & 23 deletions runtime/Swift/Sources/Antlr4/ANTLRInputStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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?
Expand Down Expand Up @@ -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")

}

Expand Down Expand Up @@ -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).
///
Expand Down Expand Up @@ -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 {
Expand Down
99 changes: 36 additions & 63 deletions runtime/Swift/Sources/Antlr4/BufferedTokenStream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
///
Expand All @@ -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<Token> = Array<Token>()
// Array<Token>(100
internal var tokens = [Token]()

///
/// The index into _#tokens_ of the current token (next token to
Expand All @@ -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
Expand All @@ -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) {

public init(_ tokenSource: TokenSource) {
self.tokenSource = tokenSource
}

Expand Down Expand Up @@ -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
}

Expand All @@ -156,12 +155,12 @@ public class BufferedTokenStream: TokenStream {
}

for i in 0..<n {
let t: Token = try tokenSource.nextToken()
if t is WritableToken {
(t as! WritableToken).setTokenIndex(tokens.count)
let t = try tokenSource.nextToken()
if let wt = t as? WritableToken {
wt.setTokenIndex(tokens.count)
}

tokens.append(t) //add
tokens.append(t)
if t.getType() == BufferedTokenStream.EOF {
fetchedEOF = true
return i + 1
Expand All @@ -173,27 +172,26 @@ public class BufferedTokenStream: TokenStream {

public func get(_ i: Int) throws -> 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<Token>? {
var stop = stop
if start < 0 || stop < 0 {
return nil
}
try lazyInit()
var subset: Array<Token> = Array<Token>()
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
}
Expand Down Expand Up @@ -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]
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -379,72 +376,52 @@ 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<Token>? {
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<Token>? {
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<Token>? {
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 {
// obviously no tokens can appear before the first token
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] {
Expand Down Expand Up @@ -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
}
Expand Down
Loading