Commit graph

132 commits

Author SHA1 Message Date
Kevin Newton
127325a4ba [ruby/prism] No writing to numbered parameters
Fixes [Bug #21117]

19d4bab5a0
2025-02-13 20:04:02 +00:00
Matt Valentine-House
39d0ebb6e8 [ruby/prism] Blocks & kwargs are not valid in index assignments
Ruby feature: https://bugs.ruby-lang.org/issues/20952

e612df5f36
2024-12-16 15:00:27 +00:00
Aaron Patterson
6877c38866 [ruby/prism] Fix error messages for unterminated ( and {
If we hit an EOF token, and the character before the EOF is a newline,
we should make EOF token start at the previous newline.  That way any
errors reported will occur on that line.

For example "foo(\n" should report an error on line 1 even though the
EOF technically occurs on line 2.

[Bug #20918]
https://bugs.ruby-lang.org/issues/20918

60bc43de8e
2024-12-05 18:42:01 +00:00
Kevin Newton
24770c30f8 [ruby/prism] Sync from ruby/ruby
280517c325
2024-07-25 18:59:10 +00:00
Kevin Newton
aa473489a2 [ruby/prism] Various cleanup for initializers and typechecks
86cf82794a
2024-07-11 14:25:54 -04:00
Kevin Newton
678dd769e5 [ruby/prism] Reconfigure error tests
fb7e1ebb7f
2024-07-11 14:25:54 -04:00
Kevin Newton
ce7299d09a [ruby/prism] Handle block exits under modifiers
6b78f5309b
2024-06-26 20:46:27 +00:00
Kevin Newton
7529591df1 [ruby/prism] Ensure ranges are non-associative
f59295938b
2024-06-14 19:22:54 +00:00
Kevin Newton
47322b592a [ruby/prism] Match CRuby error message for unknown regexp options
73669b59f6
2024-06-11 12:34:00 +00:00
Kevin Newton
cbc83c4a92 Remove circular parameter syntax error
https://bugs.ruby-lang.org/issues/20478
2024-06-06 16:29:50 -04:00
Kevin Newton
72452f4387 [ruby/prism] Tests overhaul
6f886be0a4
2024-05-30 15:18:20 -04:00
eileencodes
c96b5791ef [ruby/prism] Raise void value expression in begin clauses
In some cases Prism was either not raising an appropriate `void value
expression` error, or raising that error when the syntax is considered
valid.

To fix this Prism needs to check whether we have other clauses on the
`begin` rather than just returning `cast->statements`.

* If the `cast->statements` are null and the `cast->ensure_clause` is
not null, set the code to `cast->ensure_clause`
* else
  * If there is a `cast->rescue_clause`
    * Check if `cast->statements` are null and `cast->rescue_clause->statements`
    are null, and return `NULL`
    * Check if there is an `else_clause`, and set the node to
      `cast->else_clause`.
    * Otherwise return `cast->statements` as the node
  * return `cast->statements` as the node

See tests for test cases. Note I took these directly from CRuby so if
desired I can delete them since the test will now pass. This only fixes
one test in the `test_parse` file, taking failures from 14 to 13.

This fixes `TestParse#test_void_value_in_rhs` and is related to
issue #2791.

398152b412
2024-05-23 19:30:03 +00:00
Kevin Newton
e575954887 [ruby/prism] Fix support for 'it' implicit local variable
53bbcfe513
2024-05-22 16:34:04 -04:00
Kevin Newton
ff43b4a28b [ruby/prism] Add error for numbered parameter used in inner block
c386ba6d48
2024-05-21 15:46:45 +00:00
Kevin Newton
98e1e610f6 [ruby/prism] Update more escape error messages to match CRuby
ab43b3ab66
2024-05-16 18:40:20 +00:00
Kevin Newton
910bb49230 [ruby/prism] Update error message for invalid % at EOF
178d4f66fd
2024-05-16 14:52:34 +00:00
Kevin Newton
7a4b9d1614 [ruby/prism] Match CRuby error message for unknown % string
863197629c
2024-05-16 14:23:36 +00:00
Kevin Newton
011668883c [ruby/prism] Always go to string lex mode even if EOF
57d5c9be2c
2024-05-16 14:16:32 +00:00
Kevin Newton
c4a5866798 [ruby/prism] Replace end of file with end-of-input
dd532ded95
2024-05-15 15:44:16 +00:00
Kevin Newton
83f481ebf0 [ruby/prism] Enhance parameter forwarding error messages
826657232e
2024-05-10 15:54:15 +00:00
Kevin Newton
21b94ae0d1 [ruby/prism] Extend error message for unterminated HEREDOC
c1b07ec11b
2024-05-07 18:03:03 +00:00
Kevin Newton
7caeff2bae [ruby/prism] Disallow keywords and blocks in index expressions
e950dc1e83
2024-05-03 13:01:40 -04:00
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