Skip to content

Commit 9a4487a

Browse files
authored
Move parse_command method to Context (#993)
Since Context dictates whether a line is a command or an expression, moving the parse_command method to Context makes the relationship more explicit.
1 parent 949f032 commit 9a4487a

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

lib/irb.rb

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,7 +1131,7 @@ def build_statement(code)
11311131
end
11321132

11331133
code.force_encoding(@context.io.encoding)
1134-
if (command, arg = parse_command(code))
1134+
if (command, arg = @context.parse_command(code))
11351135
command_class = Command.load_command(command)
11361136
Statement::Command.new(code, command_class, arg)
11371137
else
@@ -1140,35 +1140,8 @@ def build_statement(code)
11401140
end
11411141
end
11421142

1143-
ASSIGN_OPERATORS_REGEXP = Regexp.union(%w[= += -= *= /= %= **= &= |= &&= ||= ^= <<= >>=])
1144-
1145-
def parse_command(code)
1146-
command_name, arg = code.strip.split(/\s+/, 2)
1147-
return unless code.lines.size == 1 && command_name
1148-
1149-
arg ||= ''
1150-
command = command_name.to_sym
1151-
# Command aliases are always command. example: $, @
1152-
if (alias_name = @context.command_aliases[command])
1153-
return [alias_name, arg]
1154-
end
1155-
1156-
# Assignment-like expression is not a command
1157-
return if arg.start_with?(ASSIGN_OPERATORS_REGEXP) && !arg.start_with?(/==|=~/)
1158-
1159-
# Local variable have precedence over command
1160-
return if @context.local_variables.include?(command)
1161-
1162-
# Check visibility
1163-
public_method = !!Kernel.instance_method(:public_method).bind_call(@context.main, command) rescue false
1164-
private_method = !public_method && !!Kernel.instance_method(:method).bind_call(@context.main, command) rescue false
1165-
if Command.execute_as_command?(command, public_method: public_method, private_method: private_method)
1166-
[command, arg]
1167-
end
1168-
end
1169-
11701143
def command?(code)
1171-
!!parse_command(code)
1144+
!!@context.parse_command(code)
11721145
end
11731146

11741147
def configure_io

lib/irb/context.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module IRB
1313
# A class that wraps the current state of the irb session, including the
1414
# configuration of IRB.conf.
1515
class Context
16+
ASSIGN_OPERATORS_REGEXP = Regexp.union(%w[= += -= *= /= %= **= &= |= &&= ||= ^= <<= >>=])
1617
# Creates a new IRB context.
1718
#
1819
# The optional +input_method+ argument:
@@ -635,6 +636,31 @@ def evaluate_expression(code, line_no) # :nodoc:
635636
result
636637
end
637638

639+
def parse_command(code)
640+
command_name, arg = code.strip.split(/\s+/, 2)
641+
return unless code.lines.size == 1 && command_name
642+
643+
arg ||= ''
644+
command = command_name.to_sym
645+
# Command aliases are always command. example: $, @
646+
if (alias_name = command_aliases[command])
647+
return [alias_name, arg]
648+
end
649+
650+
# Assignment-like expression is not a command
651+
return if arg.start_with?(ASSIGN_OPERATORS_REGEXP) && !arg.start_with?(/==|=~/)
652+
653+
# Local variable have precedence over command
654+
return if local_variables.include?(command)
655+
656+
# Check visibility
657+
public_method = !!Kernel.instance_method(:public_method).bind_call(main, command) rescue false
658+
private_method = !public_method && !!Kernel.instance_method(:method).bind_call(main, command) rescue false
659+
if Command.execute_as_command?(command, public_method: public_method, private_method: private_method)
660+
[command, arg]
661+
end
662+
end
663+
638664
def inspect_last_value # :nodoc:
639665
@inspect_method.inspect_value(@last_value)
640666
end

0 commit comments

Comments
 (0)