Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » How to get xtext comment information?
How to get xtext comment information? [message #1839353] Fri, 19 March 2021 13:24 Go to next message
wings cu is currently offline wings cuFriend
Messages: 14
Registered: January 2021
Junior Member
I want to preserve the comments in my dsl and change the comments like python's grammar.
such as:
In my dsl:
// comments
/* comments */ A = B /* comments */; 

generated python code:
// comments
''' comments ''' A = B /* comments */

If I define a new comment rule like
Comment: /* (name+=Any_Other)* */
, I cannot figure out a way to put the rule in my grammar. ( I have to restrict the place where comment can exist! )
Is there a simple way to achieve this?
Re: How to get xtext comment information? [message #1839354 is a reply to message #1839353] Fri, 19 March 2021 13:26 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you need to access the node model and traverce it. NodeModelUtils might be a starting point.

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get xtext comment information? [message #1839366 is a reply to message #1839354] Fri, 19 March 2021 14:19 Go to previous messageGo to next message
wings cu is currently offline wings cuFriend
Messages: 14
Registered: January 2021
Junior Member
I have looked through some information in the node by debug mode, but I don't know how to get the comment in one node. The function getTokenText will still remove hidden tokens such as whitespace/comments. Is there any way to change the default hidden action for comments?
Re: How to get xtext comment information? [message #1839368 is a reply to message #1839366] Fri, 19 March 2021 14:25 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
you can call INode.getText and INode.getPrevious/getNextSibling
you may let you inspire from MultiLineCommentDocumentationProvider


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get xtext comment information? [message #1839379 is a reply to message #1839368] Fri, 19 March 2021 15:52 Go to previous messageGo to next message
wings cu is currently offline wings cuFriend
Messages: 14
Registered: January 2021
Junior Member
Thank you ! Now I can get text of the nodes. But when it comes to preserve the comments, I wonder where should I use these nodes and comments.
For example, in my xtend file I define compile functions to translate my rules to python. In these functions I can get nodes and then get the comments. But I still need to know the location of comments. Otherwise it cannot handle code such as
/**/ A /**/ = B;

My compile function is like this:
private def compile(AbstractElement e) {
    	var ret = new String;
    	/*Where should I get and add the comments to the return string?*/
    	if(...) {
    		ret += '''«e.a.compile»'''
    	}
    	else
    		ret += '''«e.b.compile»'''
        return ret;
   }
Re: How to get xtext comment information? [message #1839385 is a reply to message #1839379] Fri, 19 March 2021 16:28 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
use the node of your objects or features of object as starting point (see nodeModelUtils)

Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get xtext comment information? [message #1839392 is a reply to message #1839385] Fri, 19 March 2021 18:39 Go to previous messageGo to next message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

You might find that https://git.eclipse.org/r/plugins/gitiles/ocl/org.eclipse.ocl/+/refs/heads/master/examples/org.eclipse.ocl.examples.xtext.idioms/src/org/eclipse/ocl/examples/xtext/idioms/serializer/IdiomsCommentSegmentSupport.java gives you some ideas.

Regards

Ed Willink
Re: How to get xtext comment information? [message #1839405 is a reply to message #1839385] Sat, 20 March 2021 06:08 Go to previous messageGo to next message
wings cu is currently offline wings cuFriend
Messages: 14
Registered: January 2021
Junior Member
Do you mean I have to generate comments like this:
private def compile(AbstractElement e) {
    	var ret = new String;
    	if(findcomment()) { ret +=  commentStirng; }
    	ret += '''«e.a.compile»'''
        if(findcomment()) { ret +=  commentStirng; }
    	ret += '''«e.b.compile»'''
        if(findcomment()) { ret +=  commentStirng; }
        return ret;
   }
Re: How to get xtext comment information? [message #1839406 is a reply to message #1839405] Sat, 20 March 2021 06:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Besides the very inconvenient use of
Xtend Rich strings more or less yes


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: How to get xtext comment information? [message #1839414 is a reply to message #1839406] Sat, 20 March 2021 13:20 Go to previous message
Ed Willink is currently offline Ed WillinkFriend
Messages: 7655
Registered: July 2009
Senior Member
Hi

Note that the Xtext philosophy is that comments are transient, i.e. they are converted from source text to the node model but are not incorporated in the final AS model, so if you save the AS model and reload and serialize back to text the comments are lost.

If you care enough about comments to extract them from the node model, you should perhaps make them first class rather than hidden components of your grammar. This will avoid all need to recover comments from the node model but will incur a bot of a fight to support comments everywhere, which isn't always that sensible, so supporting only sensible comments may be adequate.

In Java, who really wants to be able to type

import/*this is an import*/ java// This is the system package
./*separated from*/lang./*and now the real class*/String;

(Both Xtext and Java get this perversion right!).

Regards

Ed Willink
Previous Topic:Xtext 2.14 - programmatic EMF model manipulation with minimal re-indexing
Next Topic:Not valid for cross reference
Goto Forum:
  


Current Time: Fri Apr 19 05:18:57 GMT 2024

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

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

Back to the top