Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » get source position of tokens
get source position of tokens [message #793167] Tue, 07 February 2012 21:05 Go to next message
Bodo Koch is currently offline Bodo KochFriend
Messages: 8
Registered: February 2012
Junior Member
How one can get the source position (line, column) of a token?

For my runtime environment, i wish to inform the user about the source location in case of failure.

Thanks!

Bodo

[Updated on: Wed, 08 February 2012 05:26]

Report message to a moderator

Re: get source postion of tokens [message #793183 is a reply to message #793167] Tue, 07 February 2012 21:27 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13425
Registered: July 2009
Senior Member
Hi,

can you give use more context please? from which place do you want to do this? can you give us an example?

~Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: get source postion of tokens [message #793332 is a reply to message #793167] Wed, 08 February 2012 02:28 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
When you parse the source, you get a tree of INode. An INode knows about
the position in the text.

When there are parse errors, the reported issues carry information about
position.

When there are validation or linking errors, the reported issues carry
information about positions.

If these are not enough to help you find what you want, then more
information about what you are trying to do is required.

- henrik

On 2012-07-02 22:05, Bodo Koch wrote:
> How one can get the source position (line, column) of a token?
>
> For my runtime environment, i wich to inform the user about the source
> location in case of failure.
>
> Thanks!
>
> Bodo
Re: get source postion of tokens [message #793428 is a reply to message #793183] Wed, 08 February 2012 05:42 Go to previous messageGo to next message
Bodo Koch is currently offline Bodo KochFriend
Messages: 8
Registered: February 2012
Junior Member
Hi,

Let say a tiny scripting language can deal with some kind of objects. To refer to object members one can specify the grammar like:
ObjectRef: objId=ID ('.' objRef+=ID)*;

If the user writes sentences like:
a b

he/she will get a syntax error in the eclipse/xtext IDE.

If the user writes a syntactically correct sentence like
a.z

but z is not a member of object a one will try to catch that error by the validation phase and issue an error.

BUT if also the static semantic is correct like
a.b

but a or b is not instantiated one will get an null pointer exception during execution time of this script. In a normal eclipse/java IDE (if the program runs in this context) one will get an error message in the control window and can jump direct to the erroneous statement. I do not expect this comfort in exclipse/xtext because this compiled script will not run in that context, but I want to inform the user at what source code position this error occurs.

During the validation one can access the abstract syntax tree members with the methods or.getObjId() and or.getObjRef(), if or is of class ObjectRef. How one can get the source position info out of this elements?

Thanks, and hopefully this explanation helps

Bodo

[Updated on: Wed, 08 February 2012 05:46]

Report message to a moderator

Re: get source postion of tokens [message #793712 is a reply to message #793428] Wed, 08 February 2012 13:14 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
So, your evaluator simply needs to get the INode for the part of the
model that causes the problem.

If you are generating code, you must naturally ensure that there are
references in the generated code that allows you to map that back to the
source.

- henrik

On 2012-08-02 6:42, Bodo Koch wrote:
> Hi,
>
> Let say a tiny scripting language can deal with some kind of objects. To
> refer to object members one can specify the grammar like:
> objId=ID ('.' objRef+=ID)*
> If the user writes sentences like:
> a b
> he/she will get a syntax error in the eclipse/xtext IDE.
>
> If the user writes a syntactically correct sentence like
> a.z
> but z is not a member of object a one will try to catch that error by
> the validation phase and issue an error.
>
> BUT if also the static semantic is correct like
> a.b
> but a or b is not instantiated one will get an null pointer exception
> during execution time of this script. In a normal eclipse/java IDE (if
> the program runs in this context) one will get an error message in the
> control window and can jump direct to the erroneous statement. I do not
> expect this comfort in exclipse/xtext because this compiled script will
> not run in that context, but I want to inform the user at what source
> code position this error occurs.
>
> Thanks, and hopefully this explanation helps
>
> Bodo
>
Re: get source postion of tokens [message #794528 is a reply to message #793712] Thu, 09 February 2012 12:02 Go to previous messageGo to next message
Bodo Koch is currently offline Bodo KochFriend
Messages: 8
Registered: February 2012
Junior Member
Henrik Lindberg wrote on Wed, 08 February 2012 08:14
So, your evaluator simply needs to get the INode for the part of the
model that causes the problem.

If you are generating code, you must naturally ensure that there are
references in the generated code that allows you to map that back to the
source.

- henrik



Hm, this hint doesn't realy helped me. I tried the following code:
/* wie geil ist das denn? */	
public String getTokenPosition (EObject e)
	{
        CompositeNode cn  = NodeUtil.getNodeAdapter(e).getParserNode();
        return (cn.getLine() + "/" + cn.getOffset(
        		) + "/" + cn.getLength());	
	}


Can I do this with die INode stuff some how?

Thanks

Bodo
Re: get source postion of tokens [message #794629 is a reply to message #794528] Thu, 09 February 2012 14:20 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 13425
Registered: July 2009
Senior Member
Hi, we are talking about Xtext 2.x you are talking about Xtext <=
1.0.x regards Christian


Need professional support for Xtext, Xpand, EMF?
Go to: https://www.itemis.com/en/xtext/
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: get source postion of tokens [message #794778 is a reply to message #794629] Thu, 09 February 2012 17:45 Go to previous messageGo to next message
Bodo Koch is currently offline Bodo KochFriend
Messages: 8
Registered: February 2012
Junior Member
Christian Dietrich wrote on Thu, 09 February 2012 09:20
Hi, we are talking about Xtext 2.x you are talking about Xtext <=
1.0.x regards Christian


OK, and how do you do that in Xtext 2.1?

Regards

Bodo
Re: get source postion of tokens [message #794783 is a reply to message #794778] Thu, 09 February 2012 17:50 Go to previous messageGo to next message
Henrik Lindberg is currently offline Henrik LindbergFriend
Messages: 2509
Registered: July 2009
Senior Member
On 2012-09-02 18:45, Bodo Koch wrote:

> OK, and how do you do that in Xtext 2.1?
>
INode node = NodeModelUtils.getNode(eObj)

- henrik
icon14.gif  Re: get source postion of tokens [message #794800 is a reply to message #794783] Thu, 09 February 2012 18:09 Go to previous message
Bodo Koch is currently offline Bodo KochFriend
Messages: 8
Registered: February 2012
Junior Member
Nice!

Now my LIMBO workbench also works in 2.1

Great

Bodo
Previous Topic:Partial parsing can't resolve some references
Next Topic:[Xtext 2.2] Using syntactic predicates within multi-cardinality constructs
Goto Forum:
  


Current Time: Tue Apr 07 14:35:46 GMT 2020

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

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

Back to the top