Menu

Printing Line Source Code LIne Numbers

Developers
2009-10-24
2012-10-08
  • Suzanna Schmeelk

    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.

     
  • Tobias Gutzmann

    Tobias Gutzmann - 2009-10-26

    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

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.