Tree API (JEP 236): fine grained offsets (for eg. SwitchTree)
Sundararajan Athijegannathan
sundararajan.athijegannathan at oracle.com
Thu Aug 4 05:11:47 UTC 2016
hmm.. not sure if Abstract syntax tree Parser API should provide offsets
of "(", "}" etc. This API is modeled after javac's tree API - I don't
think even that goes to that level of representation of the source code...
-Sundar
On 8/3/2016 2:38 PM, Emilian Bold wrote:
> Hello,
>
> Since JEP 236 is designed to help with editing I believe the Tree API
> should provide more offsets info about the whole statement.
>
> For example SwitchTree covers the switch statement:
>
> switch ( expression ) {
> cases
> }
>
> but it is impossible to determine the offsets of the curly braces or of the
> round brackets.
>
> The closing curly brace is clearly located at SwitchTree.getEndPosition.
>
> But the first curly brace has an unknown offset somewhere between
> SwitchTree.getExpression(). getEndPosition() + 1 and the getStartPosition
> of the 1st CaseTree.
>
> Obviously exact offsets are necessary to provide matching brace
> highlighting or code folding for the switch expression in an editor.
>
> Ideally SwitchTree would have contained a (Switch)BlockTree which in turn
> would have contained the list of CaseTree-s.
>
> For the round brackets, SwitchTree.getExpression() could have returned
> a ParenthesizedTree instead.
>
> PS: I wonder, would it be possible to guarantee that there is a way to
> re-generate the exact input string based on the (CompilationUnit)Tree?
> Because this is what seems necessary for an editor or for
> minimally-invasive source-code processing tools.
>
> --emi
More information about the nashorn-dev
mailing list