summaryrefslogtreecommitdiff
path: root/lib/irb/statement.rb
diff options
context:
space:
mode:
authorStan Lo <[email protected]>2023-08-16 11:13:41 +0100
committergit <[email protected]>2023-08-16 10:13:46 +0000
commit5a40f7db54dfcc7dadb75dde32c25b88c78d6a85 (patch)
tree928a522f7c48a4c93fe0b7302ba469953e30c8d9 /lib/irb/statement.rb
parent0982c5fa00f6163d04b17229e72d128263b98d50 (diff)
[ruby/irb] Encapsulate input details in Statement objects
(https://github.com/ruby/irb/pull/682) * Introduce Statement class * Split Statement class for better clarity https://github.com/ruby/irb/commit/65e8e68690
Diffstat (limited to 'lib/irb/statement.rb')
-rw-r--r--lib/irb/statement.rb78
1 files changed, 78 insertions, 0 deletions
diff --git a/lib/irb/statement.rb b/lib/irb/statement.rb
new file mode 100644
index 0000000000..9493c3ffb1
--- /dev/null
+++ b/lib/irb/statement.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+module IRB
+ class Statement
+ attr_reader :code
+
+ def is_assignment?
+ raise NotImplementedError
+ end
+
+ def suppresses_echo?
+ raise NotImplementedError
+ end
+
+ def should_be_handled_by_debugger?
+ raise NotImplementedError
+ end
+
+ def evaluable_code
+ raise NotImplementedError
+ end
+
+ class Expression < Statement
+ def initialize(code, is_assignment)
+ @code = code
+ @is_assignment = is_assignment
+ end
+
+ def suppresses_echo?
+ @code.match?(/;\s*\z/)
+ end
+
+ def should_be_handled_by_debugger?
+ true
+ end
+
+ def is_assignment?
+ @is_assignment
+ end
+
+ def evaluable_code
+ @code
+ end
+ end
+
+ class Command < Statement
+ def initialize(code, command, arg, command_class)
+ @code = code
+ @command = command
+ @arg = arg
+ @command_class = command_class
+ end
+
+ def is_assignment?
+ false
+ end
+
+ def suppresses_echo?
+ false
+ end
+
+ def should_be_handled_by_debugger?
+ IRB::ExtendCommand::DebugCommand > @command_class
+ end
+
+ def evaluable_code
+ # Hook command-specific transformation to return valid Ruby code
+ if @command_class.respond_to?(:transform_args)
+ arg = @command_class.transform_args(@arg)
+ else
+ arg = @arg
+ end
+
+ [@command, arg].compact.join(' ')
+ end
+ end
+ end
+end