Project

General

Profile

Actions

Feature #19013

closed

Error Tolerant Parser

Feature #19013: Error Tolerant Parser

Added by yui-knk (Kaneko Yuichiro) about 3 years ago. Updated about 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
[ruby-core:109977]

Description

Background

Implementation for Language Server Protocol (LSP) sometimes needs to parse incomplete ruby script for example users want to complement expressions in the middle of statement like below:

class A
  def m
    a = 10
    if # here users want to run completion
  end
end

In such case, LSP implementation wants to get partial AST instead of syntax error.

Proposal

At the moment I want to propose 3 types of tolerance

1. Complement end when lexer hits to end-of-input but end is not enough

This is a case. Lexer will generate 1 end before generates end-of-input.

describe "1" do
  describe "2" do
    describe "3" do
      it "here" do
    end
  end
end

2. Extract "end" as keyword not identifier based on an indent

This is a case. Normal parser recognizes "end" on line 4 as "local variable or method".
This causes not only syntax error but also bar method definition is assumed as Z::Foo#bar.
Other approach is suppress !IS_lex_state(EXPR_DOT) checks for "end".

module Z
  class Foo
    foo.
  end

  def bar
  end
end

3. Change locations of error

Currently error is put into top_stmts and stmts like top_stmts: error top_stmt and stmts: error stmt.
However these are too strict to catch syntax error then want to move it to stmt: error and expr_value: error.