01: package antlr;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.cs.usfca.edu
05: * Software rights: http://www.antlr.org/license.html
06: */
07:
08: import java.util.Hashtable;
09:
10: import antlr.ASdebug.ASDebugStream;
11: import antlr.ASdebug.IASDebugStream;
12: import antlr.ASdebug.TokenOffsetInfo;
13: import antlr.collections.Stack;
14: import antlr.collections.impl.LList;
15:
16: /** A token stream MUX (multiplexor) knows about n token streams
17: * and can multiplex them onto the same channel for use by token
18: * stream consumer like a parser. This is a way to have multiple
19: * lexers break up the same input stream for a single parser.
20: * Or, you can have multiple instances of the same lexer handle
21: * multiple input streams; this works great for includes.
22: */
23: public class TokenStreamSelector implements TokenStream {
24: /** The currently-selected token stream input */
25: protected TokenStream input = null;
26:
27: /** Used to track stack of input streams */
28: protected Stack streamStack = new LList();
29:
30: public TokenStreamSelector() {
31: }
32:
33: /** Return the stream from tokens are being pulled at
34: * the moment.
35: */
36: /*public TokenStream getCurrentStream() {
37: return input;
38: }*/
39:
40: public final Token nextToken() throws TokenStreamException {
41: return input.nextToken();
42: }
43:
44: public final TokenStream pop() {
45: TokenStream stream = (TokenStream) streamStack.pop();
46: select(stream);
47: return stream;
48: }
49:
50: public final void push(TokenStream stream) {
51: streamStack.push(input); // save current stream
52: select(stream);
53: }
54:
55: public final boolean isEmpty() {
56: return streamStack.height() == 0;
57: }
58:
59: /** Set the stream without pushing old stream */
60: public final void select(TokenStream stream) {
61: input = stream;
62: }
63: }
|