Cross referencing to an attribute of an instance of a class [message #901536] |
Mon, 13 August 2012 09:40 |
Pooyan Behnamghader 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 |
|
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
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.02435 seconds