I need to find a db block in a module and copy it entirely to a new file.
So, I basically need a way to pass a module's code to a macro where I'll iterate over its nodes to find the necessary one.
So far, I haven't found a way do so. Naïve attempts like this one don't work, since I'm getting the tree of the include statement and not the included module:
dumpTree:
include anothermodule.nim
Of course, I could always treat the module as a text file and just look for patterns, but this looks like a poor solution. There must be a better way.
Try this.
import macros
template tt() : untyped =
include anothermodule
macro t(a : typed) : untyped =
echo treeRepr a
t(tt())
Now I'm even more confused.
Tried this simple code:
import macros
macro foo(body: untyped): untyped =
echo treeRepr body
foo:
var x = 123
This won't print anything to stdout. I could swear it should print the tree representation of var x = 123.
The proposed solution works even without the intermediate template. If the arg type is typed then the content of the included file is passed to the macro and not the include statement.
However, there's still a problem with it. If the included module contains macros, they are expanded before being passed to the macro. So I can't examine the macros declared in the included module, only the code generated from them.
And for my specific task, I do need to find a particular macro block.
I couldn't find anything like rawInclude or literalInclude. Maybe anyone could point to the right thing?
I use let fileAST = parseStmt(slurp(file)) with file being a static string path to the file.
Thanks! This is exactly what I was looking for.
I wish there was a way to mark the thread resolved.