Skip to main content



      Home
Home » Modeling » TMF (Xtext) » [SOLVED] Grammar Warning The rule ... may be consumed without object instantiation.
[SOLVED] Grammar Warning The rule ... may be consumed without object instantiation. [message #846089] Sun, 15 April 2012 15:00 Go to next message
Eclipse UserFriend
Hey Guys!

I'm reading the Xtext documentation and was playing around with some examples. I found a nice one that uses unordered groups to explain the modifiers of the Java language:

enum Visibility:
	PUBLIC='public' | PRIVATE='private' | PROTECTED='protected';

Modifier:
	(static?='static')? & (final?='final')? & visibility=Visibility;


Since the Java language also provides the none (or package) modifier, I extended the example (wrapped the visibility assignment with optional cardinality):

enum Visibility:
	PUBLIC='public' | PRIVATE='private' | PROTECTED='protected';

Modifier:
	(static?='static')? & (final?='final')? & (visibility=Visibility)?;


Now, I'm getting a warning: The rule 'Modifier' may be consumed without object instantiation. Add an action to ensure object creation, e.g. '{Modifier}'.

I found some similar issues on this website and understand the reason for the warning, but I wasn't able to get rid of it.

I don't know if my approach is straightforward. Thank you Smile

Kon

[Updated on: Sun, 15 April 2012 16:45] by Moderator

Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #846104 is a reply to message #846089] Sun, 15 April 2012 15:22 Go to previous messageGo to next message
Eclipse UserFriend
Modifier:
{Modifier} ((static?='static')? & (final?='final')? & (visibility=Visibility)?);
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #846162 is a reply to message #846104] Sun, 15 April 2012 16:44 Go to previous messageGo to next message
Eclipse UserFriend
Thank you Christian! That example worked.
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #852807 is a reply to message #846162] Sun, 22 April 2012 06:37 Go to previous messageGo to next message
Eclipse UserFriend
I just noticed that even with the cardinality set to ? for Visibility, still one instance is created at runtime. Since an enum is required to have one of its declared values, it is set to public (apparently the first one). The following input is correct for my language:

final static int SPEEDLIMIT = 30;

I actually thought, that my Modifier instance is going to look as followed (static and final are primitive values, so they always going to have a default value):

static=true
final=true
visibility=NULL

I'm using NULL value as indicator for package modifier. Am I doing something wrong? Sad

I tried the following and it works as expected:

Test:
'public' | 'private' | 'protected'
;

ModifierField:
{ModifierField}((static?='static')? & (final?='final')? & (visibility=Test)?);


Any suggestions Smile ?

Thank you!
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #852817 is a reply to message #852807] Sun, 22 April 2012 06:52 Go to previous messageGo to next message
Eclipse UserFriend
Hi,

the only Possibility i know is to switch to a manually maintained metamodel

~Christian
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #852827 is a reply to message #852817] Sun, 22 April 2012 07:06 Go to previous messageGo to next message
Eclipse UserFriend
Thank you Christian! Than I guess, I'll stick with my "Test" solution Wink

By the way, is this behaviour actually intended?

[Updated on: Sun, 22 April 2012 07:09] by Moderator

Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #852834 is a reply to message #852827] Sun, 22 April 2012 07:14 Go to previous messageGo to next message
Eclipse UserFriend
i guess yes. it is explained in the docs
http://www.eclipse.org/Xtext/documentation/2_1_0/020-grammar-language.php#syntax_8

Quote:
Please note, that Ecore does not support unset values for enums.

[Updated on: Sun, 22 April 2012 07:16] by Moderator

Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #853715 is a reply to message #852834] Mon, 23 April 2012 05:11 Go to previous messageGo to next message
Eclipse UserFriend
Seems like the simplest solution is to have something like this, no?

enum Visibility:
	DEFAULT_VIS='__packetvisibilitydefault' | PUBLIC='public' | PRIVATE='private' | PROTECTED='protected';

Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #854133 is a reply to message #853715] Mon, 23 April 2012 13:45 Go to previous messageGo to next message
Eclipse UserFriend
yes
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #856485 is a reply to message #854133] Wed, 25 April 2012 14:02 Go to previous messageGo to next message
Eclipse UserFriend
Apparently, this adds an additional keyword that can be used by the user e.g.

final static __packetvisibilitydefault int SPEEDLIMIT = 30;
final static int SPEEDLIMIT = 30;


Both expressions are valid and have the same meaning. Since code competition suggests __packetvisibilitydefault as possible keyword, I'll keep using:

Visibility:
'public' | 'private' | 'protected'
;


Thanks!
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #856559 is a reply to message #856485] Wed, 25 April 2012 15:38 Go to previous messageGo to next message
Eclipse UserFriend
hi as said before. Maintain the meta model manually. Alternatively
overwrite completeKeyword. In the proposal provider and suppress the
super call if the keyword is the __xxxx one
Re: [SOLVED] Grammar Warning The rule ... may be consumed without object instantiation. [message #857047 is a reply to message #846089] Thu, 26 April 2012 03:44 Go to previous message
Eclipse UserFriend
Hey Christian,

thanks for the information, I didn't think that far - I'm new to Xtext Smile

Kon
Previous Topic:XText grammar for ecore model generated from XSD with choice elements
Next Topic:General purpose language possible
Goto Forum:
  


Current Time: Fri Jul 04 12:31:46 EDT 2025

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

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

Back to the top