[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [Newsgroup Home]
[news.eclipse.technology.dltk] Re: Tips about building AST

Hi Alex,

Your answer is very quick and helpful. I'm still working on AST
generation and I'm wondering if it's possible to use an AST from another
language as reference.

I mean, I have an AST representing the code in another language that is
gifted for AST manipulation, can I do  a proxy through it with a
ModuleDeclaration Object ? Is it a good idea ? Did anyone tried it before?

Regards

Alex Panchenko a écrit :
> Hi Kevin,
> 
> ModuleDeclaration is just a class to return AST of the source module, so
> it should be only one instance at the top of tree.
> 
> The natural way to navigate over AST is traverse() method, getChild() is
> just a helper method and ASTNode's implementation uses traverse() to
> collect it's direct children.
> 
> So, your classes should look like:
> 
> public class RubyIfStatement extends ASTNode {
>     private ASTNode fCondition;
>     private ASTNode fThenStatement;
>     private ASTNode fElseStatement;
> 
>     public RubyIfStatement(ASTNode condition, ASTNode thenStatement,
> ASTNode elseStatement) {
>         this.fCondition = condition;
>         this.fThenStatement = thenStatement;
>         this.fElseStatement = elseStatement;
>     }
> 
>     public void traverse(ASTVisitor pVisitor) throws Exception {
>         if (pVisitor.visit(this)) {
>             if (fCondition != null) {
>                 fCondition.traverse(pVisitor);
>             }
>             if (fThenStatement != null) {
>                 fThenStatement.traverse(pVisitor);
>             }
>             if (fElseStatement != null) {
>                 fElseStatement.traverse(pVisitor);
>             }
>             pVisitor.endvisit(this);
>         }
>     }
> ....
> }
> 
> Regards,
> Alex
> 
> Kevin KIN-FOO wrote:
>> Hi,
>>
>> I'm trying to build a DLTK compliant AST and I'm facing an issue
>> building the parser. I've noticed that there is a "getChilds()" method
>> on every ASTNode, but you cant add any child node. Reading the code of
>> the Python IDE example, I discovered that ModuleDeclaration objects
>> accept child nodes with the "addStatement()" method. So, there is My
>> question.
>>
>> Is it possible to have several ModuleDeclaration objects in one single
>> AST? If no, what is the proper way to link ASTNode objects together ?
>>
>> Regards
>