13447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/*
23447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein [The "BSD license"]
33447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Copyright (c) 2005-2009 Terence Parr
43447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein All rights reserved.
53447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
63447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein Redistribution and use in source and binary forms, with or without
73447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein modification, are permitted provided that the following conditions
83447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein are met:
93447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 1. Redistributions of source code must retain the above copyright
103447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer.
113447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 2. Redistributions in binary form must reproduce the above copyright
123447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     notice, this list of conditions and the following disclaimer in the
133447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     documentation and/or other materials provided with the distribution.
143447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein 3. The name of the author may not be used to endorse or promote products
153447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein     derived from this software without specific prior written permission.
163447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
173447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
183447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
193447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
203447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
213447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
223447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
233447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
243447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
253447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
263447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
273447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
283447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpackage org.antlr.runtime;
293447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
303447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport org.antlr.runtime.misc.LookaheadStream;
313447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
323447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.List;
333447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinimport java.util.NoSuchElementException;
343447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
353447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein/** A token stream that pulls tokens from the code source on-demand and
363447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  without tracking a complete buffer of the tokens. This stream buffers
373447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  the minimum number of tokens possible.  It's the same as
383447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  OnDemandTokenStream except that OnDemandTokenStream buffers all tokens.
393447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *
403447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  You can't use this stream if you pass whitespace or other off-channel
413447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  tokens to the parser. The stream can't ignore off-channel tokens.
423447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *
433447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  You can only look backwards 1 token: LT(-1).
443447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *
453447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  Use this when you need to read from a socket or other infinite stream.
463447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *
473447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  @see BufferedTokenStream
483447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein *  @see CommonTokenStream
493447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein */
503447a5916aa62f44de24cc441fc9987116ddff52Andrew Sappersteinpublic class UnbufferedTokenStream extends LookaheadStream<Token> implements TokenStream {
513447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	protected TokenSource tokenSource;
523447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    protected int tokenIndex = 0; // simple counter to set token index in tokens
533447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
543447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    /** Skip tokens on any channel but this one; this is how we skip whitespace... */
553447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    protected int channel = Token.DEFAULT_CHANNEL;
563447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
573447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public UnbufferedTokenStream(TokenSource tokenSource) {
583447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		this.tokenSource = tokenSource;
593447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
603447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
613447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public Token nextElement() {
623447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		Token t = tokenSource.nextToken();
633447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        t.setTokenIndex(tokenIndex++);
643447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein		return t;
653447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	}
663447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
673447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public boolean isEOF(Token o) { return o.getType() == Token.EOF; }
683447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
693447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public TokenSource getTokenSource() { return tokenSource; }
703447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
713447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String toString(int start, int stop) { return "n/a"; }
723447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
733447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String toString(Token start, Token stop) { return "n/a"; }
743447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
753447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public int LA(int i) { return LT(i).getType(); }
763447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
773447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    public Token get(int i) {
783447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein        throw new UnsupportedOperationException("Absolute token indexes are meaningless in an unbuffered stream");
793447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein    }
803447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein
813447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein	public String getSourceName() {	return tokenSource.getSourceName();	}
823447a5916aa62f44de24cc441fc9987116ddff52Andrew Sapperstein}
83