1 package persistence.antlr; 2 3 8 9 import java.io.*; 10 import java.util.Hashtable ; 11 import java.util.Enumeration ; 12 13 import persistence.antlr.collections.impl.Vector; 14 15 class SimpleTokenManager implements TokenManager, Cloneable { 16 protected int maxToken = Token.MIN_USER_TYPE; 17 protected Vector vocabulary; 19 private Hashtable table; 21 protected Tool antlrTool; 23 protected String name; 25 26 protected boolean readOnly = false; 27 28 SimpleTokenManager(String name_, Tool tool_) { 29 antlrTool = tool_; 30 name = name_; 31 vocabulary = new Vector(1); 33 table = new Hashtable (); 34 35 TokenSymbol ts = new TokenSymbol("EOF"); 37 ts.setTokenType(Token.EOF_TYPE); 38 define(ts); 39 40 vocabulary.ensureCapacity(Token.NULL_TREE_LOOKAHEAD); 42 vocabulary.setElementAt("NULL_TREE_LOOKAHEAD", Token.NULL_TREE_LOOKAHEAD); 43 } 44 45 public Object clone() { 46 SimpleTokenManager tm; 47 try { 48 tm = (SimpleTokenManager)super.clone(); 49 tm.vocabulary = (Vector)this.vocabulary.clone(); 50 tm.table = (Hashtable )this.table.clone(); 51 tm.maxToken = this.maxToken; 52 tm.antlrTool = this.antlrTool; 53 tm.name = this.name; 54 } 55 catch (CloneNotSupportedException e) { 56 antlrTool.panic("cannot clone token manager"); 57 return null; 58 } 59 return tm; 60 } 61 62 63 public void define(TokenSymbol ts) { 64 vocabulary.ensureCapacity(ts.getTokenType()); 66 vocabulary.setElementAt(ts.getId(), ts.getTokenType()); 67 mapToTokenSymbol(ts.getId(), ts); 69 } 70 71 72 public String getName() { 73 return name; 74 } 75 76 77 public String getTokenStringAt(int idx) { 78 return (String )vocabulary.elementAt(idx); 79 } 80 81 82 public TokenSymbol getTokenSymbol(String sym) { 83 return (TokenSymbol)table.get(sym); 84 } 85 86 87 public TokenSymbol getTokenSymbolAt(int idx) { 88 return getTokenSymbol(getTokenStringAt(idx)); 89 } 90 91 92 public Enumeration getTokenSymbolElements() { 93 return table.elements(); 94 } 95 96 public Enumeration getTokenSymbolKeys() { 97 return table.keys(); 98 } 99 100 103 public Vector getVocabulary() { 104 return vocabulary; 105 } 106 107 108 public boolean isReadOnly() { 109 return false; 110 } 111 112 113 public void mapToTokenSymbol(String name, TokenSymbol sym) { 114 table.put(name, sym); 116 } 117 118 119 public int maxTokenType() { 120 return maxToken - 1; 121 } 122 123 124 public int nextTokenType() { 125 return maxToken++; 126 } 127 128 129 public void setName(String name_) { 130 name = name_; 131 } 132 133 public void setReadOnly(boolean ro) { 134 readOnly = ro; 135 } 136 137 138 public boolean tokenDefined(String symbol) { 139 return table.containsKey(symbol); 140 } 141 } 142 | Popular Tags |