Skip to main content
Participant
February 7, 2009
Question

Access and manipulate all ASTs for a given project

  • February 7, 2009
  • 2 replies
  • 587 views
Hi,

I would like to do the following: for every item in a Flex project (MXML documents, AS classes, etc.) I would like to gain access to its AST (so for MXML, this is after it gets converted to the equivalent AS) and for each of those I would like to walk it and output information about it (this is for a code analysis and optimisation tool).

By digging around I've found flash.swf.tools.as3.EvaluatorAdapter and the interface it implements which seem to be what I want in order to walk an AST, but I can't seem to figure out what I need to do before that. All the examples and documentation are for very high level things (load a source, set the output, call build()) and not for this sort of more custom tool.

Is there some documentation out there about the various phases that Flex goes through to build its output, and what they map to in the code? I'm guessing I need to call whatever finds all the files that are involved, then parse them, and possibly after that call a few evaluators that may tweak the tree, and then put my own. But it's a big code base, I've been walking all over it, and I'm a little stumped.

Thanks for any pointers!
This topic has been closed for replies.

2 replies

matt_chotin
Inspiring
February 13, 2009
I don’t’ know of a way of adding the extension, I’m guessing you’d need to provide a patch that enables this generically.





On 2/10/09 12:07 PM, "Robin Berjon" < member@adobeforums.com> wrote:



A new message was posted by Robin Berjon in



Developers --

  Access and manipulate all ASTs for a given project



Thanks Matt, I hadn't found that page in the wiki and it's certainly been useful. I've managed to wire in my own extra compiler extension with asc.addCompilerExtension and it's working.



I have a follow-up though which I haven't managed to figure out: is there a way to add a compiler extension without patching the code directly?



Ideally I'd like the base application to simply do something like this:



  Application app = new Application(new File("....mxml"));  

  app.setOutput(new File("....swf"));

  app.addCompilerExtension(myComp);

  long res = app.build(true);



Or perhaps to do something similar through the configuration. Right now I have to patch ImplementationCompiler directly, which is inelegant. I see that ImplementationCompiler amd MxmlCompiler expose ways of getting to the asc instance or adding extensions to it, but they are both instantiated inside compile() which makes them unreachable.



Ideally I'd like to bring in the Flex JAR as is, and not have to monkey around with it too much. I guess there's the option of subclassing Application and doing a cut and paste of compile() just to do that, but it's not exactly very nice either!



Thanks for any help,



Robin




View/reply at Access and manipulate all ASTs for a given project < http://www.adobeforums.com/webx?13@@.59b7cff3/1>

Replies by email are OK.

Use the unsubscribe < http://www.adobeforums.com/webx?280@@.59b7cff3!folder=.3c060fa3>  form to cancel your email subscription.





matt_chotin
Inspiring
February 7, 2009
Some of this is out of date for the trunk potentially, but we did produce a document describing the basics of the compiler as written for Flex 3.



http://opensource.adobe.com/wiki/display/flexsdk/Flex+3+Compiler+Design



Matt





On 2/7/09 5:15 AM, "Robin Berjon" < member@adobeforums.com> wrote:



A new discussion was started by Robin Berjon in



Developers --

  Access and manipulate all ASTs for a given project



Hi,



I would like to do the following: for every item in a Flex project (MXML documents, AS classes, etc.) I would like to gain access to its AST (so for MXML, this is after it gets converted to the equivalent AS) and for each of those I would like to walk it and output information about it (this is for a code analysis and optimisation tool).



By digging around I've found flash.swf.tools.as3.EvaluatorAdapter and the interface it implements which seem to be what I want in order to walk an AST, but I can't seem to figure out what I need to do before that. All the examples and documentation are for very high level things (load a source, set the output, call build()) and not for this sort of more custom tool.



Is there some documentation out there about the various phases that Flex goes through to build its output, and what they map to in the code? I'm guessing I need to call whatever finds all the files that are involved, then parse them, and possibly after! that call a few evaluators that may tweak the tree, and then put my own. But it's a big code base, I've been walking all over it, and I'm a little stumped.



Thanks for any pointers!




View/reply at Access and manipulate all ASTs for a given project < http://www.adobeforums.com/webx?13@@.59b7cff3>

Replies by email are OK.

Use the unsubscribe < http://www.adobeforums.com/webx?280@@.59b7cff3!folder=.3c060fa3>  form to cancel your email subscription.





Participant
February 10, 2009
Thanks Matt, I hadn't found that page in the wiki and it's certainly been useful. I've managed to wire in my own extra compiler extension with asc.addCompilerExtension and it's working.

I have a follow-up though which I haven't managed to figure out: is there a way to add a compiler extension without patching the code directly?

Ideally I'd like the base application to simply do something like this:

Application app = new Application(new File("....mxml"));
app.setOutput(new File("....swf"));
app.addCompilerExtension(myComp);
long res = app.build(true);

Or perhaps to do something similar through the configuration. Right now I have to patch ImplementationCompiler directly, which is inelegant. I see that ImplementationCompiler amd MxmlCompiler expose ways of getting to the asc instance or adding extensions to it, but they are both instantiated inside compile() which makes them unreachable.

Ideally I'd like to bring in the Flex JAR as is, and not have to monkey around with it too much. I guess there's the option of subclassing Application and doing a cut and paste of compile() just to do that, but it's not exactly very nice either!

Thanks for any help,

Robin