1 /*
2  * Copyright (c) 2012-2020 The ANTLR Project. All rights reserved.
3  * Use of this file is governed by the BSD 3-clause license that
4  * can be found in the LICENSE.txt file in the project root.
5  */
6 
7 module antlr.v4.runtime.TokenStream;
8 
9 import antlr.v4.runtime.IntStream;
10 import antlr.v4.runtime.Token;
11 import antlr.v4.runtime.TokenSource;
12 import antlr.v4.runtime.RuleContext;
13 import antlr.v4.runtime.misc.Interval;
14 import std.variant;
15 
16 /**
17  * This TokenStream uses the data type Variant for storing text.
18  *
19  * Therefore tabulator based syntax can be used more complex text elements,
20  * i.e. text string in combination with number of tabs.
21  */
22 interface TokenStream : IntStream
23 {
24 
25     /**
26      * Get the {@link Token} instance associated with the value returned by
27      * {@link #LA LA(k)}. This method has the same pre- and post-conditions as
28      * {@link IntStream#LA}. In addition, when the preconditions of this method
29      * are met, the return value is non-null and the value of
30      * {@code LT(k).getType()==LA(k)}.
31      *
32      *  @see IntStream#LA
33      */
34     public Token LT(int k);
35 
36     /**
37      * Gets the {@link Token} at the specified {@code index} in the stream. When
38      * the preconditions of this method are met, the return value is non-null.
39      *
40      * <p>The preconditions for this method are the same as the preconditions of
41      * {@link IntStream#seek}. If the behavior of {@code seek(index)} is
42      * unspecified for the current state and given {@code index}, then the
43      * behavior of this method is also unspecified.</p>
44      *
45      * <p>The symbol referred to by {@code index} differs from {@code seek()} only
46      * in the case of filtering streams where {@code index} lies before the end
47      * of the stream. Unlike {@code seek()}, this method does not adjust
48      * {@code index} to point to a non-ignored symbol.</p>
49      *
50      * Throws:
51      * - IllegalArgumentException if {code index} is less than 0<br>
52      * - UnsupportedOperationException if the stream does not support
53      *  retrieving the token at the specified index
54      */
55     public Token get(size_t index);
56 
57     /**
58      * Gets the underlying {@link TokenSource} which provides tokens for this
59      * stream.
60      */
61     public TokenSource getTokenSource();
62 
63     /**
64      * Return the text of all tokens within the specified {@code interval}. This
65      * method behaves like the following code (including potential exceptions)
66      * for violating preconditions of {@link #get}, but may be optimized by the
67      * specific implementation.
68      *
69      * <pre>
70      * TokenStream stream = ...;
71      * String text = "";
72      * for (int i = interval.a; i &lt;= interval.b; i++) {
73      *   text += stream.get(i).getText();
74      * }
75      * </pre>
76      *
77      *  @param interval The interval of tokens within this stream to get text
78      * for.
79      *  @return The text of all tokens within the specified interval in this
80      * stream.
81      *
82      *  Throws:
83      *  NullPointerException if {@code interval} is {@code null}
84      */
85     public Variant getText(Interval interval);
86 
87     /**
88      * Return the text of all tokens in the stream. This method behaves like the
89      * following code, including potential exceptions from the calls to
90      * {@link IntStream#size} and {@link #getText(Interval)}, but may be
91      * optimized by the specific implementation.
92      *
93      * <pre>
94      * TokenStream stream = ...;
95      * String text = stream.getText(new Interval(0, stream.size()));
96      * </pre>
97      *
98      *  @return The text of all tokens in the stream.
99      */
100     public Variant getText();
101 
102     /**
103      * Return the text of all tokens in the source interval of the specified
104      * context. This method behaves like the following code, including potential
105      * exceptions from the call to {@link #getText(Interval)}, but may be
106      * optimized by the specific implementation.
107      *
108      * <p>If {@code ctx.getSourceInterval()} does not return a valid interval of
109      * tokens provided by this stream, the behavior is unspecified.</p>
110      *
111      * <pre>
112      * TokenStream stream = ...;
113      * String text = stream.getText(ctx.getSourceInterval());
114      * </pre>
115      *
116      *  @param ctx The context providing the source interval of tokens to get
117      *  text for.
118      *  @return The text of all tokens within the source interval of {@code ctx}.
119      */
120     public Variant getText(RuleContext ctx);
121 
122     /**
123      * Return the text of all tokens in this stream between {@code start} and
124      * {@code stop} (inclusive).
125      *
126      * <p>If the specified {@code start} or {@code stop} token was not provided by
127      * this stream, or if the {@code stop} occurred before the {@code start}
128      * token, the behavior is unspecified.</p>
129      *
130      * <p>For streams which ensure that the {@link Token#getTokenIndex} method is
131      * accurate for all of its provided tokens, this method behaves like the
132      * following code. Other streams may implement this method in other ways
133      * provided the behavior is consistent with this at a high level.</p>
134      *
135      * <pre>
136      * TokenStream stream = ...;
137      * String text = "";
138      * for (int i = start.getTokenIndex(); i &lt;= stop.getTokenIndex(); i++) {
139      *   text += stream.get(i).getText();
140      * }
141      * </pre>
142      *
143      *  @param start The first token in the interval to get text for.
144      *  @param stop The last token in the interval to get text for (inclusive).
145      *  @return The text of all tokens lying between the specified {@code start}
146      *  and {@code stop} tokens.
147      *
148      *  @throws UnsupportedOperationException if this stream does not support
149      *  this method for the specified tokens
150      */
151     public Variant getText(Token start, Token stop);
152 
153 }