1 package persistence.antlr.preprocessor; 2 3 8 9 import persistence.antlr.collections.impl.IndexedVector; 10 11 import java.util.Hashtable ; 12 import java.util.Enumeration ; 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 symbols; protected Hashtable files; 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 (10); 32 files = new Hashtable (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 symbols.put(gr.getName(), gr); 47 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 fileName) { 57 GrammarFile f = getFile(fileName); 58 for (Enumeration 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) { return g; 67 } 68 Grammar sg = g.getSuperGrammar(); 70 if (sg == null) return g; return findRoot(sg); 72 } 73 74 public GrammarFile getFile(String fileName) { 75 return (GrammarFile)files.get(fileName); 76 } 77 78 public Grammar getGrammar(String gr) { 79 return (Grammar)symbols.get(gr); 80 } 81 82 public static String optionsToString(IndexedVector options) { 83 String s = "options {" + System.getProperty("line.separator"); 84 for (Enumeration 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 file) throws FileNotFoundException { 94 Reader grStream = new BufferedReader(new FileReader(file)); 95 addGrammarFile(new GrammarFile(antlrTool, file)); 96 97 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 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 117 public boolean verifyThatHierarchyIsComplete() { 118 boolean complete = true; 119 for (Enumeration e = symbols.elements(); e.hasMoreElements();) { 121 Grammar c = (Grammar)e.nextElement(); 122 if (c.getSuperGrammarName() == null) { 123 continue; } 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()); } 131 } 132 133 if (!complete) return false; 134 135 for (Enumeration e = symbols.elements(); e.hasMoreElements();) { 139 Grammar c = (Grammar)e.nextElement(); 140 if (c.getSuperGrammarName() == null) { 141 continue; } 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 |