Commit graph

110 commits

Author SHA1 Message Date
Kevin Newton
f109a83ddf [PRISM] Fix up error message expectations 2024-05-01 19:19:07 -04:00
Kevin Newton
fc8fb581cf [ruby/prism] CRuby error message for trailing underscore in number
4e34f236d3
2024-05-01 19:51:09 +00:00
Kevin Newton
6a296089c6 [ruby/prism] Add a flag on returns when they are redundant
450541d2c3
2024-04-26 18:10:42 +00:00
Kevin Newton
3c0756752c [ruby/prism] Better error message on statement inside argument list
3b1a99526a
2024-04-12 21:46:42 +00:00
Kevin Newton
1521af3259 [ruby/prism] Better error message on invalid def
d398e7d22c
2024-04-12 17:49:17 +00:00
Kevin Newton
52b862398d [ruby/prism] Syntax error for block argument on yield
9feeafbc67
2024-04-12 17:30:37 +00:00
Kevin Newton
0424c1fa7b [ruby/prism] Fix up embdoc lexing on EOF
8ee43be26d
2024-04-12 16:50:34 +00:00
Kevin Newton
0107954f25 [ruby/prism] Fix up invalid global variable error message
8ce9ae487f
2024-04-09 16:29:01 +00:00
Kevin Newton
dcec1e0dc6 [ruby/prism] Replace old circular parameter definition detection
c739f8e194
2024-04-05 19:24:00 +00:00
Kevin Newton
440c63df31 [ruby/prism] Change forwarding error messages to match CRuby
6a15e475c9
2024-04-04 20:59:08 +00:00
Kevin Newton
198d197aeb [ruby/prism] Allow block exits in defined? and fix modifier while/until
2752f0b8df
2024-04-03 17:34:12 -04:00
Kevin Newton
664a5082cb [ruby/prism] Check for syntax errors using RubyVM
9e200dd1c1
2024-04-03 17:34:12 -04:00
Kevin Newton
1153bcde6b [ruby/prism] Fix up errors test
5f86742537
2024-04-03 17:34:12 -04:00
Kevin Newton
3147404d64 [ruby/prism] Match circular parameter error message
c0381b10e4
2024-04-03 17:34:12 -04:00
Kevin Newton
b7597dac93 [ruby/prism] Match more error messages
0cc3a9d63a
2024-04-01 19:28:06 +00:00
Kevin Newton
a9658b6409 [ruby/prism] Do not track locals starting with _
0d5a6d936a
2024-04-01 18:13:45 +00:00
Kevin Newton
ec879e7eac [ruby/prism] Use RubyVM::InstructionSequence instead of Ripper for validity check
ddec1c163d
2024-04-01 18:13:45 +00:00
Kevin Newton
c2735c48a1 [ruby/prism] Track duplicate hash keys for pattern matching
71ea82f299
2024-04-01 18:13:45 +00:00
Kevin Newton
f1e385aad9 [ruby/prism] Track captures in pattern matching for duplicates
aa2182f064
2024-04-01 18:13:44 +00:00
Kevin Newton
cdb8d208c9 [PRISM] Fix error message for duplicate parameter name 2024-03-29 19:32:23 -04:00
Koichi ITO
4a78d75213 [ruby/prism] Fix an incorrect range of Prism::Location when PM_ERR_RETURN_INVALID
This PR fixes the following incorrect range of `Prism::Location` when `PM_ERR_RETURN_INVALID`.

It may be hard to tell from the text, but this Ruby error highlights `return`:

```console
$ ruby -e 'class Foo return end'
-e:1: Invalid return in class/module body
class Foo return end
-e: compile error (SyntaxError)
```

Previously, the error's `Prism::Location` pointed to `end`:

