Commit graph

99 commits

Author SHA1 Message Date
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
Kevin Newton
641f15b1c6 [ruby/prism] Mark Prism as ractor-safe
c02429765b
2025-03-19 21:11:57 +00:00
Kevin Newton
af76b7f4d9 [ruby/prism] Revert "Mark extension as Ractor-safe"
56eaf53732
2025-03-12 19:56:22 +00:00
Kevin Newton
242e99eb0f [ruby/prism] Mark extension as Ractor-safe
10e5431b38
2025-03-12 19:15:03 +00:00
Earlopain
f27ed98eff [ruby/prism] Freeze Prism::VERSION
Closes https://github.com/ruby/prism/pull/3422

b488a84253
2025-01-19 12:44:20 +00:00
Kevin Newton
51d3d6ac8c [ruby/prism] Support forwarding flags on scopes
When parent scopes around an eval are forwarding parameters (like
*, **, &, or ...) we need to know that information when we are in
the parser. As such, we need to support passing that information
into the scopes option. In order to do this, unfortunately we need
a bunch of changes.

The scopes option was previously an array of array of strings.
These corresponded to the names of the locals in the parent scopes.
We still support this, but now additionally support passing in a
Prism::Scope instance at each index in the array. This Prism::Scope
class holds both the names of the locals as well as an array of
forwarding parameter names (symbols corresponding to the forwarding
parameters). There is convenience function on the Prism module that
creates a Prism::Scope object using Prism.scope.

In JavaScript, we now additionally support an object much the same
as the Ruby side. In Java, we now have a ParsingOptions.Scope class
that holds that information. In the dump APIs, these objects in all
3 languages will add an additional byte for the forwarding flags in
the middle of the scopes serialization.

All of this is in service of properly parsing the following code:

```ruby
def foo(*) = eval("bar(*)")
```

21abb6b7c4
2025-01-14 20:31:38 +00:00
Kevin Newton
da93c9ae29 [ruby/prism] Refactor serializer
8ab2532f09
2025-01-14 15:32:41 +00:00
Kevin Newton
713f31872a [ruby/prism] Freeze AST option
To make it so that you can pass `freeze: true` to Prism parse
methods and get back a deeply-frozen AST that is Ractor-
shareable.

8e6a93b2d2
2025-01-14 15:32:39 +00:00
S-H-GAMELINKS
6393d2950d [ruby/prism] Supress string_query function warning
0635814327
2024-10-13 12:22:58 +00:00
Kevin Newton
5f62522d5b [ruby/prism] Prism::StringQuery
Introduce StringQuery to provide methods to access some metadata
about the Ruby lexer.

d3f55b67b9
2024-10-11 19:34:57 +00:00
Benoit Daloze
ed4a55fc4d [ruby/prism] Accept all 3.3.x and 3.4.x Ruby versions for Prism.parse
a4fcd5339a
2024-09-24 12:24:19 +00:00
Benoit Daloze
b8baf3f3ee [ruby/prism] check_string() should always return a valid C string
* Otherwise it is invalid e.g. to call strlen() to the result,
  or to assume the argument was a string.
* All callers are already checking for nil before.

