1 /* 2 * [The "BSD license"] 3 * Copyright (c) 2012 Terence Parr 4 * Copyright (c) 2012 Sam Harwell 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 module antlr.v4.runtime.TokenStream; 32 33 import antlr.v4.runtime.IntStream; 34 import antlr.v4.runtime.Token; 35 import antlr.v4.runtime.TokenSource; 36 import antlr.v4.runtime.RuleContext; 37 import antlr.v4.runtime.misc.Interval; 38 39 /** 40 * TODO add interface description 41 */ 42 interface TokenStream : IntStream 43 { 44 45 /** 46 * Get the {@link Token} instance associated with the value returned by 47 * {@link #LA LA(k)}. This method has the same pre- and post-conditions as 48 * {@link IntStream#LA}. In addition, when the preconditions of this method 49 * are met, the return value is non-null and the value of 50 * {@code LT(k).getType()==LA(k)}. 51 * 52 * @see IntStream#LA 53 */ 54 public Token LT(int k); 55 56 /** 57 * Gets the {@link Token} at the specified {@code index} in the stream. When 58 * the preconditions of this method are met, the return value is non-null. 59 * 60 * <p>The preconditions for this method are the same as the preconditions of 61 * {@link IntStream#seek}. If the behavior of {@code seek(index)} is 62 * unspecified for the current state and given {@code index}, then the 63 * behavior of this method is also unspecified.</p> 64 * 65 * <p>The symbol referred to by {@code index} differs from {@code seek()} only 66 * in the case of filtering streams where {@code index} lies before the end 67 * of the stream. Unlike {@code seek()}, this method does not adjust 68 * {@code index} to point to a non-ignored symbol.</p> 69 * 70 * @throws IllegalArgumentException if {code index} is less than 0 71 * @throws UnsupportedOperationException if the stream does not support 72 * retrieving the token at the specified index 73 */ 74 public Token get(int index); 75 76 /** 77 * Gets the underlying {@link TokenSource} which provides tokens for this 78 * stream. 79 */ 80 public TokenSource getTokenSource(); 81 82 /** 83 * Return the text of all tokens within the specified {@code interval}. This 84 * method behaves like the following code (including potential exceptions 85 * for violating preconditions of {@link #get}, but may be optimized by the 86 * specific implementation. 87 * 88 * <pre> 89 * TokenStream stream = ...; 90 * String text = ""; 91 * for (int i = interval.a; i <= interval.b; i++) { 92 * text += stream.get(i).getText(); 93 * } 94 * </pre> 95 * 96 * @param interval The interval of tokens within this stream to get text 97 * for. 98 * @return The text of all tokens within the specified interval in this 99 * stream. 100 * 101 * @throws NullPointerException if {@code interval} is {@code null} 102 */ 103 public string getText(Interval interval); 104 105 /** 106 * Return the text of all tokens in the stream. This method behaves like the 107 * following code, including potential exceptions from the calls to 108 * {@link IntStream#size} and {@link #getText(Interval)}, but may be 109 * optimized by the specific implementation. 110 * 111 * <pre> 112 * TokenStream stream = ...; 113 * String text = stream.getText(new Interval(0, stream.size())); 114 * </pre> 115 * 116 * @return The text of all tokens in the stream. 117 */ 118 public string getText(); 119 120 /** 121 * Return the text of all tokens in the source interval of the specified 122 * context. This method behaves like the following code, including potential 123 * exceptions from the call to {@link #getText(Interval)}, but may be 124 * optimized by the specific implementation. 125 * 126 * <p>If {@code ctx.getSourceInterval()} does not return a valid interval of 127 * tokens provided by this stream, the behavior is unspecified.</p> 128 * 129 * <pre> 130 * TokenStream stream = ...; 131 * String text = stream.getText(ctx.getSourceInterval()); 132 * </pre> 133 * 134 * @param ctx The context providing the source interval of tokens to get 135 * text for. 136 * @return The text of all tokens within the source interval of {@code ctx}. 137 */ 138 public string getText(RuleContext ctx); 139 140 /** 141 * Return the text of all tokens in this stream between {@code start} and 142 * {@code stop} (inclusive). 143 * 144 * <p>If the specified {@code start} or {@code stop} token was not provided by 145 * this stream, or if the {@code stop} occurred before the {@code start} 146 * token, the behavior is unspecified.</p> 147 * 148 * <p>For streams which ensure that the {@link Token#getTokenIndex} method is 149 * accurate for all of its provided tokens, this method behaves like the 150 * following code. Other streams may implement this method in other ways 151 * provided the behavior is consistent with this at a high level.</p> 152 * 153 * <pre> 154 * TokenStream stream = ...; 155 * String text = ""; 156 * for (int i = start.getTokenIndex(); i <= stop.getTokenIndex(); i++) { 157 * text += stream.get(i).getText(); 158 * } 159 * </pre> 160 * 161 * @param start The first token in the interval to get text for. 162 * @param stop The last token in the interval to get text for (inclusive). 163 * @return The text of all tokens lying between the specified {@code start} 164 * and {@code stop} tokens. 165 * 166 * @throws UnsupportedOperationException if this stream does not support 167 * this method for the specified tokens 168 */ 169 public string getText(Token start, Token stop); 170 171 }