<!--
  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="queryparser" default="default">
  <description>
    Query parsers and parsing framework
  </description>

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

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

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

  <target name="javadocs" depends="javadocs-queries,javadocs-sandbox,compile-core,check-javadocs-uptodate"
          unless="javadocs-uptodate-${name}">
    <invoke-module-javadoc>
      <links>
        <link href="../queries"/>
        <link href="../sandbox"/>
      </links>
    </invoke-module-javadoc>
  </target>

  <target name="javacc" depends="javacc-QueryParser,javacc-surround,javacc-flexible"/>
  
  <macrodef name="generalReplaces">
    <attribute name="dir"/>
    <sequential>
      <!-- StringBuffer -> StringBuilder -->
      <replace token="StringBuffer" value="StringBuilder" encoding="UTF-8">
         <fileset dir="@{dir}" includes="ParseException.java TokenMgrError.java"/>
      </replace>
      <!-- Remove debug stream (violates forbidden-apis) -->
      <replaceregexp match="/\*\* Debug.*debugStream\s*=\s*ds;\s*}" replace="" flags="s" encoding="UTF-8">
         <fileset dir="@{dir}" includes="*TokenManager.java"/>
      </replaceregexp>
      <!-- Add warnings supression -->
      <replaceregexp match="^\Qpublic class\E" replace="@SuppressWarnings(&quot;cast&quot;)${line.separator}\0" flags="m" encoding="UTF-8">
         <fileset dir="@{dir}" includes="*TokenManager.java"/>
      </replaceregexp>
    </sequential>
  </macrodef>
  
  <target name="javacc-QueryParser" depends="resolve-javacc">
    <sequential>
      <invoke-javacc target="src/java/org/apache/lucene/queryparser/classic/QueryParser.jj"
                     outputDir="src/java/org/apache/lucene/queryparser/classic"/>

      <!-- Change the incorrect public ctors for QueryParser to be protected instead -->
      <replaceregexp file="src/java/org/apache/lucene/queryparser/classic/QueryParser.java"
         byline="true"
         match="public QueryParser\(CharStream "
         replace="protected QueryParser(CharStream "/>
      <replaceregexp file="src/java/org/apache/lucene/queryparser/classic/QueryParser.java"
         byline="true"
         match="public QueryParser\(QueryParserTokenManager "
         replace="protected QueryParser(QueryParserTokenManager "/>
      <!-- change an exception used for signaling to be static -->
      <replaceregexp file="src/java/org/apache/lucene/queryparser/classic/QueryParser.java"
         byline="true"
         match="final private LookaheadSuccess jj_ls ="
         replace="static final private LookaheadSuccess jj_ls =" />
      <generalReplaces dir="src/java/org/apache/lucene/queryparser/classic"/>
    </sequential>
  </target>

  <target name="javacc-surround" depends="resolve-javacc" description="generate surround query parser">
    <invoke-javacc target="src/java/org/apache/lucene/queryparser/surround/parser/QueryParser.jj"
                   outputDir="src/java/org/apache/lucene/queryparser/surround/parser"
    />
    <generalReplaces dir="src/java/org/apache/lucene/queryparser/surround/parser"/>
  </target>

  <target name="javacc-flexible" depends="resolve-javacc">
    <invoke-javacc target="src/java/org/apache/lucene/queryparser/flexible/standard/parser/StandardSyntaxParser.jj"
                   outputDir="src/java/org/apache/lucene/queryparser/flexible/standard/parser"
    />
        <replaceregexp file="src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java"
                             match="public class ParseException extends Exception"
                             replace="public class ParseException extends QueryNodeParseException"
                             flags="g"
                             byline="false"/>
        <replaceregexp file="src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java"
                             match="package org.apache.lucene.queryparser.flexible.standard.parser;"
                             replace="package org.apache.lucene.queryparser.flexible.standard.parser;${line.separator}
${line.separator}
import org.apache.lucene.queryparser.flexible.messages.Message;${line.separator}
import org.apache.lucene.queryparser.flexible.messages.MessageImpl;${line.separator}
import org.apache.lucene.queryparser.flexible.core.*;${line.separator}
import org.apache.lucene.queryparser.flexible.core.messages.*;"
                             flags="g"
                             byline="false"/>
        <replaceregexp file="src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java"
                             match="^  public ParseException\(Token currentTokenVal.*$(\s\s[^}].*\n)*  \}"
                             replace="  public ParseException(Token currentTokenVal,${line.separator}
    int[][] expectedTokenSequencesVal, String[] tokenImageVal) {${line.separator}
    super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, initialise(${line.separator}
    currentTokenVal, expectedTokenSequencesVal, tokenImageVal)));${line.separator}
    this.currentToken = currentTokenVal;${line.separator}
    this.expectedTokenSequences = expectedTokenSequencesVal;${line.separator}
    this.tokenImage = tokenImageVal;${line.separator}
  }"
                             flags="gm"
                             byline="false"/>
        <replaceregexp file="src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java"
                             match="^  public ParseException\(String message.*$(\s\s[^}].*\n)*  \}"
                             replace="  public ParseException(Message message) {${line.separator}
    super(message);${line.separator}
  }"
                             flags="gm"
                             byline="false"/>
        <replaceregexp file="src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java"
                             match="^  public ParseException\(\).*$(\s\s[^}].*\n)*  \}"
                             replace="  public ParseException() {${line.separator}
    super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, &quot;Error&quot;));${line.separator}
  }"
                             flags="gm"
                             byline="false"/>
        <replaceregexp file="src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java"
                             match="^  public String getMessage\(\).*$(\s\s\s\s[^}].*\n)*    \}"
                             replace="  private static String initialise(Token currentToken, int[][] expectedTokenSequences, String[] tokenImage) {${line.separator}
    String eol = System.getProperty(&quot;line.separator&quot;, &quot;\n&quot;);"
                             flags="gm"
                             byline="false"/>
        <replaceregexp file="src/java/org/apache/lucene/queryparser/flexible/standard/parser/ParseException.java"
                             match="\s*protected String add_escapes.*"
                             replace="  static private String add_escapes(String str) {"
                             flags="g"
                             byline="true"/>
        <generalReplaces dir="src/java/org/apache/lucene/queryparser/flexible/standard/parser"/>
  </target>

  <target name="resolve-javacc" xmlns:ivy="antlib:org.apache.ivy.ant">
    <!-- setup a "fake" JavaCC distribution folder in ${build.dir} to make JavaCC ANT task happy: -->
    <ivy:retrieve organisation="net.java.dev.javacc" module="javacc" revision="5.0" symlink="${ivy.symlink}"
      inline="true" conf="default" transitive="false" type="jar" sync="true"
      pattern="${build.dir}/javacc/bin/lib/[artifact].[ext]"/>
  </target>

  <macrodef name="invoke-javacc">
    <attribute name="target"/>
    <attribute name="outputDir"/>
    <sequential>
      <mkdir dir="@{outputDir}"/>
      <delete>
        <fileset dir="@{outputDir}" includes="*.java">
          <containsregexp expression="Generated.*By.*JavaCC"/>
        </fileset>
      </delete>
      <javacc
          target="@{target}"
          outputDirectory="@{outputDir}"
          javacchome="${build.dir}/javacc"
          jdkversion="1.${javac.release}"
      />
      <fixcrlf srcdir="@{outputDir}" includes="*.java" encoding="UTF-8">
        <containsregexp expression="Generated.*By.*JavaCC"/>
      </fixcrlf>
    </sequential>
  </macrodef>

  <target name="regenerate" depends="javacc"/>

</project>
