Remove tokenNames / getTokenNames from the Recognizer interface.

This has been ported over from the Java code, but it was deprecated there.
There's no point having it in the Swift runtime because we don't have the
legacy code to support.  Also, it wasn't implemented properly, so it
never worked.

Remove {DFA,IntervalSet}.toString(_:[String?]?)
and the inits in ParserInterpreter and DFASerializer for the same reason.
Switch the unit tests to use the alternate toString(_:Vocabulary).
This commit is contained in:
Ewan Mellor 2017-10-19 23:46:19 -07:00
parent 23f532ddbd
commit 6c11160b51
No known key found for this signature in database
GPG Key ID: 7CE1C6BC9EC8645D
9 changed files with 2 additions and 173 deletions

View File

@ -72,7 +72,7 @@ TokenStartColumnEquals(i) ::= <%self._tokenStartCharPositionInLine == <i>%>
ImportListener(X) ::= "" ImportListener(X) ::= ""
GetExpectedTokenNames() ::= "try self.getExpectedTokens().toString(self.tokenNames)" GetExpectedTokenNames() ::= "try self.getExpectedTokens().toString(self.getVocabulary())"
RuleInvocationStack() ::= "getRuleInvocationStack().description.replacingOccurrences(of: \"\\\"\", with: \"\")" RuleInvocationStack() ::= "getRuleInvocationStack().description.replacingOccurrences(of: \"\\\"\", with: \"\")"

View File

@ -372,16 +372,6 @@ open class Lexer: Recognizer<LexerATNSimulator>, TokenSource {
return nil return nil
} }
///
/// Used to print out token names like ID during debugging and
/// error reporting. The generated parsers implement a method
/// that overrides this to point to their String[] tokenNames.
///
override
open func getTokenNames() -> [String?]? {
return nil
}
/// ///
/// Return a list of all Token objects in input char stream. /// Return a list of all Token objects in input char stream.
/// Forces load of all tokens. Does not include EOF token. /// Forces load of all tokens. Does not include EOF token.

View File

