Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » The trick doesn't work for a rule that may be consumed without object instantiation
The trick doesn't work for a rule that may be consumed without object instantiation [message #1787156] Sun, 20 May 2018 10:39 Go to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Hello,

So I came across what seems to be a common issue as searching yields lots of questions on it, the message:

The rule 'BlockType' may be consumed without object instantiation. Add an action to ensure object creation, e.g. '{BlockType}'.


Basically, all answers just say "add {rulename}" and the reply is "oh thanks, it works" but in my case it doesn't. See self-contained grammar for my case.

grammar org.thisAndThat.dsl.SbeDsl with org.eclipse.xtext.common.Terminals

generate thatThatDsl "http://www.thisAndThat.org/dsl/thatThatDsl"

Specification:
    As = TheAs Bs=TheBs;

TheAs:
    'theAs' '{' (theAs+=A)+ '}';

TheBs:
    'theBs' '{'(theBs+=B)+ '}';


A:
    'a' name=Name;

B:
    'b' name=Name '{'
    block=BlockType
    '}';

BlockType:  {BlockType}
    (thisFields+=This)* | (thatFields+=That)*;

This:
    'this' name=Name ':' type=[A];

That:
    'that' name=Name ':' type=[A];

Name:
    ID;


I would appreciate if you would explain what exactly is happening under the hood as it seems like nobody really goes into the subject... From what I gather, each rule creates an object of some (generated) type under the hood, so the fact that there are two optional (*) groups (thisFields and thatFields) means that "it may be consumed without object instantiation". Indeed, forcing "thatFields" makes the warning go away:

BlockType: {BlockType}
    (thisFields+=This)* | (thatFields+=That)+;


However, forcing "thisFields" does not have the same effect, as the below makes the warning persist:

BlockType:  {BlockType}
    (thisFields+=This)+ | (thatFields+=That)*;


In reality however, I don't want my grammar to require the presence of "that": it should be possible to have only "this" or only "that" or both.

1) What is the implication of this warning if I leave BlockType as-is?

2) If I wanted to only exclude the ABSENCE of both, I would need to do that outside the grammar with custom validation, correct?
Re: The trick doesn't work for a rule that may be consumed without object instantiation [message #1787157 is a reply to message #1787156] Sun, 20 May 2018 10:45 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
the easiest would be to move the '{' '}' to the BlockType.
the forced instatiation only works if there is something to be consumed at all.


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: The trick doesn't work for a rule that may be consumed without object instantiation [message #1787158 is a reply to message #1787157] Sun, 20 May 2018 14:09 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
Maybe I've misunderstood, but this still issues the warning:

B:
    'b' name=Name 
    block=BlockType
    ;

BlockType: {BlockType} 
   '{'  (thisFields+=This)* | (thatFields+=That)* '}';


What I've found to work is:

B:
    'b' name=Name '{'
    block=BlockType
    '}';

BlockType:  
   {BlockType} (thisFields+=This)* |{BlockType} (thatFields+=That)* ;


Still don't understand what's happening though...

EDIT: antlr chokes on this though, so still no joy...

[Updated on: Sun, 20 May 2018 14:20]

Report message to a moderator

Re: The trick doesn't work for a rule that may be consumed without object instantiation [message #1787159 is a reply to message #1787158] Sun, 20 May 2018 14:19 Go to previous messageGo to next message
Alex Mising name is currently offline Alex Mising nameFriend
Messages: 149
Registered: March 2010
Senior Member
This seems to achieve what I want (moving optionality)

BlockType: {BlockType}
    thisList =ThisList?
    thatList = ThatList?;

ThisList:
    (thisFields+=This)+
;

ThatList:
    (thatFields+=That)+
;
Re: The trick doesn't work for a rule that may be consumed without object instantiation [message #1787160 is a reply to message #1787159] Sun, 20 May 2018 15:40 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Precedence

BlockType: {BlockType}
'{' ((thisFields+=This)* | (thatFields+=That)*) '}';


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:Unsuccessful build using Maven due missing EMF classes
Next Topic:Generic Map get does not return the right object?
Goto Forum:
  


Current Time: Thu Apr 25 00:49:40 GMT 2024

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

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

Back to the top