Commit graph

125 commits

Author SHA1 Message Date
Earlopain
3071c5d04c [ruby/prism] Fix parser translator with trailing backslash in %W /%I array
https://docs.ruby-lang.org/en/master/syntax/literals_rdoc.html#label-25w+and+-25W-3A+String-Array+Literals
> %W allow escape sequences described in Escape Sequences. However the continuation line <newline> is not usable because it is interpreted as the escaped newline described above.

f5c7460ad5
2025-06-30 12:32:31 +00:00
Earlopain
970813d982 [ruby/prism] Fix parser translator during string escaping with invalid utf-8
Instead, prefer `scan_byte` over `get_byte` since that already returns the byte as an integer, sidestepping conversion issues.

Fixes https://github.com/ruby/prism/issues/3582

7f3008b2b5
2025-06-11 18:07:43 +00:00
Nobuyoshi Nakada
991cf2dd4d [ruby/prism] [DOC] Specify markdown mode to RDoc
12af4e144e
2025-05-29 04:45:58 +00:00
viralpraxis
543dd77cc3 [ruby/prism] Fix parsing rescued exception via indexed assignment
Given this code

```ruby
begin
  raise '42'
rescue => A[]
end
```

Prism fails with this backtrace

```
Error: test_unparser/corpus/literal/rescue.txt(Prism::ParserTest): NoMethodError: undefined method `arguments' for nil
prism/lib/prism/translation/parser/compiler.rb:1055:in `visit_index_target_node'
prism/lib/prism/node.rb:9636:in `accept'
prism/lib/prism/compiler.rb:30:in `visit'
prism/lib/prism/translation/parser/compiler.rb:218:in `visit_begin_node'
```

Seems like

```diff
-            visit_all(node.arguments.arguments),
+            visit_all(node.arguments&.arguments || []),
```

fixes the problem.

76d01aeb6c
2025-04-12 17:43:57 +00:00
Earlopain
334c261cc9 [ruby/prism] Fix parser translator when splatting in pattern matching pin
Because it ends up treating it as a local variable, and `a.x`
is not a valid local variable name.

I'm not big on pattern matching, but conceptually it makes sense to me
to treat anything inside ^() to not be
pattern matching syntax?

80dbd85c45
2025-04-02 20:51:54 +00:00
Earlopain
d7e46543b5 [ruby/prism] Fix parser translator when pinning hash with string keys
`StringNode` and `SymbolNode` don't have the same shape
(`content` vs `value`) and that wasn't handled.

I believe the logic for the common case can be reused.
I simply left the special handling for implicit nodes in pattern matching
and fall through otherwise.

NOTE: patterns.txt is not actually tested at the moment,
because it contains syntax that `parser` mistakenly rejects.
But I checked manually that this doesn't introduce other failures.
https://github.com/whitequark/parser/pull/1060

55adfaa895
2025-03-30 17:24:05 +00:00
Kevin Newton
052794bfe1 [ruby/prism] Accept a newline after the defined? keyword
[Bug #21197]

22be955ce9
2025-03-30 13:22:41 -04:00
Kevin Newton
b003d40194 Fix up merge conflicts for prism sync 2025-03-18 13:36:53 -04:00
Earlopain
e3c8464630 [ruby/prism] Only unnest parser mlhs nodes when no rest argument is provided
```
(a,), = []

PARSER====================
s(:masgn,
  s(:mlhs,
    s(:mlhs,
      s(:lvasgn, :a))),
  s(:array))
PRISM====================
s(:masgn,
  s(:mlhs,
    s(:lvasgn, :a)),
  s(:array))
