Use Eclipse AST Parser To Identify Variables in a Statement & Obtain their Variable Declaration [message #244388] |
Fri, 08 June 2007 17:39  |
Eclipse User |
|
|
|
Originally posted by: nitrousfiz.googlemail.com
Hello,
I need to write a (parser) program that will behave in the following way -
Input - some line number
Processing - identify all variable names in that line & find line numbers of
the declarations of the identified variables
Output - line numbers of the declarations of the identified variables
Example: Suppose you are given the following Java program -
-------------------------------------------------------
1. public class HelloWorld {
2. public static void main(String[] args) {
3. int a;
4. int b;
5. int c;
6. a=1;
7. b=2;
8. c=3;
9. a=a+c;
10. }
11. }
-------------------------------------------------------
Input: Line 9
Processing:
* Identify variables in line 9 - variables a and c
* Find declaration line number of variable a & c - Lines 3 and 5
* Output these line numbers
Output: Lines 3, 5
I was told that this is possible by using the Eclipse AST Parser.
Please advice how can I start solving this problem using it and what code to
put inside which method(s) in the AST explorer/traverser?
Thanks for your help.
Fayezin
|
|
|
|
Re: Use Eclipse AST Parser To Identify Variables in a Statement & Obtain their Variable Declarat [message #244497 is a reply to message #244411] |
Wed, 13 June 2007 06:09   |
Eclipse User |
|
|
|
Originally posted by: jacek.pospychala.pl.ibm.com
Fayezin,
you should have a look at org.eclipse.jdt.core plugin and specially
ASTParser class there. Just to launch the parser, the following code
would be enough:
ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setKind(ASTParser.K_COMPILATION_UNIT); // you tell parser, that
source is whole java file. parser can also process single statements
parser.setSource(source);
CompilationUnit cu = (CompilationUnit) parser.createAST(null); //
CompilationUnit here is of type org.eclipse.jdt.core.dom.CompilationUnit
source is either char array, like this: "public class A { int i = 9; int
j; }".toCharArray(), or org.eclipse.jdt.core.ICompilationUnit type,
which represents java source files
in workspace.
after the AST is built, you can traverse it with visitor, that extends
ASTVisitor, like this:
cu.accept(new ASTVisitor() {
public boolean visit(SimpleName node) {
System.out.println(node); // print all simple names in
compilation unit. in our example it would be A, i, j (class name, and
then variables)
return true;
}
});
so far good, but parser is offset oriented and doesn't store line
numbers information. The easiest way is to create (or get, if you
already have) an IDocument for your source code.
Then use IDocument.getLineInformation(lineNumber) and
getLineInformationForOffset(offset) to convert between line numbers and
offsets.
Finally, looking for variables and their declarations. You can do it
either manually (some nice algorithms) or exploit bindings mechanism.
A lot more details is here:
http://www.eclipsecon.org/2007/index.php?page=sub/&id=36 93
Hope this helps
Fayezin Islam wrote:
> Any one with any ideas?
>
> Please help!
>
> Thanks,
>
> Fayezin
>
> "Fayezin Islam" <nitrousfiz@googlemail.com> wrote in message
> news:f4ci87$paa$1@build.eclipse.org...
>
>> Hello,
>>
>> I need to write a (parser) program that will behave in the following way -
>>
>> Input - some line number
>> Processing - identify all variable names in that line & find line numbers
>> of the declarations of the identified variables
>> Output - line numbers of the declarations of the identified variables
>>
>> Example: Suppose you are given the following Java program -
>>
>> -------------------------------------------------------
>> 1. public class HelloWorld {
>> 2. public static void main(String[] args) {
>> 3. int a;
>> 4. int b;
>> 5. int c;
>> 6. a=1;
>> 7. b=2;
>> 8. c=3;
>> 9. a=a+c;
>> 10. }
>> 11. }
>> -------------------------------------------------------
>>
>> Input: Line 9
>>
>> Processing:
>> * Identify variables in line 9 - variables a and c
>> * Find declaration line number of variable a & c - Lines 3 and 5
>> * Output these line numbers
>>
>> Output: Lines 3, 5
>>
>> I was told that this is possible by using the Eclipse AST Parser.
>>
>> Please advice how can I start solving this problem using it and what code
>> to put inside which method(s) in the AST explorer/traverser?
>>
>> Thanks for your help.
>>
>> Fayezin
>>
>>
>
>
>
|
|
|
|
Re: Use Eclipse AST Parser To Identify Variables in a Statement & Obtain their Variable Declarat [message #245052 is a reply to message #245003] |
Thu, 28 June 2007 12:39  |
Eclipse User |
|
|
|
Originally posted by: jacek.pospychala.pl.ibm.com
thanks for clarifying Frederic,
i'd only add, that I had to be careful, when creating sourceA -> AST and
then AST -> sourceB. sourceA and sourceB were not necessarily the same
and eventually line numbers as well.
In this case i see
org.eclipse.jdt.core.dom.CompilationUnit#getLineNumber(int) refers to
original source string - sourceA.
Frederic Fusier wrote:
> Jacek Pospychala wrote:
>> so far good, but parser is offset oriented and doesn't store line
>> numbers information. The easiest way is to create (or get, if you
>> already have) an IDocument for your source code.
>> Then use IDocument.getLineInformation(lineNumber) and
>> getLineInformationForOffset(offset) to convert between line numbers
>> and offsets.
>>
> This is not true, line numbers are stored while building DOM/AST tree.
> You can get the line number of a position in your source using
> org.eclipse.jdt.core.dom.CompilationUnit#getLineNumber(int) API method.
>
> I modified the snippet given by Jacek to show a simple usage of this API:
>
> final CompilationUnit cu = (CompilationUnit) parser.createAST(null);
> cu.accept(new ASTVisitor() {
> Set names = new HashSet();
>
> public boolean visit(VariableDeclarationFragment node) {
> SimpleName name = node.getName();
> this.names.add(name.getIdentifier());
> System.out.println("Declaration of '" + name + "' at line " +
> unit.getLineNumber(name.getStartPosition()));
> return false; // do not continue to avoid usage info
> }
>
> public boolean visit(SimpleName node) {
> if (this.names.contains(node.getIdentifier())) {
> System.out.println("Usage of '" + node + "' at line " +
> unit.getLineNumber(node.getStartPosition()));
> }
> return true;
> }
> }
>
> This visitor will produce following output when accepted by the
> compilation unit:
> Declaration of 'a' at line 3
> Declaration of 'b' at line 4
> Declaration of 'c' at line 5
> Usage of 'a' at line 6
> Usage of 'b' at line 7
> Usage of 'c' at line 8
> Usage of 'a' at line 9
> Usage of 'a' at line 9
> Usage of 'c' at line 9
>
> HTH
|
|
|
Powered by
FUDForum. Page generated in 0.02964 seconds