1 package org.codehaus.groovy.syntax; 2 3 /* 4 $Id: TokenStream.java,v 1.6 2004/03/07 12:18:54 jstrachan Exp $ 5 6 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. 7 8 Redistribution and use of this software and associated documentation 9 ("Software"), with or without modification, are permitted provided 10 that the following conditions are met: 11 12 1. Redistributions of source code must retain copyright 13 statements and notices. Redistributions must also contain a 14 copy of this document. 15 16 2. Redistributions in binary form must reproduce the 17 above copyright notice, this list of conditions and the 18 following disclaimer in the documentation and/or other 19 materials provided with the distribution. 20 21 3. The name "groovy" must not be used to endorse or promote 22 products derived from this Software without prior written 23 permission of The Codehaus. For written permission, 24 please contact info@codehaus.org. 25 26 4. Products derived from this Software may not be called "groovy" 27 nor may "groovy" appear in their names without prior written 28 permission of The Codehaus. "groovy" is a registered 29 trademark of The Codehaus. 30 31 5. Due credit should be given to The Codehaus - 32 http://groovy.codehaus.org/ 33 34 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS 35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 38 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 45 OF THE POSSIBILITY OF SUCH DAMAGE. 46 47 */ 48 49 50 /*** 51 * Provides a stream of lexer tokens for use by (primarily) the parsing 52 * systems. 53 */ 54 55 public interface TokenStream 56 { 57 // ---------------------------------------------------------------------- 58 // Interface 59 // ---------------------------------------------------------------------- 60 61 /*** 62 * Looks-ahead to the next token. 63 * 64 * <p> 65 * This method is equivalent to <code>la(1)</code>. 66 * </p> 67 * 68 * @see #la(int) 69 * 70 * @return The next token or null if no more tokens 71 * available. 72 * 73 * @throws ReadException If an error occurs attempting to lookahead 74 * a token. 75 */ 76 77 Token la() throws ReadException, SyntaxException; 78 79 80 /*** 81 * Looks-ahead to the <code>k</code><i>th</i> token. 82 * 83 * @param k Number of token to look ahead. 84 * 85 * @return the <code>k</code><i>th</i> token or null if no 86 * more tokens available. 87 * 88 * @throws ReadException If an error occurs attempting to lookahead 89 * a token. 90 */ 91 92 Token la(int k) throws ReadException, SyntaxException; 93 94 95 /*** 96 * Consumes the next token. 97 * 98 * @param type The token type. 99 * 100 * @return The consumed token or null if no more tokens 101 * available. 102 * 103 * @throws ReadException If an error occurs attempting to consume 104 * a token. 105 */ 106 107 Token consume(int type) throws ReadException, SyntaxException; 108 109 110 /*** 111 * Returns a description of the source location (typically a file path). 112 */ 113 114 String getSourceLocator(); 115 116 117 /*** 118 * Checkpoints a point in the stream that we can go back to 119 */ 120 121 void checkpoint(); 122 123 124 /*** 125 * Restores to the previous checkpoint 126 */ 127 128 void restore(); 129 130 131 /*** 132 * Returns true if the stream is out of tokens, possibly 133 * ignoring trailing whitespace. 134 */ 135 136 boolean atEnd( boolean ignoringWhitespace ); 137 138 139 140 /*** 141 * Returns true if the stream is out of tokens. 142 */ 143 144 boolean atEnd( ); 145 146 }