xref: /template/strap/vendor/antlr/antlr4-php-runtime/src/TokenStream.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 * An {@see IntStream} whose symbols are {@see Token} instances.
9*37748cd8SNickeau */
10*37748cd8SNickeauinterface TokenStream extends IntStream
11*37748cd8SNickeau{
12*37748cd8SNickeau    /**
13*37748cd8SNickeau     * Get the {@see Token} instance associated with the value returned
14*37748cd8SNickeau     * by {@see TokenStream::LA()}. This method has the same pre- and post-conditions
15*37748cd8SNickeau     * as {@see IntStream::LA()}. In addition, when the preconditions
16*37748cd8SNickeau     * of this method are met, the return value is non-null and the value
17*37748cd8SNickeau     * of `LT($k)->getType() === LA($k)`.
18*37748cd8SNickeau     *
19*37748cd8SNickeau     * @see IntStream::LA()
20*37748cd8SNickeau     */
21*37748cd8SNickeau    public function LT(int $k) : ?Token;
22*37748cd8SNickeau
23*37748cd8SNickeau    /**
24*37748cd8SNickeau     * Gets the {@see Token} at the specified `index` in the stream.
25*37748cd8SNickeau     * When the preconditions of this method are met, the return value is non-null.
26*37748cd8SNickeau     *
27*37748cd8SNickeau     * The preconditions for this method are the same as the preconditions
28*37748cd8SNickeau     * of {@see IntStream::seek()}. If the behavior of {@see TokenStream::seek()}
29*37748cd8SNickeau     * is unspecified for the current state and given `index`, then the behavior
30*37748cd8SNickeau     * of this method is also unspecified.
31*37748cd8SNickeau     *
32*37748cd8SNickeau     * The symbol referred to by `index` differs from {@see TokenStream::seek()} only
33*37748cd8SNickeau     * in the case of filtering streams where `index` lies before the end
34*37748cd8SNickeau     * of the stream. Unlike {@see TokenStream::seek()}, this method does not adjust
35*37748cd8SNickeau     * `index` to point to a non-ignored symbol.
36*37748cd8SNickeau     */
37*37748cd8SNickeau    public function get(int $index) : Token;
38*37748cd8SNickeau
39*37748cd8SNickeau    /**
40*37748cd8SNickeau     * Gets the underlying {@see TokenSource} which provides tokens for this stream.
41*37748cd8SNickeau     */
42*37748cd8SNickeau    public function getTokenSource() : TokenSource;
43*37748cd8SNickeau
44*37748cd8SNickeau    /**
45*37748cd8SNickeau     * Return the text of all tokens within the specified `interval`.
46*37748cd8SNickeau     * This method behaves like the following code (including potential exceptions
47*37748cd8SNickeau     * for violating preconditions of {@see TokenStream::get()}, but may be optimized
48*37748cd8SNickeau     * by the specific implementation.
49*37748cd8SNickeau     *
50*37748cd8SNickeau     *     $stream = ...;
51*37748cd8SNickeau     *     $text = '';
52*37748cd8SNickeau     *     for ($i = $interval->a; $i <= $interval->b; $i++) {
53*37748cd8SNickeau     *         $text += $stream->get($i)->getText();
54*37748cd8SNickeau     *     }
55*37748cd8SNickeau     *
56*37748cd8SNickeau     * @param Interval $interval The interval of tokens within this stream
57*37748cd8SNickeau     *                           to get text for.
58*37748cd8SNickeau     *
59*37748cd8SNickeau     * @return string The text of all tokens within the specified interval
60*37748cd8SNickeau     *                in this stream.
61*37748cd8SNickeau     */
62*37748cd8SNickeau    public function getTextByInterval(Interval $interval) : string;
63*37748cd8SNickeau
64*37748cd8SNickeau    /**
65*37748cd8SNickeau     * Return the text of all tokens in the stream. This method behaves like
66*37748cd8SNickeau     * the following code, including potential exceptions from the calls
67*37748cd8SNickeau     * to {@see IntStream::size()} and {@see TokenStream::getText()}, but may
68*37748cd8SNickeau     * be optimized by the specific implementation.
69*37748cd8SNickeau     *
70*37748cd8SNickeau     *     $stream = ...;
71*37748cd8SNickeau     *     $text = $stream->getText(new Interval(0, $stream->size()));
72*37748cd8SNickeau     *
73*37748cd8SNickeau     * @return string The text of all tokens in the stream.
74*37748cd8SNickeau     */
75*37748cd8SNickeau    public function getText() : string;
76*37748cd8SNickeau
77*37748cd8SNickeau    /**
78*37748cd8SNickeau     * Return the text of all tokens in the source interval of the specified context.
79*37748cd8SNickeau     * This method behaves like the following code, including potential exceptions
80*37748cd8SNickeau     * from the call to {@see TokenStream::getText()}, but may be optimized
81*37748cd8SNickeau     * by the specific implementation.
82*37748cd8SNickeau     *
83*37748cd8SNickeau     * If `ctx.getSourceInterval()` does not return a valid interval of tokens
84*37748cd8SNickeau     * provided by this stream, the behavior is unspecified.
85*37748cd8SNickeau     *
86*37748cd8SNickeau     *     $stream = ...;
87*37748cd8SNickeau     *     $text = $stream->getText($ctx->getSourceInterval());
88*37748cd8SNickeau     *
89*37748cd8SNickeau     * @param RuleContext $context The context providing the source interval
90*37748cd8SNickeau     *                             of tokens to get text for.
91*37748cd8SNickeau     *
92*37748cd8SNickeau     * @return string The text of all tokens within the source interval of `context`.
93*37748cd8SNickeau     */
94*37748cd8SNickeau    public function getTextByContext(RuleContext $context) : string;
95*37748cd8SNickeau
96*37748cd8SNickeau    /**
97*37748cd8SNickeau     * Return the text of all tokens in this stream between `start` and `stop`
98*37748cd8SNickeau     * (inclusive).
99*37748cd8SNickeau     *
100*37748cd8SNickeau     * If the specified `start` or `stop` token was not provided by this stream,
101*37748cd8SNickeau     * or if the `stop` occurred before the `start` token, the behavior
102*37748cd8SNickeau     * is unspecified.
103*37748cd8SNickeau     *
104*37748cd8SNickeau     * For streams which ensure that the {@see Token::getTokenIndex()} method
105*37748cd8SNickeau     * is accurate for all of its provided tokens, this method behaves like
106*37748cd8SNickeau     * the following code. Other streams may implement this method in other ways
107*37748cd8SNickeau     * provided the behavior is consistent with this at a high level.
108*37748cd8SNickeau     *
109*37748cd8SNickeau     *     $stream = ...;
110*37748cd8SNickeau     *     $text = '';
111*37748cd8SNickeau     *     for ($i = $start->getTokenIndex(); $i <= $stop->getTokenIndex(); $i++) {
112*37748cd8SNickeau     *         $text += $stream->get($i)->getText();
113*37748cd8SNickeau     *     }
114*37748cd8SNickeau     *
115*37748cd8SNickeau     * @param Token $start The first token in the interval to get text for.
116*37748cd8SNickeau     * @param Token $stop  The last token in the interval to get text for (inclusive).
117*37748cd8SNickeau     *
118*37748cd8SNickeau     * @return string The text of all tokens lying between the specified
119*37748cd8SNickeau     *                `start` and `stop` tokens.
120*37748cd8SNickeau     */
121*37748cd8SNickeau    public function getTextByTokens(?Token $start, ?Token $stop) : string;
122*37748cd8SNickeau}
123