Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » C# like #region Preprocessor(Get token till end of the line as text.)
icon5.gif  C# like #region Preprocessor [message #695257] Mon, 11 July 2011 11:43 Go to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Hi developers.

I want to create some kind of the C# #region preprocessor. It simply allows to group sections of code:

C#

class MyClass {
    #region MyClass Contents
    ...
    #region Properties 
    public int A {get;set;}
    public int B {get;set;}
    #endregion End of Properties
    ...
    #endregion end of myclass contents
}


Is like a group of 2 single line comments (start/end).

The problem is: I do not know how to define any non-terminal rule which stores captures all text till end of the line. I tried to create terminal rules like the single-line comment but this will throw up some problems: #region and #endregion won't get recognized as keywords and I can't store the description texts seperate.

I would like something like this:
Group:
    {Group}
    '#region' (startName = TEXT_TILL_EOL)?
        declarations += Declaration* 
    '#endregion' (endName = TEXT_TILL_EOL)?
;  


Any terminal defined like "TEXT_TILL_EOL" will get detected everywhere. I only want to allow this terminal at this place.

Best Regards
Daniel
Re: C# like #region Preprocessor [message #695268 is a reply to message #695257] Mon, 11 July 2011 12:05 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,

you could use a datatype rule instead, one that allows everything other than new line. However, you'd have to split the WS rule into two (regular white spaces and new lines)

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: C# like #region Preprocessor [message #695307 is a reply to message #695268] Mon, 11 July 2011 13:26 Go to previous messageGo to next message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Hi.

How can I define "everything other than new line" within a rule? Isn't he '!' operator only allowed within terminals? If I would create such a terminal it would make the parser match every single line as such a terminal ignoring all defined rules. Can you please give me a small example?

Re: C# like #region Preprocessor [message #695314 is a reply to message #695307] Mon, 11 July 2011 13:31 Go to previous messageGo to next message
Alexander Nittka is currently offline Alexander NittkaFriend
Messages: 1193
Registered: July 2009
Senior Member
Hi,
as you said, negation is only allowed in terminal rules. I was proposing a datatype rule

FreeText hidden(): ID|WS_WITHOUT_EOL|INT|STRING|'keyword1'|'keyword2'...;

Alex


Need training, onsite consulting or any other kind of help for Xtext?
Go visit http://xtext.itemis.com or send a mail to xtext@itemis.de
Re: C# like #region Preprocessor [message #695336 is a reply to message #695314] Mon, 11 July 2011 14:19 Go to previous message
Daniel Missing name is currently offline Daniel Missing nameFriend
Messages: 101
Registered: July 2011
Senior Member
Thanks. I nearly got it working. Is it really neccessary to include all keywords in this list? This is quite hard for mainentance. It's also a little bit annoying that all keywords within the "region comment" section get highlighted.

I also thought of an other solution:
I probably will need to specify this #region section as a single line comment and highlight #region and #endregion manually as keywords. Something like this:

terminal REGION_START: '#region' !('\n'|'\r')* ('\r'? '\n')?; 
terminal REGION_START: '#endregion' !('\n'|'\r')* ('\r'? '\n')?; 


Is there probably a trick which allows me to define '#region' and '#endregion' as keywords within the grammar?
Previous Topic:Using element define in one grammar into another
Next Topic:Content assistant for Datatypes
Goto Forum:
  


Current Time: Thu Apr 25 15:06:17 GMT 2024

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

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

Back to the top