KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > ImportVocabTokenManager


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/ImportVocabTokenManager.java#8 $
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 /** Static implementation of the TokenManager, used for importVocab option */
17 class ImportVocabTokenManager extends SimpleTokenManager implements Cloneable JavaDoc {
18     private String JavaDoc filename;
19     protected Grammar grammar;
20
21     // FIXME: it would be nice if the path to the original grammar file was
22
// also searched.
23
ImportVocabTokenManager(Grammar grammar, String JavaDoc filename_, String JavaDoc name_, Tool tool_) {
24         // initialize
25
super(name_, tool_);
26
27         this.grammar = grammar;
28         this.filename = filename_;
29
30         // Figure out exactly where the file lives. Check $PWD first,
31
// and then search in -o <output_dir>.
32
//
33
File grammarFile = new File(filename_);
34
35         if (!grammarFile.exists()) {
36             grammarFile = new File(antlrTool.getOutputDirectory(), filename_);
37
38             if (!grammarFile.exists()) {
39                 antlrTool.panic("Cannot find importVocab file '" + filename);
40             }
41         }
42
43         setReadOnly(true);
44
45         // Read a file with lines of the form ID=number
46
try {
47             Reader fileIn = new BufferedReader(new FileReader(grammarFile));
48             ANTLRTokdefLexer tokdefLexer = new ANTLRTokdefLexer(fileIn);
49             ANTLRTokdefParser tokdefParser = new ANTLRTokdefParser(tokdefLexer);
50             tokdefParser.setFilename(filename);
51             tokdefParser.file(this);
52         }
53         catch (FileNotFoundException fnf) {
54             antlrTool.panic("Cannot find importVocab file '" + filename);
55         }
56         catch (RecognitionException ex) {
57             antlrTool.panic("Error parsing importVocab file '" + filename + "': " + ex.toString());
58         }
59         catch (TokenStreamException ex) {
60             antlrTool.panic("Error reading importVocab file '" + filename + "'");
61         }
62     }
63
64     public Object JavaDoc clone() {
65         ImportVocabTokenManager tm;
66         tm = (ImportVocabTokenManager)super.clone();
67         tm.filename = this.filename;
68         tm.grammar = this.grammar;
69         return tm;
70     }
71
72     /** define a token. */
73     public void define(TokenSymbol ts) {
74         super.define(ts);
75     }
76
77     /** define a token. Intended for use only when reading the importVocab file. */
78     public void define(String JavaDoc s, int ttype) {
79         TokenSymbol ts = null;
80         if (s.startsWith("\"")) {
81             ts = new StringLiteralSymbol(s);
82         }
83         else {
84             ts = new TokenSymbol(s);
85         }
86         ts.setTokenType(ttype);
87         super.define(ts);
88         maxToken = (ttype + 1) > maxToken ? (ttype + 1) : maxToken; // record maximum token type
89
}
90
91     /** importVocab token manager is read-only if output would be same as input */
92     public boolean isReadOnly() {
93         return readOnly;
94     }
95
96     /** Get the next unused token type. */
97     public int nextTokenType() {
98         return super.nextTokenType();
99     }
100 }
101
Popular Tags