0

In my hard drive I have follow source:

package DAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CountDownLatch;


public abstract class A {
public class A4{
    public String teste4(int i){
        return (new String("teste"));
    }
}
public class A3{
    public String teste3(int i){
        return (new String("teste"));
    }
    public A4 teste33(int i){
        return (new A4());
    }       
}
public class A2{
    public A3 teste2(int i){
        return (new A3());
    }
}
public class A1{
    public A2 teste1(int i){
        return (new A2());
    }
}


  public int[] toIntArray(List<Integer> list) {
     A1 q=new A1();

     Integer t=q.teste1(
                (new A1()).
                teste1(0).
                teste2(0).
                teste33(0).
                teste4(0).
                length() ).
            teste2(0).
            teste3(0).
            length();
  } 
}

Note that in this file (A.java) we have many blank lines and one command not necessarily was written in the same line, in others words, it's maybe spread in many lines (See last line of method "toIntArray"). When I parse this source code with AST, the "toString" of "CompilationUnit"show the follow struct (AST Struct):

package DAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;
import java.util.concurrent.CountDownLatch;
public abstract class A {
public class A4 {
    public String teste4(    int i){
      return (new String("teste"));
    }
  }
public class A3 {
    public String teste3(    int i){
      return (new String("teste"));
    }
    public A4 teste33(    int i){
      return (new A4());
    }
  }
public class A2 {
    public A3 teste2(    int i){
      return (new A3());
    }
  }
public class A1 {
    public A2 teste1(    int i){
      return (new A2());
    }
  }
  public int[] toIntArray(  List<Integer> list){
    A1 q=new A1();
    Integer t=q.teste1((new A1()).teste1(0).teste2(0).teste33(0).teste4(0).length()).teste2(0).teste3(0).length();
  }
}

Note that in this case, all blank lines was removed and the command "Integer t=q.teste1...." was write in the same line. It's fine to me. Thus, in visit "MethodInvocation", I want to get line number of these invocations. To do this, I make follow source in my astVisitors:

public boolean visit(final CompilationUnit node) {
this.CompilationUnit=node;
}
public boolean visit(final MethodInvocation node) {
Integer LineInFile=this.CompilationUnit.getLineNumber(node.getStartPosition());
}

But the command "getLineNumber" return Line Number in source file in my hard drive, not in AST Struct. In this case, to the line "q.teste1((new.....", the command "getLineNumber" return the lines 44 and 45, but want that this return only the line 44. So, How get de line number in "AST Struct"?

1
  • Why do you want the "line number" in the "AST Struct" rather than the external file? The external file is the only thing the user cares about, or has seen, or would recognize. She has zero interest in the AST struct, let alone knowledge that it exists. So what is the point of this? Commented Oct 14, 2016 at 21:31

2 Answers 2

1

The CompilationUnit.toString method is just doing a rough format of the internal AST using the NaiveASTFlattener class.

The actual internal structure of the AST is just a large number of class instances of things like Block, Comment, Statement, ... As such the AST itself does not have line numbers. The only line numbers the AST knows about are the lines in the original source code.

Sign up to request clarification or add additional context in comments.

2 Comments

In this case and in terms of lines, expected the follow return: 44, 45, 46,....53.
The method invocation of teste1 is just lines 44 to 45. The rest of that statement is invocations of other methods.
1

We may start by expanding AST to "abstract syntax tree", which emphasizes that we have no (direct) connection to the source level (=concrete) syntax. You may call it a "model", which captures the semantically relevant aspects, while omitting accidental details like white space. A language could even rename its keywords without any changes to the abstract syntax etc.

It's a convenience for users, that a CompilationUnit additionally stores the positions of line ends, so it can map an AST node back to its original location in the source code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.