hylo
hylo copied to clipboard
Parser gets confused when first token of a subscript is `sink`
This should parse but doesn't:
public subscript unsafe_bitcast<T, U>(_ value: T): U {
sink let p = Pointer.to[value].copy().value
yield p as* (remote let U)
}
I think I understand what is happening, but I don't know how to fix it.
A subscript implementation is a combination of implIntroducer and maybe functionBody.
https://github.com/hylo-lang/hylo/blob/5b773b79f9ea009c7480625c3b8021cb74817dc9/Sources/FrontEnd/Parse/Parser.swift#L1218
The implIntroducer consumes the sink, but actually it should be let because the sink let is a BindingPattern.Introducer in the function body.(?)
https://github.com/hylo-lang/hylo/blob/5b773b79f9ea009c7480625c3b8021cb74817dc9/Sources/FrontEnd/Parse/Parser.swift#L1165-L1170
https://github.com/hylo-lang/hylo/blob/5b773b79f9ea009c7480625c3b8021cb74817dc9/Sources/FrontEnd/Parse/Parser.swift#L2638-L2641
This this close? Any hints about how to fix this?
Perhaps use state.peek(2) to read the next two tokens. If it's .sink and .let, then implIntroducer should return let. I might be way off base. 😅
I think the problem also occurs when the first token in the body of a subscript bundle is let or inout. I'm not sure that increasing lookahead will catch all cases (we need more rigorous testing for the parser) but that might be worth a try. A more expensive solution would be to backtrack.