Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to format nested loops ?
How to format nested loops ? [message #1800924] Wed, 09 January 2019 20:39 Go to next message
Charalampos Alexopoulos is currently offline Charalampos AlexopoulosFriend
Messages: 11
Registered: July 2012
Junior Member
Hi all

I am trying to format nested loops but i fail all the time, given my lack of experience it is not a surprise. Can someone help me?
Thank you in advance

Here is my simplified grammar
Package:
	Statements;
	
Statements:
	 (statements+=Statement)+
;

Statement:
	ForStatement
	;
	
	ForStatement:
	for="for("")" block=Child
	;
	
	Child:
	{Children} (";"
	| "{" (children+=Child)* "}"
	| it=Statement)
	;


and here is the Formatter code
	def dispatch void format(Statements statements, extension IFormattableDocument document) {
		// TODO: format HiddenRegions around keywords, attributes, cross references, etc. 
		for (statement : statements.statements) {
			statement.format
		}
	}

	def dispatch void format(ForStatement forStatement, extension IFormattableDocument document) {
		// TODO: format HiddenRegions around keywords, attributes, cross references, etc. 
		forStatement.block.format
	}
	
	def dispatch void format(Children children, extension IFormattableDocument document) {
		// TODO: format HiddenRegions around keywords, attributes, cross references, etc. 
		val open = children.regionFor.keyword("{")
		val close = children.regionFor.keyword("}")
		open.append[newLine]
		open.prepend[newLine]
		close.append[newLine]
		close.prepend[newLine]
		interior(open, close, [indent])


This is what i expect
for()
{
	for()
	{
		for()
		{
			
		}
	}
}


and this is what i get
for()
{
	for(){for(){}}
}
Re: How to format nested loops ? [message #1800925 is a reply to message #1800924] Wed, 09 January 2019 20:59 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13071
Registered: July 2009
Senior Member
the simply straight forward approach does not work?

	def dispatch void format(Children children, extension IFormattableDocument document) {
		// TODO: format HiddenRegions around keywords, attributes, cross references, etc. 
		val open = children.regionFor.keyword("{")
		val close = children.regionFor.keyword("}")
		open.append[newLine]
		open.prepend[newLine]
		close.append[newLine]
		close.prepend[newLine]
		interior(open, close, [indent])
		// you dont format the content, no wonder it does not work
		for (subchild : children.children) {
			subchild.format
		}
		// nor the it
		children.it?.format
	}


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to format nested loops ? [message #1800926 is a reply to message #1800925] Wed, 09 January 2019 21:20 Go to previous messageGo to next message
Charalampos Alexopoulos is currently offline Charalampos AlexopoulosFriend
Messages: 11
Registered: July 2012
Junior Member
Yes it works fine. Thank you very much

During my three days of struggling i have tried this among million of other things
for (subchild : children.children) {
			subchild.format
		}


without the
children.it?.format
which make the difference. Can you give me some info what is this it? ?

Thank you again
Re: How to format nested loops ? [message #1800928 is a reply to message #1800926] Wed, 09 January 2019 21:36 Go to previous message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13071
Registered: July 2009
Senior Member
Its your strange grammar

Child:
{Children} (";"
| "{" (children+=Child)* "}"
| it=Statement)
;


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Previous Topic:is it possible to override TaskMarkerContributor?
Next Topic:How to represent triple double quote string
Goto Forum:
  


Current Time: Sun Aug 25 12:20:27 GMT 2019

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

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

Back to the top