KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java_cup > anttask > CUPTask


1 /**
2  * java_cup.anttask.CUPTask.java
3  *
4  * @author Michael Petter, 2003
5  *
6  * Ant-Task for CUP Parser Generator for Java
7  * -- tested with Ant 1.5.1;
8  * -- compiles with javac -classpath .:${ANT_HOME}/lib/ant.jar java_cup.anttask.CUPTask.java
9  * -- overrides org.apache.tools.ant.taskdefs.Java
10  * -- providing cool interface to CUP
11  * -- mapping all existing parameters to attributes
12  * -- trys to add new useful features to CUP, like
13  * - automatic package discovery
14  * - re-generate .java only when necessary
15  * - possibility to generate into a dest-directory
16  *
17  * my code is not perfect (in some cases it is pretty
18  * ugly :-) ), but i didn't encounter any major error
19  * until now
20  */

21
22 package java_cup.anttask;
23
24 import org.apache.tools.ant.Task;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.types.Path;
27
28 import java.util.List JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileReader JavaDoc;
32 import java.io.BufferedReader JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.net.URL JavaDoc;
35
36 import java_cup.version;
37
38 public class CUPTask extends Task
39 {
40     private String JavaDoc srcfile=null;
41     private String JavaDoc parser=null;
42     private String JavaDoc _package=null;
43     private String JavaDoc symbols=null;
44     private String JavaDoc destdir=null;
45     private boolean _interface=false;
46     private boolean nonterms=false;
47     private String JavaDoc expect=null;
48     private boolean compact_red=false;
49     private boolean nowarn=false;
50     private boolean nosummary=false;
51     private boolean progress=false;
52     private boolean dump_grammar=false;
53     private boolean dump_states=false;
54     private boolean dump_tables=false;
55     private boolean dump=false;
56     private boolean time=false;
57     private boolean debug=false;
58     private boolean nopositions=false;
59     private boolean noscanner=false;
60     private boolean force=false;
61     private boolean quiet=false;
62   
63     /**
64      * executes the task
65      * parses all attributes and validates options...
66      *
67      */

68
69     public void execute() throws BuildException
70     {
71         List JavaDoc sc = new ArrayList JavaDoc(); // sc = simulated commandline
72
// here, we parse our elements
73
if (parser!=null) { sc.add("-parser"); sc.add(parser);}
74         else parser="parser"; // set the default name to check actuality
75
if (_package!=null){ sc.add("-package"); sc.add(_package); }
76     if (symbols!=null) { sc.add("-symbols"); sc.add(symbols); }
77         else symbols="sym";
78     if (expect!=null) { sc.add("-expect"); sc.add(expect); }
79     if (_interface) { sc.add("-interface"); }
80     if (nonterms) { sc.add("-nonterms"); }
81     if (compact_red) { sc.add("-compact_red"); }
82     if (nowarn) { sc.add("-nowarn"); }
83     if (nosummary) { sc.add("-nosummary");}
84     if (progress) { sc.add("-progress"); }
85     if (dump_grammar) { sc.add("-dump_grammar"); }
86     if (dump_states) { sc.add("-dump_states"); }
87     if (dump_tables) { sc.add("-dump_tables"); }
88     if (dump) { sc.add("-dump"); }
89     if (time) { sc.add("-time"); }
90     if (debug) { sc.add("-debug"); }
91     if (nopositions) { sc.add("-nopositions"); }
92     if (noscanner) { sc.add("-noscanner"); }
93     if (!quiet) log ("This is "+version.title_str);
94         if (!quiet) log ("Authors : "+version.author_str);
95     if (!quiet) log ("Bugreports to petter@cs.tum.edu");
96
97     // look for package name and add to destdir
98
String JavaDoc packagename = inspect(srcfile);
99     
100
101     // now, that's sweet:
102
if (destdir==null) {
103         destdir=System.getProperty("user.dir");
104         if (!quiet) log("No destination directory specified; using working directory: "+destdir);
105     }
106     File JavaDoc dest = new File JavaDoc(destdir+packagename);
107     if (!(dest).exists()) {
108         if (!quiet) log("Destination directory didn't exist; creating new one: "+destdir+packagename);
109         dest.mkdirs();
110         force=true;
111     }
112     else {
113         if (force&& !quiet) { log("anyway, this generation will be processed because of option force set to \"true\""); }
114         else { if (!quiet) log("checking, whether this run is necessary"); }
115         // let's check, whether there exists any Parser fragment...
116
File JavaDoc parserfile = new File JavaDoc(destdir+packagename,parser+".java");
117         File JavaDoc symfile = new File JavaDoc(destdir+packagename,symbols+".java");
118         File JavaDoc cupfile = new File JavaDoc(srcfile);
119         
120         if (!parserfile.exists() || !symfile.exists()) {
121         if (!quiet) log("Either Parserfile or Symbolfile didn't exist");
122         force=true;
123         }else { if (!quiet) log("Parserfile and symbolfile are existing"); }
124         
125         
126         if (parserfile.lastModified()<=cupfile.lastModified()) {
127         if (!quiet) log("Parserfile "+parserfile+" isn't actual");
128         force=true;
129         }else { if (!quiet) log("Parserfile "+parserfile+" is actual"); }
130         
131         if (symfile.lastModified()<=cupfile.lastModified()) {
132         if (!quiet) log("Symbolfile "+symfile+" isn't actual");
133         force=true;
134         }else { if (!quiet) log("Symbolfile"+symfile+" is actual"); }
135         
136         
137         if (!force) {
138         if (!quiet) log("skipping generation of "+srcfile);
139         if (!quiet) log("use option force=\"true\" to override");
140         return;
141         }
142     }
143         
144     sc.add("-destdir");
145         sc.add(dest.getAbsolutePath());
146         
147         // also catch the not existing input file
148
if (srcfile==null) throw new BuildException("Input file needed: Specify <cup srcfile=\"myfile.cup\"> ");
149     if (!(new File JavaDoc(srcfile)).exists()) throw new BuildException("Input file not found: srcfile=\""+srcfile+"\" ");
150     
151         sc.add(srcfile);
152     String JavaDoc[] args = new String JavaDoc[sc.size()];
153         for (int i=0;i<args.length;i++) args[i]=(String JavaDoc)sc.get(i);
154         
155
156     try {
157             java_cup.Main.main(args);
158         }catch(Exception JavaDoc e){
159             log("CUP error occured int CUP task: "+e);
160         }
161     
162     // this is a dirty hack to determine the apropriate class path
163
// URL url = CUPTask.class.getResource("/java_cup/Main.class");
164
// String path = url.getPath().substring(0,url.getPath().length()-20);
165
// // if called from a .jar or .zip remove the last "!"
166
// if (path.endsWith("!")) path=path.substring(0,path.length()-1);
167
// createClasspath().setPath(path);
168
//
169
// setFailonerror(true);
170
// setFork(true);
171
//
172

173     // here, we prepare for calling CUP
174
// setClassname("java_cup.Main");
175

176     // let's call CUP
177
// super.execute();
178

179     }
180
181     /**
182      * Let's search for package name
183      *
184      * @param cupfile where we have to search for the package name
185      *
186      * @return the package folder structure
187      */

188     protected String JavaDoc inspect(String JavaDoc cupfile){
189     try{
190     BufferedReader JavaDoc br = new BufferedReader JavaDoc(new FileReader JavaDoc(cupfile));
191     while (br.ready()){
192         String JavaDoc line = br.readLine();
193         if ((line.startsWith("package"))&&(line.indexOf(";")!=-1))
194         {
195             String JavaDoc result = line.substring(8,line.indexOf(";"));
196             result = result.replace('.',System.getProperty("file.separator").charAt(0));
197             return System.getProperty("file.separator") + result;
198         }
199         
200     }
201     }catch (IOException JavaDoc ioe){
202     }
203     return "";
204     }
205
206     /**
207      * Gets the value of quiet
208      *
209      * @return the value of quiet
210      */

211     public boolean getQuiet() {
212     return this.quiet;
213     }
214
215     /**
216      * Sets the value of quiet
217      *
218      * @param arg_quiet Value to assign to this.quiet
219      */

220     public void setQuiet(boolean argquiet) {
221     this.quiet = argquiet;
222     }
223     /**
224      * Gets the value of force
225      *
226      * @return the value of force
227      */

228     public boolean getForce() {
229     return this.force;
230     }
231
232     /**
233      * Sets the value of force
234      *
235      * @param arg_package Value to assign to this.force
236      */

237     public void setForce(boolean argforce) {
238     this.force = argforce;
239     }
240     /**
241      * Gets the value of _package
242      *
243      * @return the value of _package
244      */

245     public String JavaDoc getPackage() {
246     return this._package;
247     }
248
249     /**
250      * Sets the value of _package
251      *
252      * @param arg_package Value to assign to this._package
253      */

254     public void setPackage(String JavaDoc arg_package) {
255     this._package = arg_package;
256     }
257
258     /**
259      * Gets the value of destdir
260      *
261      * @return the value of destdir
262      */

263     public String JavaDoc getDestdir() {
264     return this.destdir;
265     }
266
267     /**
268      * Sets the value of destdir
269      *
270      * @param arg_package Value to assign to this.destdir
271      */

272     public void setDestdir(String JavaDoc destdir) {
273     this.destdir = destdir;
274     }
275
276     /**
277      * Gets the value of _interface
278      *
279      * @return the value of _interface
280      */

281     public boolean isInterface() {
282     return this._interface;
283     }
284
285     /**
286      * Sets the value of _interface
287      *
288      * @param arg_interface Value to assign to this._interface
289      */

290     public void setInterface(boolean arg_interface) {
291     this._interface = arg_interface;
292     }
293
294     /**
295      * Get the Srcfile value.
296      * @return the Srcfile value.
297      */

298     public String JavaDoc getSrcfile() {
299     return srcfile;
300     }
301
302     /**
303      * Set the Srcfile value.
304      * @param newSrcfile The new Srcfile value.
305      */

306     public void setSrcfile(String JavaDoc newSrcfile) {
307     this.srcfile = newSrcfile;
308     }
309
310   
311
312     /**
313      * Gets the value of parser
314      *
315      * @return the value of parser
316      */

317     public String JavaDoc getParser() {
318     return this.parser;
319     }
320
321     /**
322      * Sets the value of parser
323      *
324      * @param argParser Value to assign to this.parser
325      */

326     public void setParser(String JavaDoc argParser){
327     this.parser = argParser;
328     }
329
330     /**
331      * Gets the value of symbols
332      *
333      * @return the value of symbols
334      */

335     public String JavaDoc getSymbols() {
336     return this.symbols;
337     }
338
339     /**
340      * Sets the value of symbols
341      *
342      * @param argSymbols Value to assign to this.symbols
343      */

344     public void setSymbols(String JavaDoc argSymbols){
345     this.symbols = argSymbols;
346     }
347
348     /**
349      * Gets the value of nonterms
350      *
351      * @return the value of nonterms
352      */

353     public boolean isNonterms() {
354     return this.nonterms;
355     }
356
357     /**
358      * Sets the value of nonterms
359      *
360      * @param argNonterms Value to assign to this.nonterms
361      */

362     public void setNonterms(boolean argNonterms){
363     this.nonterms = argNonterms;
364     }
365
366     /**
367      * Gets the value of expect
368      *
369      * @return the value of expect
370      */

371     public String JavaDoc getExpect() {
372     return this.expect;
373     }
374
375     /**
376      * Sets the value of expect
377      *
378      * @param argExpect Value to assign to this.expect
379      */

380     public void setExpect(String JavaDoc argExpect){
381     this.expect = argExpect;
382     }
383
384     /**
385      * Gets the value of compact_red
386      *
387      * @return the value of compact_red
388      */

389     public boolean isCompact_red() {
390     return this.compact_red;
391     }
392
393     /**
394      * Sets the value of compact_red
395      *
396      * @param argCompact_red Value to assign to this.compact_red
397      */

398     public void setCompact_red(boolean argCompact_red){
399     this.compact_red = argCompact_red;
400     }
401
402     /**
403      * Gets the value of nowarn
404      *
405      * @return the value of nowarn
406      */

407     public boolean isNowarn() {
408     return this.nowarn;
409     }
410
411     /**
412      * Sets the value of nowarn
413      *
414      * @param argNowarn Value to assign to this.nowarn
415      */

416     public void setNowarn(boolean argNowarn){
417     this.nowarn = argNowarn;
418     }
419
420     /**
421      * Gets the value of nosummary
422      *
423      * @return the value of nosummary
424      */

425     public boolean isNosummary() {
426     return this.nosummary;
427     }
428
429     /**
430      * Sets the value of nosummary
431      *
432      * @param argNosummary Value to assign to this.nosummary
433      */

434     public void setNosummary(boolean argNosummary){
435     this.nosummary = argNosummary;
436     }
437
438     /**
439      * Gets the value of progress
440      *
441      * @return the value of progress
442      */

443     public boolean isProgress() {
444     return this.progress;
445     }
446
447     /**
448      * Sets the value of progress
449      *
450      * @param argProgress Value to assign to this.progress
451      */

452     public void setProgress(boolean argProgress){
453     this.progress = argProgress;
454     }
455
456     /**
457      * Gets the value of dump_grammar
458      *
459      * @return the value of dump_grammar
460      */

461     public boolean isDump_grammar() {
462     return this.dump_grammar;
463     }
464
465     /**
466      * Sets the value of dump_grammar
467      *
468      * @param argDump_grammar Value to assign to this.dump_grammar
469      */

470     public void setDump_grammar(boolean argDump_grammar){
471     this.dump_grammar = argDump_grammar;
472     }
473
474     /**
475      * Gets the value of dump_states
476      *
477      * @return the value of dump_states
478      */

479     public boolean isDump_states() {
480     return this.dump_states;
481     }
482
483     /**
484      * Sets the value of dump_states
485      *
486      * @param argDump_states Value to assign to this.dump_states
487      */

488     public void setDump_states(boolean argDump_states){
489     this.dump_states = argDump_states;
490     }
491
492     /**
493      * Gets the value of dump_tables
494      *
495      * @return the value of dump_tables
496      */

497     public boolean isDump_tables() {
498     return this.dump_tables;
499     }
500
501     /**
502      * Sets the value of dump_tables
503      *
504      * @param argDump_tables Value to assign to this.dump_tables
505      */

506     public void setDump_tables(boolean argDump_tables){
507     this.dump_tables = argDump_tables;
508     }
509
510     /**
511      * Gets the value of dump
512      *
513      * @return the value of dump
514      */

515     public boolean isDump() {
516     return this.dump;
517     }
518
519     /**
520      * Sets the value of dump
521      *
522      * @param argDump Value to assign to this.dump
523      */

524     public void setDump(boolean argDump){
525     this.dump = argDump;
526     }
527
528     /**
529      * Gets the value of time
530      *
531      * @return the value of time
532      */

533     public boolean isTime() {
534     return this.time;
535     }
536
537     /**
538      * Sets the value of time
539      *
540      * @param argTime Value to assign to this.time
541      */

542     public void setTime(boolean argTime){
543     this.time = argTime;
544     }
545
546     /**
547      * Gets the value of debug
548      *
549      * @return the value of debug
550      */

551     public boolean isDebug() {
552     return this.debug;
553     }
554
555     /**
556      * Sets the value of debug
557      *
558      * @param argDebug Value to assign to this.debug
559      */

560     public void setDebug(boolean argDebug){
561     this.debug = argDebug;
562     }
563
564     /**
565      * Gets the value of nopositions
566      *
567      * @return the value of nopositions
568      */

569     public boolean isNopositions() {
570     return this.nopositions;
571     }
572
573     /**
574      * Sets the value of nopositions
575      *
576      * @param argNopositions Value to assign to this.nopositions
577      */

578     public void setNopositions(boolean argNopositions){
579     this.nopositions = argNopositions;
580     }
581
582     /**
583      * Gets the value of noscanner
584      *
585      * @return the value of noscanner
586      */

587     public boolean isNoscanner() {
588     return this.noscanner;
589     }
590
591     /**
592      * Sets the value of noscanner
593      *
594      * @param argNoscanner Value to assign to this.noscanner
595      */

596     public void setNoscanner(boolean argNoscanner){
597     this.noscanner = argNoscanner;
598     }
599
600
601 }
602
603   
604   
605
Popular Tags