Well I finally got around to modifying Thomas Haas' original go at the JavaCC
taskdef, and updated it so that it works with the javacc tool instead of the
mparse tool. I also added a JJTree taskdef which is another tool for the javacc
package. I have included both of them as attachments. I hope that attachments
works for those who check into CVS. Both of these taskdefs should go into the
javacc optional package.

Thanks,
Michael
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs.optional.javacc;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Path;

import java.io.File;
import java.io.IOException;

/**
 * Taskdef for the JavaCC compiler compiler.
 *
 * @author [EMAIL PROTECTED]
 * @author Michael Saunders <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
 */
public class JavaCC extends Task {

    private int lookahead            = 1;
    private int choiceAmbiguityCheck = 2;
    private int otherAmbiguityCheck  = 1;

    private boolean staticParser         = true;
    private boolean debugParser          = false;
    private boolean debugLookahead       = false;
    private boolean debugTokenManager    = false;
    private boolean optimizeTokenManager = true;
    private boolean errorReporting       = true;
    private boolean javaUnicodeEscape    = false;
    private boolean unicodeInput         = false;
    private boolean ignoreCase           = false;
    private boolean commonTokenAction    = false;
    private boolean userTokenManager     = false;
    private boolean userCharStream       = false;
    private boolean buildParser          = true;
    private boolean buildTokenManager    = true;
    private boolean sanityCheck          = true;
    private boolean forceLACheck         = false;
    private boolean cacheTokens          = false;

    private File outputDirectory = null;
    private File target          = null;
    private File javaccHome      = null;

    private CommandlineJava cmdl = new CommandlineJava();


    public void setLookahead(int lookahead) {
        this.lookahead = lookahead;
    }

    public void setChoiceambiguitycheck(int choiceAmbiguityCheck) {
        this.choiceAmbiguityCheck = choiceAmbiguityCheck;
    }

    public void setOtherambiguityCheck(int otherAmbiguityCheck) {
        this.otherAmbiguityCheck = otherAmbiguityCheck;
    }

    public void setStatic(boolean staticParser) {
        this.staticParser = staticParser;
    }

    public void setDebugparser(boolean debugParser) {
        this.debugParser = debugParser;
    }

    public void setDebuglookahead(boolean debugLookahead) {
        this.debugLookahead = debugLookahead;
    }

    public void setDebugtokenmanager(boolean debugTokenManager) {
        this.debugTokenManager = debugTokenManager;
    }

    public void setOptimizetokenmanager(boolean optimizeTokenManager) {
        this.optimizeTokenManager = optimizeTokenManager;
    }

    public void setErrorreporting(boolean errorReporting) {
        this.errorReporting = errorReporting;
    }

    public void setJavaunicodeescape(boolean javaUnicodeEscape) {
        this.javaUnicodeEscape = javaUnicodeEscape;
    }

    public void setUnicodeinput(boolean unicodeInput) {
        this.unicodeInput = unicodeInput;
    }

    public void setIgnorecase(boolean ignoreCase) {
        this.ignoreCase = ignoreCase;
    }

    public void setCommontokenaction(boolean commonTokenAction) {
        this.commonTokenAction = commonTokenAction;
    }

    public void setUsertokenmanager(boolean userTokenManager) {
        this.userTokenManager = userTokenManager;
    }

    public void setUsercharstream(boolean userCharStream) {
        this.userCharStream = userCharStream;
    }

    public void setBuildparser(boolean buildParser) {
        this.buildParser = buildParser;
    }

    public void setBuildtokenmanager(boolean buildTokenManager) {
        this.buildTokenManager = buildTokenManager;
    }

    public void setSanitycheck(boolean sanityCheck) {
        this.sanityCheck = sanityCheck;
    }

    public void setForcelacheck(boolean forceLACheck) {
        this.forceLACheck = forceLACheck;
    }

    public void setCachetokens(boolean cacheTokens) {
        this.cacheTokens = cacheTokens;
    }

    public void setOutputdirectory(File outputDirectory) {
        this.outputDirectory = outputDirectory;
    }

    public void setTarget(File target) {
        this.target = target;
    }

    public void setJavacchome(File javaccHome) {
        this.javaccHome = javaccHome;
    }

    public JavaCC() {
        cmdl.setVm("java");
        cmdl.setClassname("COM.sun.labs.javacc.Main");
    }

