Hoping this is trivial, but let’s see. The task is to extract documentation from a parsed struct definition using MacroTools. I also checked that splitstructdef
fails in this case.
using MacroTools
sexpr = Meta.parse("""
"You can't extract me na-na-na-na!"
struct Foo
x
end
""")
function getdocs(ex::Expr)
if @capture(ex, @doc str_ struct name_
body__
end)
return str
end
end
@show getdocs(sexpr) # getdocs(sexpr) = nothing
If I look at dump(sexpr)
I can see that the structure changes depending on how I annotate the struct. The above leaves a GlobalRef
to Core.@doc
, which I don’t know how to pattern match. If I annotate using @doc
as in the pattern, I can get the docstring out successfully.
Any ideas? My end goal is to parse a bunch of strings that may contain structs and docstrings, and group them by identical struct definitions (fields + name).