Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Cross referencing to an attribute of an instance of a class
Cross referencing to an attribute of an instance of a class [message #901536] Mon, 13 August 2012 09:40 Go to next message
Pooyan Behnamghader is currently offline Pooyan BehnamghaderFriend
Messages: 7
Registered: July 2012
Junior Member
Hello.
I'm a beginner in xtext and I have a problem with cross referencing to an attribute of an instance of a class.
Here is my DSL:

def dispatch String compileClass( IntegerD id){
		var res= ""
		res = res + 'String ' + id.name +'\n' // IntegerD.name +'\n'
		return res
}
class IntegerD{
	var name = "-var name-"
}


I want to access to id.name but I cannot refer to it. I can Only refer to IntegerD.name.
Here is my grammer (just important parts):

Function:
	'def' 'dispatch'? type=Type name=ID '('params+=Pramater*')' '{'
		res=Variable
		stts+=Statement*
		'return' ret=[Variable]
	'}' ;
Statement:
	left=[Variable] '=' right=StringExp;
StringExp returns Expression:
	PrimaryExp ({StringExp.left=current} '+' right=PrimaryExp)*;
PrimaryExp returns Expression:
	StringLit | VariableRef;
StringLit:
	value=STRING;
VariableRef:
	value=[Variable|QualifiedName];
Variable:
	'var' name=ID ('=' value=STRING)?;
Pramater returns Variable:
	type=[Class|ID] name=ID;
Class:
	'class' name=ID '{'
		vars+= Variable*
	'}';


I was wondering if any one can help me to solve the problem.
I appreciate your answers in advance.

[Updated on: Mon, 13 August 2012 09:47]

Report message to a moderator

Re: Cross referencing to an attribute of an instance of a class [message #901639 is a reply to message #901536] Mon, 13 August 2012 18:19 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13493
Registered: July 2009
Senior Member
Hi,

you have to adjust scoping for that e.g.

public class MyDslScopeProvider extends AbstractDeclarativeScopeProvider {
	
	IScope scope_VariableRef_value(Function context, EReference ref) {
		List<IEObjectDescription> scope = new ArrayList<>();
		for (final Variable v : context.getParams()) {
			com.google.common.base.Function<Variable, QualifiedName> op = new com.google.common.base.Function<Variable, QualifiedName>() {
				
				public QualifiedName apply(Variable var) {
					return QualifiedName.create(v.getName(), var.getName());
				}
				
			};
			Iterables.addAll(scope, Scopes.scopedElementsFor(v.getType().getVars(), op ));
			List<Variable> localVars = new ArrayList<>();
			localVars.add(context.getRes());
			for (Statement s : context.getStts()) {
				localVars.addAll(EcoreUtil2.getAllContentsOfType(s, Variable.class));
			}
			Iterables.addAll(scope, Scopes.scopedElementsFor(localVars));
			
		}
		
		return new SimpleScope(scope);
	}

}


~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Cross referencing to an attribute of an instance of a class [message #901688 is a reply to message #901639] Tue, 14 August 2012 07:39 Go to previous message
Pooyan Behnamghader is currently offline Pooyan BehnamghaderFriend
Messages: 7
Registered: July 2012
Junior Member
It works now Smile
Your answer was so clear and helpful.
Thank you Christian.
Previous Topic:NullPointerException reading Xtext model outside of Eclipse
Next Topic:How to provide functionality on CTRL+click ?
Goto Forum:
  


Current Time: Thu Jul 09 09:51:55 GMT 2020

Powered by FUDForum. Page generated in 0.01885 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top