|
Re: use the importURI mechanism to incude an ecore model [message #875475 is a reply to message #875280] |
Tue, 22 May 2012 20:18 |
|
Hi,
yes this is possible. Xtext itself does such a thing (for .xtext files) but they do not use import uris.
Here a very simple example (using namespace uri imports)
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
Model:
elements+=Element*;
Element:
(g+=Import)+ f=Foreach
;
Import:
'import' package=[ecore::EPackage|STRING] 'as'name=ID
;
Foreach:
'Foreach' name=ID 'in' table=Table
;
Table:
ecoreTypes=[Import]'.'class=[ecore::EClass|ID]
;
public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
public IScope scope_Import_package(Import context, EReference reference) {
IScope result = new FilteringScope(delegateGetScope(context, reference), new Predicate<IEObjectDescription>() {
@Override
public boolean apply(IEObjectDescription input) {
String isNSURI = input.getUserData("nsURI");
return "true".equals(isNSURI);
}
});
return result;
}
IScope scope_Table_class(Table t, EReference reference) {
return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(t.getEcoreTypes().getPackage(), EClass.class));
}
}
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
public Class<? extends IQualifiedNameConverter> bindIQualifiedNameConverter() {
return XtextQualifiedNameConverter.class;
}
}
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Re: use the importURI mechanism to incude an ecore model [message #876014 is a reply to message #875922] |
Wed, 23 May 2012 18:40 |
|
Hi,
id do this like this (to get content assist working)
IScope scope_Table_class(Model context, EReference reference) {
return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getIm().getPackage(), EClass.class));
}
or (if that would not count)
IScope scope_Table_class(Table t, EReference reference) {
Model m = EcoreUtil2.getContainerOfType(t, Model.class);
return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(m.getIm().getPackage(), EClass.class));
}
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: use the importURI mechanism to incude an ecore model [message #882438 is a reply to message #876014] |
Wed, 06 June 2012 13:12 |
asma geg Messages: 5 Registered: April 2012 |
Junior Member |
|
|
Hi,
I developped the grammer and the scope class. Now, I have another problem : it seems that there are ambiguities between rules (rule Fonction and rule Variable) while using autocompletion, and I don't know how to resolve that.
This is my grammar : (In the Foreach rule, we can call Variable rule or Fonction rule.)
import "..." as ecore
generate ...
Model:
im=Import
(elements+=Template_file)*;
Import:
'import' package=[ecore::EPackage|STRING]
;
Template_file:
Foreach
;
Foreach:
'Foreach' name=ID 'in' table=Create_table
(tpl+=(Variable|Fonction))+
'EndForeach'
;
Create_table:
sgbd=SGBD_Querry | fct=Fonction
;
SGBD_Querry:
'SELECT * FROM' class=[ecore::EClass| ID]
;
Variable:
'$' foreach=[Foreach] '.' attribut=[ecore::EAttribute|ID]
;
Fonction:
'$' field=[ecore::EClass |ID] '.' Operation=[ecore::EOperation |ID] '(' (parametres+=ID (',' parametres+=ID)*)* ');'
;
And the scope class is:
public class Test1ScopeProvider extends AbstractDeclarativeScopeProvider {
public IScope scope_Import_package(Import context, EReference reference) {
IScope result = new FilteringScope(delegateGetScope(context, reference), new Predicate<IEObjectDescription>() {
@Override
public boolean apply(IEObjectDescription input) {
String isNSURI = input.getUserData("nsURI");
return "true".equals(isNSURI);
}
});
return result;
}
public IScope scope_SGBD_Querry_class(Model context, EReference reference) {
return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getIm().getPackage(), EClass.class));
}
public IScope scope_Variable_attribut(Variable context,EReference reference){
return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getForeach().getTable().getSgbd().getClass_(), EAttribute.class));
}
public IScope scope_Fonction_field(Model context,EReference reference ){
// I return only classes that have EOperations as children
ArrayList<EObject> result= new ArrayList<EObject>();
java.util.List<EClass> allClasses=EcoreUtil2.getAllContentsOfType(context.getIm().getPackage(), EClass.class);
int i =0;
while(i<allClasses.size()){
EObject classe=allClasses.get(i);
List<EOperation> allOperations= EcoreUtil2.getAllContentsOfType(classe,EOperation.class);
if(!allOperations.isEmpty()){
result.add(classe);
}
i++;
}
return Scopes.scopeFor(result);
}
public IScope scope_Fonction_Operation(Fonction context,EReference reference ){
return Scopes.scopeFor(EcoreUtil2.getAllContentsOfType(context.getField(), EOperation.class));
}
}
I don't have problems of compilation, but when I instantiate the editor, autocompletion tool gives wrong propositions!!
This is an example of the ecore models I use : (I can't post images,so this is the textual representation of the model)
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0"
xmlns:xmi="..." xmlns:xsi="..."
xmlns:ecore="..." name="MyPackage"
nsURI="..." nsPrefix="">
<eClassifiers xsi:type="ecore:EClass" name="Class1">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="Att1"/>
<eStructuralFeatures xsi:type="ecore:EAttribute" name="Att2"/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="class2">
<eStructuralFeatures xsi:type="ecore:EAttribute" name="Att" defaultValueLiteral=""/>
</eClassifiers>
<eClassifiers xsi:type="ecore:EClass" name="Operation">
<eOperations name="operation1"/>
<eOperations name="operation2"/>
</eClassifiers>
</ecore:EPackage>
And this what I have in the editor:
import "..."
Foreach i in SELECT * FROM Class1
$i.Att1 //Nothing was proposed after $i. so I wrote the name of the attribute
//Autocompletion proposes here $ or ( !!!
$Operation. //Nothing is proposed !!!
EndForeach
Have you an idea of the problem?
Thanks
Asma
[Updated on: Wed, 06 June 2012 13:15] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03508 seconds