Skip to content

Commit

Permalink
Merge pull request #405 from st0012/refactor-lexer
Browse files Browse the repository at this point in the history
Refactor ruby-lex.rb
  • Loading branch information
peterzhu2118 authored Oct 2, 2022
2 parents be5a7b7 + 0e760d2 commit a768b3b
Showing 1 changed file with 20 additions and 28 deletions.
48 changes: 20 additions & 28 deletions lib/irb/ruby-lex.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,19 @@ def set_prompt(p = nil, &block)
def self.ripper_lex_without_warning(code, context: nil)
verbose, $VERBOSE = $VERBOSE, nil
if context
lvars = context&.workspace&.binding&.local_variables
lvars = context.workspace&.binding&.local_variables
if lvars && !lvars.empty?
code = "#{lvars.join('=')}=nil\n#{code}"
line_no = 0
else
line_no = 1
end
end
tokens = nil

compile_with_errors_suppressed(code, line_no: line_no) do |inner_code, line_no|
lexer = Ripper::Lexer.new(inner_code, '-', line_no)
if lexer.respond_to?(:scan) # Ruby 2.7+
tokens = []
lexer.scan.each do |t|
lexer.scan.each_with_object([]) do |t, tokens|
next if t.pos.first == 0
prev_tk = tokens.last
position_overlapped = prev_tk && t.pos[0] == prev_tk.pos[0] && t.pos[1] < prev_tk.pos[1] + prev_tk.tok.bytesize
Expand All @@ -163,10 +162,9 @@ def self.ripper_lex_without_warning(code, context: nil)
end
end
else
tokens = lexer.parse.reject { |it| it.pos.first == 0 }
lexer.parse.reject { |it| it.pos.first == 0 }
end
end
tokens
ensure
$VERBOSE = verbose
end
Expand Down Expand Up @@ -205,12 +203,7 @@ def set_auto_indent(context)
last_line = lines[line_index]&.byteslice(0, byte_pointer)
code += last_line if last_line
@tokens = self.class.ripper_lex_without_warning(code, context: context)
corresponding_token_depth = check_corresponding_token_depth(lines, line_index)
if corresponding_token_depth
corresponding_token_depth
else
nil
end
check_corresponding_token_depth(lines, line_index)
end
end
end
Expand Down Expand Up @@ -304,7 +297,7 @@ def process_continue(tokens = @tokens)
return true
elsif tokens.size >= 1 and tokens[-1].event == :on_heredoc_end # "EOH\n"
return false
elsif tokens.size >= 2 and defined?(Ripper::EXPR_BEG) and tokens[-2].state.anybits?(Ripper::EXPR_BEG | Ripper::EXPR_FNAME) and tokens[-2].tok !~ /\A\.\.\.?\z/
elsif tokens.size >= 2 and tokens[-2].state.anybits?(Ripper::EXPR_BEG | Ripper::EXPR_FNAME) and tokens[-2].tok !~ /\A\.\.\.?\z/
# end of literal except for regexp
# endless range at end of line is not a continue
return true
Expand Down Expand Up @@ -385,21 +378,20 @@ def check_code_block(code, tokens = @tokens)
$VERBOSE = verbose
end

if defined?(Ripper::EXPR_BEG)
last_lex_state = tokens.last.state
if last_lex_state.allbits?(Ripper::EXPR_BEG)
return false
elsif last_lex_state.allbits?(Ripper::EXPR_DOT)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_CLASS)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_FNAME)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_VALUE)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_ARG)
return false
end
last_lex_state = tokens.last.state

if last_lex_state.allbits?(Ripper::EXPR_BEG)
return false
elsif last_lex_state.allbits?(Ripper::EXPR_DOT)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_CLASS)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_FNAME)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_VALUE)
return true
elsif last_lex_state.allbits?(Ripper::EXPR_ARG)
return false
end

false
Expand Down

0 comments on commit a768b3b

Please sign in to comment.