prism 0.28.0 → 0.30.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 (73) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +41 -1
  3. data/CONTRIBUTING.md +0 -4
  4. data/README.md +1 -0
  5. data/config.yml +95 -26
  6. data/docs/fuzzing.md +1 -1
  7. data/docs/ripper_translation.md +22 -0
  8. data/ext/prism/api_node.c +70 -52
  9. data/ext/prism/extconf.rb +27 -23
  10. data/ext/prism/extension.c +107 -372
  11. data/ext/prism/extension.h +1 -1
  12. data/include/prism/ast.h +170 -102
  13. data/include/prism/diagnostic.h +18 -3
  14. data/include/prism/node.h +0 -21
  15. data/include/prism/parser.h +23 -25
  16. data/include/prism/regexp.h +17 -8
  17. data/include/prism/static_literals.h +3 -2
  18. data/include/prism/util/pm_char.h +1 -2
  19. data/include/prism/util/pm_constant_pool.h +0 -8
  20. data/include/prism/util/pm_integer.h +16 -9
  21. data/include/prism/util/pm_string.h +0 -8
  22. data/include/prism/version.h +2 -2
  23. data/include/prism.h +0 -11
  24. data/lib/prism/compiler.rb +3 -0
  25. data/lib/prism/desugar_compiler.rb +4 -4
  26. data/lib/prism/dispatcher.rb +14 -0
  27. data/lib/prism/dot_visitor.rb +54 -35
  28. data/lib/prism/dsl.rb +23 -18
  29. data/lib/prism/ffi.rb +25 -4
  30. data/lib/prism/inspect_visitor.rb +26 -24
  31. data/lib/prism/mutation_compiler.rb +6 -1
  32. data/lib/prism/node.rb +314 -389
  33. data/lib/prism/node_ext.rb +175 -17
  34. data/lib/prism/parse_result/comments.rb +1 -8
  35. data/lib/prism/parse_result/newlines.rb +102 -12
  36. data/lib/prism/parse_result.rb +17 -0
  37. data/lib/prism/reflection.rb +11 -9
  38. data/lib/prism/serialize.rb +91 -68
  39. data/lib/prism/translation/parser/compiler.rb +288 -138
  40. data/lib/prism/translation/parser.rb +7 -2
  41. data/lib/prism/translation/ripper.rb +24 -22
  42. data/lib/prism/translation/ruby_parser.rb +32 -14
  43. data/lib/prism/visitor.rb +3 -0
  44. data/lib/prism.rb +0 -4
  45. data/prism.gemspec +2 -4
  46. data/rbi/prism/node.rbi +114 -57
  47. data/rbi/prism/node_ext.rbi +5 -0
  48. data/rbi/prism/parse_result.rbi +1 -1
  49. data/rbi/prism/visitor.rbi +3 -0
  50. data/rbi/prism.rbi +6 -0
  51. data/sig/prism/dsl.rbs +13 -10
  52. data/sig/prism/lex_compat.rbs +10 -0
  53. data/sig/prism/mutation_compiler.rbs +1 -0
  54. data/sig/prism/node.rbs +72 -48
  55. data/sig/prism/node_ext.rbs +4 -0
  56. data/sig/prism/visitor.rbs +1 -0
  57. data/sig/prism.rbs +21 -0
  58. data/src/diagnostic.c +56 -27
  59. data/src/node.c +432 -1690
  60. data/src/prettyprint.c +97 -54
  61. data/src/prism.c +1286 -1196
  62. data/src/regexp.c +133 -68
  63. data/src/serialize.c +22 -17
  64. data/src/static_literals.c +63 -84
  65. data/src/token_type.c +4 -4
  66. data/src/util/pm_constant_pool.c +0 -8
  67. data/src/util/pm_integer.c +39 -11
  68. data/src/util/pm_string.c +0 -12
  69. data/src/util/pm_strpbrk.c +32 -6
  70. metadata +3 -5
  71. data/include/prism/util/pm_string_list.h +0 -44
  72. data/lib/prism/debug.rb +0 -249
  73. data/src/util/pm_string_list.c +0 -28
