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