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

compiler plugin source code positions

2 replies
Nicholas Tung
Joined: 2009-05-19,
User offline. Last seen 42 years 45 weeks ago.
Hi all,

   Is there a way to get more source code position information from the compiler -- namely, the range of the given AST node? I am looking at tree.pos, where tree is the argument passed into a Transformer's transform() function.

    I tried looking around with Java reflection... no luck. The RangePosition in Position.scala seems to only be used in nsc/interactive/ (I also tried putting a println in it's constructor).

Thanks in advance,
Nicholas
Nicholas Tung
Joined: 2009-05-19,
User offline. Last seen 42 years 45 weeks ago.
Re: compiler plugin source code positions

Nicholas Tung wrote:
> Hi all,
>
> Is there a way to get more source code position information from
> the compiler -- namely, the range of the given AST node? I am looking
> at tree.pos, where tree is the argument passed into a Transformer's
> transform() function.
>
> I tried looking around with Java reflection... no luck. The
> RangePosition in Position.scala seems to only be used in
> nsc/interactive/ (I also tried putting a println in it's constructor).
>
> Thanks in advance,
> Nicholas
Hi all,

I think I found a solution -- to override rangePos in
nsc.util.Global. However, is there a way to integrate this in a plugin
(while keeping it as a plugin versus a top-level Java class?).

Also, I seem to be having problems re-compiling; for example, if I
change rangePos between the two options below, if I don't do "ant
clean", I get this error:
"Exception in thread "main" java.lang.NoSuchMethodError:
scala.tools.nsc.Global.rangePos(Lscala/tools/nsc/util/SourceFile;III)Lscala/tools/nsc/util/RangePosition;
at
scala.tools.nsc.ast.parser.Parsers$UnitParser.r2p(Parsers.scala:73)"

This results in latency for changing anything in the compiler at 9
minutes (after ant clean)... while one can execute $(ant clean) after
syntax is valid, this still seems rather slow.

def rangePos(source: SourceFile, start: Int, mid: Int, end: Int) =
// first compile option: new RangePosition(source, start, mid, end)
// second compile option: OffsetPosition(source, mid)

Thanks so much!
Nicholas

Nicholas Tung
Joined: 2009-05-19,
User offline. Last seen 42 years 45 weeks ago.
Re: compiler plugin source code positions

Nicholas Tung wrote:
> Hi all,
>
> Is there a way to get more source code position information from
> the compiler -- namely, the range of the given AST node? I am looking
> at tree.pos, where tree is the argument passed into a Transformer's
> transform() function.
>
> I tried looking around with Java reflection... no luck. The
> RangePosition in Position.scala seems to only be used in
> nsc/interactive/ (I also tried putting a println in it's constructor).
>
> Thanks in advance,
> Nicholas
Hi all,

I think I found a solution -- to override rangePos in
nsc.util.Global. However, is there a way to integrate this in a plugin
(while keeping it as a plugin versus a top-level Java class?).

Also, I seem to be having problems re-compiling; for example, if I
change rangePos between the two options below, if I don't do "ant
clean", I get this error:
"Exception in thread "main" java.lang.NoSuchMethodError:
scala.tools.nsc.Global.rangePos(Lscala/tools/nsc/util/SourceFile;III)Lscala/tools/nsc/util/RangePosition;
at
scala.tools.nsc.ast.parser.Parsers$UnitParser.r2p(Parsers.scala:73)"

This results in latency for changing anything in the compiler at 9
minutes (after ant clean)... while one can execute $(ant clean) after
syntax is valid, this still seems rather slow.

def rangePos(source: SourceFile, start: Int, mid: Int, end: Int) =
// first compile option: new RangePosition(source, start, mid, end)
// second compile option: OffsetPosition(source, mid)

Thanks so much!
Nicholas

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