macros - nim - 自定义宏/编译指示获取完整模块但得到“无法附加自定义编译指示”
问题描述
我想使用 nim 访问完整模块(文件)的 AST。我发现,任何宏都可以用作自定义编译指示,所以我在文件 foo.nim 中做了这样的事情:
import macros
macro getAst(ast: untyped): untyped =
echo "ast = ", treeRepr(ast)
{.getAst.} # <-- This should invoke the getAst() macro with the AST of the file
proc hello() =
echo "hello world"
但我得到编译器错误cannot attach a custom pragma to 'foo'
有没有办法做到这一点?我发现我可以像这样将宏用作块宏,但我真的更喜欢通过编译指示使用它。
getAst:
proc hello() =
echo "hello world"
解决方案
无法将自定义编译指示附加到文件,但您可以这样做
proc hello() {.getAst.} =
echo "hello world"
将编译指示附加到 proc。我不确定,但似乎{.push.}
不适用于宏,仅适用于template
编译指示,如下所示:
template dbIgnore {.pragma.}
{.push dbIgnore.}
所以你最好的选择是用 pragma 注释你想要的所有 procs。
推荐阅读
- asp.net-mvc - Azure AD OWIN 和 OpenID Connect 身份验证问题 IDX21323
- arrays - Swift - 基于另一个称为查询的数组有效地查询一个整数数组,该数组具有第一个数组的索引
- reactjs - 反应钩子,不能通过道具更新状态
- python - 导入错误 python 模块
- php - 我正在尝试在 laravel 中安装一个包
- javascript - 在 Angular js 中如何使用路由(控制器和方法名称)
- mysql - Mysql Now() 不工作 phpmyadmin
- sql - SAS - PROC SQL - 算术没有意义的情况
- reactjs - Redux 存储不创建
- amazon-web-services - AWS 访问密钥轮换