Uploaded image for project: 'BlueJ'
  1. BlueJ
  2. BLUEJ-182

Node positions are strange, especially FieldNode

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Medium
    • Resolution: Done
    • Affects Version/s: 2.5.3
    • Fix Version/s: 3.0.6
    • Component/s: Parser

      Description

      Here is a Java class that I have in BlueJ:

      /**
       * Write a description of class Short here.
       */
      public class Short
      {
          /**
           * Constructor for objects of class Short
           */
          public Short()
          {
              // initialise instance variables
              x = 0;
          }
          private int x;
      
      }
      

      I am looking at the NodeAndPosition representation of that code to understand how the tree of parse nodes is built. I'm traversing the NodeTree, effectively, rather than using the findAtOrAfter methods. Here is the tree of NodeAndPosition items, with position and end values (from getPosition and getEnd):

      bluej.parser.nodes.ParsedCUNode@19299f5(0,245)
        bluej.parser.nodes.ParsedTypeNode@842d9a(1,244)
          bluej.parser.nodes.CommentNode@186ed7a(0,51)
          bluej.parser.nodes.TypeInnerNode@1ce00b4(21,191)
            bluej.parser.nodes.MethodNode@1e3cc77(5,149)
              bluej.parser.nodes.CommentNode@150f0a7(0,57)
              bluej.parser.nodes.ParentParsedNode@19067e5(25,86)
                bluej.parser.nodes.CommentNode@e771f3(9,41)
                bluej.parser.nodes.ExpressionNode@ea3932(9,14)
            bluej.parser.nodes.FieldNode@15118a7(12,19)
      

      Here, for reference, is the file marked up with element offsets, in the format start-of-line -> first-non-whitespace-character, end-of-line:

       // 0->0,1: 
      /** // 1->1,5: 
      * Write a description of class Short here. // 5->6,49: 
      */ // 49->50,53: 
      public class Short // 53->53,72: 
      { // 72->72,74: 
      /** // 74->78,82: 
      * Constructor for objects of class Short // 82->87,128: 
      */ // 128->133,136: 
      public Short() // 136->140,155: 
      { // 155->159,161: 
      // initialise instance variables // 161->169,202: 
      x = 0; // 202->210,217: 
      } // 217->221,223: 
      private int x; // 223->227,242: 
       // 242->242,243: 
      } // 243->243,245: 
       // 245->245,246: 
      

      If we look at the first TypeInnerNode, its start position claims to be at 21. That isn't right; 21 characters is midway through the first comment. To correct the indent, I believe I need to add on the end position (or possibly size) of its prior sibling node (the CommentNode), giving 21+51=72, but it's bit confusing to add on offsets from siblings (not just from parents). Judging by the MethodNode, it seems you need to add the end position/size of siblings, and the start position of parent nodes to get to the offset from the beginning of the whole file. Is that right?

      The FieldNode would appear to back up this idea of size from sibling nodes and offset from parent, 51+21+144+12=228 (shouldn't it be 227, though? And why is the field node only 7 characters long?). But, now see what happens if I add a method after that field, giving the Java source:

      /**
       * Write a description of class Short here.
       */
      public class Short
      {
          /**
           * Constructor for objects of class Short
           */
          public Short()
          {
              // initialise instance variables
              x = 0;
          }
          private int x;
          public void doNothing()
          {
          }
      
      }
      

      And the output:

      bluej.parser.nodes.ParsedCUNode@19299f5(0,285)
        bluej.parser.nodes.ParsedTypeNode@94aa42(1,284)
          bluej.parser.nodes.CommentNode@1a4eb56(0,51)
          bluej.parser.nodes.TypeInnerNode@35dc95(21,231)
            bluej.parser.nodes.MethodNode@a39de(5,149)
              bluej.parser.nodes.CommentNode@566633(0,57)
              bluej.parser.nodes.ParentParsedNode@24ea85(25,86)
                bluej.parser.nodes.CommentNode@4fdf11(9,41)
                bluej.parser.nodes.ExpressionNode@1be0369(9,14)
            bluej.parser.nodes.FieldNode@1d1fc02(161,168)
            bluej.parser.nodes.MethodNode@15d45d9(5,40)
              bluej.parser.nodes.ParentParsedNode@f800db(29,34)
       // 0->0,1: 
      /** // 1->1,5: 
      * Write a description of class Short here. // 5->6,49: 
      */ // 49->50,53: 
      public class Short // 53->53,72: 
      { // 72->72,74: 
      /** // 74->78,82: 
      * Constructor for objects of class Short // 82->87,128: 
      */ // 128->133,136: 
      public Short() // 136->140,155: 
      { // 155->159,161: 
      // initialise instance variables // 161->169,202: 
      x = 0; // 202->210,217: 
      } // 217->221,223: 
      private int x; // 223->227,242: 
      public void doNothing() // 242->246,270: 
      { // 270->274,276: 
      } // 276->280,282: 
       // 282->282,283: 
      } // 283->283,285: 
       // 285->285,286: 
      

      Note how the FieldNode offset has jumped by about 150 by adding something after it in the class. Regardless of any other misunderstandings, that looks like a bug, so hence this ticket. (All this node position stuff is in aid of the auto-indentation feature, where I need to traverse the node tree to work out what my indent should be on the current line, as it depends on the parent nodes.)

        Attachments

          Activity

            People

            • Assignee:
              davmac Davin McCall
              Reporter:
              nccb Neil Brown
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: