How does the string pkg"..." work?

What magic is involved when I write

macro pkg_str(s)
    return esc(s)
end
@info pkg"Top of the morning to you, squire!"

Does the macro compiler “know” that this means “call @pkg_str when you see pkg as a string prefix”?

I think from here https://github.com/JuliaLang/julia/blob/bbbcc4fed67337c038b485f00677e2e88d24becd/src/julia-parser.scm#L1293-L1298 it just knows how to parse it correctly, using https://github.com/JuliaLang/julia/blob/bbbcc4fed67337c038b485f00677e2e88d24becd/src/julia-parser.scm#L1180-L1183

1 Like

Thanks, Daniel. Did I miss this in the Julia manual? Or is it there somewhere?

1 Like

I’m not sure if it’s specifically mentioned in the docs. The only place I can see it is here Julia ASTs · The Julia Language which technically shows the parsing I guess.

2 Likes

Cool, you’re a :mage:

1 Like

Here’s where JuliaSyntax.jl parses makes macro calls out of literals, since it seems it’ll be in 1.10+: https://github.com/JuliaLang/JuliaSyntax.jl/blob/main/src/literal_parsing.jl#L429-L434. The handling of the input string as raw or standard seems to come in a subsequent parse_julia_literal call.

See the manual section on Non-Standard String Literals

4 Likes