KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > SimpleTokenManager


1 package persistence.antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/license.html
6  *
7  */

8
9 import java.io.*;
10 import java.util.Hashtable JavaDoc;
11 import java.util.Enumeration JavaDoc;
12
13 import persistence.antlr.collections.impl.Vector;
14
15 class SimpleTokenManager implements TokenManager, Cloneable JavaDoc {
16     protected int maxToken = Token.MIN_USER_TYPE;
17     // Token vocabulary is Vector of String's
18
protected Vector vocabulary;
19     // Hash table is a mapping from Strings to TokenSymbol
20
private Hashtable JavaDoc table;
21     // the ANTLR tool
22
protected Tool antlrTool;
23     // Name of the token manager
24
protected String JavaDoc name;
25
26     protected boolean readOnly = false;
27
28     SimpleTokenManager(String JavaDoc name_, Tool tool_) {
29         antlrTool = tool_;
30         name = name_;
31         // Don't make a bigger vector than we need, because it will show up in output sets.
32
vocabulary = new Vector(1);
33         table = new Hashtable JavaDoc();
34
35         // define EOF symbol
36
TokenSymbol ts = new TokenSymbol("EOF");
37         ts.setTokenType(Token.EOF_TYPE);
38         define(ts);
39
40         // define <null-tree-lookahead> but only in the vocabulary vector
41
vocabulary.ensureCapacity(Token.NULL_TREE_LOOKAHEAD);
42         vocabulary.setElementAt("NULL_TREE_LOOKAHEAD", Token.NULL_TREE_LOOKAHEAD);
43     }
44
45     public Object JavaDoc clone() {
46         SimpleTokenManager tm;
47         try {
48             tm = (SimpleTokenManager)super.clone();
49             tm.vocabulary = (Vector)this.vocabulary.clone();
50             tm.table = (Hashtable JavaDoc)this.table.clone();
51             tm.maxToken = this.maxToken;
52             tm.antlrTool = this.antlrTool;
53             tm.name = this.name;
54         }
55         catch (CloneNotSupportedException JavaDoc e) {
56             antlrTool.panic("cannot clone token manager");
57             return null;
58         }
59         return tm;
60     }
61
62     /** define a token */
63     public void define(TokenSymbol ts) {
64         // Add the symbol to the vocabulary vector
65
vocabulary.ensureCapacity(ts.getTokenType());
66         vocabulary.setElementAt(ts.getId(), ts.getTokenType());
67         // add the symbol to the hash table
68
mapToTokenSymbol(ts.getId(), ts);
69     }
70
71     /** Simple token manager doesn't have a name -- must be set externally */
72     public String JavaDoc getName() {
73         return name;
74     }
75
76     /** Get a token symbol by index */
77     public String JavaDoc getTokenStringAt(int idx) {
78         return (String JavaDoc)vocabulary.elementAt(idx);
79     }
80
81     /** Get the TokenSymbol for a string */
82     public TokenSymbol getTokenSymbol(String JavaDoc sym) {
83         return (TokenSymbol)table.get(sym);
84     }
85
86     /** Get a token symbol by index */
87     public TokenSymbol getTokenSymbolAt(int idx) {
88         return getTokenSymbol(getTokenStringAt(idx));
89     }
90
91     /** Get an enumerator over the symbol table */
92     public Enumeration JavaDoc getTokenSymbolElements() {
93         return table.elements();
94     }
95
96     public Enumeration JavaDoc getTokenSymbolKeys() {
97         return table.keys();
98     }
99
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 JavaDoc 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 JavaDoc 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 JavaDoc symbol) {
139         return table.containsKey(symbol);
140     }
141 }
142
Popular Tags