1 package antlr.preprocessor; 2 3 9 10 import antlr.collections.impl.IndexedVector; 11 12 import java.util.Hashtable ; 13 import java.util.Enumeration ; 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 symbols; protected Hashtable files; 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 (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.setFilename(file); 102 103 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 116 public boolean verifyThatHierarchyIsComplete() { 117 boolean complete = true; 118 for (Enumeration e = symbols.elements(); e.hasMoreElements();) { 120 Grammar c = (Grammar)e.nextElement(); 121 if (c.getSuperGrammarName() == null) { 122 continue; } 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()); } 130 } 131 132 if (!complete) return false; 133 134 for (Enumeration e = symbols.elements(); e.hasMoreElements();) { 138 Grammar c = (Grammar)e.nextElement(); 139 if (c.getSuperGrammarName() == null) { 140 continue; } 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 |