生成 PHP 代码(从解析器令牌)
是否有任何可用的解决方案用于从 解析器令牌(重新)生成 PHP 代码由 token_get_all
返回?也欢迎其他用于生成 PHP 代码的解决方案,最好带有相关的词法分析器/解析器(如果有)。
Is there any available solution for (re-)generating PHP code from the Parser Tokens returned by token_get_all
? Other solutions for generating PHP code are welcome as well, preferably with the associated lexer/parser (if any).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在“其他解决方案”类别中,您可以尝试PHP Parser。
In the category of "other solutions", you could try PHP Parser.
从我的评论来看:
经过一番寻找,我在 this 问题,实际上使用了 PHP Tokenizer 接口,以及一些更可配置的 PHP 代码格式化工具(但需要如上所述的解决方案)。
这些可用于快速实现解决方案。当我有时间做这个时,我会发回这里。
PHP_Beautifier 的解决方案
这是我制定的快速解决方案,我将把它留在这里作为问题的一部分。请注意,它要求您通过将 private 更改为 protected 的所有内容(可能不是所有内容,但这更容易)来打开 PHP_Beautifier 类,以允许您实际上使用 PHP_Beautifier 的内部工作原理(否则不可能在不重新实现一半代码的情况下重用 PHP_Beautifier 的功能)。
该类的示例用法如下:
file: main.php
由于 PHP2PHP 扩展了 PHP_Beautifier,它允许在 PHP_Beautifier 使用的同一 API 下进行相同的微调。该类本身是:
文件:PHP2PHP.php
From my comment:
After some looking, I found a PHP homemade solution in this question, that actually uses the PHP Tokenizer interface, as well as some PHP code formatting tools which are more configurable (but would require the solution as described above).
These could be used to quickly realize a solution. I'll post back here when I find some time to cook this up.
Solution with PHP_Beautifier
This is the quick solution I cooked up, I'll leave it here as part of the question. Note that it requires you to break open the PHP_Beautifier class, by changing everything (probably not everything, but this is easier) that is private to protected, to allow you to actually use the internal workings of PHP_Beautifier (otherwise it was impossible to reuse the functionality of PHP_Beautifier without reimplementing half their code).
An example usage of the class would be:
file: main.php
As PHP2PHP extends PHP_Beautifier, it allows for the same fine-tuning under the same API that PHP_Beautifier uses. The class itself is:
file: PHP2PHP.php
如果我没记错的话 http://pear.php.net/package/PHP_Beautifier 使用 token_get_all (),然后重写流。它使用诸如 t_else 和 t_close_brace 之类的大量方法来输出每个标记。也许您可以为了简单起见而劫持它。
If I'm not mistaken http://pear.php.net/package/PHP_Beautifier uses token_get_all() and then rewrites the stream. It uses heaps of methods like
t_else
andt_close_brace
to output each token. Maybe you can hijack this for simplicity.请参阅我们的 PHP 前端。它是一个完整的 PHP 解析器,自动构建 AST,以及一个匹配的漂亮打印机,可以重新生成带有原始注释的可编译 PHP 代码。 (编辑 12/2011:
请参阅此 SO 答案,了解有关从 AST 进行漂亮打印所需的更多详细信息,AST 只是令牌的组织版本:https:// stackoverflow.com/a/5834775/120163)
前端构建在我们的 DMS 软件再工程工具包,支持 PHP AST 的分析和转换(然后通过 PrettyPrinter 代码)。
See our PHP Front End. It is a full PHP parser, automatically building ASTs, and a matching prettyprinter that regenerates compilable PHP code complete with the original commments. (EDIT 12/2011:
See this SO answer for more details on what it takes to prettyprint from ASTs, which are just an organized version of the tokens: https://stackoverflow.com/a/5834775/120163)
The front end is built on top of our DMS Software Reengineering Toolkit, enabling the analysis and transformation of PHP ASTs (and then via the prettyprinter code).