Skip to content

Commit 7a7f75f

Browse files
authored
Use Prism::ParseResult#continuable? if possible (#1184)
1 parent 8a56ad0 commit 7a7f75f

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

lib/irb/ruby-lex.rb

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,22 @@ def should_continue?(tokens, line, line_num)
135135

136136
def check_code_syntax(code, local_variables:)
137137
result = Prism.lex(code, scopes: [local_variables])
138-
return :valid if result.success?
138+
if result.success?
139+
:valid
140+
elsif result.respond_to?(:continuable?)
141+
result.continuable? ? :recoverable_error : :unrecoverable_error
142+
else # For Prism <= 1.9.0. Drop this branch when IRB requires Prism >= 1.10.0.
143+
check_syntax_error_heuristics(result)
144+
end
145+
end
146+
147+
# Prism <= 1.9.0 does not have `ParseResult#continuable?` method.
148+
# Fallback to legacy heuristics based on error messages and error locations.
149+
def check_syntax_error_heuristics(prism_parse_result)
139150

140151
# Get the token excluding trailing comments and newlines
141152
# to compare error location with the last or second-last meaningful token location
142-
tokens = result.value.map(&:first)
153+
tokens = prism_parse_result.value.map(&:first)
143154
until tokens.empty?
144155
case tokens.last.type
145156
when :COMMENT, :NEWLINE, :IGNORED_NEWLINE, :EMBDOC_BEGIN, :EMBDOC_LINE, :EMBDOC_END, :EOF
@@ -150,7 +161,7 @@ def check_code_syntax(code, local_variables:)
150161
end
151162

152163
unknown = false
153-
result.errors.each do |error|
164+
prism_parse_result.errors.each do |error|
154165
case error.message
155166
when /unexpected character literal|incomplete expression at|unexpected .%.|too short escape sequence/i
156167
# Ignore these errors. Likely to appear only at the end of code.

0 commit comments

Comments
 (0)