Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Static Enumeration Literal Assignment(Assigned action for user enum possible?)
Static Enumeration Literal Assignment [message #897389] Mon, 23 July 2012 23:31 Go to next message
Stefan Kuhn is currently offline Stefan KuhnFriend
Messages: 8
Registered: July 2009
Junior Member
I'm trying to implement mini java[1] with xText. While refactoring the grammar, I didn't find a solution to statically assign an Enumeration Literal to a property.

Right now, it looks like:

Type returns AbstactType	: 	
		{IntegerArrayType} 	"int"  "["  "]"  
	| 	{BooleanType} 		"boolean"
	| 	{IntegerType} 		"int"
	| 	{ClassifierType} 	name=Identifier;


I would like to group fixed types together, using an enumeration, e.g.:

enum FixedTypeKind  :
		INTEGER | INTEGER_ARRAY | BOOLEAN ;


doing something like this

Type returns AbstactType	: 	
		{FixedType.value=FixedTypeKind.INTEGER_ARRAY}	"int"  "["  "]"  
	| 	{FixedType.value=FixedTypeKind.BOOLEAN}		"boolean"
	| 	{FixedType.value=FixedTypeKind.INTEGER} 	"int"
	| 	{ClassifierType} 				name=Identifier;


Is this possible?

[1] www.cambridge.org/us/features/052182060X/grammar.html
Re: Static Enumeration Literal Assignment [message #897392 is a reply to message #897389] Tue, 24 July 2012 00:39 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2500
Registered: July 2009
Senior Member
Why would you want something like that?
Why not simply:

Type returns AbstractType
: FixedType
| ClassifierType
;

FixedType returns AbstractType
: IntegerType
| IntegerArrayType
| BooleanType
;

IntegerType : {IntegerType} "int" ;
.... etc

There are many concerns to think about when doing this. Do you want to
be able to reference types (including the fixed ones)? How do you want
code completion to work? (It is different for data values such as enums
and for keywords.

And... as you start down this path, why not reuse Xbase or Xtend to
benefit from the java type system support? (Will save you lots of work
(and trouble)).

- henrik

On 2012-24-07 1:31, Stefan Kuhn wrote:
> I'm trying to implement mini java[1] with xText. While refactoring the
> grammar, I didn't find a solution to statically assign an Enumeration
> Literal to a property.
>
> Right now, it looks like:
>
> Type returns AbstactType :
> {IntegerArrayType} "int" "[" "]" |
> {BooleanType} "boolean"
> | {IntegerType} "int"
> | {ClassifierType} name=Identifier;
>
> I would like to group fixed types together, using an enumeration, e.g.:
>
> enum FixedTypeKind :
> INTEGER | INTEGER_ARRAY | BOOLEAN ;
>
> doing something like this
>
> Type returns AbstactType :
> {FixedType.value=FixedTypeKind.INTEGER_ARRAY} "int" "["
> "]" | {FixedType.value=FixedTypeKind.BOOLEAN} "boolean"
> | {FixedType.value=FixedTypeKind.INTEGER} "int"
> | {ClassifierType} name=Identifier;
>
> Is this possible?
>
> [1] www.cambridge.org/us/features/052182060X/grammar.html
Re: Static Enumeration Literal Assignment [message #897403 is a reply to message #897392] Tue, 24 July 2012 01:55 Go to previous messageGo to next message
Stefan Kuhn is currently offline Stefan KuhnFriend
Messages: 8
Registered: July 2009
Junior Member
Why? It appears to be cleaner to use an enumeration literal instead of a new metaclass for the 'build in types'. It seems to me that my metamodel is unnecessarily cluttered.

Henrik Lindberg wrote on Mon, 23 July 2012 20:39
Why would you want something like that?
Why not simply:

Type returns AbstractType
: FixedType
| ClassifierType
;

FixedType returns AbstractType
: IntegerType
| IntegerArrayType
| BooleanType
;

IntegerType : {IntegerType} "int" ;
.... etc


yes, that's afaik the verbose form of
Type returns AbstactType	: 	
		{IntegerArrayType} 	"int"  "["  "]" 
	| 	{BooleanType} 		"boolean"
	| 	{IntegerType} 		"int"
	| 	{ClassifierType} 	name=Identifier;


Henrik Lindberg wrote on Mon, 23 July 2012 20:39

Do you want to be able to reference types (including the fixed ones)? How do you want
code completion to work? (It is different for data values such as enums and for keywords.

I want to reference Classes, but not build in types, yes. This seems to be pretty well documented, so don't bother. I would want code completion to work like before. Actually if I would have assigned 'boolean' 'int' to enum literals this would make a difference, but I just want to statically assign a property of FixedType to an enum literal.

Henrik Lindberg wrote on Mon, 23 July 2012 20:39

why not reuse Xbase or Xtend to benefit from the java type system support? (Will save you lots of work (and trouble)).

There are actually just these 4 types in mini java. It seems overkill to me, to introduce Xbase for this. First, I do want to keep things simple and don't want to worry about excluding other types etc.

Thanks for your fast reply.
Re: Static Enumeration Literal Assignment [message #897430 is a reply to message #897403] Tue, 24 July 2012 06:35 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 2936
Registered: July 2009
Senior Member
Hi Stefan,

it seems like you are confusing the concrete syntax, abstract syntax and
type system of your language. One would usually use something like

TypeReference : type=[Type] ({ArrayType.component=current} '[' ']')?;

Otherwise it is not possible to create arrays of classifiers or
booleans. Does that make sense to you?

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 24.07.12 03:55, schrieb Stefan Kuhn:
> Why? It appears to be cleaner to use an enumeration literal instead of a
> new metaclass for the 'build in types'. It seems to me that my metamodel
> is unnecessarily cluttered.
>
> Henrik Lindberg wrote on Mon, 23 July 2012 20:39
>> Why would you want something like that?
>> Why not simply:
>>
>> Type returns AbstractType
>> : FixedType
>> | ClassifierType
>> ;
>>
>> FixedType returns AbstractType
>> : IntegerType
>> | IntegerArrayType
>> | BooleanType
>> ;
>>
>> IntegerType : {IntegerType} "int" ;
>> .... etc
>
>
> yes, that's afaik the verbose form of
> Type returns AbstactType :
> {IntegerArrayType} "int" "[" "]" |
> {BooleanType} "boolean"
> | {IntegerType} "int"
> | {ClassifierType} name=Identifier;
>
> Henrik Lindberg wrote on Mon, 23 July 2012 20:39
>> Do you want to be able to reference types (including the fixed ones)?
>> How do you want
>> code completion to work? (It is different for data values such as
>> enums and for keywords.
>
> I want to reference Classes, but not build in types, yes. This seems to
> be pretty well documented, so don't bother. I would want code completion
> to work like before. Actually if I would have assigned 'boolean' 'int'
> to enum literals this would make a difference, but I just want to
> statically assign a property of FixedType to an enum literal.
>
> Henrik Lindberg wrote on Mon, 23 July 2012 20:39
>> why not reuse Xbase or Xtend to benefit from the java type system
>> support? (Will save you lots of work (and trouble)).
>
> There are actually just these 4 types in mini java. It seems overkill to
> me, to introduce Xbase for this. First, I do want to keep things simple
> and don't want to worry about excluding other types etc.
> Thanks for your fast reply.
Re: Static Enumeration Literal Assignment [message #897513 is a reply to message #897430] Tue, 24 July 2012 10:46 Go to previous messageGo to next message
Stefan Kuhn is currently offline Stefan KuhnFriend
Messages: 8
Registered: July 2009
Junior Member
Sebastian Zarnekow wrote on Tue, 24 July 2012 02:35

it seems like you are confusing the concrete syntax, abstract syntax and
type system of your language. One would usually use something like

TypeReference : type=[Type] ({ArrayType.component=current} '[' ']')?;

Otherwise it is not possible to create arrays of classifiers or
booleans. Does that make sense to you?

Hi sebastian,
this makes perfekt sense to me. Mini Java doesn't support arrays of classifiers. Enumerating primitive types (like int, char, boolean, NOT Integer, ...) still seems valid to me, mini java just doesn't has type constructors.
Maybe it's less confusing, if the problem isn't described using types: I'm still basically looking for a switch on multiple tokens like

Color: 
	{BlackAndWhite} "black" "white" ";"
	{White}		"white" ";"
	{Black}		"black ";"


So now instead of creating a new metaclass for each color, I would like to have one metaclass with a value:ColorEnum attribute and a ColorEnum with 3 literals, BlackAndWhite, White and Black. e.g.

Color: 
	{Color.value=ColorKind.BlackAndWhite} 	"black" "white" ";"
	{Color.value=ColorKind.White}		"white" ";"
	{Color.value=ColorKind.Black}		"black ";"

enum ColorKind :
	BlackAndWhite  | Black   | White 


Note that this is already possible, *if* the numeration value just depends on one token, e.g.

enum ColorKind :
	BlackAndWhite = 'black-and-white' | Black = 'black | White = 'white'

Color: 
	value=ColorKind ";"


[Updated on: Tue, 24 July 2012 12:02]

Report message to a moderator

Re: Static Enumeration Literal Assignment [message #898108 is a reply to message #897513] Wed, 25 July 2012 19:46 Go to previous messageGo to next message
Stefan Kuhn is currently offline Stefan KuhnFriend
Messages: 8
Registered: July 2009
Junior Member
Is my question unclear, is this regarded as an exotic use case or is it simply not possible?
Re: Static Enumeration Literal Assignment [message #898182 is a reply to message #898108] Thu, 26 July 2012 00:36 Go to previous message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2500
Registered: July 2009
Senior Member
On 2012-25-07 21:46, Stefan Kuhn wrote:
> Is my question unclear, is this regarded as an exotic use case or is it
> simply not possible?

To use an enum the way you outlined, you could do something like:

1. Use a manually managed meta model instead of the generated one.
(Which you probably want anyway at some point in the lifecycle
of your language).

2. In the manually managed meta model, create the wanted enum using
symbolic names such as INTEGER - e.g. FixedTypeKind.INTEGER, etc.

3. In the grammar:
FixedType : kind = BuiltInType ;
BuiltInType returns FixedTypeKind
: "int"
| "int" "[" "]"
| "boolean"
;

4. Write a value converter that translates to/from string-form to your
FixedTypeKind java enum. Associate the value converter with the rule
BuiltInType.

(Or some variation on the above)

Since this is a bit of work, and perhaps not all that clear what is
going on when reading the grammar, I suggested a different approach with
a class per symbol instead of an enumerator value. It naturally takes up
a bit more memory, but that is perhaps not of importance unless you have
a huge number of instances. Effort in implementation is minimal.

Another approach would be to *link* to the type - this way, type of
something is then just a reference, and there is only one instance of
the actual type. You can then use a "library" of built in types. You
probably do not save much work compared to the enum solution since if
your references include "int" "[" "]" the name of the type is not a
simple identifier, so you would want some sort of conversion anyway. You
could do something like:

somewhere... : type = [FixedType | BuiltInType ]
BuiltInType : "int" ("[" "]")? | "boolean" ;

This is too simplistic due to hidden/visible WS and comments, etc. but
shows the idea - you link to a type using a String that is the name of
the type. (Naturally, elsewhere, instances of FixedType with matching
'name' feature must be visible in the given scope.

As I said earlier, what is best depends on what you want to do, and
subsequent features that you want to make available to the user while
taking as much advantage as possible of what comes for free with Xtext.

The standard approach is to use a "library" of built in types, a common
base class for type, and most often the ability to declare new types in
the language. Also standard approach is also to use ID/FQN as name of type.

I hope that helps answer your questions.

- henrik
Previous Topic:JVMModelInferrer - suppressing java code generation
Next Topic:Access EObject from an outline view filter
Goto Forum:
  


Current Time: Fri Nov 21 12:36:28 GMT 2014

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

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