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.IOException ; 15 16 class Grammar { 17 protected String name; 18 protected String fileName; protected String superGrammar; protected String type; protected IndexedVector rules; protected IndexedVector options; protected String tokenSection; protected String preambleAction; protected String memberAction; protected Hierarchy hier; protected boolean predefined = false; protected boolean alreadyExpanded = false; 29 protected boolean specifiedVocabulary = false; 31 protected String importVocab = null; 32 protected String exportVocab = null; 33 protected antlr.Tool antlrTool; 34 35 public Grammar(antlr.Tool tool, String name, String superGrammar, IndexedVector rules) { 36 this.name = name; 37 this.superGrammar = superGrammar; 38 this.rules = rules; 39 this.antlrTool = tool; 40 } 41 42 public void addOption(Option o) { 43 if (options == null) { options = new IndexedVector(); 45 } 46 options.appendElement(o.getName(), o); 47 } 48 49 public void addRule(Rule r) { 50 rules.appendElement(r.getName(), r); 51 } 52 53 58 public void expandInPlace() { 59 if (alreadyExpanded) { 61 return; 62 } 63 64 Grammar superG = getSuperGrammar(); 66 if (superG == null) 67 return; if (exportVocab == null) { 69 exportVocab = getName(); 71 } 72 if (superG.isPredefined()) 73 return; superG.expandInPlace(); 75 76 alreadyExpanded = true; 78 GrammarFile gf = hier.getFile(getFileName()); 80 gf.setExpanded(true); 81 82 IndexedVector inhRules = superG.getRules(); 84 for (Enumeration e = inhRules.elements(); e.hasMoreElements();) { 85 Rule r = (Rule)e.nextElement(); 86 inherit(r, superG); 87 } 88 89 IndexedVector inhOptions = superG.getOptions(); 92 if (inhOptions != null) { 93 for (Enumeration e = inhOptions.elements(); e.hasMoreElements();) { 94 Option o = (Option)e.nextElement(); 95 inherit(o, superG); 96 } 97 } 98 99 if ((options != null && options.getElement("importVocab") == null) || options == null) { 101 Option inputV = new Option("importVocab", superG.exportVocab + ";", this); 103 addOption(inputV); 104 String originatingGrFileName = superG.getFileName(); 106 String path = antlrTool.pathToFile(originatingGrFileName); 107 String superExportVocabFileName = path + superG.exportVocab + 108 antlr.CodeGenerator.TokenTypesFileSuffix + 109 antlr.CodeGenerator.TokenTypesFileExt; 110 String newImportVocabFileName = antlrTool.fileMinusPath(superExportVocabFileName); 111 if (path.equals("." + System.getProperty("file.separator"))) { 112 } 115 else { 116 try { 117 antlrTool.copyFile(superExportVocabFileName, newImportVocabFileName); 118 } 119 catch (IOException io) { 120 antlrTool.toolError("cannot find/copy importVocab file " + superExportVocabFileName); 121 return; 122 } 123 } 124 } 125 126 inherit(superG.memberAction, superG); 128 } 129 130 public String getFileName() { 131 return fileName; 132 } 133 134 public String getName() { 135 return name; 136 } 137 138 public IndexedVector getOptions() { 139 return options; 140 } 141 142 public IndexedVector getRules() { 143 return rules; 144 } 145 146 public Grammar getSuperGrammar() { 147 if (superGrammar == null) return null; 148 Grammar g = (Grammar)hier.getGrammar(superGrammar); 149 return g; 150 } 151 152 public String getSuperGrammarName() { 153 return superGrammar; 154 } 155 156 public String getType() { 157 return type; 158 } 159 160 public void inherit(Option o, Grammar superG) { 161 if (o.getName().equals("importVocab") || 163 o.getName().equals("exportVocab")) { 164 return; 165 } 166 167 Option overriddenOption = null; 168 if (options != null) { overriddenOption = (Option)options.getElement(o.getName()); 170 } 171 if (overriddenOption == null) { addOption(o); } 175 } 176 177 public void inherit(Rule r, Grammar superG) { 178 Rule overriddenRule = (Rule)rules.getElement(r.getName()); 180 if (overriddenRule != null) { 181 if (!overriddenRule.sameSignature(r)) { 183 antlrTool.warning("rule " + getName() + "." + overriddenRule.getName() + 185 " has different signature than " + 186 superG.getName() + "." + overriddenRule.getName()); 187 } 188 } 189 else { addRule(r); 191 } 192 } 193 194 public void inherit(String memberAction, Grammar superG) { 195 if (this.memberAction != null) return; if (memberAction != null) { this.memberAction = memberAction; 198 } 199 } 200 201 public boolean isPredefined() { 202 return predefined; 203 } 204 205 public void setFileName(String f) { 206 fileName = f; 207 } 208 209 public void setHierarchy(Hierarchy hier) { 210 this.hier = hier; 211 } 212 213 public void setMemberAction(String a) { 214 memberAction = a; 215 } 216 217 public void setOptions(IndexedVector options) { 218 this.options = options; 219 } 220 221 public void setPreambleAction(String a) { 222 preambleAction = a; 223 } 224 225 public void setPredefined(boolean b) { 226 predefined = b; 227 } 228 229 public void setTokenSection(String tk) { 230 tokenSection = tk; 231 } 232 233 public void setType(String t) { 234 type = t; 235 } 236 237 public String toString() { 238 String s = ""; 239 if (preambleAction != null) { 240 s += preambleAction; 241 } 242 if (superGrammar == null) { 243 return "class " + name + ";"; 244 } 245 String sup = ""; 246 s += "class " + name + " extends " + type + sup + ";" + 247 System.getProperty("line.separator") + 248 System.getProperty("line.separator"); 249 if (options != null) { 250 s += Hierarchy.optionsToString(options); 251 } 252 if (tokenSection != null) { 253 s += tokenSection + "\n"; 254 } 255 if (memberAction != null) { 256 s += memberAction + System.getProperty("line.separator"); 257 } 258 for (int i = 0; i < rules.size(); i++) { 259 Rule r = (Rule)rules.elementAt(i); 260 if (!getName().equals(r.enclosingGrammar.getName())) { 261 s += "// inherited from grammar " + r.enclosingGrammar.getName() + System.getProperty("line.separator"); 262 } 263 s += r + 264 System.getProperty("line.separator") + 265 System.getProperty("line.separator"); 266 } 267 return s; 268 } 269 } 270 | Popular Tags |