1	package antlr;
2	
3	/**
4	 * <b>SOFTWARE RIGHTS</b>
5	 * <p>
6	 * ANTLR 2.5.0 MageLang Institute, 1998
7	 * <p>
8	 * We reserve no legal rights to the ANTLR--it is fully in the
9	 * public domain. An individual or company may do whatever
10	 * they wish with source code distributed with ANTLR or the
11	 * code generated by ANTLR, including the incorporation of
12	 * ANTLR, or its output, into commerical software.
13	 * <p>
14	 * We encourage users to develop software with ANTLR. However,
15	 * we do ask that credit is given to us for developing
16	 * ANTLR. By "credit", we mean that if you use ANTLR or
17	 * incorporate any source code into one of your programs
18	 * (commercial product, research project, or otherwise) that
19	 * you acknowledge this fact somewhere in the documentation,
20	 * research report, etc... If you like ANTLR and have
21	 * developed a nice tool with the output, please mention that
22	 * you developed it using ANTLR. In addition, we ask that the
23	 * headers remain intact in our source code. As long as these
24	 * guidelines are kept, we expect to continue enhancing this
25	 * system and expect to make other tools available as they are
26	 * completed.
27	 * <p>
28	 * The ANTLR gang:
29	 * @version ANTLR 2.5.0 MageLang Institute, 1998
30	 * @author Terence Parr, <a href=http://www.MageLang.com>MageLang Institute</a>
31	 * @author <br>John Lilley, <a href=http://www.Empathy.com>Empathy Software</a>
32	 */
33	import java.io.*;
34	import java.util.Hashtable;
35	import java.util.Enumeration;
36	import antlr.collections.impl.Vector;
37	
38	class SimpleTokenManager implements TokenManager {
39		protected int maxToken = Token.MIN_USER_TYPE;
40		// Token vocabulary is Vector of String's
41		protected Vector vocabulary;
42		// Hash table is a mapping from Strings to TokenSymbol
43		private Hashtable table;
44		// the ANTLR tool
45		protected Tool tool;
46		// Name of the token manager
47		protected String name;
48	
49	
50		SimpleTokenManager(String name_, Tool tool_) {
51			tool = tool_;
52			name = name_;
53			// Don't make a bigger vector than we need, because it will show up in output sets.
54			vocabulary = new Vector(1);
55			table = new Hashtable();
56	
57			// define EOF symbol
58			TokenSymbol ts = new TokenSymbol("EOF");
59			ts.setTokenType(Token.EOF_TYPE);
60			define(ts);
61	
62	/*
63			// define <epsilon> but only in the vocabulary vector
64			vocabulary.ensureCapacity(GrammarAnalyzer.EPSILON_TYPE);
65			// The only time epsilon can appear (in warning) is when
66			// it implies end-of-syn-pred, so we just set its string here.
67			vocabulary.setElementAt("<end-of-syn-pred>", GrammarAnalyzer.EPSILON_TYPE);
68	*/
69	
70			// define <null-tree-lookahead> but only in the vocabulary vector
71			vocabulary.ensureCapacity(Token.NULL_TREE_LOOKAHEAD);
72			vocabulary.setElementAt("NULL_TREE_LOOKAHEAD", Token.NULL_TREE_LOOKAHEAD);
73		}
74		/** define a token */
75		public void define(TokenSymbol ts) {
76			// Add the symbol to the vocabulary vector
77			vocabulary.ensureCapacity(ts.getTokenType());
78			vocabulary.setElementAt(ts.getId(), ts.getTokenType());
79			// add the symbol to the hash table
80			table.put(ts.getId(), ts);
81		}
82		/** Simple token manager doesn't have a name -- must be set externally */
83		public String getName() { return name; }
84		/** Get a token symbol by index */
85		public String getTokenStringAt(int idx) {
86			return (String)vocabulary.elementAt(idx);
87		}
88		/** Get the TokenSymbol for a string */
89		public TokenSymbol getTokenSymbol(String sym) {
90			return (TokenSymbol)table.get(sym);
91		}
92		/** Get an enumerator over the symbol table */
93		public Enumeration getTokenSymbolElements() {
94			return table.elements();
95		}
96		/** Get the token vocabulary (read-only).
97		 * @return A Vector of TokenSymbol 
98		 */
99		public Vector getVocabulary() {
100			return vocabulary;
101		}
102		/** Simple token manager is not read-only */
103		public boolean isReadOnly() { return false; }
104		/** Get the highest token type in use */
105		public int maxTokenType() {
106			return maxToken-1;
107		}
108		/** Get the next unused token type */
109		public int nextTokenType() {
110			return maxToken++;
111		}
112		/** Set the name of the token manager */
113		public void setName(String name_) { name = name_; }
114		/** Is a token symbol defined? */
115		public boolean tokenDefined(String symbol) {
116			return table.containsKey(symbol);
117		}
118	}
119