1 /* Copyright (c) 2012-2017 The ANTLR Project. All rights reserved. 2 * Use of this file is governed by the BSD 3-clause license that 3 * can be found in the LICENSE.txt file in the project root. 4 */ 5 6 7 /// 8 /// A source of tokens must provide a sequence of tokens via _#nextToken()_ 9 /// and also must reveal it's source of characters; _org.antlr.v4.runtime.CommonToken_'s text is 10 /// computed from a _org.antlr.v4.runtime.CharStream_; it only store indices into the char 11 /// stream. 12 /// 13 /// Errors from the lexer are never passed to the parser. Either you want to keep 14 /// going or you do not upon token recognition error. If you do not want to 15 /// continue lexing then you do not want to continue parsing. Just throw an 16 /// exception not under _org.antlr.v4.runtime.RecognitionException_ and Java will naturally toss 17 /// you all the way out of the recognizers. If you want to continue lexing then 18 /// you should not throw an exception to the parser--it has already requested a 19 /// token. Keep lexing until you get a valid one. Just report errors and keep 20 /// going, looking for a valid token. 21 /// 22 23 public protocol TokenSource: class { 24 /// 25 /// Return a _org.antlr.v4.runtime.Token_ object from your input stream (usually a 26 /// _org.antlr.v4.runtime.CharStream_). Do not fail/return upon lexing error; keep chewing 27 /// on the characters until you get a good one; errors are not passed through 28 /// to the parser. 29 /// nextTokennull30 func nextToken() throws -> Token 31 32 /// 33 /// Get the line number for the current position in the input stream. The 34 /// first line in the input is line 1. 35 /// 36 /// - Returns: The line number for the current position in the input stream, or 37 /// 0 if the current token source does not track line numbers. 38 /// 39 func getLine() -> Int 40 41 /// 42 /// Get the index into the current line for the current position in the input 43 /// stream. The first character on a line has position 0. 44 /// 45 /// - Returns: The line number for the current position in the input stream, or 46 /// -1 if the current token source does not track character positions. 47 /// 48 func getCharPositionInLine() -> Int 49 50 /// 51 /// Get the _org.antlr.v4.runtime.CharStream_ from which this token source is currently 52 /// providing tokens. 53 /// 54 /// - Returns: The _org.antlr.v4.runtime.CharStream_ associated with the current position in 55 /// the input, or `null` if no input stream is available for the token 56 /// source. 57 /// 58 func getInputStream() -> CharStream? 59 60 /// 61 /// Gets the name of the underlying input source. This method returns a 62 /// non-null, non-empty string. If such a name is not known, this method 63 /// returns _org.antlr.v4.runtime.IntStream#UNKNOWN_SOURCE_NAME_. 64 /// 65 func getSourceName() -> String 66 67 /// 68 /// Set the _org.antlr.v4.runtime.TokenFactory_ this token source should use for creating 69 /// _org.antlr.v4.runtime.Token_ objects from the input. 70 /// 71 /// - Parameter factory: The _org.antlr.v4.runtime.TokenFactory_ to use for creating tokens. 72 /// 73 func setTokenFactory(_ factory: TokenFactory) 74 75 /// 76 /// Gets the _org.antlr.v4.runtime.TokenFactory_ this token source is currently using for 77 /// creating _org.antlr.v4.runtime.Token_ objects from the input. 78 /// 79 /// - Returns: The _org.antlr.v4.runtime.TokenFactory_ currently used by this token source. 80 /// 81 func getTokenFactory() -> TokenFactory 82 } 83