UNPKG

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