data/lib/prism/ffi.rb CHANGED
@@ -200,8 +200,8 @@ module Prism
200
200
 
201
201
  class << self
202
202
  # Mirror the Prism.dump API by using the serialization API.
203
- def dump(code, **options)
204
- LibRubyParser::PrismString.with_string(code) { |string| dump_common(string, options) }
203
+ def dump(source, **options)
204
+ LibRubyParser::PrismString.with_string(source) { |string| dump_common(string, options) }
205
205
  end
206
206
 
207
207
  # Mirror the Prism.dump_file API by using the serialization API.
@@ -302,6 +302,27 @@ module Prism
302
302
  !parse_file_success?(filepath, **options)
303
303
  end
304
304
 
305
+ # Mirror the Prism.profile API by using the serialization API.
306
+ def profile(source, **options)
307
+ LibRubyParser::PrismString.with_string(source) do |string|
308
+ LibRubyParser::PrismBuffer.with do |buffer|
309
+ LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
310
+ nil
311
+ end
312
+ end
313
+ end
314
+
315
+ # Mirror the Prism.profile_file API by using the serialization API.
316
+ def profile_file(filepath, **options)
317
+ LibRubyParser::PrismString.with_file(filepath) do |string|
318
+ LibRubyParser::PrismBuffer.with do |buffer|
319
+ options[:filepath] = filepath
320
+ LibRubyParser.pm_serialize_parse(buffer.pointer, string.pointer, string.length, dump_options(options))
321
+ nil
322
+ end
323
+ end
324
+ end
325
+
305
326
  private
306
327
 
307
328
  def dump_common(string, options) # :nodoc:
@@ -394,7 +415,7 @@ module Prism
394
415
 
395
416
  template << "L"
396
417
  if (encoding = options[:encoding])
397
- name = encoding.name
418
+ name = encoding.is_a?(Encoding) ? encoding.name : encoding
398
419
  values.push(name.bytesize, name.b)
399
420
  template << "A*"
400
421
  else
@@ -408,7 +429,7 @@ module Prism
408
429
  values << dump_options_command_line(options)
409
430
 
410
431
  template << "C"
411
- values << { nil => 0, "3.3.0" => 1, "3.4.0" => 0, "latest" => 0 }.fetch(options[:version])
432
+ values << { nil => 0, "3.3.0" => 1, "3.3.1" => 1, "3.4.0" => 0, "latest" => 0 }.fetch(options[:version])
412
433
 
413
434
  template << "L"
414
435
  if (scopes = options[:scopes])
@@ -396,8 +396,8 @@ module Prism
396
396
  commands << ["├── message_loc: #{inspect_location(node.message_loc)}\n", indent]
397
397
  commands << ["├── read_name: #{node.read_name.inspect}\n", indent]
398
398
  commands << ["├── write_name: #{node.write_name.inspect}\n", indent]
399
- commands << ["├── operator: #{node.operator.inspect}\n", indent]
400
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
399
+ commands << ["├── binary_operator: #{node.binary_operator.inspect}\n", indent]
400
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
401
401
  commands << ["└── value:\n", indent]
402
402
  commands << [node.value, "#{indent} "]
403
403
  end
@@ -539,10 +539,10 @@ module Prism
539
539
  commands << [inspect_node("ClassVariableOperatorWriteNode", node), indent]
540
540
  commands << ["├── name: #{node.name.inspect}\n", indent]
541
541
  commands << ["├── name_loc: #{inspect_location(node.name_loc)}\n", indent]
542
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
542
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
543
543
  commands << ["├── value:\n", indent]
544
544
  commands << [node.value, "#{indent}│ "]
