<?xml version="1.0"?>
<!--
    Licensed to the Apache Software Foundation (ASF) under one or more
    contributor license agreements.  See the NOTICE file distributed with
    this work for additional information regarding copyright ownership.
    The ASF licenses this file to You under the Apache License, Version 2.0
    the "License"); you may not use this file except in compliance with
    the License.  You may obtain a copy of the License at
 
        http://www.apache.org/licenses/LICENSE-2.0
 
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
 -->
<project name="expressions" default="default">

  <description>
    Dynamically computed values to sort/facet/search on based on a pluggable grammar.
  </description>

  <!-- some files for testing that do not have license headers -->
  <property name="rat.excludes" value="**/*.tokens"/>

  <import file="../module-build.xml"/>

  <path id="classpath">
    <path refid="base.classpath"/>
    <fileset dir="lib"/>
    <pathelement path="${queries.jar}"/>
  </path>

  <path id="test.classpath">
    <path refid="test.base.classpath"/>
    <fileset dir="lib"/>
    <pathelement path="src/test-files"/>
  </path>

  <target name="compile-core" depends="jar-queries,common.compile-core" />

  <target name="javadocs" depends="javadocs-queries,compile-core">
    <invoke-module-javadoc>
      <links>
        <link href="../queries"/>
      </links>
    </invoke-module-javadoc>
  </target>

  <target name="regenerate" depends="run-antlr"/>

  <target name="resolve-antlr" xmlns:ivy="antlib:org.apache.ivy.ant">
    <ivy:cachepath organisation="org.antlr" module="antlr" revision="3.5"
                  inline="true" conf="default" type="jar" pathid="antlr.classpath"/>
  </target>

  <target name="run-antlr" depends="resolve-antlr">
    <regen-grammar package="js" grammar="Javascript"/>
  </target>
  
  <macrodef name="replace-value">
    <attribute name="value" />
    <attribute name="property" />
    <attribute name="from" />
    <attribute name="to" />
    <sequential>
      <loadresource property="@{property}">
        <string value="@{value}"/>
        <filterchain>
          <tokenfilter>
            <filetokenizer/>
            <replacestring from="@{from}" to="@{to}"/>
          </tokenfilter>
        </filterchain>
      </loadresource>
    </sequential>
  </macrodef>

  <macrodef name="regen-grammar">
    <attribute name="package" />
    <attribute name="grammar" />
    <sequential>
      <!-- delete parser and lexer so files will be generated -->
      <delete>
        <fileset dir="src/java/org/apache/lucene/expressions/@{package}">
          <include name="@{grammar}Lexer.java" />
          <include name="@{grammar}Parser.java" />
        </fileset>
      </delete>
      <local name="grammar.path"/>
      <local name="grammar.matchpath"/>
      <local name="-grammar.relative.path"/>
      <local name="grammar.relative.path"/>
      <property name="grammar.path" location="src/java/org/apache/lucene/expressions/@{package}"/>
      <!-- this is a hack for windows, because antlr writes absolute paths with double backslashes (no idea why...): -->
      <replace-value property="grammar.matchpath" value="${grammar.path}${file.separator}" from="\" to="\\"/>
      <property name="-grammar.relative.path" location="${grammar.path}" relative="true"/>
      <replace-value property="grammar.relative.path" value="${-grammar.relative.path}${file.separator}" from="${file.separator}" to="/"/>
      <java classname="org.antlr.Tool" fork="true" failonerror="true" classpathref="antlr.classpath" taskname="antlr">
        <!-- this is a hack because antlr generates code comments in nondeterministic order
         (using HashMap somewhere it should use LinkedHashMap). This hack only works for Java 7,
         Java 8 always uses murmurhash for strings and uses time-of-day as seed. -->
        <sysproperty key="jdk.map.althashing.threshold" value="-1"/>
        <sysproperty key="file.encoding" value="UTF-8"/>
        <sysproperty key="user.language" value="en"/>
        <sysproperty key="user.country" value="US"/>
        <sysproperty key="user.variant" value=""/>
        <arg value="-verbose"/>
        <arg value="-make"/>
        <arg value="-o"/>
        <arg path="${grammar.path}"/>
        <arg path="${grammar.path}/@{grammar}.g"/>
      </java>
      <!-- replace absolute paths by relative ones -->
      <replace file="${grammar.path}/@{grammar}Parser.java" token="${grammar.matchpath}" value="${grammar.relative.path}" encoding="UTF-8"/>
      <replace file="${grammar.path}/@{grammar}Lexer.java" token="${grammar.matchpath}" value="${grammar.relative.path}" encoding="UTF-8"/>
      <!-- make the generated classes package private (it's an antlr option with 4.0) -->
      <replace file="${grammar.path}/@{grammar}Parser.java" token="public class @{grammar}Parser" value="class @{grammar}Parser" encoding="UTF-8"/>
      <replace file="${grammar.path}/@{grammar}Lexer.java" token="public class @{grammar}Lexer" value="class @{grammar}Lexer" encoding="UTF-8"/>
      <!-- nuke timestamps in generated files -->
      <replaceregexp file="${grammar.path}/@{grammar}Parser.java" match=".*" replace="\/\/ ANTLR GENERATED CODE: DO NOT EDIT" encoding="UTF-8"/>
      <replaceregexp file="${grammar.path}/@{grammar}Lexer.java" match=".*" replace="\/\/ ANTLR GENERATED CODE: DO NOT EDIT" encoding="UTF-8"/>
      <!-- remove tabs in antlr generated files -->
      <replaceregexp file="${grammar.path}/@{grammar}Parser.java" match="\t" flags="g" replace="  " encoding="UTF-8"/>
      <replaceregexp file="${grammar.path}/@{grammar}Lexer.java" match="\t" flags="g" replace="  " encoding="UTF-8"/>
      <!-- fix line endings -->
      <fixcrlf file="${grammar.path}/@{grammar}Parser.java"/>
      <fixcrlf file="${grammar.path}/@{grammar}Lexer.java"/>
    </sequential>
  </macrodef>
</project>
