Author: hibou
Date: Sun Jun 17 19:59:12 2012
New Revision: 1351146
URL: http://svn.apache.org/viewvc?rev=1351146&view=rev
Log:
Add support for macros
Modified:
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AntDSL.g
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java
ant/sandbox/antdsl/test/build.ant
Modified:
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AntDSL.g
URL:
http://svn.apache.org/viewvc/ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AntDSL.g?rev=1351146&r1=1351145&r2=1351146&view=diff
==============================================================================
--- ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AntDSL.g
(original)
+++ ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/AntDSL.g
Sun Jun 17 19:59:12 2012
@@ -12,6 +12,10 @@ import org.apache.ant.antdsl.AbstractAnt
import org.apache.ant.antdsl.IfTask.ConditionnalSequential;
import org.apache.tools.ant.*;
import org.apache.tools.ant.taskdefs.*;
+import org.apache.tools.ant.taskdefs.MacroDef.Attribute;
+import org.apache.tools.ant.taskdefs.MacroDef.NestedSequential;
+import org.apache.tools.ant.taskdefs.MacroDef.TemplateElement;
+import org.apache.tools.ant.taskdefs.MacroDef.Text;
import org.apache.tools.ant.taskdefs.condition.*;
import java.util.LinkedHashMap;
}
@@ -84,7 +88,7 @@ target returns [Target t = new Target()]
;
taskLists returns [List<Task> tl = new ArrayList<Task>()]:
- '{' (t=task { tl.add(t); } )+ '}';
+ '{' (t=task { tl.add(t); } )* '}';
targetList returns [List<String> tl = new ArrayList<String>()]:
n=NAME { tl.add($n.text); }
@@ -136,23 +140,54 @@ conditionedTasks returns [ConditionnalSe
tl=taskLists { for (Task t : tl) { seq.addTask(t); } }
;
-macrodef:
- 'macrodef' NAME '(' attributes? ')' taskLists;
-
-attributes:
- attribute (',' attribute)*;
-
-attribute:
- simpleAttribute | textAttribute | elementAttribute;
-
-simpleAttribute:
- NAME ('=' STRING)?;
-
-textAttribute:
- 'text' 'optional'? 'trimmed'? NAME;
+macrodef returns [MacroDef macroDef = new MacroDef()]:
+ 'macrodef' NAME { macroDef.setName($NAME.text); }
+ '(' ( atts=attributes
+ { for (Object att : atts) {
+ if (att instanceof Attribute) {
+ macroDef.addConfiguredAttribute((Attribute) att);
+ } else if (att instanceof Text) {
+ macroDef.addConfiguredText((Text) att);
+ } else if (att instanceof TemplateElement) {
+ macroDef.addConfiguredElement((TemplateElement) att);
+ } else {
+ throw new IllegalArgumentException("Unsupported macro
attribute " + att.getClass().getName());
+ }
+ }
+ } )? ')'
+ tl=taskLists
+ {
+ NestedSequential seq = macroDef.createSequential();
+ for (Task t : tl) { seq.addTask(t); }
+ macroDef.setProject(project);
+ macroDef.execute();
+ }
+ ;
-elementAttribute:
- 'element' 'optional'? 'implicit'? NAME;
+attributes returns [List atts = new ArrayList()]:
+ att=attribute { atts.add(att); }
+ (',' att=attribute { atts.add(att); } )*;
+
+attribute returns [Object att]:
+ satt=simpleAttribute { att = satt; }
+ | tatt=textAttribute { att = tatt; }
+ | eatt=elementAttribute { att = eatt; };
+
+simpleAttribute returns [Attribute att = new Attribute()]:
+ NAME { att.setName($NAME.text); }
+ ('=' STRING { att.setDefault($STRING.text); } )?;
+
+textAttribute returns [Text text = new Text()]:
+ 'text'
+ ('optional' { text.setOptional(true); } )?
+ ('trimmed' { text.setTrim(true); } )?
+ NAME { text.setName($NAME.text); };
+
+elementAttribute returns [TemplateElement element = new TemplateElement()]:
+ 'element'
+ ('optional' { element.setOptional(true); } )?
+ ('implicit' { element.setImplicit(true); } )?
+ NAME { element.setName($NAME.text); };
DOC:
'%' ~('\n'|'\r')* '\r'? '\n'
Modified:
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext
URL:
http://svn.apache.org/viewvc/ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext?rev=1351146&r1=1351145&r2=1351146&view=diff
==============================================================================
---
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext
(original)
+++
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDSL.xtext
Sun Jun 17 19:59:12 2012
@@ -23,7 +23,8 @@ ETarget:
description=DOC? 'target' name=NAME (('extensionOf'
extensionsOf=ETargetList)? & ('depends' depends=ETargetList)?)
tasks=ETaskLists?;
ETaskLists:
- '{' tasks+=ETask+ '}';
+ {ETaskLists}
+ '{' tasks+=ETask* '}';
ETargetList:
names+=NAME (',' names+=NAME)*;
@@ -68,10 +69,10 @@ ESimpleAttribute:
name=NAME ('=' default=STRING)?;
ETextAttribute:
- 'text' 'optional'? 'trimmed'? name=NAME;
+ 'text' (optional ?= 'optional')? (trimmed ?= 'trimmed')? name=NAME;
EElementAttribute:
- 'element' 'optional'? 'implicit'? name=NAME;
+ 'element' (optional ?= 'optional')? (implicit ?= 'implicit')? name=NAME;
EExpression:
STRING | PROPERTY;
Modified:
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java
URL:
http://svn.apache.org/viewvc/ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java?rev=1351146&r1=1351145&r2=1351146&view=diff
==============================================================================
---
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java
(original)
+++
ant/sandbox/antdsl/org.apache.ant.antdsl/src/org/apache/ant/antdsl/xtext/AntDslXTextProjectHelper.java
Sun Jun 17 19:59:12 2012
@@ -13,8 +13,11 @@ import org.apache.ant.antdsl.IfTask;
import org.apache.ant.antdsl.IfTask.ConditionnalSequential;
import org.apache.ant.antdsl.xtext.antdsl.EArgument;
import org.apache.ant.antdsl.xtext.antdsl.EArguments;
+import org.apache.ant.antdsl.xtext.antdsl.EAttribute;
+import org.apache.ant.antdsl.xtext.antdsl.EAttributes;
import org.apache.ant.antdsl.xtext.antdsl.EBranch;
import org.apache.ant.antdsl.xtext.antdsl.EConditionedTasks;
+import org.apache.ant.antdsl.xtext.antdsl.EElementAttribute;
import org.apache.ant.antdsl.xtext.antdsl.EExtensionPoint;
import org.apache.ant.antdsl.xtext.antdsl.EInnerElement;
import org.apache.ant.antdsl.xtext.antdsl.EInnerElements;
@@ -22,15 +25,22 @@ import org.apache.ant.antdsl.xtext.antds
import org.apache.ant.antdsl.xtext.antdsl.ENamespace;
import org.apache.ant.antdsl.xtext.antdsl.EProject;
import org.apache.ant.antdsl.xtext.antdsl.EPropertyAssignment;
+import org.apache.ant.antdsl.xtext.antdsl.ESimpleAttribute;
import org.apache.ant.antdsl.xtext.antdsl.ETarget;
import org.apache.ant.antdsl.xtext.antdsl.ETargetList;
import org.apache.ant.antdsl.xtext.antdsl.ETask;
import org.apache.ant.antdsl.xtext.antdsl.ETaskLists;
+import org.apache.ant.antdsl.xtext.antdsl.ETextAttribute;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.ExtensionPoint;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
+import org.apache.tools.ant.taskdefs.MacroDef;
+import org.apache.tools.ant.taskdefs.MacroDef.Attribute;
+import org.apache.tools.ant.taskdefs.MacroDef.NestedSequential;
+import org.apache.tools.ant.taskdefs.MacroDef.TemplateElement;
+import org.apache.tools.ant.taskdefs.MacroDef.Text;
import org.apache.tools.ant.taskdefs.Property;
import org.apache.tools.ant.taskdefs.Sequential;
import org.apache.tools.ant.taskdefs.condition.Condition;
@@ -105,8 +115,46 @@ public class AntDslXTextProjectHelper ex
}
}
- private void mapMacro(Project project, AntDslContext context, EMacrodef
macro) {
- // TODO
+ private void mapMacro(Project project, AntDslContext context, EMacrodef
emacro) {
+ MacroDef macroDef = new MacroDef();
+ macroDef.setName(emacro.getName());
+ EAttributes eatts = emacro.getAttributes();
+ if (eatts != null) {
+ for (EAttribute eatt : eatts.getAttributes()) {
+ if (eatt instanceof ESimpleAttribute) {
+ ESimpleAttribute esimpleatt = (ESimpleAttribute) eatt;
+ Attribute att = new Attribute();
+ att.setName(esimpleatt.getName());
+ att.setDefault(esimpleatt.getDefault());
+ macroDef.addConfiguredAttribute(att);
+ } else if (eatt instanceof ETextAttribute) {
+ ETextAttribute etextatt = (ETextAttribute) eatt;
+ Text text = new Text();
+ text.setName(etextatt.getName());
+ text.setTrim(etextatt.isTrimmed());
+ text.setOptional(etextatt.isOptional());
+ macroDef.addConfiguredText(text);
+ } else if (eatt instanceof EElementAttribute) {
+ EElementAttribute eelematt = (EElementAttribute) eatt;
+ TemplateElement element = new TemplateElement();
+ element.setImplicit(eelematt.isImplicit());
+ element.setOptional(eelematt.isOptional());
+ element.setName(eelematt.getName());
+ macroDef.addConfiguredElement(element);
+ } else {
+ throw new IllegalArgumentException("Unsupported macro
attribute " + eatt.getClass().getName());
+ }
+ }
+ }
+ ETaskLists etasks = emacro.getTasks();
+ if (etasks != null) {
+ NestedSequential seq = macroDef.createSequential();
+ for (ETask etask : etasks.getTasks()) {
+ seq.addTask(mapTask(project, context, etask));
+ }
+ }
+ macroDef.setProject(project);
+ macroDef.execute();
}
private Target mapTarget(Project project, AntDslContext context, ETarget
eTarget) {
Modified: ant/sandbox/antdsl/test/build.ant
URL:
http://svn.apache.org/viewvc/ant/sandbox/antdsl/test/build.ant?rev=1351146&r1=1351145&r2=1351146&view=diff
==============================================================================
--- ant/sandbox/antdsl/test/build.ant (original)
+++ ant/sandbox/antdsl/test/build.ant Sun Jun 17 19:59:12 2012
@@ -6,7 +6,11 @@ default : build
echo(message="${foo}")
}
-macrodef mymacro(dest, arg2 = "defaultvalue", element source) {
+macrodef mymacro(t = "mymacro") {
+ echo(message = "@{t}")
+}
+
+macrodef mymacro2(dest, element implicit source) {
copy(todir = "@{dest}") {
source()
}
@@ -19,16 +23,15 @@ target build {
jar(file = "${basedir}/build/test/my.jar") {
fileset(dir="${basedir}/test", includes="*.ant")
}
+ mymacro()
+ mymacro(t = "simple macro")
/*
* Some multiline comment
*/
if (available(file = "${basedir}/test/build.ant")) {
- /* NOT WORKING YET
- mymacro(dest = "${basedir}/build/test/dest") {
+ mymacro2(dest = "${basedir}/build/test/dest") {
fileset(dir = "${basedir}", includes="build.*")
}
- */
- echo(message = "mymacro call")
} else {
fail(message = "fail !")
}