xref: /plugin/combo/vendor/antlr/antlr4-php-runtime/src/TokenSource.php (revision 37748cd8654635afbeca80942126742f0f4cc346)
1*37748cd8SNickeau<?php
2*37748cd8SNickeau
3*37748cd8SNickeaudeclare(strict_types=1);
4*37748cd8SNickeau
5*37748cd8SNickeaunamespace Antlr\Antlr4\Runtime;
6*37748cd8SNickeau
7*37748cd8SNickeau/**
8*37748cd8SNickeau * A source of tokens must provide a sequence of tokens via {@see TokenSource::nextToken()}
9*37748cd8SNickeau * and also must reveal it's source of characters; {@see CommonToken}'s text
10*37748cd8SNickeau * is computed from a {@see CharStream}; it only store indices into
11*37748cd8SNickeau * the char stream.
12*37748cd8SNickeau *
13*37748cd8SNickeau * Errors from the lexer are never passed to the parser. Either you want to keep
14*37748cd8SNickeau * going or you do not upon token recognition error. If you do not want
15*37748cd8SNickeau * to continue lexing then you do not want to continue parsing. Just throw
16*37748cd8SNickeau * an exception not under {@see RecognitionException} and PHP will naturally
17*37748cd8SNickeau * toss you all the way out of the recognizers. If you want to continue lexing
18*37748cd8SNickeau * then you should not throw an exception to the parser--it has already requested
19*37748cd8SNickeau * a token. Keep lexing until you get a valid one. Just report errors and
20*37748cd8SNickeau * keep going, looking for a valid token.
21*37748cd8SNickeau */
22*37748cd8SNickeauinterface TokenSource
23*37748cd8SNickeau{
24*37748cd8SNickeau    /**
25*37748cd8SNickeau     * Return a {@see Token} object from your input stream (usually a
26*37748cd8SNickeau     * {@see CharStream}). Do not fail/return upon lexing error; keep chewing
27*37748cd8SNickeau     * on the characters until you get a good one; errors are not passed through
28*37748cd8SNickeau     * to the parser.
29*37748cd8SNickeau     */
30*37748cd8SNickeau    public function nextToken() : ?Token;
31*37748cd8SNickeau
32*37748cd8SNickeau    /**
33*37748cd8SNickeau     * Get the line number for the current position in the input stream.
34*37748cd8SNickeau     * The first line in the input is line 1.
35*37748cd8SNickeau     *
36*37748cd8SNickeau     * @return int The line number for the current position in the input stream,
37*37748cd8SNickeau     *             or 0 if the current token source does not track line numbers.
38*37748cd8SNickeau     */
39*37748cd8SNickeau    public function getLine() : int;
40*37748cd8SNickeau
41*37748cd8SNickeau    /**
42*37748cd8SNickeau     * Get the index into the current line for the current position in
43*37748cd8SNickeau     * the input stream. The first character on a line has position 0.
44*37748cd8SNickeau     *
45*37748cd8SNickeau     * @return int The line number for the current position in the input stream,
46*37748cd8SNickeau     *             or -1 if the current token source does not track
47*37748cd8SNickeau     *             character positions.
48*37748cd8SNickeau     */
49*37748cd8SNickeau    public function getCharPositionInLine() : int;
50*37748cd8SNickeau
51*37748cd8SNickeau    /**
52*37748cd8SNickeau     * Get the {@see CharStream} from which this token source is currently
53*37748cd8SNickeau     * providing tokens.
54*37748cd8SNickeau     *
55*37748cd8SNickeau     * @return CharStream The {@see CharStream} associated with the current
56*37748cd8SNickeau     *                    position in the input, or `null` if no input stream
57*37748cd8SNickeau     *                    is available for the token source.
58*37748cd8SNickeau     */
59*37748cd8SNickeau    public function getInputStream() : ?IntStream;
60*37748cd8SNickeau
61*37748cd8SNickeau    /**
62*37748cd8SNickeau     * Gets the name of the underlying input source. This method returns
63*37748cd8SNickeau     * a non-null, non-empty string. If such a name is not known, this method
64*37748cd8SNickeau     * returns {@see IntStream::UNKNOWN_SOURCE_NAME}.
65*37748cd8SNickeau     */
66*37748cd8SNickeau    public function getSourceName() : string;
67*37748cd8SNickeau
68*37748cd8SNickeau    /**
69*37748cd8SNickeau     * Set the {@see TokenFactory} this token source should use for creating
70*37748cd8SNickeau     * {@see Token} objects from the input.
71*37748cd8SNickeau     *
72*37748cd8SNickeau     * @param TokenFactory $factory The {@see TokenFactory} to use
73*37748cd8SNickeau     *                              for creating tokens.
74*37748cd8SNickeau     */
75*37748cd8SNickeau    public function setTokenFactory(TokenFactory $factory) : void;
76*37748cd8SNickeau
77*37748cd8SNickeau    /**
78*37748cd8SNickeau     * Gets the {@see TokenFactory} this token source is currently using
79*37748cd8SNickeau     * f objects from the input.
80*37748cd8SNickeau     *
81*37748cd8SNickeau     * @return TokenFactory The {@see TokenFactory} currently used
82*37748cd8SNickeau     *                      by this token source.
83*37748cd8SNickeau     */
84*37748cd8SNickeau    public function getTokenFactory() : TokenFactory;
85*37748cd8SNickeau}
86