有谁知道如何编写 Flex 预编译器,类似于在构建过程中完成 MXML 的方式?
这是一个有点疯狂的问题,但是有人知道如何在 Flex 编译器中编写额外的编译步骤吗?这个想法是,MXML 编译器会像以前那样剔除 MXML 到 AS3 代码,但我们在 AS3 代码和字节码之间有一个额外的步骤。
我之所以问这个问题,是因为这将是处理元数据标签之类的事情以进行代码更改的重要一步。许多框架中的常见做法是在变量之前添加 [Inject] 标签。即[注入] public var user:IUser。
如果注入标签可以被识别、以某种方式解释,然后硬连线到单例,或者其他什么,那就真的非常酷了。例如,您可以有一个配置文件,将 IUser 定义为 User 对象。编译器会解释这一点,并将 [Inject] public var user:IUser 替换为 public var user:IUser=UserConfig.instance.user;
如果我知道如何执行这一步,这只是可能实现的众多事情之一。我只是想知道是否有人知道从哪里开始如何去做这件事。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该编译器是开源的,代码库中有很多示例,展示了编译器如何在编译时修改抽象语法树(源代码解析为的内容)。
我建议查看 [Embed] 或 [SkinPart] 扩展。 (我之前已经通过示例链接讨论过这个问题此处。)
但请注意 -这个东西不适合胆小的人。如果您对编译器及其工作原理不熟悉,我强烈推荐这本书关于 ANTLR,它全面概述了将源代码解析为 AST 的过程以及如何解释它们。
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.
这是一个广泛的问题,但我可以为您的具体情况找到答案。
您需要编写接受字节码作为输入并输出字节码的代码:
[Injected]
元数据的特征。UserConfig.instance.user
的字节码,要集成到您的构建过程中,您需要添加一个在 Flex 之后运行的新 Program 构建器(假设您使用的是 Flash Builder)。
另外两种方法是:
This is a broad question, but I can think of an answer for your specific case.
You would need to write code that accepts bytecode as input, and outputs bytecode:
[Injected]
metadata.UserConfig.instance.user
To integrate into your build process, you add a new Program builder (assuming you're using Flash Builder) that runs after Flex.
The other 2 ways would be: