prism 0.22.0 → 0.24.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +39 -1
  3. data/README.md +2 -1
  4. data/docs/releasing.md +67 -17
  5. data/docs/ruby_parser_translation.md +19 -0
  6. data/docs/serialization.md +2 -0
  7. data/ext/prism/api_node.c +1982 -1538
  8. data/ext/prism/extension.c +12 -7
  9. data/ext/prism/extension.h +2 -2
  10. data/include/prism/diagnostic.h +3 -4
  11. data/include/prism/encoding.h +7 -0
  12. data/include/prism/util/pm_constant_pool.h +1 -1
  13. data/include/prism/util/pm_newline_list.h +4 -3
  14. data/include/prism/util/pm_strpbrk.h +4 -1
  15. data/include/prism/version.h +2 -2
  16. data/lib/prism/desugar_compiler.rb +225 -80
  17. data/lib/prism/dsl.rb +302 -299
  18. data/lib/prism/ffi.rb +103 -77
  19. data/lib/prism/lex_compat.rb +1 -0
  20. data/lib/prism/node.rb +3624 -2114
  21. data/lib/prism/node_ext.rb +25 -2
  22. data/lib/prism/parse_result.rb +56 -19
  23. data/lib/prism/serialize.rb +605 -303
  24. data/lib/prism/translation/parser/compiler.rb +1 -1
  25. data/lib/prism/translation/parser/rubocop.rb +11 -3
  26. data/lib/prism/translation/parser.rb +25 -12
  27. data/lib/prism/translation/parser33.rb +12 -0
  28. data/lib/prism/translation/parser34.rb +12 -0
  29. data/lib/prism/translation/ripper.rb +696 -0
  30. data/lib/prism/translation/ruby_parser.rb +1521 -0
  31. data/lib/prism/translation.rb +3 -3
  32. data/lib/prism.rb +0 -1
  33. data/prism.gemspec +6 -2
  34. data/src/diagnostic.c +10 -11
  35. data/src/encoding.c +16 -17
  36. data/src/options.c +7 -2
  37. data/src/prettyprint.c +3 -3
  38. data/src/prism.c +172 -97
  39. data/src/serialize.c +24 -13
  40. data/src/token_type.c +3 -3
  41. data/src/util/pm_constant_pool.c +1 -1
  42. data/src/util/pm_newline_list.c +6 -3
  43. data/src/util/pm_strpbrk.c +122 -14
  44. metadata +8 -4
  45. data/lib/prism/ripper_compat.rb +0 -285
@@ -1477,7 +1477,7 @@ module Prism
1477
1477
  # ^^^^^
1478
1478
  def visit_string_node(node)
1479
1479
  if node.opening&.start_with?("<<")
1480
- children, closing = visit_heredoc(InterpolatedStringNode.new(node.opening_loc, [node.copy(opening_loc: nil, closing_loc: nil, location: node.content_loc)], node.closing_loc, node.location))
1480
+ children, closing = visit_heredoc(InterpolatedStringNode.new(node.send(:source), node.opening_loc, [node.copy(opening_loc: nil, closing_loc: nil, location: node.content_loc)], node.closing_loc, node.location))
1481
1481
  builder.string_compose(token(node.opening_loc), children, closing)
1482
1482
  elsif node.opening == "?"
1483
1483
  builder.character([node.unescaped, srange(node.location)])
@@ -9,18 +9,26 @@ require "prism/translation/parser"
9
9
  module Prism
10
10
  module Translation
11
11
  class Parser
12
- # This is the special version number that should be used in rubocop
12
+ # This is the special version numbers that should be used in RuboCop
13
13
  # configuration files to trigger using prism.
14
+
15
+ # For Ruby 3.3
14
16
  VERSION_3_3 = 80_82_73_83_77.33
15
17
 
18
+ # For Ruby 3.4
19
+ VERSION_3_4 = 80_82_73_83_77.34
20
+
16
21
  # This module gets prepended into RuboCop::AST::ProcessedSource.
17
22
  module ProcessedSource
18
23
  # Redefine parser_class so that we can inject the prism parser into the
19
24
  # list of known parsers.
20
25
  def parser_class(ruby_version)
