KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > SimpleTokenManager


1 package antlr;
2
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.jGuru.com
5  * Software rights: http://www.antlr.org/RIGHTS.html
6  *
7  * $Id: //depot/code/org.antlr/main/main/antlr/SimpleTokenManager.java#6 $
8  */

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

104     public Vector getVocabulary() {
105         return vocabulary;
106     }
107
108     /** Simple token manager is not read-only */
109     public boolean isReadOnly() {
110         return false;
111     }
112
113     /** Map a label or string to an existing token symbol */
114     public void mapToTokenSymbol(String JavaDoc name, TokenSymbol sym) {
115         // System.out.println("mapToTokenSymbol("+name+","+sym+")");
116
table.put(name, sym);
117     }
118
119     /** Get the highest token type in use */
120     public int maxTokenType() {
121         return maxToken - 1;
122     }
123
124     /** Get the next unused token type */
125     public int nextTokenType() {
126         return maxToken++;
127     }
128
129     /** Set the name of the token manager */
130     public void setName(String JavaDoc name_) {
131         name = name_;
132     }
133
134     public void setReadOnly(boolean ro) {
135         readOnly = ro;
136     }
137
138     /** Is a token symbol defined? */
139     public boolean tokenDefined(String JavaDoc symbol) {
140         return table.containsKey(symbol);
141     }
142 }
143
Popular Tags