8197be883e
2024-09-24 12:24:19 +00:00
Kevin Newton
f515a1ab4b [ruby/prism] Introduce partial_script option
b28877fa4f
2024-09-20 15:42:12 +00:00
Kevin Newton
f85efc9748 [ruby/prism] Expose main_script in serialization API
0b527ca93f
2024-09-13 19:13:21 +00:00
Kevin Newton
9afc6a981d [PRISM] Only parse shebang on main script
Fixes [Bug #20730]
2024-09-13 12:51:53 -04:00
Kevin Newton
38ba15beed [ruby/prism] Check errno for parsing directory
d68ea29d04
2024-09-12 13:43:04 -04:00
Kevin Newton
d4d6f1de83 [ruby/prism] UTF-8 characters in file name
487f0ffe78
2024-09-11 19:17:12 +00:00
Alexander Momchilov
83e72fde83 Optimize Array allocations
… by allocating them with the correct capacity.
2024-08-27 16:23:50 +00:00
Kevin Newton
5cb6954baa [ruby/prism] Fix up lex result constants
084baca463
2024-08-15 16:50:00 +00:00
Koichi ITO
ad23bbe574 [ruby/prism] [Doc] Tweak the docs for lex APIs
`Prism.lex` and `Prism.lex_file` return `ParseLexResult` instead of `Array`.
`Prism::parse_lex` and `Prism::parse_lex_file` return `ParseLexResult` instead of `ParseResult`.
This PR updates the documentation to reflect these return values.

ee331941c0
2024-08-10 08:42:36 +00:00
Kevin Newton
e77e4aa608 [ruby/prism] Have parse_stream handle NUL bytes
4a41d298c8
2024-07-17 19:44:32 +00:00
Kevin Newton
c93b70cfdd [ruby/prism] Make sure Init_prism is visible for extension
4601d3adfd
2024-06-19 00:06:01 +00:00
Kevin Newton
d827d32527 [ruby/prism] Provide ability to lock encoding while parsing
f7faedfb3f
2024-06-10 17:21:32 -04:00
Nobuyoshi Nakada
c357138aa3
Use PRISM_EXPORTED_FUNCTION consistently with the header 2024-06-09 17:34:37 +09:00
Kevin Newton
745a948b6d [ruby/prism] Remove dynamic Debug module methods
b850794db9
2024-05-24 17:19:36 +00:00
Kevin Newton
79001c8b4a [ruby/prism] Remove error formatting, put directly in CRuby
53b2866487
2024-05-24 17:19:36 +00:00
Kevin Newton
653652bcbe [ruby/prism] Remove Debug#named_captures
5050dfbe70
2024-05-24 17:19:35 +00:00
Kevin Newton
e1041a8eda [ruby/prism] Move profiling to prism module directly
75fabf7081
2024-05-24 17:19:35 +00:00
Kevin Newton
b04c959621 [ruby/prism] Remove various unused memsize infra
283938ed1f
2024-05-24 17:19:34 +00:00
Kevin Newton
b8681c2e37 [ruby/prism] Remove Debug::integer_parse
14e397598b
2024-05-24 17:19:33 +00:00
Kevin Newton
870350253e [ruby/prism] Remove Debug::static_inspect
486c71c426
2024-05-24 17:19:33 +00:00
Vinicius Stock
4fbb208185 [ruby/prism] Create specialized ASCIISource with asciionly optimizations
40993166a8
2024-05-03 18:10:21 +00:00
Kevin Newton
7d64fbda53 [ruby/prism] Remove static literals dependence on parser definition
b3e104e8a2
2024-04-24 19:39:41 +00:00
Kevin Newton
23be6599a2 [ruby/prism] Split parse result based on type
17194e096d
2024-04-19 19:25:32 +00:00
Kevin Newton
d186eb36a4 [ruby/prism] Add a reflection API for determining the fields of a node
f3f9950a74
2024-04-17 13:54:29 -04:00
Kevin Newton
d583616f32 [ruby/prism] Ensure deserialization works with errors+warnings>256
f540e830b5
2024-03-28 12:04:35 -04:00
Kevin Newton
fcc06fa82a [ruby/prism] CLI -x flag
2068e3c30a
2024-03-28 12:04:35 -04:00
Kevin Newton
9b816e674a [ruby/prism] Add option for inlining messages for error formatting
af0204a8ab
2024-03-27 13:03:11 -04:00
Koichi ITO
52cf6ec46b [ruby/prism] Fix typos
After finding the "if if" typo, some additional typos identified by running `codespell` are also being corrected:
https://github.com/codespell-project/codespell

e6a34cfeeb
2024-03-26 10:51:12 +00:00
Kevin Newton
a08954569f
[ruby/prism] Fix up minimal build setting
98c85c4acb
2024-03-25 11:54:20 -04:00
Kevin Newton
af7bf9e0d8 [ruby/prism] Provide options for reducing size
592128de4d
2024-03-20 17:32:03 -04:00
Jean Boussier
53a77d9b90 [ruby/prism] Change frozen_string_literal to be a tri-state
An explicit `false` is not equivalent to the comment being missing,
because the default can be switched with a runtime flag:

```bash
$ ruby --enable-frozen-string-literal -e 'p "foo".frozen?'
true
```

4660f58775
2024-03-13 16:03:24 +00:00
Kevin Newton
21ea290b34 [ruby/prism] Static literals inspect
4913d112da
2024-03-12 03:30:50 +00:00
Kevin Newton
2ab75bc444 [ruby/prism] Support offset
665f533373
2024-03-11 14:49:23 +00:00
Kevin Newton
f5294ebbdb [ruby/prism] Shared integer parsing logic
a2594a23c1
2024-03-07 18:02:33 -05:00
tompng
81f02eb6ba [ruby/prism] Change pm_integer_t structure
588acf823f
2024-03-07 18:02:33 -05:00
Kevin Newton
977012bae8 [ruby/prism] Remove restrict to fix windows 2015
f0a2ce1c0e
2024-03-07 21:23:18 +00:00
Kevin Newton
ec159fc8ba [ruby/prism] Support parsing streams
efdc2b7222
2024-03-07 20:40:39 +00:00
Kevin Newton
38c2774420 [ruby/prism] Expose types on diagnostics
a735c2262f
2024-03-06 21:42:54 -05:00
Kevin Newton
b88973165a [ruby/prism] Parse files from Ruby API using fread, not mmap
62d4376a53
2024-03-06 17:45:56 +00:00