Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/sablecc/ant/taskdef/Sablecc.java


1   /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2    * This file is part of AntTask.                             *
3    * See the file "LICENSE" for copyright information and the  *
4    * terms and conditions for copying, distribution and        *
5    * modification of AntTask.                                  *
6    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
7   
8   package org.sablecc.ant.taskdef;
9   
10  /**/
11  import org.apache.tools.ant.*;
12  import org.apache.tools.ant.taskdefs.MatchingTask;
13  import java.io.*;
14  import java.util.*;
15  
16  /**
17   * Parser generator task based on SableCC parser generator.
18   *
19   * This task requires the following arguments:
20   * <ul>
21   * <li>src</li>
22   * <li>outputdirectory</li>
23   * </ul>
24   * and is based on MatchingTask which means includes and exludes 
25   * are done in a standard fashion.
26   *
27   * @author Mariusz Nowostawski [marni] <a href="mailto:mariusz@rakiura.org">mariusz@rakiura.org</a>
28   */
29  public class Sablecc extends MatchingTask {
30  
31    private File srcDir;
32    private File destDir;
33    private boolean withTools = false;
34  
35    /**
36       Sets the grammars directory name. */
37    public void setSrc(String d) {
38      srcDir = project.resolveFile(d);
39    }
40  
41    public void setWithtools(boolean b){
42      withTools = b;
43    }
44    
45    /**
46       Sets the destinion where the generated files should be placed. */
47    public void setOutputDirectory(String d) {
48      destDir = project.resolveFile(d);
49    }  
50  
51    public void execute() throws BuildException {
52  
53      // first off, make sure that we've got a srcdir and destdir
54      if (srcDir == null || destDir == null ) {
55        throw new BuildException("srcDir and destDir attributes must be set!");
56      }
57  
58      DirectoryScanner ds = getDirectoryScanner(srcDir);
59      String[] files = ds.getIncludedFiles();
60  
61      // compile the source files
62      if (files.length > 0) {
63        project.log("Compiling with SableCC " + files.length + " source grammar files to " + destDir, project.MSG_INFO);
64        doSableccCompile(files);
65      }
66    }
67  
68    /**
69     * Peforms a generation of parsers using the SableCC Compiler Compiler.
70     */
71    private void doSableccCompile(String[] files) {
72      project.log("Using now SableCC parser generator...", project.MSG_VERBOSE);
73          
74      for (int i = 0; i < files.length; i++) {
75        project.log("SableCC processing grammar: "+files[i], project.MSG_VERBOSE);
76        try{
77          org.sablecc.sablecc.SableCC.processGrammar(srcDir+System.getProperty("file.separator")+files[i],
78                                                     destDir.getAbsolutePath(), withTools);
79        }catch(Exception e){
80          project.log("SableCC failed.\n"+e.getMessage(), project.MSG_ERR);        
81          StringWriter wr  = new StringWriter();
82          e.printStackTrace(new PrintWriter(wr));
83          project.log("Exception thrown was:\n"+wr.toString(), project.MSG_VERBOSE);
84          return;
85        }catch(Throwable t){
86          project.log("SableCC failed.\n"+t.getMessage(), project.MSG_ERR);
87          StringWriter wr  = new StringWriter();
88          t.printStackTrace(new PrintWriter(wr));
89          project.log("Exception thrown was:\n"+wr.toString(), project.MSG_VERBOSE);
90        }
91      }
92      project.log("Using SableCC parser generator finished succesfully!", project.MSG_VERBOSE);
93    }
94  
95  }