diff options
author | Stan Lo <stan001212@gmail.com> | 2023-08-21 16:42:10 +0100 |
---|---|---|
committer | git <svn-admin@ruby-lang.org> | 2023-08-21 15:42:15 +0000 |
commit | 86ac17efde6cf98903513cac2538b15fc4ac80b2 (patch) | |
tree | b683fd78c5380b3c61159001e4103f96d7bcd2a5 /lib/irb/ruby-lex.rb | |
parent | 2929c47243e30000ca4e273608b478cf8da42207 (diff) |
[ruby/irb] Move input processing out of RubyLex
(https://github.com/ruby/irb/pull/683)
* Add a test case for Ctrl-C handling
* Test symbol aliases with integration tests
There are a few places that also need to check symbol aliases before
`Irb#eval_input`. But since the current command test skip them, we
don't have test coverage on them.
* Move each_top_level_statement and readmultiline to Irb
This will save RubyLex from knowning information about commands and aliases.
https://github.com/ruby/irb/commit/69cb5b5615
Diffstat (limited to 'lib/irb/ruby-lex.rb')
-rw-r--r-- | lib/irb/ruby-lex.rb | 69 |
1 files changed, 2 insertions, 67 deletions
diff --git a/lib/irb/ruby-lex.rb b/lib/irb/ruby-lex.rb index 3a0173a6be..085b08997c 100644 --- a/lib/irb/ruby-lex.rb +++ b/lib/irb/ruby-lex.rb @@ -7,7 +7,6 @@ require "ripper" require "jruby" if RUBY_ENGINE == "jruby" require_relative "nesting_parser" -require_relative "statement" # :stopdoc: class RubyLex @@ -42,6 +41,8 @@ class RubyLex end end + attr_reader :line_no + def initialize(context) @context = context @line_no = 1 @@ -65,16 +66,6 @@ class RubyLex result end - def single_line_command?(code) - command = code.split(/\s/, 2).first - @context.symbol_alias?(command) || @context.transform_args?(command) - end - - # io functions - def set_input(&block) - @input = block - end - def set_prompt(&block) @prompt = block end @@ -188,62 +179,6 @@ class RubyLex @line_no += addition end - def readmultiline - save_prompt_to_context_io([], false, 0) - - # multiline - return @input.call if @context.io.respond_to?(:check_termination) - - # nomultiline - code = '' - line_offset = 0 - loop do - line = @input.call - unless line - return code.empty? ? nil : code - end - - code << line - # Accept any single-line input for symbol aliases or commands that transform args - return code if single_line_command?(code) - - tokens, opens, terminated = check_code_state(code) - return code if terminated - - line_offset += 1 - continue = should_continue?(tokens) - save_prompt_to_context_io(opens, continue, line_offset) - end - end - - def each_top_level_statement - loop do - code = readmultiline - break unless code - - if code != "\n" - yield build_statement(code), @line_no - end - increase_line_no(code.count("\n")) - rescue TerminateLineInput - end - end - - def build_statement(code) - code.force_encoding(@context.io.encoding) - command_or_alias, arg = code.split(/\s/, 2) - # Transform a non-identifier alias (@, $) or keywords (next, break) - command_name = @context.command_aliases[command_or_alias.to_sym] - command = command_name || command_or_alias - command_class = IRB::ExtendCommandBundle.load_command(command) - - if command_class - IRB::Statement::Command.new(code, command, arg, command_class) - else - IRB::Statement::Expression.new(code, assignment_expression?(code)) - end - end - def assignment_expression?(code) # Try to parse the code and check if the last of possibly multiple # expressions is an assignment type. |