Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Scoping with a custom VariableDeclaration in XBase(I'm struggeling to add my custom variable declaration to the scope.)
Scoping with a custom VariableDeclaration in XBase [message #1774365] Fri, 13 October 2017 11:08 Go to next message
René Bärnreuther is currently offline René BärnreutherFriend
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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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 #1774375 is a reply to message #1774372] Fri, 13 October 2017 12:51 Go to previous messageGo to next message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
Thanks for your help!
I tried using the first version and changed the Variable to:
Variable returns XVariableDeclaration:
	{Variable} writeable?='variable' simpleName=ID ':' typing=('integer'|'string');

just like you recommended.
Now the variable is recognized in the scope, but the type doesn't compute correctly. It always says "Error type cannot be derrived".

My typecomputer is the following now:
override dispatch computeTypes(XVariableDeclaration vrbl, ITypeComputationState state){
		vrbl.writeable = true
		if(vrbl instanceof Variable){
				_computeTypes(vrbl, state)
		}else{
			System.out.println("In else")
		}
	}
	
	protected def _computeTypes(Variable vr, ITypeComputationState state){
		if(vr.typing.equals('integer')){
				state.acceptActualType(getTypeForName(int, state))
			}
	}


Everything gets called correctly but the type doesn't set like its supposed to. Why can't the type be derrived here?
{
	variable test : integer		
	test = 3
}

This is the example I tried it with.

Thanks a lot!
Re: Scoping with a custom VariableDeclaration in XBase [message #1774380 is a reply to message #1774375] Fri, 13 October 2017 13:29 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
as i said, adapt the VALIDATOR ?!?

e.g

@Check override checkVariableDeclaration(XVariableDeclaration declaration) {
if (declaration instanceof Variable) return;
super.checkVariableDeclaration(declaration)
}


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 Go to previous messageGo to next message
René Bärnreuther is currently offline René BärnreutherFriend
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 #1774503 is a reply to message #1774498] Mon, 16 October 2017 14:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
can you please share a sample dsl and model project in github / gitlab etc so that i dont need that to reproduce everything myself

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Scoping with a custom VariableDeclaration in XBase [message #1774509 is a reply to message #1774498] Mon, 16 October 2017 15:03 Go to previous messageGo to next message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
René Bärnreuther wrote on Mon, 16 October 2017 13:54

I made a git repo with my current example: github.com/rehne93/dsl-type-mapping-example (please just copy&paste it).


Please look here. The extension is .mydsl

[Updated on: Mon, 16 October 2017 15:04]

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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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
Re: Scoping with a custom VariableDeclaration in XBase [message #1774519 is a reply to message #1774517] Mon, 16 October 2017 18:12 Go to previous message
René Bärnreuther is currently offline René BärnreutherFriend
Messages: 31
Registered: October 2017
Member
The assignType command was exactly the one where it went wrong. Now the types are calculated correctly. Even in the tooltip they're shown as the right type.
Thanks a lot for your help!
Previous Topic:2.13 nightly build on Oxygen 1a / Java 9 fails to Generate MWE2
Next Topic:Expression Language: Using ".*" in combination with Decimal Numbers
Goto Forum:
  


Current Time: Tue Apr 16 22:14:53 GMT 2024

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

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

Back to the top