Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow whitespace in macros wrapped by parenthesis #436

Open
MasonProtter opened this issue Jun 1, 2024 · 1 comment
Open

Allow whitespace in macros wrapped by parenthesis #436

MasonProtter opened this issue Jun 1, 2024 · 1 comment

Comments

@MasonProtter
Copy link

Copying from JuliaLang/julia#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
@c42f
Copy link
Member

c42f commented Jul 26, 2024

Did you have a PR for this somewhere?

As we discussed at JuliaCon, I feel we can probably make this work (despite the issues you ran into). It "probably just" involves some subtle handling of ParseState flags.

Complicated macro names like (A.B.@x y z) also need to work. And the whitespace rules which you need for this need to be disabled when entering a further context. Like

(@x
an_x_arg
begin
    @y
    not_a_y_arg
end
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants