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