    public void execute() throws BuildException {

        cmdl.createArgument().setValue("-LOOKAHEAD:"              +lookahead);
        cmdl.createArgument().setValue("-CHOICE_AMBIGUITY_CHECK:" 
+choiceAmbiguityCheck);
        cmdl.createArgument().setValue("-OTHER_AMBIGUITY_CHECK:"  
+otherAmbiguityCheck);

        cmdl.createArgument().setValue("-STATIC:"                +staticParser);
        cmdl.createArgument().setValue("-DEBUG_PARSER:"          +debugParser);
        cmdl.createArgument().setValue("-DEBUG_LOOKAHEAD:"       
+debugLookahead);
        cmdl.createArgument().setValue("-DEBUG_TOKEN_MANAGER:"   
+debugTokenManager);
        
cmdl.createArgument().setValue("-OPTIMIZE_TOKEN_MANAGER:"+optimizeTokenManager);
        cmdl.createArgument().setValue("-ERROR_REPORTING:"       
+errorReporting);
        cmdl.createArgument().setValue("-JAVA_UNICODE_ESCAPE:"   
+javaUnicodeEscape);
        cmdl.createArgument().setValue("-UNICODE_INPUT:"         +unicodeInput);
        cmdl.createArgument().setValue("-IGNORE_CASE:"           +ignoreCase);
        cmdl.createArgument().setValue("-COMMON_TOKEN_ACTION:"   
+commonTokenAction);
        cmdl.createArgument().setValue("-USER_TOKEN_MANAGER:"    
+userTokenManager);
        cmdl.createArgument().setValue("-USER_CHAR_STREAM:"      
+userCharStream);
        cmdl.createArgument().setValue("-BUILD_PARSER:"          +buildParser);
        cmdl.createArgument().setValue("-BUILD_TOKEN_MANAGER:"   
+buildTokenManager);
        cmdl.createArgument().setValue("-SANITY_CHECK:"          +sanityCheck);
        cmdl.createArgument().setValue("-FORCE_LA_CHECK:"        +forceLACheck);
        cmdl.createArgument().setValue("-CACHE_TOKENS:"          +cacheTokens);

        if (outputDirectory != null) {
            if (!outputDirectory.isDirectory()) {
                throw new BuildException("Outputdir not a directory.");
            }
            cmdl.createArgument().setValue(
                "-OUTPUT_DIRECTORY:"+outputDirectory.getAbsolutePath());
        }

        if (target == null || !target.isFile()) {
            throw new BuildException("Invalid target: " + target);
        }
        final File javaFile = new File(
            target.toString().substring(0, target.toString().indexOf(".jj")) + 
".java");
        if (javaFile.exists() && target.lastModified() < 
javaFile.lastModified()) {
            project.log("Target is already built - skipping (" + target + ")");
            return;
        }
        cmdl.createArgument().setValue(target.getAbsolutePath());

        if (javaccHome == null || !javaccHome.isDirectory()) {
            throw new BuildException("Javacchome not set.");
        }
        final Path classpath = cmdl.createClasspath(project);
        classpath.createPathElement().setLocation(javaccHome.getAbsolutePath() +
                                                  "/JavaCC.zip");

        final Commandline.Argument arg = cmdl.createVmArgument();
        arg.setValue("-mx140M");
        arg.setValue("-Dinstall.root="+javaccHome.getAbsolutePath());

        final Execute process =
            new Execute(new LogStreamHandler(this,
                                             Project.MSG_INFO,
                                             Project.MSG_INFO),
                        null);
        log(cmdl.toString(), Project.MSG_VERBOSE);
        process.setCommandline(cmdl.getCommandline());

        try {
            if (process.execute() != 0) {
                throw new BuildException("JavaCC failed.");
            }
        }
        catch (IOException e) {
            throw new BuildException("Failed to launch JavaCC: " + e);
        }
    }
}
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs.optional.javacc;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Execute;
import org.apache.tools.ant.taskdefs.LogStreamHandler;
import org.apache.tools.ant.types.Commandline;
import org.apache.tools.ant.types.CommandlineJava;
import org.apache.tools.ant.types.Path;

import java.io.File;
import java.io.IOException;

/**
 * Taskdef for the JJTree compiler compiler.
 *
 * @author Michael Saunders <a href="mailto:[EMAIL PROTECTED]">[EMAIL 
PROTECTED]</a>
 */
public class JJTree extends Task {

    private boolean buildNodeFiles  = true;
    private boolean multi           = false;
    private boolean nodeDefaultVoid = false;
    private boolean nodeFactory     = false;
    private boolean nodeScopeHook   = false;
    private boolean nodeUsesParser  = false;
    private boolean staticParser    = true;
    private boolean visitor         = false;

    private String nodePackage      = null;
    private String visitorException = null;
    private String nodePrefix       = "AST";

