JuliaLang / JuliaSyntax.jl

The Julia compiler frontend
Other
273 stars 33 forks source link

Weird precedence of unary operators, `where` and syntactic unaries #248

Open c42f opened 1 year ago

c42f commented 1 year ago

The precedence of type operators <: is meant to be lower than where as handled with a special rule parse_unary_subtype (see https://github.com/JuliaLang/julia/pull/21567).

But the way this is implemented in the reference parser (and copied here) the precedence doesn't work when chained with normal unary ops. This happens because parse_unary is still involved in parsing chained unary type operators.

julia> parsestmt(SyntaxNode, "<: A where B")
line:col│ tree                                   │ file_name
   1:1  │[<:-pre]
   1:3  │  [where]
   1:4  │    A
   1:12 │    B
julia> parsestmt(SyntaxNode, "+ <: A where B")
line:col│ tree                                   │ file_name
   1:1  │[where]
   1:1  │  [call-pre]
   1:1  │    +
   1:2  │    [<:-pre]
   1:6  │      A
   1:14 │  B

Fixing this isn't super simple - if we call parse_unary_subtype from within parse_unary, we'll break the precedence of juxtaposition which is interposed between these two. For example, we must have "√2i" parse as (juxtapose (call-pre √ 2) i).

c42f commented 1 year ago

@JeffBezanson do you think there's a way out of this conundrum?