```

8aa1f4690e
2025-03-18 13:36:53 -04:00
Earlopain
94e12ffa39 [ruby/prism] Fix parser translator multiline interpolated symbols
In 2637007929 I added tests but didn't modify them correctly

de021e74de
2025-03-18 13:36:53 -04:00
Earlopain
a8adf5e006 [ruby/prism] Further refine string handling in the parser translator
Mostly around newlines and line continuation.
* percent arrays need special backslash handling in the ast
* Fix offset issue for heredocs with many line continuations (used wrong variable as index access)
* More refined rules on when to simplify string tokens
* Handle line continuations in squiggly heredocs
* Correctly dedent squiggly heredocs with interpolation
* Consider `':foo:` and `%s[foo]` to not be interpolation

4edfe9d981
2025-03-18 13:36:53 -04:00
Kevin Newton
0b4604d5a0 [ruby/prism] Use Set.new over to_set
422d5c4c64
2025-03-18 13:36:53 -04:00
Earlopain
ad478de3f0 [ruby/prism] Optimize array inclusion checks in the parser translator
I see `Array.include?` as 2.4% runtime. Probably because of `LPAREN_CONVERSION_TOKEN_TYPES` but
the others will be faster as well.

Also remove some inline array checks. They are specifically optimized in Ruby since 3.4, but for now prism is for >= 2.7

ca9500a3fc
2025-03-18 13:36:53 -04:00
Earlopain
d5503444fd [ruby/prism] Fix parser translator crash for certain octal escapes
`Integer#chr` performs some validation that we don't want/need. Octal escapes can go above 255, where it will then raise trying to convert.

`append_as_bytes` actually allows to pass a number, so we can just skip that call.
Although, on older rubies of course we still need to handle this in the polyfill.
I don't really like using `pack` but don't know of another way to do so.

For the utf-8 escapes, this is not an issue. Invalid utf-8 in these is simply a syntax error.

161c606b1f
2025-03-18 13:36:53 -04:00
Kevin Newton
1944247a0e [ruby/prism] Handle control and meta escapes in parser translation
09c59a3aa5
2025-03-18 13:36:53 -04:00
Earlopain
fd7a10cf4a [ruby/prism] Further refine string handling in the parser translator
Mostly around newlines and line continuation.
* percent arrays need special backslash handling in the ast
* Fix offset issue for heredocs with many line continuations (used wrong variable as index access)
* More refined rules on when to simplify string tokens
* Handle line continuations in squiggly heredocs
* Correctly dedent squiggly heredocs with interpolation
* Consider `':foo:` and `%s[foo]` to not be interpolation

4edfe9d981
2025-03-18 13:36:53 -04:00
Earlopain
5d138f2b43 [ruby/prism] Better handle regexp in the parser translator
Turns out, it was already almost correct. If you disregard \c and \M style escapes, only a single character is allowed to be escaped in a regex so most tests passed already.

There was also a mistake where the wrong value was constructed for the ast, this is now fixed.
One test fails because of this, but I'm fairly sure it is because of a parser bug. For `/\“/`, the backslash is supposed to be removed because it is a multibyte character. But tbh,
I don't entirely understand all the rules.

Fixes more than half of the remaining ast differences for rubocop tests

e1c75f304b
2025-03-18 13:36:53 -04:00
Earlopain
177adf6fa5 [ruby/prism] Fix parser translator tokens for %-arrays with whitespace escapes
Also fixes a token incompatibility for the word separator. parser only considers whitespace until the first newline

bd3dd2b62a
2025-03-18 13:36:53 -04:00
Earlopain
ac728389e2 [ruby/prism] Fix parser translator edge-case when multiline string ends with \n
When the line contains no real newline but contains unescaped ones, then there will be one less entry

4ef093b600
2025-03-18 13:36:53 -04:00
Earlopain
0fcb7fc21d [ruby/prism] Better handle all kinds of multiline strings in the parser translator
This is a followup to #3373, where the implementation
was extracted

2637007929
2025-03-18 13:36:53 -04:00
Earlopain
acf404e20e [ruby/prism] Fix an incompatibility with the parser translator
The offset cache contains an entry for each byte so it can't be accessed via the string length.

Adds tests for all variants except for this:
```
"fo
o" "ba
’"
```

