Expronicon.jl
Expronicon.jl copied to clipboard
Using julia keywords as type fields
This is a corner case but it can come up when you're generating code from a language that's end not Julia, such as GraphQL and a field is called "end".
julia> JLKwStruct(;name=:ABC, fields = [JLKwField(name=:end)])
begin
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:101 =#
struct ABC
end
end
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:102 =#
begin
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:156 =#
function ABC(; end)
ABC(end)
end
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:157 =#
nothing
end
end
This will be unable to be parsed but it can work if we replace uses of end with the following:
julia> JLKwStruct(;name=:ABC, fields = [JLKwField(name=:end)])
begin
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:101 =#
struct ABC
var"end"
end
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:102 =#
begin
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:156 =#
function ABC(; var"end")
ABC(var"end")
end
#= /home/dom/.julia/packages/Expronicon/trhCs/src/codegen.jl:157 =#
nothing
end
end
so I guess the idea is to detect Julia keywords and wrap them with @var_str.
It doesn't look like this can be done on input but maybe during codegen?
actually this would be done during printing not codegen
I think our current printing is not always correct since it kinda just manually handles the expression heads in a @switch
stmts instead from a legit parser.
And I think the above is correct because you can just do the same thing in Julia Expr
, e.g
julia> print(ex)
struct Foo
#= REPL[7]:2 =#
end
end
julia> dump(ex)
Expr
head: Symbol struct
args: Array{Any}((3,))
1: Bool false
2: Symbol Foo
3: Expr
head: Symbol block
args: Array{Any}((2,))
1: LineNumberNode
line: Int64 2
file: Symbol REPL[7]
2: Symbol end
and
julia> :(struct Foo; var"end" end)
:(struct Foo
#= REPL[20]:1 =#
end
end)
So what is missing is actually a validator of Julia AST, if we hope to have something that works less specifically, which can be very hard since there hasn't been any form of formal definition of Julia grammar...
so what I can think of at the moment is a simpler validator that checks Julia keyword and error when user creating things like JLField
/JLKwField
Ah I think I misinterpreted your point, yeah the printing is wrong, it should be var"end"
, would need to handle this as a specific case for Symbol
: https://github.com/Roger-luo/Expronicon.jl/blob/master/src/printing.jl#L314
it’s pretty tricky though since you only want to do this for certain cases and also have it propagate throughout the function body for functions as well.
For my specific case i’m adding an underscore to the front and then adding this case to generated get/set property functions. GraphQL doesn’t allow underscore in the front of names so this works out
On Tuesday, March 29, 2022, Rogerluo @.***> wrote:
Ah I think I misinterpreted your point, yeah the printing is wrong, it should be var"end", would need to handle this as a specific case for Symbol: https://github.com/Roger-luo/Expronicon.jl/blob/master/src/ printing.jl#L314
— Reply to this email directly, view it on GitHub https://github.com/Roger-luo/Expronicon.jl/issues/22#issuecomment-1082431242, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAORUF7MGQ2QIOSC6FND7RTVCN72DANCNFSM5R7L42JQ . You are receiving this because you authored the thread.Message ID: @.***>
I think for keyword we can just print them as var? Since they won't appear in the args field of expressions anyway