KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > TokenStreamSelector


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/TokenStreamSelector.java#4 $
8  */

9
10 import java.util.Hashtable JavaDoc;
11
12 import antlr.collections.impl.LList;
13 import antlr.collections.Stack;
14
15 import java.io.IOException JavaDoc;
16
17 /** A token stream MUX (multiplexor) knows about n token streams
18  * and can multiplex them onto the same channel for use by token
19  * stream consumer like a parser. This is a way to have multiple
20  * lexers break up the same input stream for a single parser.
21  * Or, you can have multiple instances of the same lexer handle
22  * multiple input streams; this works great for includes.
23  */

24 public class TokenStreamSelector implements TokenStream {
25     /** The set of inputs to the MUX */
26     protected Hashtable JavaDoc inputStreamNames;
27
28     /** The currently-selected token stream input */
29     protected TokenStream input;
30
31     /** Used to track stack of input streams */
32     protected Stack streamStack = new LList();
33
34     public TokenStreamSelector() {
35         super();
36         inputStreamNames = new Hashtable JavaDoc();
37     }
38
39     public void addInputStream(TokenStream stream, String JavaDoc key) {
40         inputStreamNames.put(key, stream);
41     }
42
43     /** Return the stream from tokens are being pulled at
44      * the moment.
45      */

46     public TokenStream getCurrentStream() {
47         return input;
48     }
49
50     public TokenStream getStream(String JavaDoc sname) {
51         TokenStream stream = (TokenStream)inputStreamNames.get(sname);
52         if (stream == null) {
53             throw new IllegalArgumentException JavaDoc("TokenStream " + sname + " not found");
54         }
55         return stream;
56     }
57
58     public Token nextToken() throws TokenStreamException {
59         // return input.nextToken();
60
// keep looking for a token until you don't
61
// get a retry exception.
62
for (; ;) {
63             try {
64                 return input.nextToken();
65             }
66             catch (TokenStreamRetryException r) {
67                 // just retry "forever"
68
}
69         }
70     }
71
72     public TokenStream pop() {
73         TokenStream stream = (TokenStream)streamStack.pop();
74         select(stream);
75         return stream;
76     }
77
78     public void push(TokenStream stream) {
79         streamStack.push(input); // save current stream
80
select(stream);
81     }
82
83     public void push(String JavaDoc sname) {
84         streamStack.push(input);
85         select(sname);
86     }
87
88     /** Abort recognition of current Token and try again.
89      * A stream can push a new stream (for include files
90      * for example, and then retry(), which will cause
91      * the current stream to abort back to this.nextToken().
92      * this.nextToken() then asks for a token from the
93      * current stream, which is the new "substream."
94      */

95     public void retry() throws TokenStreamRetryException {
96         throw new TokenStreamRetryException();
97     }
98
99     /** Set the stream without pushing old stream */
100     public void select(TokenStream stream) {
101         input = stream;
102     }
103
104     public void select(String JavaDoc sname) throws IllegalArgumentException JavaDoc {
105         input = getStream(sname);
106     }
107 }
108
Popular Tags