For some reason, this still has the wrong offset.

a651126458
2025-03-18 13:36:53 -04:00
Earlopain
f49a0114e3 [ruby/prism] Fix parser translator rescue location with semicolon body
There are a few other locations that should be included in that check.
I think the end location must always be present but I left it in to be safe (maybe implicit begin somehow?)

545d07ddc3
2025-03-18 13:36:53 -04:00
Earlopain
bc506295a3 [ruby/prism] Further refine string handling in the parser translator
Mostly around newlines and line continuation.
* percent arrays need special backslash handling in the ast
* Fix offset issue for heredocs with many line continuations (used wrong variable as index access)
* More refined rules on when to simplify string tokens
* Handle line continuations in squiggly heredocs
* Correctly dedent squiggly heredocs with interpolation
* Consider `':foo:` and `%s[foo]` to not be interpolation

4edfe9d981
2025-03-18 13:36:53 -04:00
Earlopain
705bd6fadb [ruby/prism] Fix parser translator when unescaping invalid utf8
1. The string starts out as binary
2. `ち` is appended, forcing it back into utf-8
3. Some invalid byte sequences are tried to append

> incompatible character encodings: UTF-8 and BINARY (ASCII-8BIT)

This makes use of my wish to use `append_as_bytes`. Unfortunatly that method is rather new
so it needs a fallback

e31e94a775
2025-03-18 13:36:53 -04:00
Kevin Newton
f2483c79fe [ruby/prism] Use Set.new over to_set
422d5c4c64
2025-03-13 14:24:48 +00:00
Earlopain
3d4c7c3802 [ruby/prism] Use reverse_each in the parser translator
Avoids an array allocation which matters more and more
the larger the file is.

I have it at 14% of runtime.

f65b90f27d
2025-03-13 13:52:45 +00:00
Earlopain
67e6ccb23f [ruby/prism] Optimize array inclusion checks in the parser translator
I see `Array.include?` as 2.4% runtime. Probably because of `LPAREN_CONVERSION_TOKEN_TYPES` but
the others will be faster as well.

Also remove some inline array checks. They are specifically optimized in Ruby since 3.4, but for now prism is for >= 2.7

ca9500a3fc
2025-03-13 13:52:45 +00:00
Earlopain
4b844f7d9e [ruby/prism] Ensure backwards compatibility with the custom parser builder
Temoprary backwards-compat code so that current users
don't break.

Eventually the Translation::Parser initializer should asser that the correct class is passed in.

66b0162b35
2025-03-13 12:06:58 +00:00
Koichi ITO
6b4453e332 [ruby/prism] Support itblock for Prism::Translation::Parser
## Summary

`itblock` node is added to support the `it` block parameter syntax introduced in Ruby 3.4.

```console
$ ruby -Ilib -rprism -rprism/translation/parser34 -e 'buffer = Parser::Source::Buffer.new("path"); buffer.source = "proc { it }"; \
                                                      p Prism::Translation::Parser34.new.tokenize(buffer)[0]'
s(:itblock,
  s(:send, nil, :proc), :it,
  s(:lvar, :it))
```

This node design is similar to the `numblock` node, which was introduced for the numbered parameter syntax in Ruby 2.7.

```
$ ruby -Ilib -rprism -rprism/translation/parser34 -e 'buffer = Parser::Source::Buffer.new("path"); buffer.source = "proc { _1 }"; \
                                                      p Prism::Translation::Parser34.new.tokenize(buffer)[0]'
s(:numblock,
  s(:send, nil, :proc), 1,
  s(:lvar, :_1))
```

The difference is that while numbered parameters can have multiple parameters, the `it` block parameter syntax allows only a single parameter.

In Ruby 3.3, the conventional node prior to the `it` block parameter syntax is returned.

```console
$ ruby -Ilib -rprism -rprism/translation/parser33 -e 'buffer = Parser::Source::Buffer.new("path"); buffer.source = "proc { it }"; \
                                                      p Prism::Translation::Parser33.new.tokenize(buffer)[0]'
s(:block,
  s(:send, nil, :proc),
  s(:args),
  s(:send, nil, :it))
```

