001: package antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.cs.usfca.edu
005: * Software rights: http://www.antlr.org/license.html
006: */
007:
008: import java.io.*;
009: import java.util.Hashtable;
010: import java.util.Enumeration;
011:
012: import antlr.collections.impl.Vector;
013:
014: class SimpleTokenManager implements TokenManager, Cloneable {
015: protected int maxToken = Token.MIN_USER_TYPE;
016: // Token vocabulary is Vector of String's
017: protected Vector vocabulary;
018: // Hash table is a mapping from Strings to TokenSymbol
019: private Hashtable table;
020: // the ANTLR tool
021: protected Tool antlrTool;
022: // Name of the token manager
023: protected String name;
024:
025: protected boolean readOnly = false;
026:
027: SimpleTokenManager(String name_, Tool tool_) {
028: antlrTool = tool_;
029: name = name_;
030: // Don't make a bigger vector than we need, because it will show up in output sets.
031: vocabulary = new Vector(1);
032: table = new Hashtable();
033:
034: // define EOF symbol
035: TokenSymbol ts = new TokenSymbol("EOF");
036: ts.setTokenType(Token.EOF_TYPE);
037: define(ts);
038:
039: // define <null-tree-lookahead> but only in the vocabulary vector
040: vocabulary.ensureCapacity(Token.NULL_TREE_LOOKAHEAD);
041: vocabulary.setElementAt("NULL_TREE_LOOKAHEAD",
042: Token.NULL_TREE_LOOKAHEAD);
043: }
044:
045: public Object clone() {
046: SimpleTokenManager tm;
047: try {
048: tm = (SimpleTokenManager) super .clone();
049: tm.vocabulary = (Vector) this .vocabulary.clone();
050: tm.table = (Hashtable) this .table.clone();
051: tm.maxToken = this .maxToken;
052: tm.antlrTool = this .antlrTool;
053: tm.name = this .name;
054: } catch (CloneNotSupportedException e) {
055: antlrTool.fatalError("Cannot clone token manager");
056: return null;
057: }
058: return tm;
059: }
060:
061: /** define a token */
062: public void define(TokenSymbol ts) {
063: // Add the symbol to the vocabulary vector
064: vocabulary.ensureCapacity(ts.getTokenType());
065: vocabulary.setElementAt(ts.getId(), ts.getTokenType());
066: // add the symbol to the hash table
067: mapToTokenSymbol(ts.getId(), ts);
068: }
069:
070: /** Simple token manager doesn't have a name -- must be set externally */
071: public String getName() {
072: return name;
073: }
074:
075: /** Get a token symbol by index */
076: public String getTokenStringAt(int idx) {
077: return (String) vocabulary.elementAt(idx);
078: }
079:
080: /** Get the TokenSymbol for a string */
081: public TokenSymbol getTokenSymbol(String sym) {
082: return (TokenSymbol) table.get(sym);
083: }
084:
085: /** Get a token symbol by index */
086: public TokenSymbol getTokenSymbolAt(int idx) {
087: return getTokenSymbol(getTokenStringAt(idx));
088: }
089:
090: /** Get an enumerator over the symbol table */
091: public Enumeration getTokenSymbolElements() {
092: return table.elements();
093: }
094:
095: public Enumeration getTokenSymbolKeys() {
096: return table.keys();
097: }
098:
099: /** Get the token vocabulary (read-only).
100: * @return A Vector of TokenSymbol
101: */
102: public Vector getVocabulary() {
103: return vocabulary;
104: }
105:
106: /** Simple token manager is not read-only */
107: public boolean isReadOnly() {
108: return false;
109: }
110:
111: /** Map a label or string to an existing token symbol */
112: public void mapToTokenSymbol(String name, TokenSymbol sym) {
113: // System.out.println("mapToTokenSymbol("+name+","+sym+")");
114: table.put(name, sym);
115: }
116:
117: /** Get the highest token type in use */
118: public int maxTokenType() {
119: return maxToken - 1;
120: }
121:
122: /** Get the next unused token type */
123: public int nextTokenType() {
124: return maxToken++;
125: }
126:
127: /** Set the name of the token manager */
128: public void setName(String name_) {
129: name = name_;
130: }
131:
132: public void setReadOnly(boolean ro) {
133: readOnly = ro;
134: }
135:
136: /** Is a token symbol defined? */
137: public boolean tokenDefined(String symbol) {
138: return table.containsKey(symbol);
139: }
140: }
|