Search code examples
actionscript-3apache-flexcompiler-constructionmxmlmxmlc

Does anyone know how to go about writing a Flex precompiler, similar to the way MXML is done into your build process?


This is a bit of a crazy question, but does anyone out there know how to go about writing an extra compile step into the flex compiler. The idea would be that the MXML compiler would knock out the MXML to AS3 code as it does, but we have an extra step between the AS3 code and the bytecode.

The reason why I ask, is that this would be a great step to handle things like metadata tags to make code changes. A common practice in a lot of frameworks is to have an [Inject] tag before a variable. I.e. [Inject] public var user:IUser.

What would be really, really cool, is if the inject tag could be recognised, interpreted in some way, and then hardwired to a singleton, or whatever. For example, you could have a config file that defines IUser to be a User object. The compiler would interpret that, and replace the [Inject] public var user:IUser with public var user:IUser=UserConfig.instance.user;

This is jsut one of the many things that would be possible if I knew how to do this step. I was just wondering if someone would know even where to start about how to go about doing this.


Solution

  • The compiler is open source, and there's lots of examples in the codebase the show how the compiler modifies the Abstract Syntax Tree (which is what the source code gets parsed to) at compile time.

    I'd suggest taking a look at the [Embed] or [SkinPart] extensions. (I've discussed this before with links to examples here.)

    Be warned though - this stuff is not for the faint of heart. If you're new to compilers, and how they work, I highly recommend this book on ANTLR, which gives a thorough overview on the process of parsing Source Code to AST's and then how they are interpreted.