<project name="pmd" default="alljavacc" basedir="../../">

    <property name="javacc-home.path" value="target/lib" />

    <target name="alljavacc"
            description="Generates all JavaCC aspects within PMD"
            depends="checkUpToDate,init,plsqljjtree,cleanup" />

    <target name="checkUpToDate">
        <uptodate property="javaccBuildNotRequired" targetfile="${target}/last-generated-timestamp">
            <srcfiles dir="etc/grammar" includes="*.jj*"/>
        </uptodate>
        <echo message="up to date check: javaccBuildNotRequired=${javaccBuildNotRequired}"/>
    </target>

    <target name="init" unless="javaccBuildNotRequired">
        <mkdir dir="${javacc-home.path}" />
        <copy file="${javacc.jar}" tofile="${javacc-home.path}/javacc.jar" />

        <mkdir dir="${target}"/>
        <touch file="${target}/last-generated-timestamp"/>
    </target>

    <target name="cleanup">
        <delete dir="${javacc-home.path}" />
    </target>

    <target name="plsqljjtree" description="Generates the PlSQL parser and AST source files" unless="javaccBuildNotRequired">
        <delete dir="${target}/net/sourceforge/pmd/lang/plsql/ast" />
        <mkdir dir="${target}/net/sourceforge/pmd/lang/plsql/ast" />
        <jjtree target="etc/grammar/PldocAST.jjt"
                outputdirectory="${target}/net/sourceforge/pmd/lang/plsql/ast"
                javacchome="${javacc-home.path}" />
        <!-- Ensure generated using CharStream interface -->
        <javacc static="false"
                usercharstream="false"
                target="${target}/net/sourceforge/pmd/lang/plsql/ast/PldocAST.jj"
                outputdirectory="${target}/net/sourceforge/pmd/lang/plsql/ast"
                javacchome="${javacc-home.path}" />

        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/SimpleNode.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/Node.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/SimpleCharStream.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/TokenMgrError.java" />

        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTArguments.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTDatatype.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTFormalParameter.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTIfStatement.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTInput.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTMethodDeclaration.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTMethodDeclarator.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTName.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTPackageBody.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTPackageSpecification.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTPrimaryPrefix.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTPrimarySuffix.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTProgramUnit.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTTriggerTimingPointSection.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTTriggerUnit.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTTypeMethod.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTTypeSpecification.java" />
        <delete file="${target}/net/sourceforge/pmd/lang/plsql/ast/ASTVariableOrConstantDeclaratorId.java" />

        <replace file="${target}/net/sourceforge/pmd/lang/plsql/ast/PLSQLParserVisitor.java"
                 token="SimpleNode"
                 value="PLSQLNode" />
        <replace file="${target}/net/sourceforge/pmd/lang/plsql/ast/PLSQLParserTokenManager.java"
                 token="class PLSQLParserTokenManager"
                 value="class PLSQLParserTokenManager extends net.sourceforge.pmd.lang.ast.AbstractTokenManager" />
        <replace file="${target}/net/sourceforge/pmd/lang/plsql/ast/JJTPLSQLParserState.java">
            <replacetoken>public class</replacetoken>
            <replacevalue><![CDATA[import net.sourceforge.pmd.lang.ast.Node;

public class]]></replacevalue>
        </replace>
        <replace file="${target}/net/sourceforge/pmd/lang/plsql/ast/ParseException.java"
                 token="extends Exception"
                 value="extends net.sourceforge.pmd.lang.ast.ParseException" />

    </target>
</project>
