<?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="queryparser" default="default">

  <description>
    Flexible Query Parser
  </description>

  <import file="../contrib-build.xml"/>
 
  <!--
    NOTE: see the README.javacc for details on how to fully regenerate the parser
  -->
  <target name="javacc" depends="javacc-flexible,javacc-precedence"/>

  <target name="javacc-flexible" depends="init,javacc-check" if="javacc.present">
    <invoke-javacc target="src/java/org/apache/lucene/queryParser/standard/parser/StandardSyntaxParser.jj"
                   outputDir="src/java/org/apache/lucene/queryParser/standard/parser"
    />
  	    <replaceregexp file="src/java/org/apache/lucene/queryParser/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/standard/parser/ParseException.java"
  	                         match="package org.apache.lucene.queryParser.standard.parser;"
  	                         replace="package org.apache.lucene.queryParser.standard.parser;&#10;
  	&#10;
  	import org.apache.lucene.messages.Message;&#10;
  	import org.apache.lucene.messages.MessageImpl;&#10;
  	import org.apache.lucene.queryParser.core.*;&#10;
  	import org.apache.lucene.queryParser.core.messages.*;"
  	                         flags="g"
  	                         byline="false"/>
  	    <replaceregexp file="src/java/org/apache/lucene/queryParser/standard/parser/ParseException.java"
  	                         match="^  public ParseException\(Token currentTokenVal.*$(\s\s[^}].*\n)*  \}"
  	                         replace="  public ParseException(Token currentTokenVal,&#10;
  	        int[][] expectedTokenSequencesVal, String[] tokenImageVal) {&#10;
  	    super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, initialise(&#10;
  	      currentTokenVal, expectedTokenSequencesVal, tokenImageVal)));&#10;
  	    this.currentToken = currentTokenVal;&#10;
  	    this.expectedTokenSequences = expectedTokenSequencesVal;&#10;
  	    this.tokenImage = tokenImageVal;&#10;
  	  }"
  	                         flags="gm"
  	                         byline="false"/>
  	    <replaceregexp file="src/java/org/apache/lucene/queryParser/standard/parser/ParseException.java"
  	                         match="^  public ParseException\(String message.*$(\s\s[^}].*\n)*  \}"
  	                         replace="  public ParseException(Message message) {&#10;
  	    super(message);&#10;                      
  	  }"
  	                         flags="gm"
  	                         byline="false"/>
  	    <replaceregexp file="src/java/org/apache/lucene/queryParser/standard/parser/ParseException.java"
  	                         match="^  public ParseException\(\).*$(\s\s[^}].*\n)*  \}"
  	                         replace="  public ParseException() {&#10;
  	    super(new MessageImpl(QueryParserMessages.INVALID_SYNTAX, &quot;Error&quot;));&#10;
  	  }"
  	                         flags="gm"
  	                         byline="false"/>
  	    <replaceregexp file="src/java/org/apache/lucene/queryParser/standard/parser/ParseException.java"
  	                         match="^  public String getMessage\(\).*$(\s\s\s\s[^}].*\n)*    \}"
  	                         replace="  private static String initialise(Token currentToken, &#10;
  	      int[][] expectedTokenSequences, String[] tokenImage) {&#10;
  	    String eol = System.getProperty(&quot;line.separator&quot;, &quot;\n&quot;);"
  	                         flags="gm"
  	                         byline="false"/>
  	    <replaceregexp file="src/java/org/apache/lucene/queryParser/standard/parser/ParseException.java"
  	                         match="\s*protected String add_escapes.*"
  	                         replace="  static private String add_escapes(String str) {"
  	                         flags="g"
  	                         byline="false"/>
  </target>
  
  <property name="javacc.precedence.path" location="src/java/org/apache/lucene/queryParser/precedence"/>

  <target name="javacc-precedence" depends="javacc-check" description="generate precedence query parser from jj (requires javacc 3.2)">
    <delete>
      <fileset dir="${javacc.precedence.path}" includes="*.java">
        <containsregexp expression="Generated.*By.*JavaCC"/>
      </fileset>
    </delete>
    <invoke-javacc target="${javacc.precedence.path}/PrecedenceQueryParser.jj"
                   outputDir="${javacc.precedence.path}"
    />
  </target>
</project>
