<project name="jython-parser" default="parser">

    <property name="javaccHome" value="c:\bin\javacc-5.0" />
    <property name="python" value="c:\bin\Python27\python.exe" />
    <property name="basedir" value="." />
    <property name="parser.dir" value="${basedir}" />

    <!-- print out some usage hints -->
    <target name="usage" description="prints a short usage">
        <echo>
If this build.xml is called directly, you have to make sure the property javaccHome is set correctly.
This can be done either inside this build.xml (first top property), or on the command line.

This build only handles javacc.
        	
Current setting is: ${javaccHome}
        </echo>
    </target>

    <!-- checks if work to do / can be done -->
    <target name="pre">
        <!-- set the property parser.regen.notreq if PythonGrammer.java is more up to date than grammar_fstrings.jjt 
        <uptodate property="parser.regen.notreq" targetfile="${parser.dir}/PythonGrammar.java">
            <srcfiles dir="${parser.dir}" includes="grammar_fstrings.jjt" />
        </uptodate>
        -->
        <!-- fail if generation should be done, but javacc.jar is not available -->
        <property name="javacc.jar" value="${javaccHome}/bin/lib/javacc.jar" />
        <available file="${javacc.jar}" property="javacc.jar.present" />
        <fail message="${javacc.jar} not found: correct the javaccHome property">
            <condition>
                <and>
                    <not>
                        <isset property="parser.regen.notreq" />
                    </not>
                    <not>
                        <isset property="javacc.jar.present" />
                    </not>
                </and>
            </condition>
        </fail>
    </target>

    <!-- information if up to date -->
    <target name="uptodate" depends="pre" if="parser.regen.notreq">
        <echo>parser generated files are up to date</echo>
    </target>

    <!-- delete the parser generated files -->
    <target name="clean" depends="uptodate" unless="parser.regen.notreq">
        <!-- do not delete file="${parser.dir}/TokenMgrError.java" -->
        <!-- do not delete file="${parser.dir}/ParseException.java" -->
        <delete file="${parser.dir}/FStringsGrammar.java" />
        <delete file="${parser.dir}/FStringsGrammarTreeConstants.java" />
        <delete file="${parser.dir}/FStringsGrammarTokenManager.java" />
        <delete file="${parser.dir}/FStringsGrammarConstants.java" />
        <!-- +++ TODO: maybe this could be allowed (checkin): -->
        <!--<delete file="${parser.dir}/CharStream.java" />-->
        <!-- <delete file="${parser.dir}/Token.java" /> -->
    </target>

    <!-- invoke jjtree on grammar_fstrings.jjt -->
    <target name="tree" depends="clean" unless="parser.regen.notreq">
        <java classname="jjtree" classpath="${javacc.jar}" fork="yes">
            <arg value="-OUTPUT_DIRECTORY=${parser.dir}" />
            <arg file="${parser.dir}/grammar_fstrings.jjt" />
        </java>
    </target>

    <!-- invoke javacc on grammar_fstrings.jj -->
    <target name="gen" depends="tree" unless="parser.regen.notreq">
        <java classname="javacc" classpath="${javacc.jar}" fork="yes">
            <arg value="-OUTPUT_DIRECTORY=${parser.dir}" />
            <arg file="${parser.dir}/grammar_fstrings.jj" />
        </java>
        <delete file="${parser.dir}/grammar_fstrings.jj" />
        	
        <echo>Removing the files that are common among versions</echo>
        <delete file="${parser.dir}/CharStream.java" />
        <delete file="${parser.dir}/ParseException.java" />
        <delete file="${parser.dir}/Token.java" />
        <delete file="${parser.dir}/SimpleNode.java" />
        <delete file="${parser.dir}/Node.java" />
        <delete file="${parser.dir}/JJTFStringsGrammarState.java" />
    	<!--
        <delete file="${parser.dir}/FStringsGrammarTreeConstants.java" />
        <delete file="${parser.dir}/TokenMgrError.java" />
        -->
    	
        <echo>Updating AbstractTokenManager in file FStringsGrammarTokenManager.java</echo>
        <replace file="${parser.dir}/FStringsGrammarTokenManager.java" token="public class FStringsGrammarTokenManager implements FStringsGrammarConstants" 
            value="@SuppressWarnings(&quot;unused&quot;) public final class FStringsGrammarTokenManager extends AbstractFStringsTokenManager implements FStringsGrammarConstants"/>

        <replace file="${parser.dir}/FStringsGrammar.java" token="protected JJTFStringsGrammarState jjtree = new JJTFStringsGrammarState()" 
             value="protected final JJTFStringsGrammarState jjtree = new JJTFStringsGrammarState(this)"/>

        <echo>Updating jjtree.builder.openNode</echo>
        <exec executable="${python}" dir="${parser.dir}">
            <arg value="${parser.dir}/../grammarcommon/optimize_generated_grammar.py" />
            <arg value="${parser.dir}" />
        </exec>
    	
    	<fixcrlf srcdir="${parser.dir}" eol="unix"></fixcrlf>
    </target>

    <!-- confirmation -->
    <target name="done" depends="gen" unless="parser.regen.notreq">
        <echo>parser generated to directory ${parser.dir}</echo>
    </target>

    <!-- default target -->
    <target name="parser" depends="done" description="the default target" />

</project>
