Code file generators examples

Hello, has anyone generated code files?
Please give examples of packages.
At first I tried on the basis of strings, then I began to create structures-models of code blocks, which is why I decided that it was better to use expressions right away. Now I can’t just add comments to the generated code and I don’t like the style of the generated code:


julia> (: (foo (a) = a + 2)) |> Base.remove_linenums! |> println

foo (a) = begin
        a + 2
    end
 
# instead of  `foo (a) = a + 2`

julia> Meta.parse ("""
           \"\"\"
               documentation
           \"\"\"
           function test_fun ()
               return nothing
           end
       """) |> Base.remove_linenums! |> String |> println

#= none: 1 =# Core.@doc "documentation \n" function test_fun()
        return nothing
    end

Julia’s Expr does not preserve the syntactic structure of code, you may be interested in CSTParsers.jl which is more faithful to source code.

1 Like