    private File outputDirectory = null;
    private File target          = null;
    private File javaccHome      = null;

    private CommandlineJava cmdl = new CommandlineJava();


    public void setBuildnodefiles(boolean buildNodeFiles) {
        this.buildNodeFiles = buildNodeFiles;
    }

    public void setMulti(boolean multi) {
        this.multi = multi;
    }

    public void setNodedefaultvoid(boolean nodeDefaultVoid) {
        this.nodeDefaultVoid = nodeDefaultVoid;
    }

    public void setNodefactory(boolean nodeFactory) {
        this.nodeFactory = nodeFactory;
    }

    public void setNodescopehook(boolean nodeScopeHook) {
        this.nodeScopeHook = nodeScopeHook;
    }

    public void setNodeusesparser(boolean nodeUsesParser) {
        this.nodeUsesParser = nodeUsesParser;
    }

    public void setStatic(boolean staticParser) {
        this.staticParser = staticParser;
    }

    public void setVisitor(boolean visitor) {
        this.visitor = visitor;
    }

    public void setNodepackage(String nodePackage) {
        this.nodePackage = nodePackage;
    }

    public void setVisitorException(String visitorException) {
        this.visitorException = visitorException;
    }

    public void setNodeprefix(String nodePrefix) {
        this.nodePrefix = nodePrefix;
    }

    public void setOutputdirectory(File outputDirectory) {
        this.outputDirectory = outputDirectory;
    }

    public void setTarget(File target) {
        this.target = target;
    }

    public void setJavacchome(File javaccHome) {
        this.javaccHome = javaccHome;
    }

    public JJTree() {
        cmdl.setVm("java");
        cmdl.setClassname("COM.sun.labs.jjtree.Main");
    }

    public void execute() throws BuildException {

        cmdl.createArgument().setValue("-BUILD_NODE_FILES:"  +buildNodeFiles);
        cmdl.createArgument().setValue("-MULTI:"             +multi);
        cmdl.createArgument().setValue("-NODE_DEFAULT_VOID:" +nodeDefaultVoid);
        cmdl.createArgument().setValue("-NODE_FACTORY:"      +nodeFactory);
        cmdl.createArgument().setValue("-NODE_SCOPE_HOOK:"   +nodeScopeHook);
        cmdl.createArgument().setValue("-NODE_USES_PARSER:"  +nodeUsesParser);
        cmdl.createArgument().setValue("-STATIC:"            +staticParser);
        cmdl.createArgument().setValue("-VISITOR:"           +visitor);

        if (nodePackage != null) {
            cmdl.createArgument().setValue("-NODE_PACKAGE:"+nodePackage);
        }

        if (visitorException != null) {
            
cmdl.createArgument().setValue("-VISITOR_EXCEPTION:"+visitorException);
        }

        if (nodePrefix != null) {
            cmdl.createArgument().setValue("-NODE_PREFIX:"+nodePrefix);
        }

        if (outputDirectory != null) {
            if (!outputDirectory.isDirectory()) {
                throw new BuildException("Outputdir not a directory.");
            }
            cmdl.createArgument().setValue(
                "-OUTPUT_DIRECTORY:"+outputDirectory.getAbsolutePath());
        }

        if (target == null || !target.isFile()) {
            throw new BuildException("Invalid target: " + target);
        }
        final File javaFile = new File(
            target.toString().substring(0, target.toString().indexOf(".jjt")) + 
".jj");
        if (javaFile.exists() && target.lastModified() < 
javaFile.lastModified()) {
            project.log("Target is already built - skipping (" + target + ")");
            return;
        }
        cmdl.createArgument().setValue(target.getAbsolutePath());

        if (javaccHome == null || !javaccHome.isDirectory()) {
            throw new BuildException("Javacchome not set.");
        }
        final Path classpath = cmdl.createClasspath(project);
        classpath.createPathElement().setLocation(javaccHome.getAbsolutePath() +
                                                  "/JavaCC.zip");

        final Commandline.Argument arg = cmdl.createVmArgument();
        arg.setValue("-mx140M");
        arg.setValue("-Dinstall.root="+javaccHome.getAbsolutePath());

        final Execute process =
            new Execute(new LogStreamHandler(this,
                                             Project.MSG_INFO,
                                             Project.MSG_INFO),
                        null);
        log(cmdl.toString(), Project.MSG_VERBOSE);
        process.setCommandline(cmdl.getCommandline());

        try {
            if (process.execute() != 0) {
                throw new BuildException("JJTree failed.");
            }
        }
        catch (IOException e) {
            throw new BuildException("Failed to launch JJTree: " + e);
        }
    }
}

Reply via email to