21
26
  if ruby_version == Prism::Translation::Parser::VERSION_3_3
22
- require "prism/translation/parser"
23
- Prism::Translation::Parser
27
+ require "prism/translation/parser33"
28
+ Prism::Translation::Parser33
29
+ elsif ruby_version == Prism::Translation::Parser::VERSION_3_4
30
+ require "prism/translation/parser34"
31
+ Prism::Translation::Parser34
24
32
  else
25
33
  super
26
34
  end
@@ -43,7 +43,7 @@ module Prism
43
43
  source = source_buffer.source
44
44
 
45
45
  offset_cache = build_offset_cache(source)
46
- result = unwrap(Prism.parse(source, filepath: source_buffer.name), offset_cache)
46
+ result = unwrap(Prism.parse(source, filepath: source_buffer.name, version: convert_for_prism(version)), offset_cache)
47
47
 
48
48
  build_ast(result.value, offset_cache)
49
49
  ensure
@@ -56,7 +56,7 @@ module Prism
56
56
  source = source_buffer.source
57
57
 
58
58
  offset_cache = build_offset_cache(source)
59
- result = unwrap(Prism.parse(source, filepath: source_buffer.name), offset_cache)
59
+ result = unwrap(Prism.parse(source, filepath: source_buffer.name, version: convert_for_prism(version)), offset_cache)
60
60
 
61
61
  [
62
62
  build_ast(result.value, offset_cache),
@@ -75,7 +75,7 @@ module Prism
75
75
  offset_cache = build_offset_cache(source)
76
76
  result =
77
77
  begin
78
- unwrap(Prism.parse_lex(source, filepath: source_buffer.name), offset_cache)
78
+ unwrap(Prism.parse_lex(source, filepath: source_buffer.name, version: convert_for_prism(version)), offset_cache)
79
79
  rescue ::Parser::SyntaxError
80
80
  raise if !recover
81
81
  end
@@ -124,20 +124,21 @@ module Prism
124
124
  # build the parser gem AST.
125
125
  #
126
126
  # If the bytesize of the source is the same as the length, then we can
127
- # just use the offset directly. Otherwise, we build a hash that functions
128
- # as a cache for the conversion.
129
- #
130
- # This is a good opportunity for some optimizations. If the source file
131
- # has any multi-byte characters, this can tank the performance of the
132
- # translator. We could make this significantly faster by using a
133
- # different data structure for the cache.
127
+ # just use the offset directly. Otherwise, we build an array where the
128
+ # index is the byte offset and the value is the character offset.
134
129
  def build_offset_cache(source)
135
130
  if source.bytesize == source.length
136
131
  -> (offset) { offset }
137
132
  else
138
- Hash.new do |hash, offset|
139
- hash[offset] = source.byteslice(0, offset).length
133
+ offset_cache = []
134
+ offset = 0
135
+
136
+ source.each_char do |char|
137
+ char.bytesize.times { offset_cache << offset }
138
+ offset += 1
140
139
  end
140
+
141
+ offset_cache << offset
141
142
  end
142
143
  end
143
144
 
@@ -167,6 +168,18 @@ module Prism
167
168
  )
168
169
  end
169
170
 
171
+ # Converts the version format handled by Parser to the format handled by Prism.
172
+ def convert_for_prism(version)
173
+ case version
174
+ when 33
175
+ "3.3.0"
176
+ when 34
177
+ "3.4.0"
178
+ else
179
+ "latest"
180
+ end
181
+ end
182
+
170
183
  require_relative "parser/compiler"
171
184
  require_relative "parser/lexer"
172
185
 
@@ -0,0 +1,12 @@
1
+ require_relative "parser"
2
+
3
+ module Prism
4
+ module Translation
5
+ # This class is the entry-point for Ruby 3.3 of `Prism::Translation::Parser`.
6
+ class Parser33 < Parser
7
+ def version # :nodoc:
8
+ 33
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ require_relative "parser"
2
+
3
+ module Prism
4
+ module Translation
5
+ # This class is the entry-point for Ruby 3.4 of `Prism::Translation::Parser`.
6
+ class Parser34 < Parser
7
+ def version # :nodoc:
8
+ 34
9
+ end
10
+ end
11
+ end
12
+ end