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.
8 #include "TokenFactory.h"
13 /// A source of tokens must provide a sequence of tokens via <seealso cref="#nextToken()"/>
14 /// and also must reveal it's source of characters; <seealso cref="CommonToken"/>'s text is
15 /// computed from a <seealso cref="CharStream"/>; it only store indices into the char
18 /// Errors from the lexer are never passed to the parser. Either you want to keep
19 /// going or you do not upon token recognition error. If you do not want to
20 /// continue lexing then you do not want to continue parsing. Just throw an
21 /// exception not under <seealso cref="RecognitionException"/> and Java will naturally toss
22 /// you all the way out of the recognizers. If you want to continue lexing then
23 /// you should not throw an exception to the parser--it has already requested a
24 /// token. Keep lexing until you get a valid one. Just report errors and keep
25 /// going, looking for a valid token.
27 class ANTLR4CPP_PUBLIC TokenSource {
29 virtual ~TokenSource();
31 /// Return a <seealso cref="Token"/> object from your input stream (usually a
32 /// <seealso cref="CharStream"/>). Do not fail/return upon lexing error; keep chewing
33 /// on the characters until you get a good one; errors are not passed through
35 virtual std::unique_ptr<Token> nextToken() = 0;
38 /// Get the line number for the current position in the input stream. The
39 /// first line in the input is line 1.
41 /// <returns> The line number for the current position in the input stream, or
42 /// 0 if the current token source does not track line numbers. </returns>
43 virtual size_t getLine() const = 0;
46 /// Get the index into the current line for the current position in the input
47 /// stream. The first character on a line has position 0.
49 /// <returns> The line number for the current position in the input stream, or
50 /// (sze_t)-1 if the current token source does not track character positions. </returns>
51 virtual size_t getCharPositionInLine() = 0;
54 /// Get the <seealso cref="CharStream"/> from which this token source is currently
57 /// <returns> The <seealso cref="CharStream"/> associated with the current position in
58 /// the input, or {@code null} if no input stream is available for the token
59 /// source. </returns>
60 virtual CharStream* getInputStream() = 0;
63 /// Gets the name of the underlying input source. This method returns a
64 /// non-null, non-empty string. If such a name is not known, this method
65 /// returns <seealso cref="IntStream#UNKNOWN_SOURCE_NAME"/>.
67 virtual std::string getSourceName() = 0;
70 /// Set the <seealso cref="TokenFactory"/> this token source should use for creating
71 /// <seealso cref="Token"/> objects from the input.
73 /// <param name="factory"> The <seealso cref="TokenFactory"/> to use for creating tokens. </param>
75 void setTokenFactory(TokenFactory<T1> * /*factory*/) {}
78 /// Gets the <seealso cref="TokenFactory"/> this token source is currently using for
79 /// creating <seealso cref="Token"/> objects from the input.
81 /// <returns> The <seealso cref="TokenFactory"/> currently used by this token source. </returns>
82 virtual TokenFactory<CommonToken>* getTokenFactory() = 0;