KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > TokenStreamSelector


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import java.util.Hashtable JavaDoc;
10
11 import persistence.antlr.collections.impl.LList;
12 import persistence.antlr.collections.Stack;
13
14 import java.io.IOException JavaDoc;
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 set of inputs to the MUX */
25     protected Hashtable JavaDoc inputStreamNames;
26
27     /** The currently-selected token stream input */
28     protected TokenStream input;
29
30     /** Used to track stack of input streams */
31     protected Stack streamStack = new LList();
32
33     public TokenStreamSelector() {
34         super();
35         inputStreamNames = new Hashtable JavaDoc();
36     }
37
38     public void addInputStream(TokenStream stream, String JavaDoc key) {
39         inputStreamNames.put(key, stream);
40     }
41
42     /** Return the stream from tokens are being pulled at
43      * the moment.
44      */

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

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