This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Finding Declarations in the AST

7 replies
Mirko Stocker
Joined: 2009-09-10,
User offline. Last seen 45 weeks 6 days ago.

Hello

For my refactoring work, I need to find declarations of
values/variables/methods/etc, usually from a usage of it. So in the simplest
example:

val x = 1
val y = x

How can I, from the x on the rhs of the assignment to y, find the declaration
of x? I hoped that the symbols would somehow be "linked", but I haven't yet
figured out how.. (there's no documentation anywhere on such things, right?)

Thanks in advance

Mirko

Kevin Wright
Joined: 2009-06-09,
User offline. Last seen 49 weeks 3 days ago.
Re: Finding Declarations in the AST
One trick I've found useful in figuring out exactly what's going on between the AST, symbols and types is to run  global.treeBrowser.browse(unit.body)against the parsed AST
It'll give you a swing window containing a browsable overview of the unit  plus full type and symbol info (assuming namer and typer have been run).I've learnt a LOT from exploring that browser after different compiler phases.


On Thu, Dec 3, 2009 at 4:56 PM, Mirko Stocker <me@misto.ch> wrote:
Hello

For my refactoring work, I need to find declarations of
values/variables/methods/etc, usually from a usage of it. So in the simplest
example:

val x = 1
val y = x

How can I, from the x on the rhs of the assignment to y, find the declaration
of x? I hoped that the symbols would somehow be "linked", but I haven't yet
figured out how.. (there's no documentation anywhere on such things, right?)

Thanks in advance

Mirko

Caoyuan
Joined: 2009-01-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Finding Declarations in the AST

My approach in NetBeans plugin is:
1. Traverse AST tree to gather all declaration, record the (symbol -> tree)
2. For a given position, tree or symbol usage (reference), query the
recorded information.

On Fri, Dec 4, 2009 at 12:56 AM, Mirko Stocker wrote:
> Hello
>
> For my refactoring work, I need to find declarations of
> values/variables/methods/etc, usually from a usage of it. So in the simplest
> example:
>
> val x = 1
> val y = x
>
> How can I, from the x on the rhs of the assignment to y, find the declaration
> of x? I hoped that the symbols would somehow be "linked", but I haven't yet
> figured out how.. (there's no documentation anywhere on such things, right?)
>
> Thanks in advance
>
> Mirko
>

Mirko Stocker
Joined: 2009-09-10,
User offline. Last seen 45 weeks 6 days ago.
Re: Finding Declarations in the AST

On Thursday 03 December 2009 18:27:10 Caoyuan wrote:
> My approach in NetBeans plugin is:
> 1. Traverse AST tree to gather all declaration, record the (symbol -> tree)
> 2. For a given position, tree or symbol usage (reference), query the
> recorded information.

I feared such an answer :) So I have to do that myself. I knew from talking to
Miles that there's no such thing like an index, e.g. to query references to
methods etc, but I had hoped that the link from usage → declaration would be
already there somehow.

Thanks,

Mirko

Mirko Stocker
Joined: 2009-09-10,
User offline. Last seen 45 weeks 6 days ago.
Re: Finding Declarations in the AST

On Thursday 03 December 2009 18:23:18 Kevin Wright wrote:
> One trick I've found useful in figuring out exactly what's going on between
> the AST, symbols and types is to run
> global.treeBrowser.browse(unit.body)
> against the parsed AST

Yep, that's great for exploring the AST, I've used scalac -Ybrowse:typer ..

More such tipps and tricks are very welcome :)

Thanks

Mirko

Caoyuan
Joined: 2009-01-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Finding Declarations in the AST

On Fri, Dec 4, 2009 at 1:37 AM, Mirko Stocker wrote:
> On Thursday 03 December 2009 18:27:10 Caoyuan wrote:
>> My approach in NetBeans plugin is:
>> 1. Traverse AST tree to gather all declaration, record the (symbol -> tree)
>> 2. For a given position, tree or symbol usage (reference), query the
>> recorded information.
>
> I feared such an answer :) So I have to do that myself. I knew from talking to
> Miles that there's no such thing like an index, e.g. to query references to
> methods etc, but I had hoped that the link from usage → declaration would be
> already there somehow.

Yes, I wrote a bunch of code to deal with it under varies conditions:
the same position may contain more than one symbol :-)

>
> Thanks,
>
> Mirko
>

milessabin
Joined: 2008-08-11,
User offline. Last seen 33 weeks 3 days ago.
Re: Finding Declarations in the AST

On Thu, Dec 3, 2009 at 5:38 PM, Caoyuan wrote:
> On Fri, Dec 4, 2009 at 1:37 AM, Mirko Stocker wrote:
>> On Thursday 03 December 2009 18:27:10 Caoyuan wrote:
>>> My approach in NetBeans plugin is:
>>> 1. Traverse AST tree to gather all declaration, record the (symbol -> tree)
>>> 2. For a given position, tree or symbol usage (reference), query the
>>> recorded information.
>>
>> I feared such an answer :) So I have to do that myself. I knew from talking to
>> Miles that there's no such thing like an index, e.g. to query references to
>> methods etc, but I had hoped that the link from usage → declaration would be
>> already there somehow.
>
> Yes, I wrote a bunch of code to deal with it under varies conditions:
> the same position may contain more than one symbol :-)

I would be great to have something like this in an IDE-independent
form that can be used by all tools that need the functionality.

Would you be willing to contribute it to scala.tools.nsc._?

Cheers,

Miles

Caoyuan
Joined: 2009-01-18,
User offline. Last seen 42 years 45 weeks ago.
Re: Finding Declarations in the AST

I wrapped these features in another level of abstract in NetBeans.
But, I'll try to see if I can split them out as independent
implementations.

-Caoyuan

On Sun, Dec 6, 2009 at 10:57 PM, Miles Sabin wrote:
> On Thu, Dec 3, 2009 at 5:38 PM, Caoyuan wrote:
>> On Fri, Dec 4, 2009 at 1:37 AM, Mirko Stocker wrote:
>>> On Thursday 03 December 2009 18:27:10 Caoyuan wrote:
>>>> My approach in NetBeans plugin is:
>>>> 1. Traverse AST tree to gather all declaration, record the (symbol -> tree)
>>>> 2. For a given position, tree or symbol usage (reference), query the
>>>> recorded information.
>>>
>>> I feared such an answer :) So I have to do that myself. I knew from talking to
>>> Miles that there's no such thing like an index, e.g. to query references to
>>> methods etc, but I had hoped that the link from usage → declaration would be
>>> already there somehow.
>>
>> Yes, I wrote a bunch of code to deal with it under varies conditions:
>> the same position may contain more than one symbol :-)
>
> I would be great to have something like this in an IDE-independent
> form that can be used by all tools that need the functionality.
>
> Would you be willing to contribute it to scala.tools.nsc._?
>
> Cheers,
>
>
> Miles
>
> --
> Miles Sabin
> tel: +44 (0)7813 944 528
> skype:  milessabin
> http://www.chuusai.com/
> http://twitter.com/milessabin
>

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland