CDT3.0 AST Manipulation [message #148556] |
Mon, 11 July 2005 09:26  |
Eclipse User |
|
|
|
Hello,
Is there any way I can add, remove, and/or replace nodes/branches in the
AST? For example, I know there is an addStatement() method I can use from
IASTCompoundStatement, however, I would like to add nodes somewhere in
between other IASTStatements, instead of adding them to the end of the
compound-statement. I also know I can use the setPropertyInParent() to
modify the node's property.
I know I can probably do this by visiting each node and creating a whole
new tree from scratch. But that just seems like that's the long way
around to it. Is there an easier way to make the AST more "writable"?
Any help or thoughts is appreciated.
Thanks!
|
|
|
|
Re: CDT3.0 AST Manipulation [message #148569 is a reply to message #148563] |
Mon, 11 July 2005 16:11  |
Eclipse User |
|
|
|
Hello,
Prior to updating Eclipse/CDT from 3.1M6/3.0M6 to 3.1/3.0RC1, I was able
to use a workaround that would modify the AST as I was using an ASTVisitor
to visit the tree. The code snippet that did this was:
// ------------------------------------------------------------ ---
// Replaces 'statement' with a branch of nodes (ws)
// Create an AST "branch"
IASTWhileStatement ws = new CASTWhileStatement();
...
IASTCompoundStatement parentCompoundStatement =
(IASTCompoundStatement)statement.getParent();
IASTStatement[] sibs = parentCompoundStatement.getStatements();
IASTStatement[] newSibs = new IASTStatement[sibs.length];
// Copy all children up to the one that will be replaced
int i;
for (i = 0; sibs[i] != statement; ++i) {
newSibs[i] = sibs[i];
}
// Copy remaining children
for (int j = i + 1; j < sibs.length; j++) {
newSibs[j + 1] = sibs[j];
}
// Insert new branch into the "hole"
newSibs[i] = initExprStmt;
// Replace old statements with new
System.arraycopy(newSibs, i, sibs, i, sibs.length - i);
for (i = sibs.length; i < newSibs.length; i++)
parentCompoundStatement.addStatement(newSibs[i]);
// Setting parent of new statement
ws.setParent(parentCompoundStatement);
// ------------------------------------------------------------ ---
What has changed between M6 and RC1 that this workaround is no longer
valid? It would be nice to know so that I can see where I should change
my code.
Thanks! Mike
johnc wrote:
> The get&set methods are not intended to help w/the rewrite aspect of the
> AST.
> Unfortunately, we haven't plotted out that course completely yet.
> "Mike" <mcabusao@afit.edu> wrote in message
> news:97890145ee93cb8a405fbca4686afa98$1@www.eclipse.org...
>> Hello,
>> Is there any way I can add, remove, and/or replace nodes/branches in the
>> AST? For example, I know there is an addStatement() method I can use from
>> IASTCompoundStatement, however, I would like to add nodes somewhere in
>> between other IASTStatements, instead of adding them to the end of the
>> compound-statement. I also know I can use the setPropertyInParent() to
>> modify the node's property.
>>
>> I know I can probably do this by visiting each node and creating a whole
>> new tree from scratch. But that just seems like that's the long way
>> around to it. Is there an easier way to make the AST more "writable"?
>> Any help or thoughts is appreciated.
>>
>> Thanks!
>>
>>
|
|
|
Powered by
FUDForum. Page generated in 0.03965 seconds