I'm using the excellent Recoder front end and was wondering if someone could
point me to the class for printing source code line numbers? (I've only been
able to find a line number utility within the PrettyPrint class which seems to
be the output line number, not the source code line number). Thanks in
advance.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
each element in the abstract syntax tree has getStartPosition() and
getEndPosition() defined. Both return an instance of
recoder.java.SourceElement.Position. This class defines getLine() and
getColumn().
Some notes:
-- in order to reduce memory footprint, both start and end position share a
"long" field together with the relative position (relative offset
to the closest other token). In order to achieve this, the maximum column
position is limited to 255 - a token at column 300 will report 255. Line
numbers are limited to 65534 - yes, "34" at the end for internal
reasons ;-)
-- consider the following construct:
1: @Deprecated
2: public class A {
3: ...
4: }
Here, the if you have a variable classDecl referencing class "A",
you will have a start position line 2 / column 8 (the 'class' in the code). To
get the "whole" class, do the following:
classDecl.getFirstElement().getStartPosition(). The call to getFirstElement()
will return "@Deprecated" in this case.
I hope this answers your question!
Best regards,
Tobias
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm using the excellent Recoder front end and was wondering if someone could
point me to the class for printing source code line numbers? (I've only been
able to find a line number utility within the PrettyPrint class which seems to
be the output line number, not the source code line number). Thanks in
advance.
Hej,
each element in the abstract syntax tree has getStartPosition() and
getEndPosition() defined. Both return an instance of
recoder.java.SourceElement.Position. This class defines getLine() and
getColumn().
Some notes:
-- in order to reduce memory footprint, both start and end position share a
"long" field together with the relative position (relative offset
to the closest other token). In order to achieve this, the maximum column
position is limited to 255 - a token at column 300 will report 255. Line
numbers are limited to 65534 - yes, "34" at the end for internal
reasons ;-)
-- consider the following construct:
1: @Deprecated 2: public class A { 3: ... 4: }
Here, the if you have a variable classDecl referencing class "A",
you will have a start position line 2 / column 8 (the 'class' in the code). To
get the "whole" class, do the following:
classDecl.getFirstElement().getStartPosition(). The call to getFirstElement()
will return "@Deprecated" in this case.
I hope this answers your question!
Best regards,
Tobias