MasonProtter / ReplMaker.jl

Simple API for building repl modes in Julia
Other
134 stars 14 forks source link

raw string interpolation/unescaping? #39

Open anandijain opened 1 year ago

anandijain commented 1 year ago

Sorry for the spam.

It looks like REPL modes give raw strings to the input function. I'm making a ChatGPT REPL mode https://github.com/anandijain/OpenAIReplMode.jl and I think it would be really nice to be able to say

julia> x = "world"
chatgpt> hello $x

and have the string somehow get reinterpolated/unescaped?

Any recommendations for this?

jakobjpeters commented 10 months ago

This problem is equivalent to string-interpolation. So far, I'm only aware of being able to do it using Meta.parse (which would not work in this case) or writing your own parser. You'd return an expression like this:

julia> Meta.@dump "interpolate $x and $(y + 1)"
Expr
  head: Symbol string
  args: Array{Any}((4,))
    1: String "interpolate "
    2: Symbol x
    3: String " and "
    4: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol +
        2: Symbol y
        3: Int64 1