Xbase: TypeComputer for own generated Java Classes [message #1775061] |
Tue, 24 October 2017 09:18 |
René Bärnreuther Messages: 31 Registered: October 2017 |
Member |
|
|
Hello, it's me again! :-)
After understanding how to type simple types correctly with the xbase type computer, I tried implementing a version for Classes as Types.
The Grammer is, simplified, the following:
Program:
{Program}
body = XBlockExpression
types += Type
;
Variable returns XVariableDeclaration:
{Variable} writeable?='variable' simpleName=ID ':' (typing=DataType);
Type:
(Enumeration | Composition);
DataType:
(SimpleDataType | ComplexDataType | ListType);
ListType:
=> ('list' '<' type=DataType) '>';
ComplexDataType:
{ComplexDataType} typeName=[Type];
Composition:
{Composition} "type" name=ID ":" "structure" "{" (attributes+=Attribute)* "}";
Attribute:
{Attribute}
"attribute" name=ID ":" typing=DataType ";";
Enumeration:
{Enumeration} "type" name=ID ":" "enumeration" "{" values+=EnumTypeValue ("," values+=EnumTypeValue)* "}";
EnumTypeValue:
name=ID;
SimpleDataType:
{SimpleDataType} typeName=('integer' | 'string' | 'rational' | 'boolean');
@Override
XPrimaryExpression returns XExpression:
/* Default XPrimaryExpressions */ |
Variable;
I try to get the type for ComplexDataType which is a reference to a created type.
The user should be able to program software like this:
{
variable a : TestStruc
a.a = 3
}
type TestStruc : structure {
attribute a : integer;
}
The parsing seems to work. But the type of a is CompositionImpl a instead of TestStruc. The second line (a.a = 3) doesn't work either, but I think it depends on the type of the first one.
The TypeComputer for the Compositions looks like the following currently:
/* THE PROBLEM IS PROBABLY HERE BECAUSE THE TYPE DOESN'T GET CALCULATED CORRECTLY */
var cdt = vr.typing as ComplexDataType
if (cdt.typeName instanceof Enumeration) {
state.assignType(vr, getRawTypeForName((cdt.typeName as Enumeration).class, state)) // is this the right way?
} else if (cdt.typeName instanceof Composition) { /* Composition */
var comp = cdt.typeName as Composition
state.assignType(vr, getRawTypeForName((comp as Composition).class, state))
// TODO Here is the mistake I guess
}
I suppose the error is in the "state.assignType.." line, but I haven't found a way to set the type, in the example "TestStruc" correctly. What do I have to change here?
A working sample can be found here: https://github.com/rehne93/dsl-complex-type-mapping
The grammar extension is .mydsl. The relevant parts are in the compiler and the typing package.
The compiler seems to work, the variable gets generated as "TestStruc a = new TestStruc()"; but the feature call uses the type of "CompositionImpl" which doesn't work obviously.
If there are any more relevant questions, feel free to ask.
As always, thanks a lot in advance!
|
|
|
|
Re: Xbase: TypeComputer for own generated Java Classes [message #1775088 is a reply to message #1775064] |
Tue, 24 October 2017 13:11 |
René Bärnreuther Messages: 31 Registered: October 2017 |
Member |
|
|
The inferred type of the name should be "TestStruct" in the given example, right?
The function doesn't take this for me. As a String in getRawTypyeForName(comp.name, state) the method doesn't allow String as parameter. comp.name is the value "TestStruct".
The only possibility (I see) would be to add it as a Qualifiedname in getRawType(QualifiedName, JvmType, LightweightreferenceType) but here I got the problem to get the JvmType and the LightweightreferenceType.
Is there any way to access the created class TestStruct? If I add something like "Composition" it works, but then the type isnt shown correctly. It only shows "TestStruct" and the TestStruct class as derived type.
I'm using Xtext 2.12 if this matters.
[Updated on: Tue, 24 October 2017 13:13] Report message to a moderator
|
|
|
Re: Xbase: TypeComputer for own generated Java Classes [message #1775108 is a reply to message #1775088] |
Tue, 24 October 2017 16:27 |
|
well yes you have to handcraft you a method like that youself from
org.eclipse.xtext.xbase.typesystem.computation.AbstractTypeComputer.getTypeForName(Class<?>, ITypeComputationState)
org.eclipse.xtext.xbase.typesystem.computation.AbstractTypeComputer.findDeclaredType(Class<?>, ITypeComputationState)
org.eclipse.xtext.xbase.typesystem.computation.AbstractTypeComputer.findDeclaredType(Class<?>, ITypeReferenceOwner)
org.eclipse.xtext.common.types.util.TypeReferences.findDeclaredType(String, Notifier)
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04036 seconds