Commit graph

54 commits

Author SHA1 Message Date
Nobuyoshi Nakada
991cf2dd4d [ruby/prism] [DOC] Specify markdown mode to RDoc
12af4e144e
2025-05-29 04:45:58 +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
34e68548d4 [ruby/prism] Bump typechecking deps
230c8b8a48
2024-12-11 19:50:20 +00:00
Kevin Newton
7a198af7cd [ruby/prism] Prism::CodeUnitsCache
Calculating code unit offsets for a source can be very expensive,
especially when the source is large. This commit introduces a new
class that wraps the source and desired encoding into a cache that
reuses pre-computed offsets. It performs quite a bit better.

There are still some problems with this approach, namely character
boundaries and the fact that the cache is unbounded, but both of
these may be addressed in subsequent commits.

2e3e1a4d4d
2024-10-10 18:02:27 +00:00
Kevin Newton
e39e582594 [ruby/prism] Attempt to assume binary is UTF-8
343197e4ff
2024-10-09 15:42:23 +00:00
Vinicius Stock
e50754fcfa [ruby/prism] Avoid breaking code units offset on binary encoding
25a4cf6794

Co-authored-by: Kevin Newton <kddnewton@users.noreply.github.com>
2024-10-09 14:07:10 +00:00
ydah
dbb8f97eaa [ruby/prism] essentialy ==> essentially
9c68c01bcd
2024-09-04 13:13:37 +00:00
Koichi ITO
88954a0e9a [ruby/prism] Tweak inspect representation of Prism::Location
This PR tweaks inspect representation of `Prism::Location`.

## Before

During debugging, the meaning of `@location=2147483648` was unclear:

```console
$ ruby -Ilib -rprism -e 'p Prism.lex("puts :hi").value.map(&:first)[1]'
#<Prism::Token:0x000000010cd74e40 @source=#<Prism::ASCIISource:0x000000010cb5f808 @source="puts :hi", @start_line=1, @offsets=[0]>,
@type=:SYMBOL_BEGIN, @value=":", @location=2147483648>
```

## After

This PR clarifies the contents of the location object, aligning with what I think user expects:

```console
$ ruby -Ilib -rprism -e 'p Prism.lex("puts :hi").value.map(&:first)[1]'
#<Prism::Token:0x000000010e174d50 @source=#<Prism::ASCIISource:0x000000010df5efe8 @source="puts :hi", @start_line=1, @offsets=[0]>,
@type=:SYMBOL_BEGIN, @value=":", @location=#<Prism::Location @start_offset=5 @length=1 start_line=1>>
```

Although it is uncertain whether Prism will accept this change in the inspect representation, it is submitted here as a suggestion.

e7421ce1c5
2024-08-14 16:28:43 +00:00
Kevin Newton
aa473489a2 [ruby/prism] Various cleanup for initializers and typechecks
86cf82794a
2024-07-11 14:25:54 -04:00
Kevin Newton
ca48fb76fb [ruby/prism] Move location to second position for node initializers
4cc0eda4ca
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
e6340258f8 [ruby/prism] Autoload newlines and comment visitors
Having the @newline instance variable in every node adds up, and
since it is so rarely used, we only want to add it when necessary.

Moving this into an autoloaded file and moving the instance variable
out of the default initializers reduces allocated memory because the
nodes are now smaller and some fit into the compact list. On my
machine, I'm seeing about an 8% drop.

eea92c07d2
2024-05-13 15:34:47 +00:00
Kevin Newton
d4a6d0c177 [ruby/prism] Prism::Location#adjoin
a298db68e3
2024-05-03 19:14:03 +00:00
Vinicius Stock
4fbb208185 [ruby/prism] Create specialized ASCIISource with asciionly optimizations
40993166a8
2024-05-03 18:10:21 +00:00
Kevin Newton
7c0cf71049 [ruby/prism] Node#script_lines and supporting infra
cb4a8ab772
2024-05-02 15:27:18 +00:00
Kevin Newton
9688093124 [ruby/prism] Location#slice_lines, Node#slice_lines
9b61f6fdb3
2024-04-26 19:05:32 +00:00
Kevin Newton
23be6599a2 [ruby/prism] Split parse result based on type
17194e096d
2024-04-19 19:25:32 +00:00
Kevin Newton
cd516ebd20 [ruby/prism] Add Location#chop
5dd57f4b84
2024-04-11 18:53:30 +00:00
Kevin Newton
d266b71467 [ruby/prism] Use the diagnostic types in the parser translation layer
1a8a0063dc
2024-03-06 21:42:54 -05:00
Kevin Newton
38c2774420 [ruby/prism] Expose types on diagnostics
a735c2262f
2024-03-06 21:42:54 -05:00
Ufuk Kayserilioglu
de411ef0b6 [ruby/prism] Small changes to make type-checking pass
5b2970e75b
2024-03-06 21:37:54 +00:00
Ufuk Kayserilioglu
8dfe0c7c28 [ruby/prism] Fix some type-checking errors by using different method calls
For example, use `.fetch` or `.dig` instead of `[]`, and use `===` instead of `is_a?` for checking types of objects.