## Development Note

The Parser gem does not yet support the `it` block parameter syntax. This is the first case where Prism's node design precedes that of the Parser gem.
When implementing https://github.com/whitequark/parser/issues/962, this node design will need to be taken into consideration.

c141e1420a
2025-03-10 16:57:46 +00:00
Earlopain
790b3858e8 [ruby/prism] Add a custom builder class for the parser translator
I want to add new node types to the parser translator, for example `itblock`. The bulk of the work is already done by prism itself. In the `parser`
builder, this would be a 5-line change at most but we don't control that here.

Instead, we can add our own builder and either overwrite the few methods we need,
or just inline the complete builder. I'm not sure yet which would be better.

`rubocop-ast` uses its own builder for `parser`. For this to correctly work, it must explicitly choose to extend the
prism builder and use it, same as it currently chooses to use a different parser when prism is used.

I'd like to enforce that the builder for prism extends its custom one since it will lead to
some pretty weird issues otherwise. But first, I'd like to change `rubocop-ast` to make use of this.

b080e608a8
2025-02-25 15:44:56 +00:00
Earlopain
769cccba56 [ruby/prism] Fix parser translator scope issues for implicit hash values
`builder.pair_label` is no good since it makes use of variables that the parser gem encountered.
Since the prism translator doesn't keep proper track of that information, the following code interprets
the implicit value as a local variable, even though it is not in scope:

```rb
def foo
  bar = 123
end

{ bar: }
```

bbeb5b083a
2025-01-20 18:03:13 +00:00
Earlopain
56242ba495 Better handle regexp in the parser translator
Turns out, it was already almost correct. If you disregard \c and \M style escapes, only a single character is allowed to be escaped in a regex so most tests passed already.

There was also a mistake where the wrong value was constructed for the ast, this is now fixed.
One test fails because of this, but I'm fairly sure it is because of a parser bug. For `/\“/`, the backslash is supposed to be removed because it is a multibyte character. But tbh,
I don't entirely understand all the rules.

