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