KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > ImportVocabTokenManager


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 /** Static implementation of the TokenManager, used for importVocab option */
16 class ImportVocabTokenManager extends SimpleTokenManager implements Cloneable JavaDoc {
17     private String JavaDoc filename;
18     protected Grammar grammar;
19
20     // FIXME: it would be nice if the path to the original grammar file was
21
// also searched.
22
ImportVocabTokenManager(Grammar grammar, String JavaDoc filename_, String JavaDoc name_, Tool tool_) {
23         // initialize
24
super(name_, tool_);
25
26         this.grammar = grammar;
27         this.filename = filename_;
28
29         // Figure out exactly where the file lives. Check $PWD first,
30
// and then search in -o <output_dir>.
31
//
32
File grammarFile = new File(filename);
33
34         if (!grammarFile.exists()) {
35             grammarFile = new File(antlrTool.getOutputDirectory(), filename);
36
37             if (!grammarFile.exists()) {
38                 antlrTool.panic("Cannot find importVocab file '" + filename + "'");
39             }
40         }
41
42         setReadOnly(true);
43
44         // Read a file with lines of the form ID=number
45
try {
46             Reader fileIn = new BufferedReader(new FileReader(grammarFile));
47             ANTLRTokdefLexer tokdefLexer = new ANTLRTokdefLexer(fileIn);
48             ANTLRTokdefParser tokdefParser = new ANTLRTokdefParser(tokdefLexer);
49             tokdefParser.setTool(antlrTool);
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