Scoping with a custom VariableDeclaration in XBase [message #1774365] |
Fri, 13 October 2017 07:08  |
Eclipse User |
|
|
|
Hello,
I've got a problem currently while implementing my dsl based on xbase.
What I want to do is to use my own datatypes and map them to the java ones.
I'd like to use something like "integer", "string" for "int" and "String".
My Grammar looks like this:
Model:
body=XBlockExpression;
Variable:
'variable' name=ID ':' typing=('integer' | 'string');
@ Override XPrimaryExpression returns XExpression:
XConstructorCall |
/* nothing else changed in here */
XParenthesizedExpression |
Variable;
So I hardcoded the names for the typings.
My typecomputer looks like this:
def dispatch computeTypes(XExpression expr, ITypeComputationState state) {
if (expr instanceof Variable) {
_computeTypes(expr, state)
} else {
super.computeTypes(expr, state)
}
}
protected def _computeTypes(Variable vr, ITypeComputationState state) {
if (vr.typing.equals('integer')) {
state.acceptActualType(getTypeForName(int, state))
} else {
state.acceptActualType(getTypeForName(String, state))
}
// TODO Find out how to override it
addLocalToCurrentScope(vr, state)
}
// override addLocalToCurrentScope(XExpression expression, ITypeComputationState state) {
// if (expression instanceof XVariableDeclaration) {
// addLocalToCurrentScope(expression, state);
// }
// if (expression instanceof Variable) {
// addLocalToCurrentScope(expression, state)
// }
// }
//
// def addLocalToCurrentScope(Variable localVariable, ITypeComputationState state) {
// Problem in the following line
// state.addLocalToCurrentScope(localVariable);
// state.rewriteScope(localVariable);
// }
The problem I get is when adding it to the scope because the Variable rule is no JvmIdentifiableElement, but when I make it return one, I can't add it to the XPrimaryExpressions anymore. The types of the variables are set correctly and can be compiled by the following:
override _toJavaStatement(XExpression variable, ITreeAppendable a, boolean isReferenced) {
if (variable instanceof Variable) {
a.newLine()
a.append(variable.type.simpleName + " " + variable.name + ";")
} else {
super._toJavaStatement(variable, a, isReferenced)
}
}
The example code
{
variable test : integer
variable text : string
variable b : integer
}
gets correctly compiled to functional java code. The problem is can't use anything like "b = 5" e.g. because it isn't in the scope.
What do I have to fix here to get it added to the scope? Or is there any other way to use custom names for the java types for my use case?
If there are any more questions regarding my problem feel free to ask!
Thanks for your help in advance!
|
|
|
|
|
|
|
|
|
Re: Scoping with a custom VariableDeclaration in XBase [message #1774517 is a reply to message #1774509] |
Mon, 16 October 2017 12:28   |
Eclipse User |
|
|
|
but what do i need to do to get the error?
the java code
public class Klasse {
public static void main(final String[] args) {
int a;
a = Integer.valueOf(3);
}
}
is fine imho
the hover is a different thing. if you dont have a explicit type you have to treat that as well.
but you dont.
org.eclipse.xtext.xbase.ui.hover.XbaseDeclarativeHoverSignatureProvider._signature(XAbstractFeatureCall, boolean)
is the place to look at
i have no idea why its not working. i assume your type computer is bogus. should be something like
protected def _computeTypes(Variable vr, ITypeComputationState state) {
if (vr.typing.equals('integer')) {
System.out.println("Calculating int types")
vr.name = vr.simpleName
state.assignType(vr, getRawTypeForName(Integer.TYPE, state))
} else if (vr.typing.equals('string')) {
System.out.println("Calculating string types")
state.assignType(vr, getTypeForName(String, state))
} else if (vr.typing.equals('rational')) {
System.out.println("Calculationg rational type")
state.assignType(vr, getTypeForName(Double.TYPE, state))
} else {
System.out.println("Else")
}
// addLocalToCurrentScope(vr, state)
}
|
|
|
|
Powered by
FUDForum. Page generated in 0.05906 seconds