Scoping with a custom VariableDeclaration in XBase [message #1774365] |
Fri, 13 October 2017 11:08 |
René Bärnreuther Messages: 31 Registered: October 2017 |
Member |
|
|
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 #1774372 is a reply to message #1774365] |
Fri, 13 October 2017 11:49 |
|
what about simply resusing XVariableDeclaration e.g.
Variable returns XVariableDeclaration:
{Variable} writeable?='variable' simpleName=ID ':' typing=('integer' | 'string');
// dont need to call add, but need to customize e.g. validation, compiler etc
or having expression and jvmidentifiable element as supertype
Variable returns XExpression:
{Variable}'variable' simpleName=ID ':' typing=('integer' | 'string');
VariableUncalled returns JvmIdentifiableElement:
{Variable} simpleName=ID
;
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
Re: Scoping with a custom VariableDeclaration in XBase [message #1774498 is a reply to message #1774380] |
Mon, 16 October 2017 13:54 |
René Bärnreuther Messages: 31 Registered: October 2017 |
Member |
|
|
First of all, thanks for your help and patience.
I'm quite new to xbase in general and tried to work out some things by my own, but got stuck now (again..).
I changed the validator and the types of variable declarations are better now. They get compiled correctly to valid Java Code.
The problems that arrise now are in Assignments. The variable name is always from type null when I hover over it. Is this a error in the typecomputing / compiling or do I have to override the Tooltips here as well?
Is there somewhere an example to have a look at or some other documentation about this topic? I haven't found anything but maybe I was just using the wrong keywords for my research.
I made a git repo with my current example: github.com/rehne93/dsl-type-mapping-example (please just copy&paste it).
I tried overriding the Assignmenttojava Operation to
var feature = expr.getFeature()
if(feature instanceof Variable){
b.append(feature.simpleName + " = " )
internalToJavaExpression(expr.value, b)
}
(Can be found in TestComp in the src/compiler package).
It works with simple variables event though with the following example:
variable a : integer
a = 3
it generates the following java code:
int a;
a = Integer.valueOf(3);
I think the reason is, that the type is not set correctly. The type of XVariableDeclaration is currently empty because I use the typing in the grammar (see post above) to determine the type and set it in the TypeComputer. I tried setting the correct type of the XVariableDeclaration by hand in the code, but I wasn't successfull doing this.
Calculations like the sum of a double (which I called "rational") and a integer don't work either.
My current Xtext version is 2.12.0, Xtend the same.
Any hint would be much appreciated! Thanks in Advance.
For further question feel free to ask.
[Updated on: Mon, 16 October 2017 13:55] Report message to a moderator
|
|
|
|
|
Re: Scoping with a custom VariableDeclaration in XBase [message #1774517 is a reply to message #1774509] |
Mon, 16 October 2017 16:28 |
|
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)
}
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
Powered by
FUDForum. Page generated in 0.06436 seconds