ruby/lib/prism
Aaron Patterson 89d89fa49d When reading from stdin, put a wrapper around the IO object
The purpose of this commit is to fix Bug #21188.  We need to detect when
stdin has run in to an EOF case.  Unfortunately we can't _call_ the eof
function on IO because it will block.

Here is a short script to demonstrate the issue:

```ruby
x = STDIN.gets
puts x
puts x.eof?
```

If you run the script, then type some characters (but _NOT_ a newline),
then hit Ctrl-D twice, it will print the input string.  Unfortunately,
calling `eof?` will try to read from STDIN again causing us to need a
3rd Ctrl-D to exit the program.

Before introducing the EOF callback to Prism, the input loop looked
kind of like this:

```ruby
loop do
  str = STDIN.gets
  process(str)

  if str.nil?
    p :DONE
  end
end
```

Which required 3 Ctrl-D to exit.  If we naively changed it to something
like this:

```ruby
loop do
  str = STDIN.gets
  process(str)

  if STDIN.eof?
    p :DONE
  end
end
```

It would still require 3 Ctrl-D because `eof?` would block.  In this
patch, we're wrapping the IO object, checking the buffer for a newline
and length, and then using that to simulate a non-blocking eof? method.

This commit wraps STDIN and emulates a non-blocking `eof` function.

[Bug #21188]
2025-08-04 12:34:33 -07:00
..
parse_result [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
polyfill [ruby/prism] Fix parser translator during string escaping with invalid utf-8 2025-06-11 18:07:43 +00:00
translation [ruby/prism] Match RubyParser behavior for -> lambda args 2025-08-01 16:57:17 +00:00
desugar_compiler.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
ffi.rb When reading from stdin, put a wrapper around the IO object 2025-08-04 12:34:33 -07:00
lex_compat.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
node_ext.rb [ruby/prism] [DOC] Stop rdoc from processing non-rdoc comments 2025-05-29 04:45:59 +00:00
pack.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
parse_result.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
pattern.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
prism.gemspec [ruby/prism] Fix parser translator during string escaping with invalid utf-8 2025-06-11 18:07:43 +00:00
relocation.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
string_query.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00
translation.rb [ruby/prism] [DOC] Specify markdown mode to RDoc 2025-05-29 04:45:58 +00:00