1 package persistence.antlr; 2 3 8 9 import java.util.Hashtable ; 10 11 import persistence.antlr.collections.impl.LList; 12 import persistence.antlr.collections.Stack; 13 14 import java.io.IOException ; 15 16 23 public class TokenStreamSelector implements TokenStream { 24 25 protected Hashtable inputStreamNames; 26 27 28 protected TokenStream input; 29 30 31 protected Stack streamStack = new LList(); 32 33 public TokenStreamSelector() { 34 super(); 35 inputStreamNames = new Hashtable (); 36 } 37 38 public void addInputStream(TokenStream stream, String key) { 39 inputStreamNames.put(key, stream); 40 } 41 42 45 public TokenStream getCurrentStream() { 46 return input; 47 } 48 49 public TokenStream getStream(String sname) { 50 TokenStream stream = (TokenStream)inputStreamNames.get(sname); 51 if (stream == null) { 52 throw new IllegalArgumentException ("TokenStream " + sname + " not found"); 53 } 54 return stream; 55 } 56 57 public Token nextToken() throws TokenStreamException { 58 for (; ;) { 62 try { 63 return input.nextToken(); 64 } 65 catch (TokenStreamRetryException r) { 66 } 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); select(stream); 80 } 81 82 public void push(String sname) { 83 streamStack.push(input); 84 select(sname); 85 } 86 87 94 public void retry() throws TokenStreamRetryException { 95 throw new TokenStreamRetryException(); 96 } 97 98 99 public void select(TokenStream stream) { 100 input = stream; 101 } 102 103 public void select(String sname) throws IllegalArgumentException { 104 input = getStream(sname); 105 } 106 } 107 | Popular Tags |