Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Get token list for an EObject
Get token list for an EObject [message #795263] Fri, 10 February 2012 09:06 Go to next message
John J. Camilleri is currently offline John J. CamilleriFriend
Messages: 33
Registered: November 2011
Location: Göteborg
Member
Simple question but can't find an answer for it..

If I have an EObject, how do I get a String representation of all the source tokens that it covers?
Re: Get token list for an EObject [message #795274 is a reply to message #795263] Fri, 10 February 2012 09:21 Go to previous messageGo to next message
John J. Camilleri is currently offline John J. CamilleriFriend
Messages: 33
Registered: November 2011
Location: Göteborg
Member
Ok, I have found this way of doing it which seems to work:
@Inject
protected Serializer serializer;
	
protected String getTokens(EObject eobj) {
	return serializer.serialize(eobj);
}

However I am warned by Eclipse that accessing the serializer is discouraged. Is there a better way to do this?

[Updated on: Fri, 10 February 2012 09:21]

Report message to a moderator

Re: Get token list for an EObject [message #795391 is a reply to message #795263] Fri, 10 February 2012 12:23 Go to previous messageGo to next message
Lorenzo Bettini is currently offline Lorenzo BettiniFriend
Messages: 1762
Registered: July 2009
Location: Firenze, Italy
Senior Member
On 02/10/2012 10:06 AM, John J. Camilleri wrote:
> Simple question but can't find an answer for it..
>
> If I have an EObject, how do I get a String representation of all the
> source tokens that it covers?

public String getProgramText(EObject object) {
final ICompositeNode node = NodeModelUtils.getNode(object);
if (node == null)
return null;
return NodeModelUtils.getTokenText(node);
}

hope this helps
Lorenzo

--
Lorenzo Bettini, PhD in Computer Science, DI, Univ. Torino
ICQ# lbetto, 16080134 (GNU/Linux User # 158233)
HOME: http://www.lorenzobettini.it MUSIC: http://www.purplesucker.com
http://www.myspace.com/supertrouperabba
BLOGS: http://tronprog.blogspot.com http://longlivemusic.blogspot.com
http://www.gnu.org/software/src-highlite
http://www.gnu.org/software/gengetopt
http://www.gnu.org/software/gengen http://doublecpp.sourceforge.net


Re: Get token list for an EObject [message #795400 is a reply to message #795263] Fri, 10 February 2012 12:39 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
NodeModelUtils.getNode(eObj).getText()

and beweare of nulls...

- henrik

On 2012-10-02 10:06, John J. Camilleri wrote:
> Simple question but can't find an answer for it..
>
> If I have an EObject, how do I get a String representation of all the
> source tokens that it covers?
Re: Get token list for an EObject [message #795463 is a reply to message #795391] Fri, 10 February 2012 13:48 Go to previous messageGo to next message
Sebastian Zarnekow is currently offline Sebastian ZarnekowFriend
Messages: 3107
Registered: July 2009
Senior Member
Lorenzo, Henrik, et.al.

as the java doc suggests, you should have a look at
NodeModelUtils#findActualNodeFor(EObject) which is almost always the one
that you want to use.

Regards,
Sebastian
--
Need professional support for Eclipse Modeling?
Go visit: http://xtext.itemis.com

Am 10.02.12 13:23, schrieb Lorenzo Bettini:
> On 02/10/2012 10:06 AM, John J. Camilleri wrote:
>> Simple question but can't find an answer for it..
>>
>> If I have an EObject, how do I get a String representation of all the
>> source tokens that it covers?
>
> public String getProgramText(EObject object) {
> final ICompositeNode node = NodeModelUtils.getNode(object);
> if (node == null)
> return null;
> return NodeModelUtils.getTokenText(node);
> }
>
> hope this helps
> Lorenzo
>
Re: Get token list for an EObject [message #795495 is a reply to message #795463] Fri, 10 February 2012 14:35 Go to previous message
John J. Camilleri is currently offline John J. CamilleriFriend
Messages: 33
Registered: November 2011
Location: Göteborg
Member
Fantastic, thank you guys.
Here's the final & formatted version of the code for anyone else who may search for this:

final ICompositeNode node = NodeModelUtils.findActualNodeFor(eobj);		
if (node != null) {
    return NodeModelUtils.getTokenText(node);
}
Previous Topic:[Xtext 2.2] Using syntactic predicates within multi-cardinality constructs
Next Topic:How to modify Xtext resources programmatically
Goto Forum:
  


Current Time: Sun Mar 29 18:58:56 GMT 2020

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

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

Back to the top