Home » Eclipse Projects » Dynamic Languages Toolkit (DLTK) » Tips about building AST
|
Re: Tips about building AST [message #37540 is a reply to message #37503] |
Tue, 26 May 2009 03:43 |
Alex Panchenko Messages: 342 Registered: July 2009 |
Senior Member |
|
|
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
|
|
|
Re: Tips about building AST [message #37571 is a reply to message #37540] |
Wed, 27 May 2009 16:03 |
Kevin KIN-FOO Messages: 28 Registered: July 2009 |
Junior Member |
|
|
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
>
|
|
|
Re: Tips about building AST [message #37733 is a reply to message #37571] |
Thu, 28 May 2009 08:35 |
Alex Panchenko Messages: 342 Registered: July 2009 |
Senior Member |
|
|
Hi Kevin,
In current Ruby implementation DLTK AST is created from JRuby AST.
It allows reusing of already created parser, so you can gain some time
during development.
Regards,
Alex
Kevin KIN-FOO wrote:
> 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
|
|
|
Goto Forum:
Current Time: Tue Sep 17 06:44:02 GMT 2024
Powered by FUDForum. Page generated in 0.04078 seconds
|