Skip to content

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

@tompng

Description

@tompng

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions