- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Finding Declarations in the AST
Thu, 2009-12-03, 17:55
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
Thu, 2009-12-03, 18:37
#2
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
>
Thu, 2009-12-03, 18:37
#3
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
Thu, 2009-12-03, 18:47
#4
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
Thu, 2009-12-03, 18:57
#5
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
>
Sun, 2009-12-06, 16:07
#6
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
Sun, 2009-12-06, 16:17
#7
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
>
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: