Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    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 19:00 Go to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
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 20:45]

Report message to a moderator

Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #846104 is a reply to message #846089] Sun, 15 April 2012 19:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Modifier:
{Modifier} ((static?='static')? & (final?='final')? & (visibility=Visibility)?);


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #846162 is a reply to message #846104] Sun, 15 April 2012 20:44 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
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 10:37 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
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 10:52 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

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

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #852827 is a reply to message #852817] Sun, 22 April 2012 11:06 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
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 11:09]

Report message to a moderator

Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #852834 is a reply to message #852827] Sun, 22 April 2012 11:14 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de

[Updated on: Sun, 22 April 2012 11:16]

Report message to a moderator

Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #853715 is a reply to message #852834] Mon, 23 April 2012 09:11 Go to previous messageGo to next message
Mikael Karpberg is currently offline Mikael KarpbergFriend
Messages: 8
Registered: April 2012
Location: Sweden
Junior Member
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 17:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
yes

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Grammar Warning The rule ... may be consumed without object instantiation. [message #856485 is a reply to message #854133] Wed, 25 April 2012 18:02 Go to previous messageGo to next message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
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 19:38 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: [SOLVED] Grammar Warning The rule ... may be consumed without object instantiation. [message #857047 is a reply to message #846089] Thu, 26 April 2012 07:44 Go to previous message
kon f is currently offline kon fFriend
Messages: 152
Registered: March 2012
Senior Member
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: Tue Apr 23 11:49:53 GMT 2024

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

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

Back to the top