```console
$ bundle exec ruby -Ilib -rprism -ve 'p Prism.parse("class Foo return end").errors'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
[#<Prism::ParseError @type=:return_invalid @message="invalid `return` in a class or module body"
@location=#<Prism::Location @start_offset=17 @length=3 start_line=1> @level=:fatal>]

After this fix, it will indicate `return`.

```console
$ bundle exec ruby -Ilib -rprism -ve 'p Prism.parse("class Foo return end").errors'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
[#<Prism::ParseError @type=:return_invalid @message="invalid `return` in a class or module body"
@location=#<Prism::Location @start_offset=10 @length=6 start_line=1> @level=:fatal>]
```

For reference, here are the before and after of `Prism::Translation::Parser`.

Before:

```console
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("class Foo return end")'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
(string):1:18: error: invalid `return` in a class or module body
(string):1: class Foo return end
(string):1:                  ^~~
/Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/diagnostic/engine.rb:72:in `process':
invalid `return` in a class or module body (Parser::SyntaxError)
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:220:in `block in unwrap'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:218:in `each'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:218:in `unwrap'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:49:in `parse'
        from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:33:in `parse'
        from -e:1:in `<main>'
```

After:

```console
$ bundle exec ruby -Ilib -rprism -rprism/translation/parser33 -ve 'p Prism::Translation::Parser33.parse("class Foo return end")'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
(string):1:11: error: invalid `return` in a class or module body
(string):1: class Foo return end
(string):1:           ^~~~~~
/Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/diagnostic/engine.rb:72:in `process':
invalid `return` in a class or module body (Parser::SyntaxError)
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:220:in `block in unwrap'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:218:in `each'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:218:in `unwrap'
        from /Users/koic/src/github.com/ruby/prism/lib/prism/translation/parser.rb:49:in `parse'
        from /Users/koic/.rbenv/versions/3.3.0/lib/ruby/gems/3.3.0/gems/parser-3.3.0.5/lib/parser/base.rb:33:in `parse'
        from -e:1:in `<main>'
```

This PR ensures that the originally intended `return` is highlighted as it should be.

1f9af4d2ad
2024-03-26 14:41:27 -04:00
Koichi ITO
56a2fad2a4 [ruby/prism] Fix incorrect paring when using invalid regexp options
Fixes https://github.com/ruby/prism/pull/2617.

There was an issue with the lexer as follows.
The following are valid regexp options:

```console
$ bundle exec ruby -Ilib -rprism -ve 'p Prism.lex("/x/io").value.map {|token| token[0].type }'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
[:REGEXP_BEGIN, :STRING_CONTENT, :REGEXP_END, :EOF]
```

The following are invalid regexp options. Unnecessary the `IDENTIFIER` token is appearing:

```console
$ bundle exec ruby -Ilib -rprism -ve 'p Prism.lex("/x/az").value.map {|token| token[0].type }'
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-darwin22]
[:REGEXP_BEGIN, :STRING_CONTENT, :REGEXP_END, :IDENTIFIER, :EOF]
```

As a behavior of Ruby, when given `A` to `Z` and `a` to `z`, they act as invalid regexp options. e.g.,

```console
$ ruby -e '/regexp/az'
-e:1: unknown regexp options - az
/regexp/az
-e: compile error (SyntaxError)
```

Thus, it should probably not be construed as `IDENTIFIER` token.

Therefore, `pm_byte_table` has been adapted to accept those invalid regexp option values.
Whether it is a valid regexp option or not is checked by `pm_regular_expression_flags_create`.
For invalid regexp options, `PM_ERR_REGEXP_UNKNOWN_OPTIONS` is added to diagnostics.

d2a6096fcf
2024-03-25 12:16:32 +00:00
Kevin Newton
83790e5fe1 [ruby/prism] Consolidate warnings for conditional predicates
* Also add warnings for literals in predicates
* Also create flip-flops in while/until

a6b5c523c2
2024-03-12 17:32:06 +00:00
Kevin Newton
af0a6ea1d5 [ruby/prism] Add an IntegerField for parsing integer values
120d8c0479
2024-02-22 22:42:44 -05:00
Haldun Bayhantopcu
f4f57e1162 [ruby/prism] Add warning for assignments to literals in conditionals
ee87ed08fb
2024-02-16 15:33:07 +00:00
Yusuke Endoh
d5c16ddfcb Temporarily update the error message format in prism 2024-02-15 18:42:31 +09:00
Kevin Newton
e08c128417 [ruby/prism] Error messages closer to CRuby
19ffa0b980
2024-02-12 18:01:45 +00:00
Kevin Newton
aad3c36bdf [ruby/prism] Support for Ruby 2.7
1a15b70a8e
2024-02-07 16:54:34 +00:00
Kevin Newton
494778c663 [ruby/prism] Remove locals_body_index
We're not using this anymore, and it doesn't make a lot of sense
outside the context of a compiler anyway, and in anyway it's wrong
when you have local variables written in default values.

5edbd9c25b
2024-02-01 16:48:55 +00:00
Haldun Bayhantopcu
67c5690a6d [ruby/prism] Check literals for receiver
56441b08e7
2024-02-01 16:48:09 +00:00
Aaron Patterson
8e708e4a07 Update forwarding locals for prism 2024-01-30 13:19:06 -05:00
Kevin Newton
ba06a8259a [ruby/prism] Better error messages for unexpected tokens in prefix
a35b8e45ee
2024-01-30 16:10:08 +00:00
Kevin Newton
e337c9478a [ruby/prism] Error follow-up
Split up the diagnostic levels so that error and warning levels
aren't mixed. Also fix up deconstruct_keys implementation.

bd3eeb308d

Co-authored-by: Benoit Daloze <eregontp@gmail.com>
2024-01-27 18:46:16 +00:00
Benoit Daloze
de135bc247 [ruby/prism] Add level to warnings and errors to categorize them
* Fixes https://github.com/ruby/prism/issues/2082

7a74576357
2024-01-26 21:34:34 +00:00
Kevin Menard
2a509787cb [ruby/prism] Track whether a Symbol should have its encoding changed from the source encoding.
Ruby sets a Symbol literal's encoding to US-ASCII if the symbols consists only of US ASCII code points. Character escapes can also lead a Symbol to have a different encoding than its source's encoding.

f315660b31
2024-01-26 20:15:19 +00:00
Takashi Kokubun
603f2ca730 [ruby/prism] Parse it default parameter
a0c5361b9f
2024-01-17 17:47:33 +00:00
Aaron Patterson
881c5a1846 [ruby/prism] Add a "repeated flag" to parameter nodes
It's possible to repeat parameters in method definitions like so:

```ruby
def foo(_a, _a)
end
```

The compiler needs to know to adjust the local table size to account for
these duplicate names.  We'll use the repeated parameter flag to account
for the extra stack space required

b443cb1f60

Co-Authored-By: Kevin Newton <kddnewton@gmail.com>
Co-Authored-By: Jemma Issroff <jemmaissroff@gmail.com>
2024-01-10 15:24:26 +00:00
Kevin Newton
d1d50a0505 [ruby/prism] Handle parsing a line break in a receiver of a method
4d5f43ecbc
2024-01-08 14:34:59 +00:00
Kevin Newton
0215965df4 [ruby/prism] Better error recovery for content after unterminated heredoc
c2d325a886
2024-01-03 16:46:08 +00:00
Kevin Newton
adbfbd822f [ruby/prism] Ignore visibility flag
55b049ddac
2024-01-02 20:59:19 +00:00
TSUYUSATO Kitsune
0ee625ceae [ruby/prism] Fix to check multiple block arguments for forwarding arg
Fix https://github.com/ruby/prism/pull/2111

21ca243d0a
2024-01-02 19:08:15 +00:00
Hiroshi SHIBATA
fa251d60aa Revert "Revert all of commits after Prism 0.19.0 release"
This reverts commit d242e8416e.
2023-12-25 21:12:49 +09:00
Hiroshi SHIBATA
d242e8416e
Revert all of commits after Prism 0.19.0 release
We should bundle released version of Prism for Ruby 3.3.0
2023-12-16 11:08:51 +08:00
TSUYUSATO Kitsune
16830a4783 [ruby/prism] Add an error for in keyword in arguments
Fix https://github.com/ruby/prism/pull/2026

c4b41cd477
2023-12-15 13:25:54 +00:00
TSUYUSATO Kitsune
3658798dbb [ruby/prism] Make equality operators non-associative
Fix https://github.com/ruby/prism/pull/2073

0f747d9240
2023-12-14 16:39:05 +00:00
Ufuk Kayserilioglu
d313c82f79 [ruby/prism] Update tests and snapshots
0663e2bcfa
2023-12-12 13:05:09 +00:00
TSUYUSATO Kitsune
48cb70fee9 [ruby/prism] Fix parsing unterminated empty string "
Fix https://github.com/ruby/prism/pull/2034

8280e577fa
2023-12-11 13:36:37 +00:00
TSUYUSATO Kitsune
a860e3605c [ruby/prism] Fix to parse a (endless-)range with binary operators
Fix https://github.com/ruby/prism/pull/2022
Fix https://github.com/ruby/prism/pull/2030

b78d8b6525
2023-12-11 13:34:48 +00:00
Alex Koval
04eb1b6f26 [ruby/prism] fix: escape newline
a28b427dcc
2023-12-10 03:06:36 +00:00
Kevin Newton
98e3552cfb [ruby/prism] Add necessary encoding flags for symbols and regex
This doesn't actually fix the encodings for symbols and regex,
unfortunately. But I wanted to get this change in because it is
the last AST change we're going to make before 3.3 is released.

So, if consumers want, they can start to check these flags to
determine the encoding, even though it will be wrong. Then once we
actually set them correctly, everything should work.

9b35f7e891
2023-12-08 18:59:52 +00:00