KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > persistence > antlr > preprocessor > Hierarchy


1 package persistence.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/license.html
6  *
7  */

8
9 import persistence.antlr.collections.impl.IndexedVector;
10
11 import java.util.Hashtable JavaDoc;
12 import java.util.Enumeration JavaDoc;
13 import java.io.*;
14
15 import persistence.antlr.*;
16 import persistence.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 persistence.antlr.Tool antlrTool;
25
26     public Hierarchy(persistence.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.setTool(antlrTool);
102         pp.setFilename(file);
103
104         // populate the hierarchy with class(es) read in
105
try {
106             pp.grammarFile(this, file);
107         }
108         catch (TokenStreamException io) {
109             antlrTool.toolError("Token stream error reading grammar(s):\n" + io);
110         }
111         catch (ANTLRException se) {
112             antlrTool.toolError("error reading grammar(s):\n" + se);
113         }
114     }
115
116     /** Return true if hierarchy is complete, false if not */
117     public boolean verifyThatHierarchyIsComplete() {
118         boolean complete = true;
119         // Make a pass to ensure all grammars are defined
120
for (Enumeration JavaDoc e = symbols.elements(); e.hasMoreElements();) {
121             Grammar c = (Grammar)e.nextElement();
122             if (c.getSuperGrammarName() == null) {
123                 continue; // at root: ignore predefined roots
124
}
125             Grammar superG = c.getSuperGrammar();
126             if (superG == null) {
127                 antlrTool.toolError("grammar " + c.getSuperGrammarName() + " not defined");
128                 complete = false;
129                 symbols.remove(c.getName()); // super not defined, kill sub
130
}
131         }
132
133         if (!complete) return false;
134
135         // Make another pass to set the 'type' field of each grammar
136
// This makes it easy later to ask a grammar what its type
137
// is w/o having to search hierarchy.
138
for (Enumeration JavaDoc e = symbols.elements(); e.hasMoreElements();) {
139             Grammar c = (Grammar)e.nextElement();
140             if (c.getSuperGrammarName() == null) {
141                 continue; // ignore predefined roots
142
}
143             c.setType(findRoot(c).getName());
144         }
145
146         return true;
147     }
148
149     public persistence.antlr.Tool getTool() {
150         return antlrTool;
151     }
152
153     public void setTool(persistence.antlr.Tool antlrTool) {
154         this.antlrTool = antlrTool;
155     }
156 }
157
Popular Tags