|
|
|
|
Re: Cross reference for multiple identifers [message #1696665 is a reply to message #1696663] |
Wed, 27 May 2015 18:13 |
|
Hi,
this should be quite easy
Model:
initialize+=declarations*
expressions+=expression*
;
Integer_dec:
'Integer' identifiersList=identifier_list ( '=' val=Ivalues) ';'
;
Float_dec:
'float' identifiersList=identifier_list ( '=' val=Fvalues) ';'
;
constants_dec:
'constant' name=ID '=' val=Cval ';'
;
identifier_list:
identifers+=ID (',' identifers+=ID)* ;
declarations returns Declarations:
Integer_dec
| Float_dec
;
expression:
leftOP=[Declarations] (operator="+" rightOP=[Declarations])*
;
Ivalues: INT;
Fvalues: INT;
Cval: STRING;
for global cross refs you have to adopt the DefaultResourceDescriptionStrategy.
public class MyDslResourceDescriptionStrategy extends
DefaultResourceDescriptionStrategy {
@Override
public boolean createEObjectDescriptions(EObject eObject,
IAcceptor<IEObjectDescription> acceptor) {
if(eObject instanceof Declarations) {
identifier_list l = ((Declarations) eObject).getIdentifiersList();
for (String i : l.getIdentifers()) {
acceptor.accept(EObjectDescription.create(QualifiedName.create(i), eObject));
}
}
return super.createEObjectDescriptions(eObject, acceptor);
}
}
for local ones
public class MyDslImportedNamespaceAwareLocalScopeProvider extends
ImportedNamespaceAwareLocalScopeProvider {
@Override
protected IScope getLocalElementsScope(IScope parent, EObject context,
EReference reference) {
// TODO Auto-generated method stub
return super.getLocalElementsScope(parent, context, reference);
}
protected ISelectable internalGetAllDescriptions(final Resource resource) {
Iterable<EObject> allContents = new Iterable<EObject>(){
@Override
public Iterator<EObject> iterator() {
return EcoreUtil.getAllContents(resource, false);
}
};
Iterable<IEObjectDescription> transformed = IterableExtensions.flatten(Iterables.transform(allContents,
new Function<EObject, List<IEObjectDescription>>() {
@Override
public List<IEObjectDescription> apply(EObject from) {
if (from instanceof Declarations) {
List<IEObjectDescription> result = new ArrayList<IEObjectDescription>();
identifier_list l = ((Declarations) from).getIdentifiersList();
for (String i : l.getIdentifers()) {
result.add(EObjectDescription.create(QualifiedName.create(i), from));
}
return result;
} else {
final QualifiedName qualifiedName = getQualifiedNameProvider().apply(from);
if (qualifiedName != null)
return Lists.newArrayList(new EObjectDescription(qualifiedName, from, null));
return Collections.emptyList();
}
}
}));
return new MultimapBasedSelectable( Iterables.filter(transformed, Predicates.notNull()));
}
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
|
|
Re: Cross reference for multiple identifers [message #1700433 is a reply to message #1699118] |
Thu, 02 July 2015 14:42 |
|
P.S. here are the imports for the example you linked
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.resource.EObjectDescription;
import org.eclipse.xtext.resource.IEObjectDescription;
import org.eclipse.xtext.resource.ISelectable;
import org.eclipse.xtext.scoping.IScope;
import org.eclipse.xtext.scoping.impl.ImportedNamespaceAwareLocalScopeProvider;
import org.eclipse.xtext.scoping.impl.MultimapBasedSelectable;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Cross reference for multiple identifers [message #1700802 is a reply to message #1700433] |
Tue, 07 July 2015 10:10 |
Eleanor Richie Messages: 125 Registered: August 2014 |
Senior Member |
|
|
Hi Christian
Thanks alot for you help
I have been working in the scoping but I have a problem with the list of identifiers as this example:
expressions exp1 {
integer int11, int12, int13 = 10;
float flo11, flo12, flo13 = 5.5;
expressions exp2 {
integer int21, int22, int23 = 5;
int21 = int22 + int11;
}
}
Grammar rule:
expression returns Expressions:
'expressions' name=ID '{'
declarations+=declaration*
expressions={
statements+=statement*
expressions+=expression*
}
'}'
;
declaration returns Declaration:
'integer' identifiers=identifier_list (':' value=INTEGER)?
;
identifier_list returns IdentifierList:
identifierList+=simple (','identifierList+=simple)*
;
simple returns Identifier:
ID=ID
;
The problem:
When I create a scope for the declarations in the expressions, the scope is returned an empty simple scope. I create the scope in the getScope as the following :
IScope myScope= Scopes.scopeFor(((Expressions)context).getDeclarations());
I just want the scopeFor to get the declarations in the given list.
What I tried:
I override internalGetAllDescriptions and created the EObjectDescriptions as
for (Identifier i : l.getIdentifierList()) {
result.add(EObjectDescription.create(QualifiedName.create(i.getID()), from));
}
but this made the EObjectDescription to be created as int21 int22,...etc. without having the parent name concatenated i.e exp1.int11, exp1.int12,...etc. So I created a method that gets the parent's name then pass it to the QualifiedName.create(parent, i.getID()) in creating the EObjectDescription.
Also, I tried overriding the ResourceDescriptionStrategy and added to the acceptor the following:
for(Identifier i : l.getIdentifierList()){
acceptor.accept(EObjectDescription.create( QualifiedName.create(MyLangEObjectUtils.getContainerName(eObject), i.getID()), eObject));
}
.
and I have my bindings configured correctly in the RuntimeModule
But Still not working any help please
[Updated on: Tue, 07 July 2015 10:22] Report message to a moderator
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05162 seconds