001: package persistence.antlr.preprocessor;
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 persistence.antlr.collections.impl.IndexedVector;
010:
011: import java.util.Hashtable;
012: import java.util.Enumeration;
013: import java.io.*;
014:
015: import persistence.antlr.*;
016: import persistence.antlr.preprocessor.Grammar;
017:
018: public class Hierarchy {
019: protected Grammar LexerRoot = null;
020: protected Grammar ParserRoot = null;
021: protected Grammar TreeParserRoot = null;
022: protected Hashtable symbols; // table of grammars
023: protected Hashtable files; // table of grammar files read in
024: protected persistence.antlr.Tool antlrTool;
025:
026: public Hierarchy(persistence.antlr.Tool tool) {
027: this .antlrTool = tool;
028: LexerRoot = new Grammar(tool, "Lexer", null, null);
029: ParserRoot = new Grammar(tool, "Parser", null, null);
030: TreeParserRoot = new Grammar(tool, "TreeParser", null, null);
031: symbols = new Hashtable(10);
032: files = new Hashtable(10);
033:
034: LexerRoot.setPredefined(true);
035: ParserRoot.setPredefined(true);
036: TreeParserRoot.setPredefined(true);
037:
038: symbols.put(LexerRoot.getName(), LexerRoot);
039: symbols.put(ParserRoot.getName(), ParserRoot);
040: symbols.put(TreeParserRoot.getName(), TreeParserRoot);
041: }
042:
043: public void addGrammar(Grammar gr) {
044: gr.setHierarchy(this );
045: // add grammar to hierarchy
046: symbols.put(gr.getName(), gr);
047: // add grammar to file.
048: GrammarFile f = getFile(gr.getFileName());
049: f.addGrammar(gr);
050: }
051:
052: public void addGrammarFile(GrammarFile gf) {
053: files.put(gf.getName(), gf);
054: }
055:
056: public void expandGrammarsInFile(String fileName) {
057: GrammarFile f = getFile(fileName);
058: for (Enumeration e = f.getGrammars().elements(); e
059: .hasMoreElements();) {
060: Grammar g = (Grammar) e.nextElement();
061: g.expandInPlace();
062: }
063: }
064:
065: public Grammar findRoot(Grammar g) {
066: if (g.getSuperGrammarName() == null) { // at root
067: return g;
068: }
069: // return root of super.
070: Grammar sg = g.getSuperGrammar();
071: if (sg == null)
072: return g; // return this grammar if super missing
073: return findRoot(sg);
074: }
075:
076: public GrammarFile getFile(String fileName) {
077: return (GrammarFile) files.get(fileName);
078: }
079:
080: public Grammar getGrammar(String gr) {
081: return (Grammar) symbols.get(gr);
082: }
083:
084: public static String optionsToString(IndexedVector options) {
085: String s = "options {" + System.getProperty("line.separator");
086: for (Enumeration e = options.elements(); e.hasMoreElements();) {
087: s += (Option) e.nextElement()
088: + System.getProperty("line.separator");
089: }
090: s += "}" + System.getProperty("line.separator")
091: + System.getProperty("line.separator");
092: return s;
093: }
094:
095: public void readGrammarFile(String file)
096: throws FileNotFoundException {
097: Reader grStream = new BufferedReader(new FileReader(file));
098: addGrammarFile(new GrammarFile(antlrTool, file));
099:
100: // Create the simplified grammar lexer/parser
101: PreprocessorLexer ppLexer = new PreprocessorLexer(grStream);
102: ppLexer.setFilename(file);
103: Preprocessor pp = new Preprocessor(ppLexer);
104: pp.setTool(antlrTool);
105: pp.setFilename(file);
106:
107: // populate the hierarchy with class(es) read in
108: try {
109: pp.grammarFile(this , file);
110: } catch (TokenStreamException io) {
111: antlrTool
112: .toolError("Token stream error reading grammar(s):\n"
113: + io);
114: } catch (ANTLRException se) {
115: antlrTool.toolError("error reading grammar(s):\n" + se);
116: }
117: }
118:
119: /** Return true if hierarchy is complete, false if not */
120: public boolean verifyThatHierarchyIsComplete() {
121: boolean complete = true;
122: // Make a pass to ensure all grammars are defined
123: for (Enumeration e = symbols.elements(); e.hasMoreElements();) {
124: Grammar c = (Grammar) e.nextElement();
125: if (c.getSuperGrammarName() == null) {
126: continue; // at root: ignore predefined roots
127: }
128: Grammar super G = c.getSuperGrammar();
129: if (super G == null) {
130: antlrTool.toolError("grammar "
131: + c.getSuperGrammarName() + " not defined");
132: complete = false;
133: symbols.remove(c.getName()); // super not defined, kill sub
134: }
135: }
136:
137: if (!complete)
138: return false;
139:
140: // Make another pass to set the 'type' field of each grammar
141: // This makes it easy later to ask a grammar what its type
142: // is w/o having to search hierarchy.
143: for (Enumeration e = symbols.elements(); e.hasMoreElements();) {
144: Grammar c = (Grammar) e.nextElement();
145: if (c.getSuperGrammarName() == null) {
146: continue; // ignore predefined roots
147: }
148: c.setType(findRoot(c).getName());
149: }
150:
151: return true;
152: }
153:
154: public persistence.antlr.Tool getTool() {
155: return antlrTool;
156: }
157:
158: public void setTool(persistence.antlr.Tool antlrTool) {
159: this.antlrTool = antlrTool;
160: }
161: }
|