Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Textblock within ParserRule
Textblock within ParserRule [message #1479977] Wed, 19 November 2014 22:18 Go to next message
Andreas Studer is currently offline Andreas StuderFriend
Messages: 5
Registered: July 2009
Junior Member
Hello

I have the problem that I would like to get a language like this:
legend center
   This is the text
   of a legend.
endlegend


I would like to have a ParserRule like this:

Legend:
	'legend' (align=AlignEnum)?
		content=MULTILINE
	'endlegend'
;

enum AlignEnum:
	LEFT		= 'left' |
	RIGHT		= 'right' |
	CENTER		= 'center'
;


I didn't found any way to get something useful for the MULTILINE rule. Everything results in some sort of troubles (e.g. all Terminal Rules I've tried would overrule other rules according to the generator warnings, and some even result in exception stacktraces).
I also can't use the STRING terminal rule since this would mean I have to use ' or " (the target language is simply not like this and from the perspective of the language, it wouldn't make sense anyway.)
I think some sort of Data Type Rule for MULTILINE would be elegant, because then it would be part of the parsing context of the "Legend" parser rule. But I didn't found a way to build one.

Has anybody an idea how a solution could look like?
Re: Textblock within ParserRule [message #1480438 is a reply to message #1479977] Thu, 20 November 2014 07:16 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi,

you can workaround the keyword problem by introducing a so called Datatype rule e.g.

MULTILINE: (ID | INT | 'keyword1' | 'keyword2')+


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Textblock within ParserRule [message #1481147 is a reply to message #1480438] Thu, 20 November 2014 20:08 Go to previous messageGo to next message
Andreas Studer is currently offline Andreas StuderFriend
Messages: 5
Registered: July 2009
Junior Member
Hello Christian

Thanks, that helped me (at least partially).

Since you've called it "workaround" I assume it's a real problem to get a freetext string between two keywords. I think it would be helpful to put that into a FAQ.

The other problem I have is a single line. Something like
title this is a title.

My solution here is simply:
terminal TITLE_TERMINAL:
	'title' !('\n'|'\r')* ('\r'? '\n')?;

However, that's also not a neat solution too because "title" should be a keyword and not part of a terminal rule. But anyhow...
I guess there is not a Datatype rule workaround that would help here.
Re: Textblock within ParserRule [message #1481160 is a reply to message #1481147] Thu, 20 November 2014 20:22 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
hi,

keep in mind that xtext uses a lexer and that terminals are lexed.
have a look at the grammar feature 'Hidden Terminal Symbols' (see docs)
if you split up whitespace rule into ws and newline you could get that running


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Textblock within ParserRule [message #1486192 is a reply to message #1481160] Mon, 24 November 2014 22:21 Go to previous messageGo to next message
Andreas Studer is currently offline Andreas StuderFriend
Messages: 5
Registered: July 2009
Junior Member
Hello Christian

I'm sorry to say, I've tried to do something with the hidden terminal symbols to get a single line string 'till the EOL after a term, but it seems as I didn't got the idea.
If I declare something like this it doesn't work:
grammar x.y.z with org.eclipse.xtext.common.Terminals
	hidden (WS, NEWLINE, SL_COMMENT, ML_COMMENT)
(...)
SingleTitle:
	'title' title=SINGLELINE;

SINGLELINE hidden (WS, SL_COMMENT, ML_COMMENT):
	(ID|INT|ANY_OTHER)+;

terminal WS:
	(' '|'\t')+;

terminal NEWLINE:
	('\r'|'\n')+;

Could you provide me an example that works?
Re: Textblock within ParserRule [message #1486629 is a reply to message #1486192] Tue, 25 November 2014 07:06 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Please share a complete sample grammar + test model

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

[Updated on: Tue, 25 November 2014 07:32]

Report message to a moderator

Re: Textblock within ParserRule [message #1486759 is a reply to message #1486629] Tue, 25 November 2014 09:11 Go to previous message
Uli Merkel is currently offline Uli MerkelFriend
Messages: 250
Registered: June 2013
Senior Member
Hi Andreas,

I do not see some "ANY_OTHER" specification.

And in your grammar, you have the hidden clause twice,
AFAIK you have to modify the hidden clause of the with ...
Previous Topic:navigating my dsl model by using XExpression
Next Topic:XTEXT How to add data to grammar
Goto Forum:
  


Current Time: Thu Apr 18 09:45:25 GMT 2024

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

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

Back to the top