JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.79k stars 5.49k forks source link

parsing getindex (or `:ref`) with only keyword arguments (`:parameters`) #33328

Open Jutho opened 5 years ago

Jutho commented 5 years ago

I noticed that the following does not parse:

julia> Expr(:ref, :a, Expr(:parameters, :x, :y))
:(a[; x, y])
julia>:(a[; x, y])

ERROR: syntax: unexpected ";"

For a regular function call, there is no problem

julia> Expr(:call, :a, Expr(:parameters, :x, :y))
:(a(; x, y))
julia> :(a(; x, y))
:(a(; x, y))

Arises in the context of experimenting with DSLs (with macros) for tensors

KristofferC commented 5 years ago

Ref https://github.com/JuliaLang/julia/issues/25631

c42f commented 5 years ago

The place this gets tricky is in combination with typed_vcat syntax which also uses the semicolon. So we're toying with syntactic inconsistency if we have T[; x] mean something completely different from T[y; x], which already means Expr(:typed_vcat, :T, :y, :x).

Having said that, we're not very consistent at the moment. We have:

julia> dump(:(x[y; a=1]))
Expr
  head: Symbol typed_vcat
  args: Array{Any}((3,))
    1: Symbol x
    2: Symbol y
    3: Expr
      head: Symbol =
      args: Array{Any}((2,))
        1: Symbol a
        2: Int64 1

which parses but turns into a lowering error unless you translate it via a macro. And that's inconsistent with the following which parses to contain a :parameters Expr...

julia> dump(:(x[y,z; a=1]))
Expr
  head: Symbol ref
  args: Array{Any}((4,))
    1: Symbol x
    2: Expr
      head: Symbol parameters
      args: Array{Any}((1,))
        1: Expr
          head: Symbol =
          args: Array{Any}((2,))
            1: Symbol a
            2: Int64 1
    3: Symbol y
    4: Symbol z
Jutho commented 5 years ago

For my use case, I don't quite care how it is parsed, just that it parses such that it is valid input within my macro context, and I can recognise the expression and transform it.