548b54915f
2024-03-06 21:37:52 +00:00
Kevin Newton
804b2a3787 [ruby/prism] Rebase against main
813e20d449
2024-02-24 03:39:29 +00:00
Gopal Patel
aa8841405b [ruby/prism] Less code modifications. More steep:ignore for now
7905bdbf83
2024-02-24 03:39:28 +00:00
Gopal Patel
66565e36ea [ruby/prism] Add documentation for Location#source!
467e1cc2c4
2024-02-24 03:39:26 +00:00
Gopal Patel
8fa1843523 [ruby/prism] Relax Location#source to be optional
9f00fe7510
2024-02-24 03:39:26 +00:00
Gopal Patel
935d4fab62 [ruby/prism] Remove Ripper from public RBS, type-assert remaining issues
5fda7a0760
2024-02-24 03:39:23 +00:00
Gopal Patel
7556fd937c [ruby/prism] Split private types
0209d093ec
2024-02-24 03:39:22 +00:00
Kevin Newton
ae3e82a525 [ruby/prism] Fix up comment state
c6561becf0
2024-02-19 19:50:46 +00:00
Kevin Newton
792804e32f [ruby/prism] Split up comments between leading and trailing
Also make them lazy to allocate the array, and also expose ParseResult#encoding.

08ec7683ae
2024-02-18 20:57:13 +00:00
Benoit Daloze
1b9b960963 [ruby/prism] Make location methods thread-safe
* Before it could result in NoMethodError if multiple threads were
  calling location methods: https://gist.github.com/eregon/b78b7f266d7ee0a278a389cfd1782232

ff762dcccd
2024-02-15 23:04:38 +00:00
Kevin Newton
14a7277da1 [ruby/prism] Speed up creating Ruby AST
When creating the Ruby AST, we were previously allocating Location
objects for every node and every inner location. Instead, this
commit changes it to pack both the start offset and length into a
single u64 and pass that into the nodes. Then, when the locations
are requested via a reader method, we lazily allocate the Location
objects.

de203dca83

Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org>
2024-02-15 20:39:50 +00:00
Benoit Daloze
c2d8d6eba6 Initialize the Prism::Source directly with all 3 fields for the C extension
* Faster that way:
  $ ruby -Ilib -rprism -rbenchmark/ips -e 'Benchmark.ips { |x| x.report("parse") { Prism.parse("1 + 2") } }'
  195.722k (± 0.5%) i/s
  rb_iv_set():
  179.609k (± 0.5%) i/s
  rb_funcall():
  190.030k (± 0.3%) i/s
  before this PR:
  183.319k (± 0.4%) i/s
2024-02-14 15:48:33 +00:00
Benoit Daloze
1b2708b123 [ruby/prism] Remove attr_writer's for ParseResult#start_line and #offsets
* As the user should not set these.
* Use #instance_variable_set/rb_iv_set() instead internally.

cace09fb8c
2024-02-14 15:48:33 +00:00
Benoit Daloze
f0f6ffef42 [ruby/prism] Serialize the newline_list to avoid recomputing it again later
* Fixes https://github.com/ruby/prism/issues/2380

4eaaa90114
2024-02-14 15:48:32 +00:00
Kevin Newton
b1964a9204 [ruby/prism] Add code unit APIs to location
LSPs need this because the protocol dictates that you return code
units for offsets. None of our existing APIs provided that
information, and since we hid the source it's not nearly as useful
for them. Now they can pass an encoding directly to:

* Location#start_code_units_offset
* Location#end_code_units_offset
* Location#start_code_units_column
* Location#end_code_units_column

4757a2cc06

Co-Authored-By: Vinicius Stock <vinicius.stock@shopify.com>
2024-02-13 20:10:25 +00:00
Kevin Newton
aad3c36bdf [ruby/prism] Support for Ruby 2.7
1a15b70a8e
2024-02-07 16:54:34 +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
c2e2d2398b [ruby/prism] Call #inspect on diagnostic levels
* So it's clear it is a Symbol.
  Before:
  ... @level=warning_verbose_true>
  After:
  ... @level=:warning_verbose_true>

84503643b9
2024-01-27 18:41:07 +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 Newton
80da9b1547 [ruby/prism] Clarify __END__ comment
3e36d5eabc
2024-01-09 19:02:26 +00:00
Kevin Newton
c798943a4a [ruby/prism] Move DATA parsing into its own parse result field
42b60b6e95
2023-11-28 13:25:48 +00:00
Kevin Newton
f2ed7eaba0 [ruby/prism] Add character APIs for locations
(https://github.com/ruby/prism/pull/1809)

d493ccd093
2023-11-20 16:07:06 +00:00
Kevin Newton
4b5f516f2e [ruby/prism] Split comment
We were previously holding a type field on Comment to tell what
kind of comment it was. Instead, let's just use actual classes for
this.

e76830ca6e
2023-11-03 14:35:08 +00:00
Kevin Newton
8587d9a8bf
[ruby/prism] Wire up options through the Java parser
13fa262669
2023-11-03 10:13:50 -04:00
Kevin Newton
d7d3243364
[ruby/prism] Properly support the start line option
33cc75a4b7
2023-11-03 10:13:50 -04:00
Kevin Newton
79034fbd50 [ruby/prism] More Ruby docs
ca9a660f52
2023-11-01 13:10:29 -04:00
Kevin Newton
953138698e [ruby/prism] Docs for node.rb and parse_result.rb
085da4feb9
2023-11-01 13:10:29 -04:00
Kevin Newton
d1bb858d47 [ruby/prism] Match existing Ruby prettyprint
6d8358c083
2023-10-26 15:19:43 -04:00