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	/** Static implementation of the TokenManager, used for tokdef option 
39	 */
40	class TokdefTokenManager extends SimpleTokenManager {
41		private int dummy;
42		private String filename;
43		private boolean noDefine = false;		// allow new tokens with tokdef
44		protected Grammar grammar;
45	
46	
47		TokdefTokenManager(Grammar grammar, String filename_, Tool tool_) {
48			// initialize
49			super("", tool_);
50			this.grammar = grammar;
51	//		noDefine = true;
52			filename = filename_;
53	
54			// Read a file with lines of the form ID=number
55			try {
56				// SAS: changed the following for proper text io
57				FileReader fileIn = new FileReader(filename);
58				ANTLRTokdefLexer tokdefLexer = new ANTLRTokdefLexer(fileIn);
59				ANTLRTokdefParser tokdefParser = new ANTLRTokdefParser(tokdefLexer);
60				tokdefParser.file(this);
61			} 
62			catch (ParserException ex) {
63				tool.panic("Error parsing tokdef file '" + filename + "': " + ex.toString());
64			}
65			catch (IOException ex) {
66				tool.panic("Error reading tokdef file '" + filename + "'");
67			}
68		}
69		/** define a token. */
70		public void define(TokenSymbol ts) {
71			if (noDefine) {
72				tool.error("New token type defined when using tokdef option");
73			} else {
74				super.define(ts);
75			}
76			// Allow processing to continue anyway
77		}
78		/** define a token.  Intended for use only when reading the tokdef file. */
79		public void define(String s, int ttype) {
80			TokenSymbol ts=null;
81			if ( s.startsWith("\"") ) {
82				ts = new StringLiteralSymbol(s);
83			}
84			else {
85				ts = new TokenSymbol(s);
86			}	
87			ts.setTokenType(ttype);
88			super.define(ts);
89			maxToken = (ttype+1)>maxToken ? (ttype+1) : maxToken;	// record maximum token type
90		}
91		public String getName() { return grammar.getClassName(); }
92		/** tokdef token manager is read-only if output would be same as input */
93		public boolean isReadOnly() {
94			return filename.equals(grammar.getClassName()+"TokenTypes.txt");
95		}
96		/** Get the next unused token type.  Invalid for tokdefs. */
97		public int nextTokenType() {
98			if ( noDefine ) {
99				tool.error("New token type defined when using tokdef option");
100				// Return error value
101				return 0;
102			}
103			return super.nextTokenType();	
104		}
105	}
106