1 package fri.patterns.interpreter.parsergenerator.util; 2 3 import java.util.*; 4 import java.io.*; 5 import fri.patterns.interpreter.parsergenerator.syntax.*; 6 7 28 29 public class SemanticSkeletonGenerator 30 { 31 private static Hashtable keyWords; 32 private BufferedWriter bw; 33 34 42 public SemanticSkeletonGenerator(Syntax syntax, String className, String pkgName, Writer skeletonOutput) 43 throws Exception 44 { 45 if (skeletonOutput instanceof BufferedWriter) 46 bw = (BufferedWriter)skeletonOutput; 47 else 48 bw = new BufferedWriter(skeletonOutput); 49 50 if (pkgName != null && pkgName.length() > 0) { 52 writeLine("package "+pkgName+";"); 53 writeLine(); 54 } 55 writeLine("/** "); 56 writeLine(" * IMPLEMENT ME: Semantic skeleton will not be overwritten, generated"); 57 writeLine(" * at "+new Date()+"\n"); 58 writeLine(" * by fri.patterns.interpreter.parsergenerator.util.SemanticSkeletonGenerator."); 59 writeLine(" */"); 60 writeLine(); 61 writeLine("import fri.patterns.interpreter.parsergenerator.semantics.ReflectSemantic;"); 62 writeLine(); 63 writeLine("public class "+className+" extends ReflectSemantic"); 64 writeLine("{"); 65 66 Map methods = new Hashtable(); 67 68 for (int i = 0; i < syntax.size(); i++) { 70 Rule rule = syntax.getRule(i); 71 72 String nonterminal = rule.getNonterminal(); 73 74 checkMethodNameAgainstKeywords(nonterminal); 77 int countArgs = rule.rightSize(); 79 String signature = nonterminal+"("+countArgs+")"; 80 boolean alreadyHaveMethod = false; 81 82 write("\t"); 83 84 if (methods.get(signature) != null) { 85 alreadyHaveMethod = true; 86 write("//"); 88 } 89 90 write("public Object "+nonterminal+"("); 93 List args = new ArrayList(countArgs); 94 95 for (int j = 0; j < rule.rightSize(); j++) { 96 String s = rule.getRightSymbol(j); 97 98 if (j > 1 || s != null && s.trim().length() > 0) 99 write((j == 1 ? "" : ", ")+"Object "+getValidParamName(s, args, j)); 100 } 101 writeLine(")"); 103 104 if (alreadyHaveMethod == false) { 105 writeLine("\t{"); 106 107 write("\t\tSystem.err.println(\""+nonterminal+" ("); 108 for (int j = 0; j < args.size(); j++) { 109 write((j == 0 ? "" : ", ")+"'\"+"+args.get(j)+"+\"'"); 110 } 111 writeLine(")\");"); 112 113 writeLine("\t\treturn \""+signature+"\";"); writeLine("\t}"); 115 116 methods.put(signature, signature); 117 } 118 119 writeLine(); 120 } 121 122 writeLine(); 124 writeLine("}"); 125 writeLine(); 126 127 bw.close(); 128 } 129 130 131 132 private void writeLine() 133 throws IOException 134 { 135 bw.newLine(); 136 } 137 138 private void writeLine(String line) 139 throws IOException 140 { 141 bw.write(line, 0, line.length()); 142 writeLine(); 143 } 144 145 private void write(String s) 146 throws IOException 147 { 148 bw.write(s, 0, s.length()); 149 } 150 151 152 private String getValidParamName(String symbol, List argsUntilNow, int position) { 155 String s = SymbolToName.makeIdentifier(symbol, true); 156 157 if (argsUntilNow.indexOf(s) >= 0) 158 s = s+position; 159 160 argsUntilNow.add(s); 161 162 return s; 163 } 164 165 private void checkMethodNameAgainstKeywords(String method) { 167 if (keyWords == null) { 168 keyWords = new Hashtable(); 169 keyWords.put("boolean", ""); 170 keyWords.put("byte", ""); 171 keyWords.put("short", ""); 172 keyWords.put("int", ""); 173 keyWords.put("long", ""); 174 keyWords.put("char", ""); 175 keyWords.put("float", ""); 176 keyWords.put("double", ""); 177 keyWords.put("abstract", ""); 178 keyWords.put("break", ""); 179 keyWords.put("case", ""); 180 keyWords.put("catch", ""); 181 keyWords.put("class", ""); 182 keyWords.put("const", ""); 183 keyWords.put("continue", ""); 184 keyWords.put("default", ""); 185 keyWords.put("do", ""); 186 keyWords.put("else", ""); 187 keyWords.put("extends", ""); 188 keyWords.put("false", ""); 189 keyWords.put("final", ""); 190 keyWords.put("finally", ""); 191 keyWords.put("for", ""); 192 keyWords.put("goto", ""); 193 keyWords.put("if", ""); 194 keyWords.put("implements", ""); 195 keyWords.put("import", ""); 196 keyWords.put("instanceof", ""); 197 keyWords.put("interface", ""); 198 keyWords.put("native", ""); 199 keyWords.put("new", ""); 200 keyWords.put("null", ""); 201 keyWords.put("package", ""); 202 keyWords.put("public", ""); 203 keyWords.put("protected", ""); 204 keyWords.put("private", ""); 205 keyWords.put("return", ""); 206 keyWords.put("static", ""); 207 keyWords.put("strictfp", ""); 208 keyWords.put("super", ""); 209 keyWords.put("switch", ""); 210 keyWords.put("synchronized", ""); 211 keyWords.put("this", ""); 212 keyWords.put("throw", ""); 213 keyWords.put("throws", ""); 214 keyWords.put("transient", ""); 215 keyWords.put("true", ""); 216 keyWords.put("try", ""); 217 keyWords.put("volatile", ""); 218 keyWords.put("void", ""); 219 keyWords.put("while", ""); 220 } 221 222 if (keyWords.get(method) != null) 223 throw new IllegalArgumentException ("Nonterminal \""+method+"\" is a Java keyword and can not be used as methodname with AbstractSemantic!"); 224 } 225 226 } 227 | Popular Tags |