KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > antlr > preprocessor > Hierarchy


1 package antlr.preprocessor;
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/preprocessor/Hierarchy.java#7 $
8  */

9
10 import antlr.collections.impl.IndexedVector;
11
12 import java.util.Hashtable JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.io.*;
15
16 import antlr.preprocessor.Grammar;
17
18 public class Hierarchy {
19     protected Grammar LexerRoot = null;
20     protected Grammar ParserRoot = null;
21     protected Grammar TreeParserRoot = null;
22     protected Hashtable JavaDoc symbols; // table of grammars
23
protected Hashtable JavaDoc files; // table of grammar files read in
24
protected antlr.Tool antlrTool;
25
26     public Hierarchy(antlr.Tool tool) {
27         this.antlrTool = tool;
28         LexerRoot = new Grammar(tool, "Lexer", null, null);
29         ParserRoot = new Grammar(tool, "Parser", null, null);
30         TreeParserRoot = new Grammar(tool, "TreeParser", null, null);
31         symbols = new Hashtable JavaDoc(10);
32         files = new Hashtable JavaDoc(10);
33
34         LexerRoot.setPredefined(true);
35         ParserRoot.setPredefined(true);
36         TreeParserRoot.setPredefined(true);
37
38         symbols.put(LexerRoot.getName(), LexerRoot);
39         symbols.put(ParserRoot.getName(), ParserRoot);
40         symbols.put(TreeParserRoot.getName(), TreeParserRoot);
41     }
42
43     public void addGrammar(Grammar gr) {
44         gr.setHierarchy(this);
45         // add grammar to hierarchy
46
symbols.put(gr.getName(), gr);
47         // add grammar to file.
48
GrammarFile f = getFile(gr.getFileName());
49         f.addGrammar(gr);
50     }
51
52     public void addGrammarFile(GrammarFile gf) {
53         files.put(gf.getName(), gf);
54     }
55
56     public void expandGrammarsInFile(String JavaDoc fileName) {
57         GrammarFile f = getFile(fileName);
58         for (Enumeration JavaDoc e = f.getGrammars().elements(); e.hasMoreElements();) {
59             Grammar g = (Grammar)e.nextElement();
60             g.expandInPlace();
61         }
62     }
63
64     public Grammar findRoot(Grammar g) {
65         if (g.getSuperGrammarName() == null) { // at root
66
return g;
67         }
68         // return root of super.
69
Grammar sg = g.getSuperGrammar();
70         if (sg == null) return g; // return this grammar if super missing
71
return findRoot(sg);
72     }
73
74     public GrammarFile getFile(String JavaDoc fileName) {
75         return (GrammarFile)files.get(fileName);
76     }
77
78     public Grammar getGrammar(String JavaDoc gr) {
79         return (Grammar)symbols.get(gr);
80     }
81
82     public static String JavaDoc optionsToString(IndexedVector options) {
83         String JavaDoc s = "options {" + System.getProperty("line.separator");
84         for (Enumeration JavaDoc e = options.elements(); e.hasMoreElements();) {
85             s += (Option)e.nextElement() + System.getProperty("line.separator");
86         }
87         s += "}" +
88             System.getProperty("line.separator") +
89             System.getProperty("line.separator");
90         return s;
91     }
92
93     public void readGrammarFile(String JavaDoc file) throws FileNotFoundException {
94         Reader grStream = new BufferedReader(new FileReader(file));
95         addGrammarFile(new GrammarFile(antlrTool, file));
96
97         // Create the simplified grammar lexer/parser
98
PreprocessorLexer ppLexer = new PreprocessorLexer(grStream);
99         ppLexer.setFilename(file);
100         Preprocessor pp = new Preprocessor(ppLexer);
101         pp.setFilename(file);
102
103         // populate the hierarchy with class(es) read in
104
try {
105             pp.grammarFile(this, file);
106         }
107         catch (TokenStreamException io) {
108             antlrTool.toolError("Token stream error reading grammar(s):\n" + io);
109         }
110         catch (ANTLRException se) {
111             antlrTool.toolError("error reading grammar(s):\n" + se);
112         }
113     }
114
115     /** Return true if hierarchy is complete, false if not */
116     public boolean verifyThatHierarchyIsComplete() {
117         boolean complete = true;
118         // Make a pass to ensure all grammars are defined
119
for (Enumeration JavaDoc e = symbols.elements(); e.hasMoreElements();) {
120             Grammar c = (Grammar)e.nextElement();
121             if (c.getSuperGrammarName() == null) {
122                 continue; // at root: ignore predefined roots
123
}
124             Grammar superG = c.getSuperGrammar();
125             if (superG == null) {
126                 antlrTool.toolError("grammar " + c.getSuperGrammarName() + " not defined");
127                 complete = false;
128                 symbols.remove(c.getName()); // super not defined, kill sub
129
}
130         }
131
132         if (!complete) return false;
133
134         // Make another pass to set the 'type' field of each grammar
135
// This makes it easy later to ask a grammar what its type
136
// is w/o having to search hierarchy.
137
for (Enumeration JavaDoc e = symbols.elements(); e.hasMoreElements();) {
138             Grammar c = (Grammar)e.nextElement();
139             if (c.getSuperGrammarName() == null) {
140                 continue; // ignore predefined roots
141
}
142             c.setType(findRoot(c).getName());
143         }
144
145         return true;
146     }
147
148     public antlr.Tool getTool() {
149         return antlrTool;
150     }
151
152     public void setTool(antlr.Tool antlrTool) {
153         this.antlrTool = antlrTool;
154     }
155 }
156
Popular Tags