Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » From custom types to Java types
From custom types to Java types [message #1850446] Thu, 03 March 2022 09:45 Go to next message
Mandy Neumann is currently offline Mandy NeumannFriend
Messages: 6
Registered: March 2022
Junior Member
Suppose I want users to be able to write a model like this:

entity Foo {

  name: Text
  amount: Number
  relative: Bar

}

entity Bar {
  ...
}


That is, features of an entity can have as type either another entity, or some pre-defined type such as 'Text' or 'Number'. I will need to validate that this is true, so that one cannot use some other arbitrary type. I will also need to map these pre-defined types to Java types when generating code, e.g. 'Text' -> 'java.lang.String', and 'Number' -> 'java.math.Bigdecimal'.

How can I express this in a grammar? This is what i got so far:

grammar my.package.Dsl with org.eclipse.xtext.xbase.Xtype

generate dsl "..."

Model:
    (elements+=Entity)*;

Entity:
    'entity' name=ID '{'
        (features+=Feature)*
    '}';

Feature:
    name=ID ':' type=[FeatureType|QualifiedName];

FeatureType:
	 Entity;

// this is possible in the grammar, although with warning, and I cannot check for the type in validation and code generation:
//FeatureType:
//    Entity | 'Text' | 'Number';



Any help appreciated.
Re: From custom types to Java types [message #1850459 is a reply to message #1850446] Thu, 03 March 2022 15:10 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
if you do the xbase route you should check https://www.eclipse.org/Xtext/documentation/104_jvmdomainmodel.html
if not you should have something like

Model:
    (elements+=Entity)*;

Entity:
    'entity' name=ID '{'
        (features+=Feature)*
    '}';

Feature:
    name=ID ':' type=FeatureType;
FeatureType: SimpleType | Ref;
Ref: e=[Entity];
SimpleType: type=('Text' | 'Number');
    ;



Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: From custom types to Java types [message #1850460 is a reply to message #1850446] Thu, 03 March 2022 15:11 Go to previous messageGo to next message
Rubén Porras Campo is currently offline Rubén Porras CampoFriend
Messages: 67
Registered: July 2009
Member
Hi Mandy,

I think the best is to avoid having Number and Text as part of the grammar. Instead you can have the types as built-in types by defining them in an xmi file and the accessing the file using custom scoping.

See an example file in https://github.com/dsldevkit/dsl-devkit/blob/master/com.avaloq.tools.ddk.typesystem/model-instance/BuiltInTypeModel.xmi and the code to read the file and create the EObjects in https://github.com/dsldevkit/dsl-devkit/blob/master/com.avaloq.tools.ddk.typesystem/src/com/avaloq/tools/ddk/typesystem/BuiltInTypeModelAccess.java

Regards
Re: From custom types to Java types [message #1850473 is a reply to message #1850459] Fri, 04 March 2022 07:32 Go to previous messageGo to next message
Mandy Neumann is currently offline Mandy NeumannFriend
Messages: 6
Registered: March 2022
Junior Member
Christian Dietrich wrote on Thu, 03 March 2022 15:10
if you do the xbase route you should check https://www.eclipse.org/Xtext/documentation/104_jvmdomainmodel.html


Thanks Christian, I read this tutorial earlier but I am not sure if it helps in my case. The idea of my language is that the person writing a model does not have to know anything about concrete Java types, this is why I want to pre-define just a few "types of types" such as textual or numeric types, also dates etc. Only during generation these shall be mapped to specific Java types like String, Double etc. This way I could also decide to change the types later, e.g. when I find that BigDecimal would be a better fit than Double for the "numeric" types. But the user should not be bothered with this and should not need to worry about imports etc like in the example.

Rubén Porras Campo wrote on Thu, 03 March 2022 15:11
I think the best is to avoid having Number and Text as part of the grammar. Instead you can have the types as built-in types by defining them in an xmi file and the accessing the file using custom scoping.


Thanks Rubén, sounds like an interesting idea. I will have a look at it and try to figure out how to use it. Would I need to put it into a separate project and reference it from my xtext project?
Re: From custom types to Java types [message #1850479 is a reply to message #1850473] Fri, 04 March 2022 11:34 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
but then the

FeatureType: SimpleType | Ref;
Ref: e=[Entity];
SimpleType: type=('Text' | 'Number');
;

should work, doesnt it?


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: From custom types to Java types [message #1850480 is a reply to message #1850479] Fri, 04 March 2022 12:39 Go to previous messageGo to next message
Mandy Neumann is currently offline Mandy NeumannFriend
Messages: 6
Registered: March 2022
Junior Member
Christian Dietrich wrote on Fri, 04 March 2022 11:34
but then the

FeatureType: SimpleType | Ref;
Ref: e=[Entity];
SimpleType: type=('Text' | 'Number');
;

should work, doesnt it?


Kind of. In this simple example yes. My actual grammar is a bit more complex, I simplified it here for you. Actually I have more than one type of entity and I will need to figure out the correct rules, currently the ANTLR parser generator does not accept my grammar because of ambiguities ("Decision can match input such as "RULE_ID" using multiple alternatives"). But since the simplified example is working, I guess I should get it to work in the real grammar, too.
Re: From custom types to Java types [message #1850484 is a reply to message #1850480] Fri, 04 March 2022 16:03 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
if my snippet there in 1 id only
the implicit one e=[Entity]; ==> e=[Entity|ID];

so you must have a second one somewhere else e.g

e=[X] | f=[Y]


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Installation of older SDK Versions broken
Next Topic:Export DSL to PDF along with highlighting
Goto Forum:
  


Current Time: Sat Apr 20 03:06:45 GMT 2024

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

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

Back to the top