# frozen_string_literal: false
# irb/ruby-lex.rb - ruby lexcal analyzer
# $Release Version: 0.9.6$
# by Keiju ISHITSUKA(keiju@ruby-lang.org)
require "jruby" if RUBY_ENGINE == "jruby"
class TerminateLineInput < StandardError
super("Terminate Line Input")
@exp_line_no = @line_no = 1
def self.compile_with_errors_suppressed(code)
result = yield code, line_no
result = yield code, line_no
def set_input(io, p = nil, &block)
if @io.respond_to?(:check_termination)
@io.check_termination do |code|
code.gsub!(/\s*\z/, '').concat("\n")
ltype, indent, continue, code_block_open = check_state(code)
if ltype or indent > 0 or continue or code_block_open
if @io.respond_to?(:dynamic_prompt)
@io.dynamic_prompt do |lines|
lines << '' if lines.empty?
c = lines[0..i].map{ |l| l + "\n" }.join
ltype, indent, continue, code_block_open = check_state(c)
result << @prompt.call(ltype, indent, continue || code_block_open, @line_no + i)
@input = Proc.new{@io.gets}
def set_prompt(p = nil, &block)
p = block if block_given?
@prompt = Proc.new{print p}
def ripper_lex_without_warning(code)
verbose, $VERBOSE = $VERBOSE, nil
self.class.compile_with_errors_suppressed(code) do |inner_code, line_no|
tokens = Ripper.lex(inner_code, '-', line_no)
def set_auto_indent(context)
if @io.respond_to?(:auto_indent) and context.auto_indent_mode
@io.auto_indent do |lines, line_index, byte_pointer, is_newline|
md = lines[line_index - 1].match(/(\A +)/)
prev_spaces = md.nil? ? 0 : md[1].count(' ')
@tokens = ripper_lex_without_warning(lines[0..line_index].join("\n"))
depth_difference = check_newline_depth_difference
prev_spaces + depth_difference * 2
code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join
last_line = lines[line_index]&.byteslice(0, byte_pointer)
code += last_line if last_line
@tokens = ripper_lex_without_warning(code)
corresponding_token_depth = check_corresponding_token_depth
if corresponding_token_depth
corresponding_token_depth
@tokens = ripper_lex_without_warning(code)
ltype = process_literal_type
indent = process_nesting_level
continue = process_continue
code_block_open = check_code_block(code)
[ltype, indent, continue, code_block_open]
@prompt.call(@ltype, @indent, @continue, @line_no)
def each_top_level_statement
throw :TERM_INPUT if @line == ''
@line_no += l.count("\n")
if @code_block_open or @ltype or @continue or @indent > 0
@line.force_encoding(@io.encoding)
yield @line, @exp_line_no
rescue TerminateLineInput
if @io.respond_to?(:check_termination)
code = @line + (line.nil? ? '' : line)
code.gsub!(/\s*\z/, '').concat("\n")
@tokens = ripper_lex_without_warning(code)
@continue = process_continue
@code_block_open = check_code_block(code)
@indent = process_nesting_level
@ltype = process_literal_type
# last token is always newline
if @tokens.size >= 2 and @tokens[-2][1] == :on_regexp_end
elsif @tokens.size >= 2 and @tokens[-2][1] == :on_semicolon
elsif @tokens.size >= 2 and @tokens[-2][1] == :on_kw and ['begin', 'else', 'ensure'].include?(@tokens[-2][2])
elsif !@tokens.empty? and @tokens.last[2] == "\\\n"
elsif @tokens.size >= 1 and @tokens[-1][1] == :on_heredoc_end # "EOH\n"
elsif @tokens.size >= 2 and defined?(Ripper::EXPR_BEG) and @tokens[-2][3].anybits?(Ripper::EXPR_BEG | Ripper::EXPR_FNAME)
# end of literal except for regexp
def check_code_block(code)
return true if @tokens.empty?
if @tokens.last[1] == :on_heredoc_beg
begin # check if parser error are available
verbose, $VERBOSE = $VERBOSE, nil
self.class.compile_with_errors_suppressed(code) do |inner_code, line_no|
RubyVM::InstructionSequence.compile(inner_code, nil, nil, line_no)
# This is for a hash with invalid encoding symbol, {"\xAE": 1}
when /unterminated (?:string|regexp) meets end of file/
# "unterminated regexp meets end of file"
# "unterminated string meets end of file"
when /syntax error, unexpected end-of-input/
# "syntax error, unexpected end-of-input, expecting keyword_end"
when /syntax error, unexpected keyword_end/
# "syntax error, unexpected keyword_end"
when /syntax error, unexpected '\.'/
# "syntax error, unexpected '.'"
when /unexpected tREGEXP_BEG/
# "syntax error, unexpected tREGEXP_BEG, expecting keyword_do or '{' or '('"
if defined?(Ripper::EXPR_BEG)
last_lex_state = @tokens.last[3]
if last_lex_state.allbits?(Ripper::EXPR_BEG)
elsif last_lex_state.allbits?(Ripper::EXPR_DOT)
elsif last_lex_state.allbits?(Ripper::EXPR_CLASS)
elsif last_lex_state.allbits?(Ripper::EXPR_FNAME)
elsif last_lex_state.allbits?(Ripper::EXPR_VALUE)
elsif last_lex_state.allbits?(Ripper::EXPR_ARG)
def process_nesting_level
@tokens.each_with_index { |t, index|
# detecting one-liner method definition
if t[3].allbits?(Ripper::EXPR_ENDFN)
if t[3].allbits?(Ripper::EXPR_ENDFN)
elsif t[3].allbits?(Ripper::EXPR_BEG)
elsif t[3].allbits?(Ripper::EXPR_END)
if in_oneliner_def == :BODY
# one-liner method definition
when :on_lbracket, :on_lbrace, :on_lparen, :on_tlambeg
when :on_rbracket, :on_rbrace, :on_rparen
next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME)
if index > 0 and @tokens[index - 1][3].anybits?(Ripper::EXPR_CMDARG | Ripper::EXPR_ENDFN | Ripper::EXPR_ARG)
# method_with_block do; end
# while cond do; end # also "until" or "for"
# This "do" doesn't increment indent because "while" already
when 'def', 'case', 'for', 'begin', 'class', 'module'
when 'if', 'unless', 'while', 'until'
# postfix if/unless/while/until must be Ripper::EXPR_LABEL
indent += 1 unless t[3].allbits?(Ripper::EXPR_LABEL)
# percent literals are not indented
def check_newline_depth_difference
@tokens.each_with_index do |t, index|
# detecting one-liner method definition
if t[3].allbits?(Ripper::EXPR_ENDFN)
if t[3].allbits?(Ripper::EXPR_ENDFN)
elsif t[3].allbits?(Ripper::EXPR_BEG)
elsif t[3].allbits?(Ripper::EXPR_END)
if in_oneliner_def == :BODY
# one[-liner method definition
when :on_ignored_nl, :on_nl, :on_comment
if index != (@tokens.size - 1)
when :on_lbracket, :on_lbrace, :on_lparen, :on_tlambeg
when :on_rbracket, :on_rbrace, :on_rparen
depth_difference -= 1 if open_brace_on_line > 0
next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME)
if index > 0 and @tokens[index - 1][3].anybits?(Ripper::EXPR_CMDARG | Ripper::EXPR_ENDFN | Ripper::EXPR_ARG)
# method_with_block do; end
# while cond do; end # also "until" or "for"
# This "do" doesn't increment indent because "while" already
when 'def', 'case', 'for', 'begin', 'class', 'module'
when 'if', 'unless', 'while', 'until', 'rescue'
# postfix if/unless/while/until/rescue must be Ripper::EXPR_LABEL
unless t[3].allbits?(Ripper::EXPR_LABEL)
when 'else', 'elsif', 'ensure', 'when', 'in'
def check_corresponding_token_depth
corresponding_token_depth = nil
is_first_spaces_of_line = true
is_first_printable_of_line = true
@tokens.each_with_index do |t, index|
# detecting one-liner method definition
if t[3].allbits?(Ripper::EXPR_ENDFN)
if t[3].allbits?(Ripper::EXPR_ENDFN)
elsif t[3].allbits?(Ripper::EXPR_BEG)
elsif t[3].allbits?(Ripper::EXPR_END)
if in_oneliner_def == :BODY
# one-liner method definition
if is_first_printable_of_line
corresponding_token_depth = spaces_of_nest.pop
corresponding_token_depth = nil
when :on_ignored_nl, :on_nl, :on_comment
corresponding_token_depth = nil
is_first_spaces_of_line = true
is_first_printable_of_line = true
spaces_at_line_head = t[2].count(' ') if is_first_spaces_of_line
is_first_spaces_of_line = false
when :on_lbracket, :on_lbrace, :on_lparen, :on_tlambeg
spaces_of_nest.push(spaces_at_line_head + open_brace_on_line * 2)
when :on_rbracket, :on_rbrace, :on_rparen
if is_first_printable_of_line
corresponding_token_depth = spaces_of_nest.pop
corresponding_token_depth = nil
next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME)
when 'def', 'do', 'case', 'for', 'begin', 'class', 'module'
spaces_of_nest.push(spaces_at_line_head)