@ -9,15 +9,10 @@ public class LexerInterpreter: Lexer {
internal final var grammarFileName: String internal final var grammarFileName: String
internal final var atn: ATN internal final var atn: ATN
///
/// /@Deprecated
///
internal final var tokenNames: [String?]?
internal final var ruleNames: [String] internal final var ruleNames: [String]
internal final var channelNames: [String] internal final var channelNames: [String]
internal final var modeNames: [String] internal final var modeNames: [String]
private final var vocabulary: Vocabulary? private final var vocabulary: Vocabulary?
internal final var _decisionToDFA: [DFA] internal final var _decisionToDFA: [DFA]
@ -40,13 +35,6 @@ public class LexerInterpreter: Lexer {
self.grammarFileName = grammarFileName self.grammarFileName = grammarFileName
self.atn = atn self.atn = atn
self.tokenNames = [String?]()
//new String[atn.maxTokenType];
let length = tokenNames!.count
for i in 0..<length {
tokenNames![i] = vocabulary.getDisplayName(i)
}
self.ruleNames = ruleNames self.ruleNames = ruleNames
self.channelNames = channelNames self.channelNames = channelNames
self.modeNames = modeNames self.modeNames = modeNames
@ -78,14 +66,6 @@ public class LexerInterpreter: Lexer {
return grammarFileName return grammarFileName
} }
override
///
/// /@Deprecated
///
public func getTokenNames() -> [String?]? {
return tokenNames
}
override override
public func getRuleNames() -> [String] { public func getRuleNames() -> [String] {
return ruleNames return ruleNames

View File

@ -32,10 +32,6 @@ public class ParserInterpreter: Parser {
internal final var sharedContextCache: PredictionContextCache = internal final var sharedContextCache: PredictionContextCache =
PredictionContextCache() PredictionContextCache()
///
/// /@Deprecated
///
internal final var tokenNames: [String]
internal final var ruleNames: [String] internal final var ruleNames: [String]
private final var vocabulary: Vocabulary private final var vocabulary: Vocabulary
@ -64,7 +60,6 @@ public class ParserInterpreter: Parser {
self.grammarFileName = old.grammarFileName self.grammarFileName = old.grammarFileName
self.statesNeedingLeftRecursionContext = old.statesNeedingLeftRecursionContext self.statesNeedingLeftRecursionContext = old.statesNeedingLeftRecursionContext
self.decisionToDFA = old.decisionToDFA self.decisionToDFA = old.decisionToDFA
self.tokenNames = old.tokenNames
self.ruleNames = old.ruleNames self.ruleNames = old.ruleNames
self.vocabulary = old.vocabulary self.vocabulary = old.vocabulary
try super.init(old.getTokenStream()!) try super.init(old.getTokenStream()!)
@ -73,26 +68,11 @@ public class ParserInterpreter: Parser {
sharedContextCache)) sharedContextCache))
} }
///
/// Use _#ParserInterpreter(String, org.antlr.v4.runtime.Vocabulary, java.util.Collection, org.antlr.v4.runtime.atn.ATN, org.antlr.v4.runtime.TokenStream)_ instead.
///
//@Deprecated
public convenience init(_ grammarFileName: String, _ tokenNames: Array<String?>?,
_ ruleNames: Array<String>, _ atn: ATN, _ input: TokenStream) throws {
try self.init(grammarFileName, Vocabulary.fromTokenNames(tokenNames), ruleNames, atn, input)
}
public init(_ grammarFileName: String, _ vocabulary: Vocabulary, public init(_ grammarFileName: String, _ vocabulary: Vocabulary,
_ ruleNames: Array<String>, _ atn: ATN, _ input: TokenStream) throws { _ ruleNames: Array<String>, _ atn: ATN, _ input: TokenStream) throws {
self.grammarFileName = grammarFileName self.grammarFileName = grammarFileName
self.atn = atn self.atn = atn
self.tokenNames = [String]()// new String[atn.maxTokenType];
let length = tokenNames.count
for i in 0..<length {
tokenNames[i] = vocabulary.getDisplayName(i)
}
self.ruleNames = ruleNames self.ruleNames = ruleNames
self.vocabulary = vocabulary self.vocabulary = vocabulary
self.decisionToDFA = [DFA]() //new DFA[atn.getNumberOfDecisions()]; self.decisionToDFA = [DFA]() //new DFA[atn.getNumberOfDecisions()];
@ -123,14 +103,6 @@ public class ParserInterpreter: Parser {
return atn return atn
} }
// override
///
/// /@Deprecated
///
public func getTokenNames() -> [String] {
return tokenNames
}
override override
public func getVocabulary() -> Vocabulary { public func getVocabulary() -> Vocabulary {
return vocabulary return vocabulary

View File

@ -28,20 +28,6 @@ open class Recognizer<ATNInterpreter:ATNSimulator> {
/// ///
private let ruleIndexMapCacheMutex = Mutex() private let ruleIndexMapCacheMutex = Mutex()
/// Used to print out token names like ID during debugging and
/// error reporting. The generated parsers implement a method
/// that overrides this to point to their String[] tokenNames.
///
/// Use _#getVocabulary()_ instead.
///
///
/// /@Deprecated
///
open func getTokenNames() -> [String?]? {
RuntimeException(#function + " must be overridden")
return []
}
open func getRuleNames() -> [String] { open func getRuleNames() -> [String] {
RuntimeException(#function + " must be overridden") RuntimeException(#function + " must be overridden")
return [] return []
@ -54,7 +40,7 @@ open class Recognizer<ATNInterpreter:ATNSimulator> {
/// vocabulary used by the grammar. /// vocabulary used by the grammar.
/// ///
open func getVocabulary() -> Vocabulary { open func getVocabulary() -> Vocabulary {
return Vocabulary.fromTokenNames(getTokenNames()) fatalError(#function + " must be overridden")
} }
/// ///
@ -176,43 +162,6 @@ open class Recognizer<ATNInterpreter:ATNSimulator> {
return "line \(line):\(charPositionInLine)" return "line \(line):\(charPositionInLine)"
} }
/// How should a token be displayed in an error message? The default
/// is to display just the text, but during development you might
/// want to have a lot of information spit out. Override in that case
/// to use t.toString() (which, for CommonToken, dumps everything about
/// the token). This is better than forcing you to override a method in
/// your token objects because you don't have to go modify your lexer
/// so that it creates a new Java type.
///
/// This method is not called by the ANTLR 4 Runtime. Specific
/// implementations of _org.antlr.v4.runtime.ANTLRErrorStrategy_ may provide a similar
/// feature when necessary. For example, see
/// _org.antlr.v4.runtime.DefaultErrorStrategy#getTokenErrorDisplay_.
///
///
/// /@Deprecated
///
open func getTokenErrorDisplay(_ t: Token?) -> String {
guard let t = t else {
return "<no token>"
}
var s: String
if let text = t.getText() {
s = text
} else {
if t.getType() == CommonToken.EOF {
s = "<EOF>"
} else {
s = "<\(t.getType())>"
}
}
s = s.replacingOccurrences(of: "\n", with: "\\n")
s = s.replacingOccurrences(of: "\r", with: "\\r")
s = s.replacingOccurrences(of: "\t", with: "\\t")
return "\(s)"
}
open func addErrorListener(_ listener: ANTLRErrorListener) { open func addErrorListener(_ listener: ANTLRErrorListener) {
_listeners.append(listener) _listeners.append(listener)
} }

View File

@ -170,17 +170,6 @@ public class DFA: CustomStringConvertible {
return description return description
} }
///
/// - Use _#toString(org.antlr.v4.runtime.Vocabulary)_ instead.
///
public func toString(_ tokenNames: [String?]?) -> String {
if s0 == nil {
return ""
}
let serializer: DFASerializer = DFASerializer(self, tokenNames)
return serializer.toString()
}
public func toString(_ vocabulary: Vocabulary) -> String { public func toString(_ vocabulary: Vocabulary) -> String {
if s0 == nil { if s0 == nil {
return "" return ""

View File

@ -13,14 +13,6 @@ public class DFASerializer: CustomStringConvertible {
private let dfa: DFA private let dfa: DFA
private let vocabulary: Vocabulary private let vocabulary: Vocabulary
///
/// - Use _#DFASerializer(org.antlr.v4.runtime.dfa.DFA, org.antlr.v4.runtime.Vocabulary)_ instead.
///
//@Deprecated
public convenience init(_ dfa: DFA, _ tokenNames: [String?]?) {
self.init(dfa, Vocabulary.fromTokenNames(tokenNames))
}
public init(_ dfa: DFA, _ vocabulary: Vocabulary) { public init(_ dfa: DFA, _ vocabulary: Vocabulary) {
self.dfa = dfa self.dfa = dfa
self.vocabulary = vocabulary self.vocabulary = vocabulary

View File

@ -595,14 +595,6 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible {
return buf.toString() return buf.toString()
} }
///
/// - Use _#toString(org.antlr.v4.runtime.Vocabulary)_ instead.
/// /@Deprecated
///
public func toString(_ tokenNames: [String?]?) -> String {
return toString(Vocabulary.fromTokenNames(tokenNames))
}
public func toString(_ vocabulary: Vocabulary) -> String { public func toString(_ vocabulary: Vocabulary) -> String {
let buf = StringBuilder() let buf = StringBuilder()
@ -640,15 +632,6 @@ public class IntervalSet: IntSet, Hashable, CustomStringConvertible {
return buf.toString() return buf.toString()
} }
///
/// - Use _#elementName(org.antlr.v4.runtime.Vocabulary, int)_ instead.
/// /@Deprecated
///
internal func elementName(_ tokenNames: [String?]?, _ a: Int) -> String {
return elementName(Vocabulary.fromTokenNames(tokenNames), a)
}
internal func elementName(_ vocabulary: Vocabulary, _ a: Int) -> String { internal func elementName(_ vocabulary: Vocabulary, _ a: Int) -> String {
if a == CommonToken.EOF { if a == CommonToken.EOF {
return "<EOF>" return "<EOF>"

View File

@ -300,32 +300,6 @@ private static let _SYMBOLIC_NAMES: [String?] = [
<symbolicNames:{t | <t>}; null="nil", separator=", ", wrap, anchor> <symbolicNames:{t | <t>}; null="nil", separator=", ", wrap, anchor>
] ]
public static let VOCABULARY = Vocabulary(_LITERAL_NAMES, _SYMBOLIC_NAMES) public static let VOCABULARY = Vocabulary(_LITERAL_NAMES, _SYMBOLIC_NAMES)
/**
* @deprecated Use {@link #VOCABULARY} instead.
*/
//@Deprecated
public let tokenNames: [String?]? = {
let length = _SYMBOLIC_NAMES.count
var tokenNames = [String?](repeating: nil, count: length)
for i in 0..\<length {
var name = VOCABULARY.getLiteralName(i)
if name == nil {
name = VOCABULARY.getSymbolicName(i)
}
if name == nil {
name = "\<INVALID>"
}
tokenNames[i] = name
}
return tokenNames
}()
override
<!//@Deprecated!>
open func getTokenNames() -> [String?]? {
return tokenNames
}
>> >>
dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= << dumpActions(recog, argFuncs, actionFuncs, sempredFuncs) ::= <<