-> Some words about AST [message #145080] |
Tue, 24 February 2004 15:45  |
Eclipse User |
|
|
|
Hi there,
I am fairly new to AST but not to the eclipse/plugin stuff.
I read some books about eclipse+plugin but the coverage of
AST is - well - only a small overview.
The senario I stand infront of: Having an IMethod or an IType,
how can I get an ASTNode for it?
(MethodDeclaration/TypeDeclaration)
Do I have to parse the enclosing CompilationUnit? What can
I do to reducec footprint on that kind of operations?
Any best practice (or antipattern) advices?
Thanks,
Martin (Kersten)
|
|
|
|
|
|
Re: -> Some words about AST [message #145116 is a reply to message #145104] |
Tue, 24 February 2004 17:24   |
Eclipse User |
|
|
|
Hi aksel,
> > Any snippets available?
>
> just been there - newbie though:
>
> IMethod method = xxx;
> ICompilationUnit icompilationUnit= method.getCompilationUnit();
> CompilationUnit compilationUnit = AST.parseCompilationUnit(
> icompilationUnit, true, null, null);
> MyASTVisitor myASTVisitor = new MyASTVisitor();
> compilationUnit.accept(myASTVisitor);
>
> The MyASTVisitor extends ASTVisitor, which in turn can overload
> visit(MethodDeclaration node) or visit(TypeDeclaration node)
I will copy that snippet. I found another usefull way. Using a scanner...
String source=IMethod.getSource();
IScanner scanner=ToolFactory.createScanner(false,false,false,false);
scanner.setSource(source.toCharArray());
boolean isEOF=false;
while(!isEOF) {
int kind=scanner.getNextToken();
switch(kind) {
case ITerminalSymbols.TokenNameXXX:
...
break;
case ITerminalSymbols.TokenNameEOF:
case ITerminalSymbols.TokenNameERROR:
isEOF=true;
break;
}
}
Works well and there is not such overhead involved like parsing
a whole compilation unit. But why the hack didn't they implemented
the scanner using an iteraton pattern. Something like hasNext and
next.
Dont know. Maybe I will write a fascade or does someone knows?
Thanks aksel,
Martin (Kersten)
|
|
|
|
Re: -> Some words about AST [message #145138 is a reply to message #145120] |
Wed, 25 February 2004 03:01  |
Eclipse User |
|
|
|
> look at
> org.eclipse.jdt.internal.corext.refactoring.structure.ASTNod eSearchUtil
> getMethodDeclarationNode
Saddly the underlying NodeFinder is also placed in an internal package. :(
But I had the same idea yesterday to use the search for a source range
covered
by a node (skipping every node not spanning the given range).
I implemented a visitor for that (looks similar to NodeFinder) but if the
NodeFinder
is in a non-internal package I would love to use it instead (removes one
TestCase :)
But I can't use a class of an internal package :(
The idea to look for the name and use getParent is genius :) I search for
the whole
method declaration. But this would work for nearly everything. :)
Thanks, great suggestion!
Martin (Kersten)
|
|
|
Powered by
FUDForum. Page generated in 0.04389 seconds