001: package persistence.antlr;
002:
003: /* ANTLR Translator Generator
004: * Project led by Terence Parr at http://www.jGuru.com
005: * Software rights: http://www.antlr.org/license.html
006: *
007: */
008:
009: import java.io.*;
010: import java.util.Hashtable;
011: import java.util.Enumeration;
012:
013: import persistence.antlr.collections.impl.Vector;
014:
015: /** Static implementation of the TokenManager, used for importVocab option */
016: class ImportVocabTokenManager extends SimpleTokenManager implements
017: Cloneable {
018: private String filename;
019: protected Grammar grammar;
020:
021: // FIXME: it would be nice if the path to the original grammar file was
022: // also searched.
023: ImportVocabTokenManager(Grammar grammar, String filename_,
024: String name_, Tool tool_) {
025: // initialize
026: super (name_, tool_);
027:
028: this .grammar = grammar;
029: this .filename = filename_;
030:
031: // Figure out exactly where the file lives. Check $PWD first,
032: // and then search in -o <output_dir>.
033: //
034: File grammarFile = new File(filename);
035:
036: if (!grammarFile.exists()) {
037: grammarFile = new File(antlrTool.getOutputDirectory(),
038: filename);
039:
040: if (!grammarFile.exists()) {
041: antlrTool.panic("Cannot find importVocab file '"
042: + filename + "'");
043: }
044: }
045:
046: setReadOnly(true);
047:
048: // Read a file with lines of the form ID=number
049: try {
050: Reader fileIn = new BufferedReader(new FileReader(
051: grammarFile));
052: ANTLRTokdefLexer tokdefLexer = new ANTLRTokdefLexer(fileIn);
053: ANTLRTokdefParser tokdefParser = new ANTLRTokdefParser(
054: tokdefLexer);
055: tokdefParser.setTool(antlrTool);
056: tokdefParser.setFilename(filename);
057: tokdefParser.file(this );
058: } catch (FileNotFoundException fnf) {
059: antlrTool.panic("Cannot find importVocab file '" + filename
060: + "'");
061: } catch (RecognitionException ex) {
062: antlrTool.panic("Error parsing importVocab file '"
063: + filename + "': " + ex.toString());
064: } catch (TokenStreamException ex) {
065: antlrTool.panic("Error reading importVocab file '"
066: + filename + "'");
067: }
068: }
069:
070: public Object clone() {
071: ImportVocabTokenManager tm;
072: tm = (ImportVocabTokenManager) super .clone();
073: tm.filename = this .filename;
074: tm.grammar = this .grammar;
075: return tm;
076: }
077:
078: /** define a token. */
079: public void define(TokenSymbol ts) {
080: super .define(ts);
081: }
082:
083: /** define a token. Intended for use only when reading the importVocab file. */
084: public void define(String s, int ttype) {
085: TokenSymbol ts = null;
086: if (s.startsWith("\"")) {
087: ts = new StringLiteralSymbol(s);
088: } else {
089: ts = new TokenSymbol(s);
090: }
091: ts.setTokenType(ttype);
092: super .define(ts);
093: maxToken = (ttype + 1) > maxToken ? (ttype + 1) : maxToken; // record maximum token type
094: }
095:
096: /** importVocab token manager is read-only if output would be same as input */
097: public boolean isReadOnly() {
098: return readOnly;
099: }
100:
101: /** Get the next unused token type. */
102: public int nextTokenType() {
103: return super.nextTokenType();
104: }
105: }
|