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 | */
|
5 | import { Interval } from "./misc/Interval";
|
6 | import { RuleContext } from "./RuleContext";
|
7 | import { Token } from "./Token";
|
8 | import { TokenSource } from "./TokenSource";
|
9 | import { TokenStream } from "./TokenStream";
|
10 | /**
|
11 | * This implementation of {@link TokenStream} loads tokens from a
|
12 | * {@link TokenSource} on-demand, and places the tokens in a buffer to provide
|
13 | * access to any previous token by index.
|
14 | *
|
15 | * This token stream ignores the value of {@link Token#getChannel}. If your
|
16 | * parser requires the token stream filter tokens to only those on a particular
|
17 | * channel, such as {@link Token#DEFAULT_CHANNEL} or
|
18 | * {@link Token#HIDDEN_CHANNEL}, use a filtering token stream such a
|
19 | * {@link CommonTokenStream}.
|
20 | */
|
21 | export declare class BufferedTokenStream implements TokenStream {
|
22 | /**
|
23 | * The {@link TokenSource} from which tokens for this stream are fetched.
|
24 | */
|
25 | private _tokenSource;
|
26 | /**
|
27 | * A collection of all tokens fetched from the token source. The list is
|
28 | * considered a complete view of the input once {@link #fetchedEOF} is set
|
29 | * to `true`.
|
30 | */
|
31 | protected tokens: Token[];
|
32 | /**
|
33 | * The index into {@link #tokens} of the current token (next token to
|
34 | * {@link #consume}). {@link #tokens}`[`{@link #p}`]` should be
|
35 | * {@link #LT LT(1)}.
|
36 | *
|
37 | * This field is set to -1 when the stream is first constructed or when
|
38 | * {@link #setTokenSource} is called, indicating that the first token has
|
39 | * not yet been fetched from the token source. For additional information,
|
40 | * see the documentation of {@link IntStream} for a description of
|
41 | * Initializing Methods.
|
42 | */
|
43 | protected p: number;
|
44 | /**
|
45 | * Indicates whether the {@link Token#EOF} token has been fetched from
|
46 | * {@link #tokenSource} and added to {@link #tokens}. This field improves
|
47 | * performance for the following cases:
|
48 | *
|
49 | * * {@link #consume}: The lookahead check in {@link #consume} to prevent
|
50 | * consuming the EOF symbol is optimized by checking the values of
|
51 | * {@link #fetchedEOF} and {@link #p} instead of calling {@link #LA}.
|
52 | * * {@link #fetch}: The check to prevent adding multiple EOF symbols into
|
53 | * {@link #tokens} is trivial with this field.
|
54 | */
|
55 | protected fetchedEOF: boolean;
|
56 | constructor(tokenSource: TokenSource);
|
57 | get tokenSource(): TokenSource;
|
58 | /** Reset this token stream by setting its token source. */
|
59 | set tokenSource(tokenSource: TokenSource);
|
60 | get index(): number;
|
61 | mark(): number;
|
62 | release(marker: number): void;
|
63 | seek(index: number): void;
|
64 | get size(): number;
|
65 | consume(): void;
|
66 | /** Make sure index `i` in tokens has a token.
|
67 | *
|
68 | * @returns `true` if a token is located at index `i`, otherwise
|
69 | * `false`.
|
70 | * @see #get(int i)
|
71 | */
|
72 | protected sync(i: number): boolean;
|
73 | /** Add `n` elements to buffer.
|
74 | *
|
75 | * @returns The actual number of elements added to the buffer.
|
76 | */
|
77 | protected fetch(n: number): number;
|
78 | get(i: number): Token;
|
79 | /** Get all tokens from start..stop inclusively. */
|
80 | getRange(start: number, stop: number): Token[];
|
81 | LA(i: number): number;
|
82 | protected tryLB(k: number): Token | undefined;
|
83 | LT(k: number): Token;
|
84 | tryLT(k: number): Token | undefined;
|
85 | /**
|
86 | * Allowed derived classes to modify the behavior of operations which change
|
87 | * the current stream position by adjusting the target token index of a seek
|
88 | * operation. The default implementation simply returns `i`. If an
|
89 | * exception is thrown in this method, the current stream index should not be
|
90 | * changed.
|
91 | *
|
92 | * For example, {this method to ensure that
CommonTokenStream} overrides |
93 | * the seek target is always an on-channel token.
|
94 | *
|
95 | * i The target token index.
|
96 | * The adjusted target token index.
|
97 | */
|
98 | protected adjustSeekIndex(i: number): number;
|
99 | protected lazyInit(): void;
|
100 | protected setup(): void;
|
101 | getTokens(): Token[];
|
102 | getTokens(start: number, stop: number): Token[];
|
103 | getTokens(start: number, stop: number, types: Set<number>): Token[];
|
104 | getTokens(start: number, stop: number, ttype: number): Token[];
|
105 | /**
|
106 | * Given a starting index, return the index of the next token on channel.
|
107 | * Return `i` if `tokens[i]` is on channel. Return the index of
|
108 | * the EOF token if there are no tokens on channel between `i` and
|
109 | * EOF.
|
110 | */
|
111 | protected nextTokenOnChannel(i: number, channel: number): number;
|
112 | /**
|
113 | * Given a starting index, return the index of the previous token on
|
114 | * channel. Return `i` if `tokens[i]` is on channel. Return -1
|
115 | * if there are no tokens on channel between `i` and 0.
|
116 | *
|
117 | * If `i` specifies an index at or after the EOF token, the EOF token
|
118 | * index is returned. This is due to the fact that the EOF token is treated
|
119 | * as though it were on every channel.
|
120 | */
|
121 | protected previousTokenOnChannel(i: number, channel: number): number;
|
122 | /** Collect all tokens on specified channel to the right of
|
123 | * the current token up until we see a token on {@link Lexer#DEFAULT_TOKEN_CHANNEL} or
|
124 | * EOF. If `channel` is `-1`, find any non default channel token.
|
125 | */
|
126 | getHiddenTokensToRight(tokenIndex: number, channel?: number): Token[];
|
127 | /** Collect all tokens on specified channel to the left of
|
128 | * the current token up until we see a token on {@link Lexer#DEFAULT_TOKEN_CHANNEL}.
|
129 | * If `channel` is `-1`, find any non default channel token.
|
130 | */
|
131 | getHiddenTokensToLeft(tokenIndex: number, channel?: number): Token[];
|
132 | protected filterForChannel(from: number, to: number, channel: number): Token[];
|
133 | get sourceName(): string;
|
134 | /** Get the text of all tokens in this buffer. */
|
135 | getText(): string;
|
136 | getText(interval: Interval): string;
|
137 | getText(context: RuleContext): string;
|
138 | getTextFromRange(start: any, stop: any): string;
|
139 | /** Get all tokens from lexer until EOF. */
|
140 | fill(): void;
|
141 | private isWritableToken;
|
142 | private isToken;
|
143 | }
|