From custom types to Java types [message #1850446] |
Thu, 03 March 2022 04:45  |
Eclipse User |
|
|
|
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.
|
|
|
|
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04733 seconds