If I have a string of a function how can I parse the types from it?
str = """
foo(
bar::Bar, baz::AbstractVector{Baz},
var::Dict{Symbol,Var}=d, zar::Dict{Symbol,Zar} = k,
rar::Dict{Symbol,Rar},
rar::Dict{Symbol,Rar}
)
"""
With this one the match is until the first colon it finds, which is incorrect for the Dict{Symbol,Type}
r"::(.+?)[,| =|=]"
Is there a package that already does this?
You can use Julia’s parser ex = Meta.parse(str)
and then inspect the returned expression ex
.
Why do you have a string like this in the first place though? Since this is not something you usually need to to when writing Julia code, it sounds a bit like an XY problem, and perhaps there is a better way to solve the actual problem.
1 Like
It is for generating a doc string:
(arg_name, arg_type, is_splat, default) = MacroTools.splitarg(arg)
push!(args, string(arg_name))
push!(argtypes, string(arg_type))
end
args, argtypes
end
function build_docstring(fundef, argnames, argtypes)
if options[:full_def]
if options[:arg_types_in_desc] && !isnothing(argtypes)
fundef = replace(fundef, r"(::.+?)([,|=| =])" => s"\2")
end
str = "\"\"\"\n $fundef\n\nDOCSTRING\n"
else
funname = split(fundef, '(')[1]
argstring = replace(string((string.(argnames)...,)), '"'=>"")
str = "\"\"\"\n $(funname)$(argstring)\n\nDOCSTRING\n"
end
if !isempty(argnames) && length(argnames) >= options[:min_args]
str = string(str, "\n$(options[:args_header])\n")
for (i, argname) in enumerate(argnames)
and optionally removing the arg types from the doc string.