CSTParser.jl icon indicating copy to clipboard operation
CSTParser.jl copied to clipboard

convert Expr to CST

Open Roger-luo opened this issue 4 years ago • 3 comments

Hi, I come accross from https://github.com/domluna/JuliaFormatter.jl/issues/414

I'm thinking about using JuliaFormatter as the pretty printer of Julia Expr objects, thus I need to convert things to CST objects it uses, I'm wondering if there is an API for this? or if there are related works on this?

Roger-luo avatar May 23 '21 21:05 Roger-luo

This definitely isn't possible at the moment. While a CSTParser.EXPR has a head and args like a normal Expr and is easily convertible to the latter, it does have additional info that can't be recovered from the AST. It should be possible to work around that for your case (where you never had any textual representation to begin with), but seems like a fair bit of work.

pfitzseb avatar May 25 '21 09:05 pfitzseb

I guess one workaround could be print Expr to string then parse it back using CSTParser ? sounds evil but might work I guess.

Roger-luo avatar May 25 '21 22:05 Roger-luo

The text representation won't be lossless but I think most of the time it would give you something you could be happy with.

domluna avatar May 26 '21 03:05 domluna