OpenSearch/modules/lang-painless/ant.xml

154 lines
6.1 KiB
XML
Raw Normal View History

<?xml version="1.0"?>
<project name="ant-stuff">
<!--
grammar regeneration logic
we do this with ant for several reasons:
* remove generated tabs for forbidden-apis
* remove generated timestamps/filenames for reproducible build
* fix CRLF line endings for windows consistency
* ability to make classes package-private
* keeping in source code control is easier on IDEs
* regeneration should be rare, no reason to be religious about generated files
* all logic already written and battle tested in lucene build
-->
<target name="regenerate" description="Regenerate antlr lexer and parser" depends="run-antlr"/>
<target name="run-antlr">
<regen-delete grammar="Painless"/>
<regen-lexer grammar="Painless"/>
<regen-parser grammar="Painless"/>
<regen-fix grammar="Painless"/>
</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-delete">
<attribute name="grammar" />
<sequential>
<local name="grammar.path"/>
<property name="grammar.path" location="src/main/antlr"/>
<!-- delete token files so files will be generated -->
<delete dir="${grammar.path}" includes="@{grammar}*.tokens"/>
<local name="output.path"/>
<patternset id="grammar.@{grammar}.patternset">
<include name="@{grammar}Lexer.java" />
<include name="@{grammar}Parser.java" />
<include name="@{grammar}Lexer.tokens" />
<include name="@{grammar}Parser.tokens" />
<include name="@{grammar}ParserVisitor.java" />
<include name="@{grammar}ParserBaseVisitor.java" />
</patternset>
<property name="output.path" location="src/main/java/org/elasticsearch/painless/antlr"/>
<!-- delete parser and lexer so files will be generated -->
<delete dir="${output.path}">
<patternset refid="grammar.@{grammar}.patternset"/>
</delete>
</sequential>
</macrodef>
<macrodef name="regen-lexer">
<attribute name="grammar" />
<sequential>
<local name="grammar.path"/>
<local name="output.path"/>
<property name="grammar.path" location="src/main/antlr"/>
<property name="output.path" location="src/main/java/org/elasticsearch/painless/antlr"/>
<!-- invoke ANTLR4 -->
<java classname="org.antlr.v4.Tool" fork="true" failonerror="true" classpathref="regenerate.classpath" taskname="antlr">
<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="-Werror"/>
<arg value="-package"/>
<arg value="org.elasticsearch.painless.antlr"/>
<arg value="-o"/>
<arg path="${output.path}"/>
<arg path="${grammar.path}/@{grammar}Lexer.g4"/>
</java>
</sequential>
</macrodef>
<macrodef name="regen-parser">
<attribute name="grammar" />
<sequential>
<local name="grammar.path"/>
<local name="output.path"/>
<property name="grammar.path" location="src/main/antlr"/>
<property name="output.path" location="src/main/java/org/elasticsearch/painless/antlr"/>
<!-- invoke ANTLR4 -->
<java classname="org.antlr.v4.Tool" fork="true" failonerror="true" classpathref="regenerate.classpath" taskname="antlr">
<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="-Werror"/>
<arg value="-package"/>
<arg value="org.elasticsearch.painless.antlr"/>
<arg value="-no-listener"/>
<arg value="-visitor"/>
<!-- <arg value="-Xlog"/> -->
<arg value="-o"/>
<arg path="${output.path}"/>
<arg path="${grammar.path}/@{grammar}Parser.g4"/>
</java>
</sequential>
</macrodef>
<macrodef name="regen-fix">
<attribute name="grammar" />
<sequential>
<local name="grammar.path"/>
<local name="output.path"/>
<property name="grammar.path" location="src/main/antlr"/>
<property name="output.path" location="src/main/java/org/elasticsearch/painless/antlr"/>
<patternset id="grammar.@{grammar}.patternset">
<include name="@{grammar}Lexer.java" />
<include name="@{grammar}Parser.java" />
<include name="@{grammar}ParserVisitor.java" />
<include name="@{grammar}ParserBaseVisitor.java" />
</patternset>
<!-- fileset with files to edit -->
<fileset id="grammar.fileset" dir="${output.path}">
<patternset refid="grammar.@{grammar}.patternset"/>
</fileset>
<!-- moves token files to grammar directory for use with IDE's -->
<move file="${output.path}/@{grammar}Lexer.tokens" todir="${grammar.path}"/>
<move file="${output.path}/@{grammar}Parser.tokens" todir="${grammar.path}"/>
<!-- make the generated classes package private -->
<replaceregexp match="public ((interface|class) \Q@{grammar}\E\w+)" replace="\1" encoding="UTF-8">
<fileset refid="grammar.fileset"/>
</replaceregexp>
<!-- nuke timestamps/filenames in generated files -->
<replaceregexp match="\Q// Generated from \E.*" replace="\/\/ ANTLR GENERATED CODE: DO NOT EDIT" encoding="UTF-8">
<fileset refid="grammar.fileset"/>
</replaceregexp>
<!-- remove tabs in antlr generated files -->
<replaceregexp match="\t" flags="g" replace=" " encoding="UTF-8">
<fileset refid="grammar.fileset"/>
</replaceregexp>
<!-- fix line endings -->
<fixcrlf srcdir="${output.path}">
<patternset refid="grammar.@{grammar}.patternset"/>
</fixcrlf>
</sequential>
</macrodef>
</project>