UNPKG

6.17 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 */
5import { Interval } from "./misc/Interval";
6import { IntStream } from "./IntStream";
7import { RuleContext } from "./RuleContext";
8import { Token } from "./Token";
9import { TokenSource } from "./TokenSource";
10/**
11 * An {@link IntStream} whose symbols are {@link Token} instances.
12 */
13export interface TokenStream extends IntStream {
14 /**
15 * Get the `Token` instance associated with the value returned by `LA(k)`. This method has the same pre- and
16 * post-conditions as `IntStream.LA`. In addition, when the preconditions of this method are met, the return value
17 * is non-undefined and the value of `LT(k).type === LA(k)`.
18 *
19 * A `RangeError` is thrown if `k<0` and fewer than `-k` calls to `consume()` have occurred from the beginning of
20 * the stream before calling this method.
21 *
22 * See `IntStream.LA`
23 */
24 LT(k: number): Token;
25 /**
26 * Get the `Token` instance associated with the value returned by `LA(k)`. This method has the same pre- and
27 * post-conditions as `IntStream.LA`. In addition, when the preconditions of this method are met, the return value
28 * is non-undefined and the value of `tryLT(k).type === LA(k)`.
29 *
30 * The return value is `undefined` if `k<0` and fewer than `-k` calls to `consume()` have occurred from the
31 * beginning of the stream before calling this method.
32 *
33 * See `IntStream.LA`
34 */
35 tryLT(k: number): Token | undefined;
36 /**
37 * Gets the {@link Token} at the specified `index` in the stream. When
38 * the preconditions of this method are met, the return value is non-undefined.
39 *
40 * The preconditions for this method are the same as the preconditions of
41 * {@link IntStream#seek}. If the behavior of `seek(index)` is
42 * unspecified for the current state and given `index`, then the
43 * behavior of this method is also unspecified.
44 *
45 * The symbol referred to by `index` differs from `seek()` only
46 * in the case of filtering streams where `index` lies before the end
47 * of the stream. Unlike `seek()`, this method does not adjust
48 * `index` to point to a non-ignored symbol.
49 *
50 * @throws IllegalArgumentException if {code index} is less than 0
51 * @throws UnsupportedOperationException if the stream does not support
52 * retrieving the token at the specified index
53 */
54 get(i: number): Token;
55 /**
56 * Gets the underlying {@link TokenSource} which provides tokens for this
57 * stream.
58 */
59 readonly tokenSource: TokenSource;
60 /**
61 * Return the text of all tokens within the specified `interval`. This
62 * method behaves like the following code (including potential exceptions
63 * for violating preconditions of {@link #get}, but may be optimized by the
64 * specific implementation.
65 *
66 * ```
67 * TokenStream stream = ...;
68 * String text = "";
69 * for (int i = interval.a; i <= interval.b; i++) {
70 * text += stream.get(i).text;
71 * }
72 * ```
73 *
74 * @param interval The interval of tokens within this stream to get text
75 * for.
76 * @returns The text of all tokens within the specified interval in this
77 * stream.
78 *
79 * @throws NullPointerException if `interval` is `undefined`
80 */
81 getText(/*@NotNull*/ interval: Interval): string;
82 /**
83 * Return the text of all tokens in the stream. This method behaves like the
84 * following code, including potential exceptions from the calls to
85 * {@link IntStream#size} and {@link #getText(Interval)}, but may be
86 * optimized by the specific implementation.
87 *
88 * ```
89 * TokenStream stream = ...;
90 * String text = stream.getText(new Interval(0, stream.size));
91 * ```
92 *
93 * @returns The text of all tokens in the stream.
94 */
95 getText(): string;
96 /**
97 * Return the text of all tokens in the source interval of the specified
98 * context. This method behaves like the following code, including potential
99 * exceptions from the call to {@link #getText(Interval)}, but may be
100 * optimized by the specific implementation.
101 *
102 * If `ctx.sourceInterval` does not return a valid interval of
103 * tokens provided by this stream, the behavior is unspecified.
104 *
105 * ```
106 * TokenStream stream = ...;
107 * String text = stream.getText(ctx.sourceInterval);
108 * ```
109 *
110 * @param ctx The context providing the source interval of tokens to get
111 * text for.
112 * @returns The text of all tokens within the source interval of `ctx`.
113 */
114 getText(/*@NotNull*/ ctx: RuleContext): string;
115 /**
116 * Return the text of all tokens in this stream between `start` and
117 * `stop` (inclusive).
118 *
119 * If the specified `start` or `stop` token was not provided by
120 * this stream, or if the `stop` occurred before the `start`}
121 * token, the behavior is unspecified.
122 *
123 * For streams which ensure that the `Token.tokenIndex` method is
124 * accurate for all of its provided tokens, this method behaves like the
125 * following code. Other streams may implement this method in other ways
126 * provided the behavior is consistent with this at a high level.
127 *
128 * ```
129 * TokenStream stream = ...;
130 * String text = "";
131 * for (int i = start.tokenIndex; i <= stop.tokenIndex; i++) {
132 * text += stream.get(i).text;
133 * }
134 * ```
135 *
136 * @param start The first token in the interval to get text for.
137 * @param stop The last token in the interval to get text for (inclusive).
138 * @returns The text of all tokens lying between the specified `start`
139 * and `stop` tokens.
140 *
141 * @throws UnsupportedOperationException if this stream does not support
142 * this method for the specified tokens
143 */
144 getTextFromRange(start: any, stop: any): string;
145}