Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Define grammar in Xtext for optional array dimensions
Define grammar in Xtext for optional array dimensions [message #1786413] Thu, 03 May 2018 03:27 Go to next message
Ayman Elkfrawy is currently offline Ayman ElkfrawyFriend
Messages: 2
Registered: April 2018
Junior Member
I'm trying to define grammar in xtext for arrays where dimensions can be empty like int[][] or int[5][10]

My grammar looks like:
ArrayType: 
     [BasicType] ('['(dimension+=Expression)?']')+;


The problem with that rule when I use int[][] is there is no way from the model to know how many [] included because dimension list would be empty.

So I wonder if there is a way in xtext to default value like 0 in case Expression wasn't found? Otherwise what is the best way to handle such situation.

Thanks in advance for your help.
Re: Define grammar in Xtext for optional array dimensions [message #1786512 is a reply to message #1786413] Fri, 04 May 2018 20:23 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
What about moving these to a subobject

ArrayThingy: component=[Sometype] dimensions += Dimension+;

Dimension: {Dimension}'[' exp=Expression? ']'^


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Define grammar in Xtext for optional array dimensions [message #1786530 is a reply to message #1786512] Sun, 06 May 2018 00:41 Go to previous messageGo to next message
Ayman Elkfrawy is currently offline Ayman ElkfrawyFriend
Messages: 2
Registered: April 2018
Junior Member
Thanks Christian,

I thought about this, but I didn't want to change my metamodel. I hoped to find some other solution, but it seems I will need to go with that one.
Re: Define grammar in Xtext for optional array dimensions [message #1786531 is a reply to message #1786530] Sun, 06 May 2018 06:46 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14661
Registered: July 2009
Senior Member
You can always use node model but that sucks usability wise in that usecase

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Define grammar in Xtext for optional array dimensions [message #1786533 is a reply to message #1786531] Sun, 06 May 2018 07:45 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Changing the metamodel can be a pain, but eventually not changing the metamodel can be a greater pain. Since "[]" is significant, you might care to step back a bit and realize that the indequate distinction between "[]" and "" is actually a metamodel bug that Xtext has identified for you.

Regards

Ed Willink
Previous Topic:XBASE content assist inconsistent behavior
Next Topic:How to change lexer to copy tokens from other files
Goto Forum:
  


Current Time: Thu Mar 28 09:12:52 GMT 2024

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

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

Back to the top