UnbufferedTokenStream.java revision 324c4644fee44b9898524c09511bd33c3f12e2df
1/*
2 [The "BSD license"]
3 Copyright (c) 2005-2009 Terence Parr
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions
8 are met:
9 1. Redistributions of source code must retain the above copyright
10     notice, this list of conditions and the following disclaimer.
11 2. Redistributions in binary form must reproduce the above copyright
12     notice, this list of conditions and the following disclaimer in the
13     documentation and/or other materials provided with the distribution.
14 3. The name of the author may not be used to endorse or promote products
15     derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28package org.antlr.runtime;
29
30import org.antlr.runtime.misc.LookaheadStream;
31
32import java.util.List;
33import java.util.NoSuchElementException;
34
35/** A token stream that pulls tokens from the code source on-demand and
36 *  without tracking a complete buffer of the tokens. This stream buffers
37 *  the minimum number of tokens possible.  It's the same as
38 *  OnDemandTokenStream except that OnDemandTokenStream buffers all tokens.
39 *
40 *  You can't use this stream if you pass whitespace or other off-channel
41 *  tokens to the parser. The stream can't ignore off-channel tokens.
42 *
43 *  You can only look backwards 1 token: LT(-1).
44 *
45 *  Use this when you need to read from a socket or other infinite stream.
46 *
47 *  @see BufferedTokenStream
48 *  @see CommonTokenStream
49 */
50public class UnbufferedTokenStream extends LookaheadStream<Token> implements TokenStream {
51	protected TokenSource tokenSource;
52    protected int tokenIndex = 0; // simple counter to set token index in tokens
53
54    /** Skip tokens on any channel but this one; this is how we skip whitespace... */
55    protected int channel = Token.DEFAULT_CHANNEL;
56
57	public UnbufferedTokenStream(TokenSource tokenSource) {
58		this.tokenSource = tokenSource;
59	}
60
61	public Token nextElement() {
62		Token t = tokenSource.nextToken();
63        t.setTokenIndex(tokenIndex++);
64		return t;
65	}
66
67    public boolean isEOF(Token o) { return o.getType() == Token.EOF; }
68
69	public TokenSource getTokenSource() { return tokenSource; }
70
71	public String toString(int start, int stop) { return "n/a"; }
72
73	public String toString(Token start, Token stop) { return "n/a"; }
74
75    public int LA(int i) { return LT(i).getType(); }
76
77    public Token get(int i) {
78        throw new UnsupportedOperationException("Absolute token indexes are meaningless in an unbuffered stream");
79    }
80
81	public String getSourceName() {	return tokenSource.getSourceName();	}
82}
83