1 package java_cup; 2 3 import org.apache.tools.ant.BuildException; 4 import org.apache.tools.ant.Task; 5 import java.util.Vector ; 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.InputStream ; 9 import java.io.IOException ; 10 11 180 public class JavaCUPTask extends Task { 181 182 String parserClass; 183 String symbolsClass; 184 String inputFileName; 185 File srcDir = new File (System.getProperty("user.dir")); 186 boolean newer = true; 187 Vector argVector = new Vector (); 188 189 public void execute() throws BuildException { 190 try { 191 String [] args = getArgs(); 192 Main.main(args, inputFileName); 193 } catch (IOException e) { 194 throw new BuildException(e.toString()); 195 } catch (internal_error e) { 196 throw new BuildException(e.toString()); 197 } catch (Exception e) { 198 throw new BuildException(e.toString()); 199 } 200 } 201 202 private String [] getArgs() 203 { 204 if (parserClass == null) { 205 parserClass = Main.fileNameToClass(relativeFileName(inputFileName)); 206 } 207 String packageName, className; 208 int lastDot = parserClass.lastIndexOf("."); 209 if (lastDot < 0) { 210 packageName = null; 211 className = parserClass; 212 } else { 213 packageName = parserClass.substring(0, lastDot); 214 className = parserClass.substring(lastDot + 1); 215 } 216 if (packageName != null) { 217 argVector.addElement("-package"); 218 argVector.addElement(packageName); 219 } 220 argVector.addElement("-parser"); 221 argVector.addElement(className); 222 if (symbolsClass == null) { 223 symbolsClass = className + "Sym"; 224 } 225 argVector.addElement("-symbols"); 226 argVector.addElement(symbolsClass); 227 if (inputFileName == null) { 228 throw new BuildException("You must specify inputFile."); 229 } 230 if (newer) { 231 argVector.addElement("-newer"); 232 } 233 String [] args = new String [argVector.size()]; 234 argVector.copyInto(args); 235 return args; 236 } 237 238 239 public void setParserClass(String parserClass) { 240 this.parserClass = parserClass; 241 } 242 243 244 public void setSymbolsClass(String symbolsClass) { 245 this.symbolsClass = symbolsClass; 246 } 247 248 249 public void setDump(boolean dump) { 250 if (dump) { 251 argVector.addElement("-dump"); 252 } 253 } 254 255 256 public void setDumpGrammar(boolean dumpGrammar) { 257 if (dumpGrammar) { 258 argVector.addElement("-dump_grammar"); 259 } 260 } 261 262 263 public void setDumpStates(boolean dumpStates) { 264 if (dumpStates) { 265 argVector.addElement("-dump_states"); 266 } 267 } 268 269 270 public void setDumpTables(boolean dumpTables) { 271 if (dumpTables) { 272 argVector.addElement("-dump_tables"); 273 } 274 } 275 276 277 public void setNonterms(boolean nonterms) { 278 if (nonterms) { 279 argVector.addElement("-nonterms"); 280 } 281 } 282 283 284 public void setExpect(int expect) { 285 argVector.addElement("-expect"); 286 argVector.addElement(expect + ""); 287 } 288 289 290 public void setCompactRed(boolean compactRed) { 291 if (compactRed) { 292 argVector.addElement("-compact_red"); 293 } 294 } 295 296 297 public void setWarn(boolean warn) { 298 if (!warn) { 299 argVector.addElement("-nowarn"); 300 } 301 } 302 303 304 public void setSummary(boolean summary) { 305 if (!summary) { 306 argVector.addElement("-nosummary"); 307 } 308 } 309 310 311 public void setPositions(boolean positions) { 312 if (positions) { 313 argVector.addElement("-positions"); 314 } 315 } 316 317 318 public void setScanner(boolean scanner) { 319 if (!scanner) { 320 argVector.addElement("-noscanner"); 321 } 322 } 323 324 325 public void setProgress(boolean progress) { 326 if (progress) { 327 argVector.addElement("-progress"); 328 } 329 } 330 331 332 public void setTime(boolean time) { 333 if (time) { 334 argVector.addElement("-time"); 335 } 336 } 337 338 339 public void setDebug(boolean debug) { 340 if (debug) { 341 argVector.addElement("-debug"); 342 } 343 } 344 345 346 public void setVersion(boolean version) { 347 if (version) { 348 argVector.addElement("-version"); 349 } 350 } 351 352 353 public void setNewer(boolean newer) { 354 this.newer = newer; 355 } 356 357 358 public void setInterface(boolean _interface) { 359 if (_interface) { 360 argVector.addElement("-interface"); 361 } 362 } 363 364 365 public void setInput(File inputFile) { 366 inputFileName = inputFile.toString(); 367 } 368 369 370 public void setSrcDir(File srcDir) { 371 this.srcDir = srcDir; 372 argVector.addElement("-srcdir"); 373 argVector.addElement(srcDir.toString()); 374 } 375 376 380 String relativeFileName(String fileName) 381 { 382 char fileSep = System.getProperty("file.separator").charAt(0); String srcDirName = srcDir.toString() + fileSep; if (fileName.toUpperCase().startsWith(srcDirName.toUpperCase())) { 385 fileName = fileName.substring(srcDirName.length()); 386 } 387 return fileName; 388 } 389 } 390 391 392 | Popular Tags |