How to remove comments from a Julia file?

I am processing some Julia code with regexes, to convert the colorschemes in Colorschemes.jl to go objects. I want to remove the comments before feeding the Julia code to my zsh script. What are the options?

PS: For the record, this code set is small enough that I can hardcode the problem away, but I’m curious for future reference.

In the general case, you’ll have to parse the file - simple regexes can grow very complicated once you try to take care of text that looks like a comment inside a string literal.

Rather than using regexes, you might consider simply using Julia’s parser, which gives you a syntax tree that you can walk to output anything you want. This will be a lot more robust than processing code as strings.

1 Like

In case you want to export them all from Julia, it might be simpler to just do this:

using ColorSchemes
for cs in ColorSchemes.colorschemes
    println(first(cs))
    for color in last(cs).colors
        println(color)
    end
end

=> ...
seaborn_bright
RGB{Float64}(0.00784313725490196,0.24313725490196078,1.0)
RGB{Float64}(1.0,0.48627450980392156,0.0)
RGB{Float64}(0.10196078431372549,0.788235294117647,0.2196078431372549)
RGB{Float64}(0.9098039215686274,0.0,0.043137254901960784)
RGB{Float64}(0.5450980392156862,0.16862745098039217,0.8862745098039215)
RGB{Float64}(0.6235294117647059,0.2823529411764706,0.0)
RGB{Float64}(0.9450980392156862,0.2980392156862745,0.7568627450980392)
RGB{Float64}(0.6392156862745098,0.6392156862745098,0.6392156862745098)
RGB{Float64}(1.0,0.7686274509803922,0.0)
RGB{Float64}(0.0,0.8431372549019608,1.0)

....

but parsing Julia code is obviously more fun! :slight_smile:

3 Likes