1 package antlr;
2 
3 /* ANTLR Translator Generator
4  * Project led by Terence Parr at http://www.cs.usfca.edu
5  * Software rights: http://www.antlr.org/license.html
6  *
7  * $Id: //depot/code/org.antlr/release/antlr-2.7.7/antlr/ImportVocabTokenManager.java#2 $
8  */
9 
10 import java.io.*;
11 import java.util.Hashtable;
12 import java.util.Enumeration;
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 {
18     private String filename;
19     protected Grammar grammar;
20 
21     // FIXME: it would be nice if the path to the original grammar file was
22     // also searched.
ImportVocabTokenManager(Grammar grammar, String filename_, String name_, Tool tool_)23     ImportVocabTokenManager(Grammar grammar, String filename_, String 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.setTool(antlrTool);
51             tokdefParser.setFilename(filename);
52             tokdefParser.file(this);
53         }
54         catch (FileNotFoundException fnf) {
55             antlrTool.panic("Cannot find importVocab file '" + filename + "'");
56         }
57         catch (RecognitionException ex) {
58             antlrTool.panic("Error parsing importVocab file '" + filename + "': " + ex.toString());
59         }
60         catch (TokenStreamException ex) {
61             antlrTool.panic("Error reading importVocab file '" + filename + "'");
62         }
63     }
64 
clone()65     public Object clone() {
66         ImportVocabTokenManager tm;
67         tm = (ImportVocabTokenManager)super.clone();
68         tm.filename = this.filename;
69         tm.grammar = this.grammar;
70         return tm;
71     }
72 
73     /** define a token. */
define(TokenSymbol ts)74     public void define(TokenSymbol ts) {
75         super.define(ts);
76     }
77 
78     /** define a token.  Intended for use only when reading the importVocab file. */
define(String s, int ttype)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 
92     /** importVocab token manager is read-only if output would be same as input */
isReadOnly()93     public boolean isReadOnly() {
94         return readOnly;
95     }
96 
97     /** Get the next unused token type. */
nextTokenType()98     public int nextTokenType() {
99         return super.nextTokenType();
100     }
101 }
102