/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * This file is part of AntTask. * * See the file "LICENSE" for copyright information and the * * terms and conditions for copying, distribution and * * modification of AntTask. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ package org.sablecc.ant.taskdef; /**/ import org.apache.tools.ant.*; import org.apache.tools.ant.taskdefs.MatchingTask; import java.io.*; import java.util.*; /** * An ANT optional task to invoke the SableCC parser generator tool from an ANT * build file. * * This task requires the following attributes: *
* * This task accepts one addtional optional boolean argument (with two different * but equivalent spellings): withtools or tools. When set * (to true, yes, or on) the SableCC tool will * generate, additional, user tools classes for the input grammar(s).
* * Example: *
** * @author Mariusz Nowostawski [marni] mariusz@rakiura.org * @author Raif S. Naffah */ public class Sablecc extends MatchingTask { private File srcDir; private File destDir; private boolean withtools = false; /** Sets the grammars directory name. */ public void setSrc(String d) { srcDir = project.resolveFile(d); } /** @see #setTools */ public void setWithtools(boolean b){ setTools(b); } /** * Instructs the SableCC tool to generate (true) or not (false) * user tools classes for the input grammar(s). * * @param b true to generate the user tools classes for the input * grammar(s). false otherwise. */ public void setTools(boolean b){ withtools = b; } /** @see #setDestdir */ public void setOutputDirectory(String d) { setDestdir(d); } /** * Sets the destination directory path where the generated files will be * placed. * * @param d the pathname of the destination directory. */ public void setDestdir(String d) { destDir = project.resolveFile(d); } public void execute() throws BuildException { // first off, make sure that we've got a srcdir and destdir if (srcDir == null || destDir == null ) { throw new BuildException("both src and destdir attributes must be set!"); } DirectoryScanner ds = getDirectoryScanner(srcDir); String[] files = ds.getIncludedFiles(); // compile the source files if (files.length > 0) { project.log("Compiling with SableCC " + files.length + " source grammar files to " + destDir, project.MSG_INFO); doSableccCompile(files); } } /** Generate parsers using the SableCC Compiler Compiler. */ private void doSableccCompile(String[] files) throws BuildException { project.log("Using now SableCC parser generator...", project.MSG_VERBOSE); for (int i = 0; i < files.length; i++) { project.log("SableCC processing grammar: "+files[i], project.MSG_VERBOSE); try{ org.sablecc.sablecc.SableCC.processGrammar( srcDir+System.getProperty("file.separator")+files[i], destDir.getAbsolutePath(), withtools); } catch (Throwable t) { // just in case project.log("SableCC failed.\n" + t.getMessage(), project.MSG_ERR); throw new BuildException(t); } } project.log("Using SableCC parser generator finished succesfully!", project.MSG_VERBOSE); } }* ... * * ** *