prism icon indicating copy to clipboard operation
prism copied to clipboard

Feature request for an API to check if a syntax invalid code can be syntax valid by appending continuation tokens

Open tompng opened this issue 8 months ago • 3 comments

It would be helpful if Prism have an API that works like this.

Prism.parse('1 + [').input_continuable? #=> true
Prism.parse('1 + ]').input_continuable? #=> false
Prism.parse('tap do').input_continuable? #=> true
Prism.parse('end.tap do').input_continuable? #=> false

Some other naming idea: end_of_input_error? error_recoverable?

Background

When IRB receives end[ENTER], IRB immediately evaluates it because "end\n" + code is always syntax invalid.

irb(main):001> end
(irb):1: syntax error found (SyntaxError)

When IRB receives tap do[ENTER], IRB waits for the next line input because "tap do\n" + code can be syntax valid.

irb(main):001> tap do
irb(main):002>

Both end and tap do are syntax invalid code. IRB needs to distinguish these two type of syntax errors. IRB currently uses SyntaxError#message and regexps. This check logic is fragile. Implementation in IRB: https://github.com/ruby/irb/blob/8b63f05160aa90e0205cf392b10cb85d6ecfa9a3/lib/irb/ruby-lex.rb#L229-L276

tompng avatar Mar 30 '25 14:03 tompng

If you get:

def foo
  a +
end

tap do

right now it would match :recoverable_error. What is the desired behavior in this case? You would need to go back in and put content after the a + to make it valid. Should that not be recoverable?

kddnewton avatar Aug 19 '25 15:08 kddnewton

It's currently :recoverable_error but I want it to be :unrecoverable_error. Moving cursor back and input something is needed, it should be :unrecoverable_error. Appending lines at the end can make it syntax valid, it's :recoverable_error. It would be happy if Prism support this detection because implementing this in IRB is hard, fragile and buggy, just like the above example.

By the way, is there an idea to implement this in IRB without using SyntaxError#message?

tompng avatar Aug 21 '25 16:08 tompng

Yes, I would like to support this API. I assume it would be on the Prism::ParseResult class, and it would be something like ParseResult#recoverable? or something to that effect.

kddnewton avatar Aug 21 '25 16:08 kddnewton