Fixes more than half of the remaining ast differences for rubocop tests
2025-01-14 20:33:11 +00:00
Earlopain
7c0b92a1c6 [ruby/prism] Fix parser translator tokens for %x(#{})
It falsely considered it to be a single backtick command

dd762be590
2025-01-13 18:02:28 +00:00
Earlopain
0a26a3de89 [ruby/prism] Fix parser translator heredoc dedention with leading interpolation
```rb
<<~F
  foo
#{}
  bar
F
```

has zero common whitespace.

1f3c222a06
2025-01-13 15:43:24 +00:00
Earlopain
566f9463c2 [ruby/prism] Fix parser translator tSPACE tokens for percent arrays
Tests worked around this but the incompatibility is not hard to fix.
This fixes 17 token incompatibilies in tests here that were previously passing

101962526d
2025-01-12 18:54:16 +00:00
Kevin Newton
14b9098459 [ruby/prism] Frozen strings in the AST
8d9d429155
2025-01-12 18:41:42 +00:00
Earlopain
48749afe61 [ruby/prism] Fix parser translator ranges for mulltiline strings with multiline bytes
A rather silly issue with a rather simple fix.
The ranges already use the offset cache, this effectivly double-encoded them.

66b65634c0
2025-01-12 18:34:36 +00:00
Earlopain
d0deec3ef3 [ruby/prism] Fix parser translator tokens for comment-only file
In https://github.com/ruby/prism/pull/3393 I made a mistake.
When there is no previous token, it wraps around to -1. Oops

Additionally, if a comment has no newline then the offset should be kept as is

3c266f1de4
2025-01-12 18:34:06 +00:00
Earlopain
70022224b2 [ruby/prism] Better comment token handling for the parser translator
There appear to be a bunch of rules, changing behaviour for
inline comments, multiple comments after another, etc.

This seems to line up with reality pretty closely, token differences for RuboCop tests go from 1129 to 619 which seems pretty impressive

2e1b92670c
2025-01-11 19:09:05 -05:00
Earlopain
9c962ea792 [ruby/prism] Fix parser translator tokens for backslashes in single-quoted strings and word arrays
These are not line continuations. They either should be taken literally,
or allow the word array to contain the following whitespace (newlines in this case)

Before:
```
  0...1: tSTRING_BEG     => "'"
 1...12: tSTRING_CONTENT => "foobar\\\n"
12...16: tSTRING_CONTENT => "baz\n"
16...17: tSTRING_END     => "'"
17...18: tNL             => nil
```

After:
```
  0...1: tSTRING_BEG     => "'"
  1...6: tSTRING_CONTENT => "foo\\\n"
 6...12: tSTRING_CONTENT => "bar\\\n"
12...16: tSTRING_CONTENT => "baz\n"
16...17: tSTRING_END     => "'"
17...18: tNL             => nil
```

b6554ad64e
2025-01-11 19:09:05 -05:00
Earlopain
110461c509 [ruby/prism] Implement more string token escaping in the parser translator
This leaves `\c` and `\M` escaping but I don't understand how these should even work yet. Maybe later.

13db3e8cb9
2025-01-11 19:09:05 -05:00
Earlopain
283037f7e3 [ruby/prism] Better handle all kinds of multiline strings in the parser translator
This is a followup to #3373, where the implementation
was extracted

2637007929
2025-01-11 19:09:05 -05:00
Earlopain
80fe9a1c77 [ruby/prism] Better handle multiline interpolated strings in the parser translator
Much of this logic should be shared between interpolated symbols and regexps.
It's also incorrect when the node contains a literal `\\n` (same as for plain string nodes at the moment)

561914f99b
2025-01-11 19:09:05 -05:00
Earlopain
9f38ee11cb [ruby/prism] Fix parser translator ast for empty regex
In that specific case, no string node is emitted

1166db13dd
2025-01-11 19:09:05 -05:00
Earlopain
a234fd516f [ruby/prism] Fix parser translator ast for regex with line continuation
Turns out, the vast majority of work was already done with handling the same for heredocs

I'm confident this should also apply to actual string nodes (there's even a todo for it) but
no tests change if I apply it there too, so I can't say for sure if the logic would be correct.
The individual test files are a bit too large, maybe something else would break that currently passes.

Leaving it for later to look more closely into that.

6bba1c54e1
2025-01-11 19:09:05 -05:00
Earlopain
d1a70014f9 [ruby/prism] Fix parser translator ast when using anonymous forwarding in blocks/lambda
Blocks and lambdas inherit anonymous arguments from the method they are a part of.
They themselves don't allow to introduce new anonymous arguments.
While you can write this:
```rb
def foo(*)
  bar { |**| }
end
```
referecing the new parameter inside of the block will always be a syntax error.

2cbd27e134
2025-01-11 19:09:05 -05:00
Earlopain
7cbaa3b929 [ruby/prism] Fix an incompatibility with the parser translator
The offset cache contains an entry for each byte so it can't be accessed via the string length.

Adds tests for all variants except for this:
```
"fo
o" "ba
’"
```

For some reason, this still has the wrong offset.

a651126458
2025-01-11 19:09:05 -05:00
Earlopain
c037f5a28c [ruby/prism] Fix parser translator ast for heredoc with written newlines
Heredocs that contain "\\n" don't start a new string node.

61d9d3a15e
2025-01-05 18:12:44 +00:00
Kevin Newton
2ab1b07b84 [ruby/prism] Simplify srange_find in parser compiler
34efacc618
2024-12-16 10:51:22 -05:00
Kevin Newton
cc967a470b [ruby/prism] Add do keyword tracking for While/Until
9686897290
2024-12-16 10:51:22 -05:00