545
- commands << ["└── operator: #{node.operator.inspect}\n", indent]
545
+ commands << ["└── binary_operator: #{node.binary_operator.inspect}\n", indent]
546
546
  end
547
547
 
548
548
  # Inspect a ClassVariableOrWriteNode node.
@@ -592,10 +592,10 @@ module Prism
592
592
  commands << [inspect_node("ConstantOperatorWriteNode", node), indent]
593
593
  commands << ["├── name: #{node.name.inspect}\n", indent]
594
594
  commands << ["├── name_loc: #{inspect_location(node.name_loc)}\n", indent]
595
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
595
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
596
596
  commands << ["├── value:\n", indent]
597
597
  commands << [node.value, "#{indent}│ "]
598
- commands << ["└── operator: #{node.operator.inspect}\n", indent]
598
+ commands << ["└── binary_operator: #{node.binary_operator.inspect}\n", indent]
599
599
  end
600
600
 
601
601
  # Inspect a ConstantOrWriteNode node.
@@ -641,10 +641,10 @@ module Prism
641
641
  commands << [inspect_node("ConstantPathOperatorWriteNode", node), indent]
642
642
  commands << ["├── target:\n", indent]
643
643
  commands << [node.target, "#{indent}│ "]
644
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
644
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
645
645
  commands << ["├── value:\n", indent]
646
646
  commands << [node.value, "#{indent}│ "]
647
- commands << ["└── operator: #{node.operator.inspect}\n", indent]
647
+ commands << ["└── binary_operator: #{node.binary_operator.inspect}\n", indent]
648
648
  end
649
649
 
650
650
  # Inspect a ConstantPathOrWriteNode node.
@@ -908,10 +908,10 @@ module Prism
908
908
  commands << [inspect_node("GlobalVariableOperatorWriteNode", node), indent]
909
909
  commands << ["├── name: #{node.name.inspect}\n", indent]
910
910
  commands << ["├── name_loc: #{inspect_location(node.name_loc)}\n", indent]
911
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
911
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
912
912
  commands << ["├── value:\n", indent]
913
913
  commands << [node.value, "#{indent}│ "]
914
- commands << ["└── operator: #{node.operator.inspect}\n", indent]
914
+ commands << ["└── binary_operator: #{node.binary_operator.inspect}\n", indent]
915
915
  end
916
916
 
917
917
  # Inspect a GlobalVariableOrWriteNode node.
@@ -1103,8 +1103,8 @@ module Prism
1103
1103
  commands << ["├── block:\n", indent]
1104
1104
  commands << [block, "#{indent}│ "]
1105
1105
  end
1106
- commands << ["├── operator: #{node.operator.inspect}\n", indent]
1107
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
1106
+ commands << ["├── binary_operator: #{node.binary_operator.inspect}\n", indent]
1107
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
1108
1108
  commands << ["└── value:\n", indent]
1109
1109
  commands << [node.value, "#{indent} "]
1110
1110
  end
@@ -1178,10 +1178,10 @@ module Prism
1178
1178
  commands << [inspect_node("InstanceVariableOperatorWriteNode", node), indent]
1179
1179
  commands << ["├── name: #{node.name.inspect}\n", indent]
1180
1180
  commands << ["├── name_loc: #{inspect_location(node.name_loc)}\n", indent]
1181
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
1181
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
1182
1182
  commands << ["├── value:\n", indent]
1183
1183
  commands << [node.value, "#{indent}│ "]
1184
- commands << ["└── operator: #{node.operator.inspect}\n", indent]
1184
+ commands << ["└── binary_operator: #{node.binary_operator.inspect}\n", indent]
1185
1185
  end
1186
1186
 
1187
1187
  # Inspect a InstanceVariableOrWriteNode node.
@@ -1310,6 +1310,11 @@ module Prism
1310
1310
  commands << ["└── closing_loc: #{inspect_location(node.closing_loc)}\n", indent]
1311
1311
  end
