diff options
Diffstat (limited to 'lib/irb/ruby-lex.rb')
-rw-r--r-- | lib/irb/ruby-lex.rb | 133 |
1 files changed, 106 insertions, 27 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 9914aece5e..35af148d02 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -106,24 +106,72 @@ class RubyLex end end + ERROR_TOKENS = [ + :on_parse_error, + :compile_error, + :on_assign_error, + :on_alias_error, + :on_class_name_error, + :on_param_error + ] + def ripper_lex_without_warning(code) verbose, $VERBOSE = $VERBOSE, nil tokens = nil self.class.compile_with_errors_suppressed(code) do |inner_code, line_no| - tokens = Ripper.lex(inner_code, '-', line_no) + lexer = Ripper::Lexer.new(inner_code, '-', line_no) + if lexer.respond_to?(:scan) # Ruby 2.7+ + tokens = [] + pos_to_index = {} + lexer.scan.each do |t| + if pos_to_index.has_key?(t[0]) + index = pos_to_index[t[0]] + found_tk = tokens[index] + if ERROR_TOKENS.include?(found_tk[1]) && !ERROR_TOKENS.include?(t[1]) + tokens[index] = t + end + else + pos_to_index[t[0]] = tokens.size + tokens << t + end + end + else + tokens = lexer.parse + end end - $VERBOSE = verbose tokens + ensure + $VERBOSE = verbose + end + + def find_prev_spaces(line_index) + return 0 if @tokens.size == 0 + md = @tokens[0][2].match(/(\A +)/) + prev_spaces = md.nil? ? 0 : md[1].count(' ') + line_count = 0 + @tokens.each_with_index do |t, i| + if t[2].include?("\n") + line_count += t[2].count("\n") + if line_count >= line_index + return prev_spaces + end + if (@tokens.size - 1) > i + md = @tokens[i + 1][2].match(/(\A +)/) + prev_spaces = md.nil? ? 0 : md[1].count(' ') + end + end + end + prev_spaces end 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| if 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")) + prev_spaces = find_prev_spaces(line_index) depth_difference = check_newline_depth_difference + depth_difference = 0 if depth_difference < 0 prev_spaces + depth_difference * 2 else code = line_index.zero? ? '' : lines[0..(line_index - 1)].map{ |l| l + "\n" }.join @@ -360,14 +408,8 @@ class RubyLex next if index > 0 and tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME) case t[2] when 'do' - if index > 0 and tokens[index - 1][3].anybits?(Ripper::EXPR_CMDARG | Ripper::EXPR_ENDFN | Ripper::EXPR_ARG) - # method_with_block do; end - indent += 1 - else - # while cond do; end # also "until" or "for" - # This "do" doesn't increment indent because "while" already - # incremented. - end + syntax_of_do = take_corresponding_syntax_to_kw_do(tokens, index) + indent += 1 if syntax_of_do == :method_calling when 'def', 'case', 'for', 'begin', 'class', 'module' indent += 1 when 'if', 'unless', 'while', 'until' @@ -382,6 +424,40 @@ class RubyLex indent end + def take_corresponding_syntax_to_kw_do(tokens, index) + syntax_of_do = nil + # Finding a syntax correnponding to "do". + index.downto(0) do |i| + tk = tokens[i] + # In "continue", the token isn't the corresponding syntax to "do". + #is_continue = process_continue(@tokens[0..(i - 1)]) + # continue ではなく、直前に (:on_ignored_nl|:on_nl|:on_comment):on_sp* みたいなのがあるかどうかを調べる + non_sp_index = tokens[0..(i - 1)].rindex{ |t| t[1] != :on_sp } + first_in_fomula = false + if non_sp_index.nil? + first_in_fomula = true + elsif [:on_ignored_nl, :on_nl, :on_comment].include?(tokens[non_sp_index][1]) + first_in_fomula = true + end + if tk[3].anybits?(Ripper::EXPR_CMDARG) and tk[1] == :on_ident + # The target method call to pass the block with "do". + syntax_of_do = :method_calling + break if first_in_fomula + elsif tk[1] == :on_kw && %w{while until for}.include?(tk[2]) + # A loop syntax in front of "do" found. + # + # while cond do # also "until" or "for" + # end + # + # This "do" doesn't increment indent because the loop syntax already + # incremented. + syntax_of_do = :loop_syntax + break if first_in_fomula + end + end + syntax_of_do + end + def check_newline_depth_difference depth_difference = 0 open_brace_on_line = 0 @@ -410,7 +486,7 @@ class RubyLex case t[1] when :on_ignored_nl, :on_nl, :on_comment - if index != (@tokens.size - 1) + if index != (@tokens.size - 1) and in_oneliner_def != :BODY depth_difference = 0 open_brace_on_line = 0 end @@ -428,14 +504,8 @@ class RubyLex next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME) case t[2] when 'do' - if index > 0 and @tokens[index - 1][3].anybits?(Ripper::EXPR_CMDARG | Ripper::EXPR_ENDFN | Ripper::EXPR_ARG) - # method_with_block do; end - depth_difference += 1 - else - # while cond do; end # also "until" or "for" - # This "do" doesn't increment indent because "while" already - # incremented. - end + syntax_of_do = take_corresponding_syntax_to_kw_do(@tokens, index) + depth_difference += 1 if syntax_of_do == :method_calling when 'def', 'case', 'for', 'begin', 'class', 'module' depth_difference += 1 when 'if', 'unless', 'while', 'until', 'rescue' @@ -445,6 +515,8 @@ class RubyLex end when 'else', 'elsif', 'ensure', 'when', 'in' depth_difference += 1 + when 'end' + depth_difference -= 1 end end end @@ -488,11 +560,13 @@ class RubyLex case t[1] when :on_ignored_nl, :on_nl, :on_comment - corresponding_token_depth = nil - spaces_at_line_head = 0 - is_first_spaces_of_line = true - is_first_printable_of_line = true - open_brace_on_line = 0 + if in_oneliner_def != :BODY + corresponding_token_depth = nil + spaces_at_line_head = 0 + is_first_spaces_of_line = true + is_first_printable_of_line = true + open_brace_on_line = 0 + end next when :on_sp spaces_at_line_head = t[2].count(' ') if is_first_spaces_of_line @@ -514,7 +588,12 @@ class RubyLex when :on_kw next if index > 0 and @tokens[index - 1][3].allbits?(Ripper::EXPR_FNAME) case t[2] - when 'def', 'do', 'case', 'for', 'begin', 'class', 'module' + when 'do' + syntax_of_do = take_corresponding_syntax_to_kw_do(@tokens, index) + if syntax_of_do == :method_calling + spaces_of_nest.push(spaces_at_line_head) + end + when 'def', 'case', 'for', 'begin', 'class', 'module' spaces_of_nest.push(spaces_at_line_head) when 'rescue' unless t[3].allbits?(Ripper::EXPR_LABEL) |