[XText] PostProcessing Question - Add operations with implementation [message #481140] |
Wed, 19 August 2009 13:43  |
Eclipse User |
|
|
|
Hi everyone!
I'm thinking that I need a PostProcessing Definition in my Xtext Project.
In my grammar I have a Rule like this
Block:
'MyKeyword' myContent=STRING
;
The content of myContent can contain SubStructure(s) like
<Begin>ImportantContentPart<End>
After generating all the XText stuff based on my grammar definition, I
would like to have an operation called "getImportantConentPart". This
operation should be callable using an EObject of type "Block"
(myBlock.getImportantConentPart()).
Where can I find an example or a documentation to get this using the Xtend
post processing mechanism?
Any hints?
Thanks in advance, Helko
|
|
|
|
|
|
|
|
Re: [XText] PostProcessing Question - Add operations with implementation [message #481367 is a reply to message #481232] |
Thu, 20 August 2009 12:05  |
Eclipse User |
|
|
|
Hi
I've solved my problem using the post processor to define additional
helper methods for the generated Ecore Model Code.
Regards, Helko
ps:
Here is the content of my ext file:
import ecore;
process(xtext::GeneratedMetamodel this) :
ePackage.process()
;
process(EPackage this) :
eClassifiers.process()
;
process(EClassifier this) :
null
;
process(EClass this) :
if name=='Block' then eOperations.add(addOperation("getFMIdentifier",
'String fmIDSearchString = getAttributesFormatString();
String fmIDKeywordStart = "'+getBlockFMIdentifierKeywordStart()+'";
String fmIDKeywordEnd = "'+getBlockFMIdentifierKeywordEnd()+'";
if(fmIDSearchString == null) return null;
int indexOfFMIDStart = fmIDSearchString.indexOf(fmIDKeywordStart);
int indexOfFMIDEnd;
if (indexOfFMIDStart !=-1){
if(indexOfFMIDStart != fmIDSearchString.lastIndexOf(fmIDKeywordStart)){
//System.out.println("<FMID> scheint mehrfach vorzukommen!");
return null;
}
else{
indexOfFMIDEnd = fmIDSearchString.indexOf(fmIDKeywordEnd);
if(indexOfFMIDEnd != -1){
if(indexOfFMIDStart>indexOfFMIDEnd){
//System.out.println("</FMID> liegt scheinbar vor <FMID>!");
return null;
}
else{
if(indexOfFMIDEnd != fmIDSearchString.lastIndexOf(fmIDKeywordEnd)){
//System.out.println("</FMID> scheint mehrfach vorzukommen!");
return null;
}
else{
//System.out.println(fmIDSearchString.substring(indexOfFMIDS tart+6,
indexOfFMIDEnd));
try {
java.util.UUID.fromString(fmIDSearchString.substring(indexOf FMIDStart+fmIDKeywordStart.length(),
indexOfFMIDEnd));
} catch (Exception e) {
throw new java.lang.IllegalStateException("MalFormed MDL Block FM
Identifier:" +e.getMessage());
}
return
fmIDSearchString.substring(indexOfFMIDStart+fmIDKeywordStart .length(),
indexOfFMIDEnd);
}
}
}
else{
//System.out.println("</FMID> existiert nicht!");
return null;
}
}
}
else{
//System.out.println("<FMID> existiert nicht!");
return null;
}
'))
->
if name=='Block' then eOperations.add(addOperation("getCDMIdentifier",
'String cdmIDSearchString = getAttributesFormatString();
String cdmIDKeywordStart = "'+getBlockCDMIdentifierKeywordStart()+'";
String cdmIDKeywordEnd = "'+getBlockCDMIdentifierKeywordEnd()+'";
if(cdmIDSearchString == null) return null;
int indexOfCDMIDStart = cdmIDSearchString.indexOf(cdmIDKeywordStart);
int indexOfCDMIDEnd;
if (indexOfCDMIDStart !=-1){
if(indexOfCDMIDStart !=
cdmIDSearchString.lastIndexOf(cdmIDKeywordStart)){
//System.out.println("<CDMID> scheint mehrfach vorzukommen!");
return null;
}
else{
indexOfCDMIDEnd = cdmIDSearchString.indexOf(cdmIDKeywordEnd);
if(indexOfCDMIDEnd != -1){
if(indexOfCDMIDStart>indexOfCDMIDEnd){
//System.out.println("</CDMID> liegt scheinbar vor <CDMID>!");
return null;
}
else{
if(indexOfCDMIDEnd != cdmIDSearchString.lastIndexOf(cdmIDKeywordEnd)){
//System.out.println("</CDMID> scheint mehrfach vorzukommen!");
return null;
}
else{
//System.out.println(cdmIDSearchString.substring(indexOfCDMI DStart+6,
indexOfCDMIDEnd));
try {
java.util.UUID.fromString(cdmIDSearchString.substring(indexO fCDMIDStart+cdmIDKeywordStart.length(),
indexOfCDMIDEnd));
} catch (Exception e) {
throw new java.lang.IllegalStateException("MalFormed MDL Block CDM
Identifier:" +e.getMessage());
}
return
cdmIDSearchString.substring(indexOfCDMIDStart+cdmIDKeywordSt art.length(),
indexOfCDMIDEnd);
}
}
}
else{
//System.out.println("</CDMID> existiert nicht!");
return null;
}
}
}
else{
//System.out.println("<CDMID> existiert nicht!");
return null;
}
'))
;
create ecore::EOperation addOperation(ecore::EClassifier c, String
strName, String strBody):
setName(strName) ->
setEType(getEString()) ->
eAnnotations.add(addBodyAnnotation(strBody)) ->
((ecore::EClass)c).eOperations.add(this);
ecore::EDataType getEString():
JAVA de.***.xtext.MDLPostProcessorHelper.getEString();
String getBlockFMIdentifierKeywordStart():
JAVA
de.***.xtext.MDLPostProcessorHelper.getMdlBlockFmIdentifierK eywordStart();
String getBlockFMIdentifierKeywordEnd():
JAVA
de.***.xtext.MDLPostProcessorHelper.getMdlBlockFmIdentifierK eywordEnd();
String getBlockCDMIdentifierKeywordStart():
JAVA
de.***.xtext.MDLPostProcessorHelper.getMdlBlockCdmIdentifier KeywordStart();
String getBlockCDMIdentifierKeywordEnd():
JAVA
de.***.xtext.MDLPostProcessorHelper.getMdlBlockCdmIdentifier KeywordEnd();
create ecore::EOperation addOperation(ecore::EClassifier c, String
strName, ecore::EClassifier type,
List parameters,
String strBody):
setName(strName) ->
setEType(type) ->
parameters.collect(e|((List)e).setParameter(this)) ->
eAnnotations.add(addBodyAnnotation(strBody)) ->
((ecore::EClass)c).eOperations.add(this);
create ecore::EParameter setParameter(List parameter, ecore::EOperation
op):
setName((String)parameter.get(0)) ->
setEType((ecore::EClassifier) parameter.get(1)) ->
op.eParameters.add(this)
;
create ecore::EAnnotation addBodyAnnotation(ecore::EOperation op, String
strBody):
setSource("http://www.eclipse.org/emf/2002/GenModel") ->
this.createBody(strBody) ->
op.eAnnotations.add(this);
create ecore::EStringToStringMapEntry createBody(ecore::EAnnotation anno,
String strBody):
setKey("body") ->
setValue(strBody) ->
anno.details.add(this);
|
|
|
Powered by
FUDForum. Page generated in 0.09815 seconds