1312
1312
 
1313
+ # Inspect a ItLocalVariableReadNode node.
1314
+ def visit_it_local_variable_read_node(node)
1315
+ commands << [inspect_node("ItLocalVariableReadNode", node), indent]
1316
+ end
1317
+
1313
1318
  # Inspect a ItParametersNode node.
1314
1319
  def visit_it_parameters_node(node)
1315
1320
  commands << [inspect_node("ItParametersNode", node), indent]
@@ -1381,11 +1386,11 @@ module Prism
1381
1386
  def visit_local_variable_operator_write_node(node)
1382
1387
  commands << [inspect_node("LocalVariableOperatorWriteNode", node), indent]
1383
1388
  commands << ["├── name_loc: #{inspect_location(node.name_loc)}\n", indent]
1384
- commands << ["├── operator_loc: #{inspect_location(node.operator_loc)}\n", indent]
1389
+ commands << ["├── binary_operator_loc: #{inspect_location(node.binary_operator_loc)}\n", indent]
1385
1390
  commands << ["├── value:\n", indent]
1386
1391
  commands << [node.value, "#{indent}│ "]
1387
1392
  commands << ["├── name: #{node.name.inspect}\n", indent]
1388
- commands << ["├── operator: #{node.operator.inspect}\n", indent]
1393
+ commands << ["├── binary_operator: #{node.binary_operator.inspect}\n", indent]
1389
1394
  commands << ["└── depth: #{node.depth.inspect}\n", indent]
1390
1395
  end
1391
1396
 
@@ -1777,8 +1782,10 @@ module Prism
1777
1782
  # Inspect a RationalNode node.
1778
1783
  def visit_rational_node(node)
1779
1784
  commands << [inspect_node("RationalNode", node), indent]
1780
- commands << ["└── numeric:\n", indent]
1781
- commands << [node.numeric, "#{indent} "]
1785
+ flags = [("binary" if node.binary?), ("decimal" if node.decimal?), ("octal" if node.octal?), ("hexadecimal" if node.hexadecimal?)].compact
1786
+ commands << ["├── flags: #{flags.empty? ? "∅" : flags.join(", ")}\n", indent]
1787
+ commands << ["├── numerator: #{node.numerator.inspect}\n", indent]
1788
+ commands << ["└── denominator: #{node.denominator.inspect}\n", indent]
1782
1789
  end
1783
1790
 
1784
1791
  # Inspect a RedoNode node.
@@ -2135,13 +2142,8 @@ module Prism
2135
2142
 
2136
2143
  # Compose a header for the given node.
2137
2144
  def inspect_node(name, node)
2138
- result = +"@ #{name} ("
2139
-
2140
2145
  location = node.location
2141
- result << "location: (#{location.start_line},#{location.start_column})-(#{location.end_line},#{location.end_column})"
2142
- result << ", newline: true" if node.newline?
2143
-
2144
- result << ")\n"
2146
+ "@ #{name} (location: (#{location.start_line},#{location.start_column})-(#{location.end_line},#{location.end_column}))\n"
2145
2147
  end
2146
2148
 
2147
2149
  # Compose a string representing the given inner location field.
@@ -446,6 +446,11 @@ module Prism
446
446
  node.copy(parts: visit_all(node.parts))
447
447
  end
448
448
 
449
+ # Copy a ItLocalVariableReadNode node
450
+ def visit_it_local_variable_read_node(node)
451
+ node.copy
452
+ end
453
+
449
454
  # Copy a ItParametersNode node
450
455
  def visit_it_parameters_node(node)
451
456
  node.copy
@@ -618,7 +623,7 @@ module Prism
618
623
 
619
624
  # Copy a RationalNode node
620
625
  def visit_rational_node(node)
621
- node.copy(numeric: visit(node.numeric))
626
+ node.copy
622
627
  end
623
628
 
624
629
  # Copy a RedoNode node