xref: /plugin/combo/vendor/antlr/antlr4-php-runtime/src/IntStream.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 simple stream of symbols whose values are represented as integers. This
9*37748cd8SNickeau * interface provides marked ranges with support for a minimum level of
10*37748cd8SNickeau * buffering necessary to implement arbitrary lookahead during prediction.
11*37748cd8SNickeau * For more information on marked ranges, see {@see IntStream::mark()}.
12*37748cd8SNickeau *
13*37748cd8SNickeau * Initializing Methods: Some methods in this interface have unspecified
14*37748cd8SNickeau * behavior if no call to an initializing method has occurred after the stream
15*37748cd8SNickeau * was constructed. The following is a list of initializing methods:
16*37748cd8SNickeau *
17*37748cd8SNickeau * @see IntStream::LA()
18*37748cd8SNickeau * @see IntStream::consume()
19*37748cd8SNickeau * @see IntStream::size()
20*37748cd8SNickeau */
21*37748cd8SNickeauinterface IntStream
22*37748cd8SNickeau{
23*37748cd8SNickeau    /**
24*37748cd8SNickeau     * The value returned by {@see IntStream::LA()} when the end of the stream is
25*37748cd8SNickeau     * reached.
26*37748cd8SNickeau     */
27*37748cd8SNickeau    public const EOF = -1;
28*37748cd8SNickeau
29*37748cd8SNickeau    /**
30*37748cd8SNickeau     * During lookahead operations, this "token" signifies we hit rule end ATN state
31*37748cd8SNickeau     * and did not follow it despite needing to.
32*37748cd8SNickeau     */
33*37748cd8SNickeau    public const EPSILON = -2;
34*37748cd8SNickeau
35*37748cd8SNickeau    /**
36*37748cd8SNickeau     * The value returned by {@see IntStream::getSourceName()} when the actual
37*37748cd8SNickeau     * name of the underlying source is not known.
38*37748cd8SNickeau     */
39*37748cd8SNickeau    public const UNKNOWN_SOURCE_NAME = '<unknown>';
40*37748cd8SNickeau
41*37748cd8SNickeau    /**
42*37748cd8SNickeau     * Consumes the current symbol in the stream. This method has the following
43*37748cd8SNickeau     * effects:
44*37748cd8SNickeau     *
45*37748cd8SNickeau     * - Forward movement: The value of {@see IntStream::index()} before calling
46*37748cd8SNickeau     *    this method is less than the value of `index` after calling this method.
47*37748cd8SNickeau     * - Ordered lookahead: The value of `LA(1)` before calling this method
48*37748cd8SNickeau     *    becomes the value of `LA(-1)` after calling this method.
49*37748cd8SNickeau     *
50*37748cd8SNickeau     * Note that calling this method does not guarantee that `index()` is
51*37748cd8SNickeau     * incremented by exactly 1, as that would preclude the ability to implement
52*37748cd8SNickeau     * filtering streams (e.g. {@see CommonTokenStream} which distinguishes
53*37748cd8SNickeau     * between "on-channel" and "off-channel" tokens).
54*37748cd8SNickeau     */
55*37748cd8SNickeau    public function consume() : void;
56*37748cd8SNickeau
57*37748cd8SNickeau    /**
58*37748cd8SNickeau     * Gets the value of the symbol at offset `i` from the current
59*37748cd8SNickeau     * position. When `i === 1`, this method returns the value of the current
60*37748cd8SNickeau     * symbol in the stream (which is the next symbol to be consumed). When
61*37748cd8SNickeau     * `i === -1`, this method returns the value of the previously read symbol
62*37748cd8SNickeau     * in the stream. It is not valid to call this method with `i === 0`, but
63*37748cd8SNickeau     * the specific behavior is unspecified because this method is frequently
64*37748cd8SNickeau     * called from performance-critical code.
65*37748cd8SNickeau     *
66*37748cd8SNickeau     * This method is guaranteed to succeed if any of the following are true:
67*37748cd8SNickeau     *
68*37748cd8SNickeau     * - `i > 0`
69*37748cd8SNickeau     * - `i === -1` and {@see IntStream::index index()} returns a value greater
70*37748cd8SNickeau     *    than the value of `index` after the stream was constructed and `LA(1)
71*37748cd8SNickeau     *    was called in that order. Specifying the current `index` relative
72*37748cd8SNickeau     *    to the index after the stream was created allows for filtering
73*37748cd8SNickeau     *    implementations that do not return every symbol from the underlying
74*37748cd8SNickeau     *    source. Specifying the call to `LA(1)` allows for lazily initialized
75*37748cd8SNickeau     *    streams.
76*37748cd8SNickeau     * - `LA(i)` refers to a symbol consumed within a marked region that has
77*37748cd8SNickeau     *    not yet been released.
78*37748cd8SNickeau     *
79*37748cd8SNickeau     * If `i` represents a position at or beyond the end of the stream,
80*37748cd8SNickeau     * this method returns {@see IntStream::EOF()}.
81*37748cd8SNickeau     *
82*37748cd8SNickeau     * The return value is unspecified if `i < 0` and fewer than `-i` calls to
83*37748cd8SNickeau     * {@see IntStream::consume consume()} have occurred from the beginning of
84*37748cd8SNickeau     * the stream before calling this method.
85*37748cd8SNickeau     */
86*37748cd8SNickeau    public function LA(int $i) : int;
87*37748cd8SNickeau
88*37748cd8SNickeau    /**
89*37748cd8SNickeau     * A mark provides a guarantee that {@see IntStream::seek seek()} operations
90*37748cd8SNickeau     * will be valid over a "marked range" extending from the index where `mark`
91*37748cd8SNickeau     * was called to the current {@see IntStream::index index()}. This allows
92*37748cd8SNickeau     * the use of streaming input sources by specifying the minimum buffering
93*37748cd8SNickeau     * requirements to support arbitrary lookahead during prediction.
94*37748cd8SNickeau     *
95*37748cd8SNickeau     * The returned mark is an opaque handle (type `int`) which is passed to
96*37748cd8SNickeau     * {@see IntStream::release release()} when the guarantees provided by
97*37748cd8SNickeau     * the marked range are no longer necessary. When calls to `mark`/`release`
98*37748cd8SNickeau     * are nested, the marks must be released in reverse order of which they
99*37748cd8SNickeau     * were obtained. Since marked regions are used during performance-critical
100*37748cd8SNickeau     * sections of prediction, the specific behavior of invalid usage is unspecified
101*37748cd8SNickeau     * (i.e. a mark is not released, or a mark is released twice, or marks are
102*37748cd8SNickeau     * not released in reverse order from which they were created).
103*37748cd8SNickeau     *
104*37748cd8SNickeau     * The behavior of this method is unspecified if no call to an
105*37748cd8SNickeau     * {@see IntStream::initializing()} method has occurred after this stream was
106*37748cd8SNickeau     * constructed.
107*37748cd8SNickeau     *
108*37748cd8SNickeau     * This method does not change the current position in the input stream.
109*37748cd8SNickeau     *
110*37748cd8SNickeau     * The following example shows the use of {@see IntStream::mark()},
111*37748cd8SNickeau     * {@see IntStream::release(mark)}, {@see IntStream::index()}, and
112*37748cd8SNickeau     * {@see IntStream::seek(index)} as part of an operation to safely work within a
113*37748cd8SNickeau     * marked region, then restore the stream position to its original value and
114*37748cd8SNickeau     * release the mark.
115*37748cd8SNickeau     *
116*37748cd8SNickeau     *     $stream = ...;
117*37748cd8SNickeau     *     $index = -1;
118*37748cd8SNickeau     *     $mark = $stream->mark();
119*37748cd8SNickeau     *     try {
120*37748cd8SNickeau     *         $index = $stream->index();
121*37748cd8SNickeau     *         // perform work here...
122*37748cd8SNickeau     *     } finally {
123*37748cd8SNickeau     *         if ($index !== -1) {
124*37748cd8SNickeau     *             $stream->seek($index);
125*37748cd8SNickeau     *         }
126*37748cd8SNickeau     *
127*37748cd8SNickeau     *         $stream->release($mark);
128*37748cd8SNickeau     *     }
129*37748cd8SNickeau     *
130*37748cd8SNickeau     * @return int An opaque marker which should be passed to
131*37748cd8SNickeau     *             {@see IntStream::release()} when the marked range
132*37748cd8SNickeau     *             is no longer required.
133*37748cd8SNickeau     */
134*37748cd8SNickeau    public function mark() : int;
135*37748cd8SNickeau
136*37748cd8SNickeau    /**
137*37748cd8SNickeau     * This method releases a marked range created by a call to
138*37748cd8SNickeau     * {@see IntStream::mark()}. Calls to `release` must appear in the
139*37748cd8SNickeau     * reverse order of the corresponding calls to `mark`. If a mark is
140*37748cd8SNickeau     * released twice, or if marks are not released in reverse order of the
141*37748cd8SNickeau     * corresponding calls to `mark`, the behavior is unspecified.
142*37748cd8SNickeau     *
143*37748cd8SNickeau     * For more information and an example, see {@see IntStream::mark()}.
144*37748cd8SNickeau     *
145*37748cd8SNickeau     * @param int $marker A marker returned by a call to `mark`.
146*37748cd8SNickeau     *
147*37748cd8SNickeau     * @see IntStream::mark
148*37748cd8SNickeau     */
149*37748cd8SNickeau    public function release(int $marker) : void;
150*37748cd8SNickeau
151*37748cd8SNickeau    /**
152*37748cd8SNickeau     * Return the index into the stream of the input symbol referred to by `LA(1)`.
153*37748cd8SNickeau     *
154*37748cd8SNickeau     * The behavior of this method is unspecified if no call to an
155*37748cd8SNickeau     * {@see IntStream::initializing()} has occurred after this stream was
156*37748cd8SNickeau     * constructed.
157*37748cd8SNickeau     */
158*37748cd8SNickeau    public function getIndex() : int;
159*37748cd8SNickeau
160*37748cd8SNickeau    /**
161*37748cd8SNickeau     * Set the input cursor to the position indicated by `index`. If the
162*37748cd8SNickeau     * specified index lies past the end of the stream, the operation behaves as
163*37748cd8SNickeau     * though `index` was the index of the EOF symbol. After this method
164*37748cd8SNickeau     * returns without throwing an exception, then at least one of the following
165*37748cd8SNickeau     * will be true.
166*37748cd8SNickeau     *
167*37748cd8SNickeau     * - {@see IntStream::index index()} will return the index of the first symbol
168*37748cd8SNickeau     *    appearing at or after the specified `index`. Specifically,
169*37748cd8SNickeau     *    implementations which filter their sources should automatically
170*37748cd8SNickeau     *    adjust `index` forward the minimum amount required for the
171*37748cd8SNickeau     *    operation to target a non-ignored symbol.
172*37748cd8SNickeau     * - `LA(1)` returns {@see IntStream::EOF}
173*37748cd8SNickeau     *
174*37748cd8SNickeau     * This operation is guaranteed to not throw an exception if `index` lies
175*37748cd8SNickeau     * within a marked region. For more information on marked regions, see
176*37748cd8SNickeau     * {@see IntStream::mark()}. The behavior of this method is unspecified if
177*37748cd8SNickeau     * no call to an {@see IntStream::initializing()} has occurred after this stream
178*37748cd8SNickeau     * was constructed.
179*37748cd8SNickeau     *
180*37748cd8SNickeau     * @param int $index The absolute index to seek to.
181*37748cd8SNickeau     */
182*37748cd8SNickeau    public function seek(int $index) : void;
183*37748cd8SNickeau
184*37748cd8SNickeau    /**
185*37748cd8SNickeau     * Returns the total number of symbols in the stream, including a single EOF
186*37748cd8SNickeau     * symbol.
187*37748cd8SNickeau     */
188*37748cd8SNickeau    public function getLength() : int;
189*37748cd8SNickeau
190*37748cd8SNickeau    /**
191*37748cd8SNickeau     * Gets the name of the underlying symbol source. This method returns a
192*37748cd8SNickeau     * non-null, non-empty string. If such a name is not known, this method
193*37748cd8SNickeau     * returns {@see IntStream::UNKNOWN_SOURCE_NAME}.
194*37748cd8SNickeau     */
195*37748cd8SNickeau    public function getSourceName() : string;
196*37748cd8SNickeau}
197