JuliaLang / JuliaSyntax.jl

The Julia compiler frontend
Other
266 stars 32 forks source link

Allow whitespace in macros wrapped by parenthesis #436

Open MasonProtter opened 1 month ago

MasonProtter commented 1 month ago

Copying from https://github.com/JuliaLang/julia/issues/52842:

Minimal example:

 macro var"let"(bindings, body)
    bindings.head == :tuple || error("malformed let bindings $bindings")
    bindings.head = :block
    esc(Expr(:let, bindings, body))
end
julia> (@let (x=1, y=2) (x + y))
3

julia> (@let (x=1, y=2) 
           (x + y))
ERROR: ParseError:
# Error @ REPL[14]:1:17
(@let (x=1, y=2) 
#               └ ── whitespace is not allowed here
Stacktrace:
 [1] top-level scope
   @ none:1

Kinda a weird case, but I was thinking about how it'd be nice to be able to use the form (@foo x y) instead of @foo(x, y) to avoid having to write commas, but currently the parser demands that there's no whitespace in the middle of a macro arguments.

Maybe a more realistic example of where one might want to do this would be

julia> (@inline
       function foo(x)
           x + 1
       end)
ERROR: ParseError:
# Error @ REPL[16]:2:1
(@inline
function foo(x)
└────────────┘ ── Expected `)`
Stacktrace:
 [1] top-level scope
   @ none:1

Is this something we could reasonably allow? I guess the worry is that it could alternatively be interpreted as (@foo(x); y) instead of (@foo(x, y)), but I think the parsing as (@foo(x, y)) is more consistent with how we handle infix operators, e.g.

julia> (1
       +2)
3