Limit scoping to a referenced declaration element [message #559283] |
Thu, 16 September 2010 09:27 |
awidegreen Messages: 14 Registered: July 2010 |
Junior Member |
|
|
Hej everyone!
I want to define a construct which is similar to the following grammar definition. Therefore I want to limit the scope of available elements to a declaration which is referenced through a partOf-element.
Model:
element=(Manifest|Definition);
Manifest:
'manifest' '{'
(declarations+=Declaration)+
'}';
Declaration:
name=QualifiedName;
Definition:
'partOf' partOf=[Declaration|QualifiedName]
(elements+=Element)+;
Element:
'element' name=QualifiedName '{'
'desc' '=' string=STRING
('linkTo' '=' linkTo=[Element|QualifiedName])?
'}';
QualifiedName:
ID ('.' ID)*;
The following listings illustrate the given grammar:
Define a single manifest-file:
After that two definitions with a linkTo Element.
partOf movie
element aFilm {
desc = "great film"
}
partOf actor
element anActor {
desc = "great Actor"
linkTo = aFilm
// i want to do something like: film.aFilm
}
As mentioned in the comment, I want to link the element through the declaration like: film.aFilm. In fact, I want to stick to the default scoping behavior, which means I don't want to specify the declaration every time e.g. when I define another element in the same part.
partOf film
element anotherFilm {
desc = "another film"
linkTo = aFilm
}
... well the last code doesn't make sense in that context, I know!
So is there a best practice for this feature? Can you give me a hint which Scoping-Class I maybe have to extend?
Thanks,
awidegreen
Update: thanks for the clarification Meinte
[Updated on: Thu, 16 September 2010 13:26] Report message to a moderator
|
|
|
|
Re: Scoping over a 'partOf' Element [message #559374 is a reply to message #559351] |
Thu, 16 September 2010 13:39 |
|
Hi,
i guess what you want to do is:
the qualified name of an element is:
(the name of the Declaration the Definition the Element is defined within references to using the partOf relation) + "." + the name of the Element
public class MyQNP extends DefaultDeclarativeQualifiedNameProvider{
String qualifiedName(Element e) {
return ((Definition)e.eContainer()).getPartOf().getName()+"."+e.getName();
}
}
but this will lead to the cyclic resolution of lazy links error and i have no idea how to get rid of this.
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
[Updated on: Thu, 16 September 2010 13:39] Report message to a moderator
|
|
|
Re: Scoping over a 'partOf' Element [message #559384 is a reply to message #559374] |
Thu, 16 September 2010 13:54 |
|
A ugly but working solution is:
grammar org.xtext.example.mydsl.MyDsl with org.eclipse.xtext.common.Terminals
import "http://www.eclipse.org/emf/2002/Ecore" as ecore
generate myDsl "http://www.xtext.org/example/mydsl/MyDsl"
Model:
element=(Manifest|Definition);
Manifest:
'manifest' '{'
(declarations+=Declaration)+
'}';
Declaration:
name=ID;
Definition:
'partOf' partOf=[Declaration]
(elements+=Element)+;
Element:
'element' name=ID '{'
'desc' '=' string=STRING
('linkTo' '=' linkToDeclaration=[Declaration] "." linkToElement=[Element])?
'}';
public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
IScope scope_Element_linkToElement(final Element e, EReference r) {
final String dname = e.getLinkToDeclaration().getName();
IScope delegateGetScope = delegateGetScope(e, r);
System.out.println(delegateGetScope);
return new FilteringScope(delegateGetScope,new Predicate<IEObjectDescription>() {
@Override
public boolean apply(IEObjectDescription input) {
if (input != null) {
Element eObjectOrProxy = (Element)input.getEObjectOrProxy();
if (eObjectOrProxy.eIsProxy()) {
eObjectOrProxy = (Element) EcoreUtil.resolve(eObjectOrProxy, e.eResource().getResourceSet());
}
Definition definition = (Definition)eObjectOrProxy.eContainer();
Declaration partOf = definition.getPartOf();
if (partOf.eIsProxy()) {
partOf = (Declaration) EcoreUtil.resolve(partOf, e.eResource().getResourceSet());
}
String declarationName = partOf.getName();
return declarationName.equals(dname);
}
return false;
}
});
}
}
public class MyQNP extends DefaultDeclarativeQualifiedNameProvider{
String qualifiedName(Element e) {
return e.getName();
}
}
public class MyDslRuntimeModule extends org.xtext.example.mydsl.AbstractMyDslRuntimeModule {
@Override
public Class<? extends IQualifiedNameProvider> bindIQualifiedNameProvider() {
return MyQNP.class;
}
}
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04019 seconds