prism 0.18.0 → 0.19.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/README.md +2 -1
  4. data/config.yml +188 -55
  5. data/docs/building.md +9 -2
  6. data/docs/configuration.md +10 -9
  7. data/docs/encoding.md +24 -56
  8. data/docs/local_variable_depth.md +229 -0
  9. data/docs/ruby_api.md +2 -0
  10. data/docs/serialization.md +18 -13
  11. data/ext/prism/api_node.c +337 -195
  12. data/ext/prism/extconf.rb +13 -7
  13. data/ext/prism/extension.c +96 -32
  14. data/ext/prism/extension.h +1 -1
  15. data/include/prism/ast.h +340 -137
  16. data/include/prism/defines.h +17 -0
  17. data/include/prism/diagnostic.h +11 -5
  18. data/include/prism/encoding.h +248 -0
  19. data/include/prism/options.h +2 -2
  20. data/include/prism/parser.h +62 -42
  21. data/include/prism/regexp.h +2 -2
  22. data/include/prism/util/pm_buffer.h +9 -1
  23. data/include/prism/util/pm_memchr.h +2 -2
  24. data/include/prism/util/pm_strpbrk.h +3 -3
  25. data/include/prism/version.h +2 -2
  26. data/include/prism.h +13 -15
  27. data/lib/prism/compiler.rb +12 -0
  28. data/lib/prism/debug.rb +9 -4
  29. data/lib/prism/desugar_compiler.rb +3 -3
  30. data/lib/prism/dispatcher.rb +56 -0
  31. data/lib/prism/dot_visitor.rb +476 -198
  32. data/lib/prism/dsl.rb +66 -46
  33. data/lib/prism/ffi.rb +16 -3
  34. data/lib/prism/lex_compat.rb +19 -9
  35. data/lib/prism/mutation_compiler.rb +20 -0
  36. data/lib/prism/node.rb +1173 -450
  37. data/lib/prism/node_ext.rb +41 -16
  38. data/lib/prism/parse_result.rb +12 -15
  39. data/lib/prism/ripper_compat.rb +49 -34
  40. data/lib/prism/serialize.rb +242 -212
  41. data/lib/prism/visitor.rb +12 -0
  42. data/lib/prism.rb +20 -4
  43. data/prism.gemspec +4 -10
  44. data/rbi/prism.rbi +605 -230
  45. data/rbi/prism_static.rbi +3 -0
  46. data/sig/prism.rbs +379 -124
  47. data/sig/prism_static.rbs +1 -0
  48. data/src/diagnostic.c +228 -222
  49. data/src/encoding.c +5137 -0
  50. data/src/node.c +66 -0
  51. data/src/options.c +21 -2
  52. data/src/prettyprint.c +806 -406
  53. data/src/prism.c +1092 -700
  54. data/src/regexp.c +3 -3
  55. data/src/serialize.c +227 -157
  56. data/src/util/pm_buffer.c +10 -1
  57. data/src/util/pm_memchr.c +1 -1
  58. data/src/util/pm_strpbrk.c +4 -4
  59. metadata +5 -11
  60. data/include/prism/enc/pm_encoding.h +0 -227
  61. data/src/enc/pm_big5.c +0 -116
  62. data/src/enc/pm_cp51932.c +0 -57
  63. data/src/enc/pm_euc_jp.c +0 -69
  64. data/src/enc/pm_gbk.c +0 -65
  65. data/src/enc/pm_shift_jis.c +0 -57
  66. data/src/enc/pm_tables.c +0 -2073
  67. data/src/enc/pm_unicode.c +0 -2369
  68. data/src/enc/pm_windows_31j.c +0 -57
data/include/prism.h CHANGED
@@ -61,18 +61,6 @@ PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *
61
61
  */
62
62
  PRISM_EXPORTED_FUNCTION void pm_parser_register_encoding_changed_callback(pm_parser_t *parser, pm_encoding_changed_callback_t callback);
63
63
 
64
- /**
65
- * Register a callback that will be called when prism encounters a magic comment
66
- * with an encoding referenced that it doesn't understand. The callback should
67
- * return NULL if it also doesn't understand the encoding or it should return a
68
- * pointer to a pm_encoding_t struct that contains the functions necessary to
69
- * parse identifiers.
70
- *
71
- * @param parser The parser to register the callback with.
72
- * @param callback The callback to register.
73
- */
74
- PRISM_EXPORTED_FUNCTION void pm_parser_register_encoding_decode_callback(pm_parser_t *parser, pm_encoding_decode_callback_t callback);
75
-
76
64
  /**
77
65
  * Free any memory associated with the given parser.
78
66
  *
@@ -103,7 +91,7 @@ void pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t
103
91
  * @param encoding The encoding to serialize.
104
92
  * @param buffer The buffer to serialize to.
105
93
  */
106
- void pm_serialize_encoding(pm_encoding_t *encoding, pm_buffer_t *buffer);
94
+ void pm_serialize_encoding(const pm_encoding_t *encoding, pm_buffer_t *buffer);
107
95
 
108
96
  /**
109
97
  * Serialize the encoding, metadata, nodes, and constant pool.
@@ -164,6 +152,16 @@ PRISM_EXPORTED_FUNCTION void pm_serialize_lex(pm_buffer_t *buffer, const uint8_t
164
152
  */
165
153
  PRISM_EXPORTED_FUNCTION void pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data);
166
154
 
155
+ /**
156
+ * Parse the source and return true if it parses without errors or warnings.
157
+ *
158
+ * @param source The source to parse.
159
+ * @param size The size of the source.
160
+ * @param data The optional data to pass to the parser.
161
+ * @return True if the source parses without errors or warnings.
162
+ */
163
+ PRISM_EXPORTED_FUNCTION bool pm_parse_success_p(const uint8_t *source, size_t size, const char *data);
164
+
167
165
  /**
168
166
  * Returns a string representation of the given token type.
169
167
  *
@@ -213,7 +211,7 @@ PRISM_EXPORTED_FUNCTION const char * pm_token_type_to_str(pm_token_type_t token_
213
211
  * pm_node_t *root = pm_parse(&parser);
214
212
  * printf("PARSED!\n");
215
213
  *
216
- * pm_node_destroy(root);
214
+ * pm_node_destroy(&parser, root);
217
215
  * pm_parser_free(&parser);
218
216
  * }
219
217
  * ```
@@ -265,7 +263,7 @@ PRISM_EXPORTED_FUNCTION const char * pm_token_type_to_str(pm_token_type_t token_
265
263
  * printf("*.s%\n", (int) buffer.length, buffer.value);
266
264
  *
267
265
  * pm_buffer_free(&buffer);
268
- * pm_node_destroy(root);
266
+ * pm_node_destroy(&parser, root);
269
267
  * pm_parser_free(&parser);
270
268
  * }
271
269
  * ```
@@ -102,6 +102,9 @@ module Prism
102
102
  # Compile a CallOrWriteNode node
103
103
  alias visit_call_or_write_node visit_child_nodes
104
104
 
105
+ # Compile a CallTargetNode node
106
+ alias visit_call_target_node visit_child_nodes
107
+
105
108
  # Compile a CapturePatternNode node
106
109
  alias visit_capture_pattern_node visit_child_nodes
107
110
 
@@ -243,6 +246,9 @@ module Prism
243
246
  # Compile a ImplicitNode node
244
247
  alias visit_implicit_node visit_child_nodes
245
248
 
249
+ # Compile a ImplicitRestNode node
250
+ alias visit_implicit_rest_node visit_child_nodes
251
+
246
252
  # Compile a InNode node
247
253
  alias visit_in_node visit_child_nodes
248
254
 
@@ -255,6 +261,9 @@ module Prism
255
261
  # Compile a IndexOrWriteNode node
256
262
  alias visit_index_or_write_node visit_child_nodes
257
263
 
264
+ # Compile a IndexTargetNode node
265
+ alias visit_index_target_node visit_child_nodes
266
+
258
267
  # Compile a InstanceVariableAndWriteNode node
259
268
  alias visit_instance_variable_and_write_node visit_child_nodes
260
269
 
@@ -351,6 +360,9 @@ module Prism
351
360
  # Compile a NoKeywordsParameterNode node
352
361
  alias visit_no_keywords_parameter_node visit_child_nodes
353
362
 
363
+ # Compile a NumberedParametersNode node
364
+ alias visit_numbered_parameters_node visit_child_nodes
365
+
354
366
  # Compile a NumberedReferenceReadNode node
355
367
  alias visit_numbered_reference_read_node visit_child_nodes
356
368
 
data/lib/prism/debug.rb CHANGED
@@ -103,9 +103,14 @@ module Prism
103
103
  case node
104
104
  when BlockNode, DefNode, LambdaNode
105
105
  names = node.locals
106
-
107
- params = node.parameters
108
- params = params&.parameters unless node.is_a?(DefNode)
106
+ params =
107
+ if node.is_a?(DefNode)
108
+ node.parameters
109
+ elsif node.parameters.is_a?(NumberedParametersNode)
110
+ nil
111
+ else
112
+ node.parameters&.parameters
113
+ end
109
114
 
110
115
  # prism places parameters in the same order that they appear in the
111
116
  # source. CRuby places them in the order that they need to appear
@@ -121,7 +126,7 @@ module Prism
121
126
  end
122
127
  end,
123
128
  *params.optionals.map(&:name),
124
- *((params.rest.name || :*) if params.rest && params.rest.operator != ","),
129
+ *((params.rest.name || :*) if params.rest && !params.rest.is_a?(ImplicitRestNode)),
125
130
  *params.posts.map do |post|
126
131
  if post.is_a?(RequiredParameterNode)
127
132
  post.name
@@ -157,15 +157,15 @@ module Prism
157
157
  *arguments,
158
158
  node.name_loc,
159
159
  CallNode.new(
160
+ 0,
160
161
  read_class.new(*arguments, node.name_loc),
161
162
  nil,
163
+ node.operator_loc.slice.chomp("="),
162
164
  node.operator_loc.copy(length: node.operator_loc.length - 1),
163
165
  nil,
164
- ArgumentsNode.new([node.value], 0, node.value.location),
166
+ ArgumentsNode.new(0, [node.value], node.value.location),
165
167
  nil,
166
168
  nil,
167
- 0,
168
- node.operator_loc.slice.chomp("="),
169
169
  node.location
170
170
  ),
171
171
  node.operator_loc.copy(start_offset: node.operator_loc.end_offset - 1, length: 1),
@@ -233,6 +233,14 @@ module Prism
233
233
  listeners[:on_call_or_write_node_leave]&.each { |listener| listener.on_call_or_write_node_leave(node) }
234
234
  end
235
235
 
236
+ # Dispatch enter and leave events for CallTargetNode nodes and continue
237
+ # walking the tree.
238
+ def visit_call_target_node(node)
239
+ listeners[:on_call_target_node_enter]&.each { |listener| listener.on_call_target_node_enter(node) }
240
+ super
241
+ listeners[:on_call_target_node_leave]&.each { |listener| listener.on_call_target_node_leave(node) }
242
+ end
243
+
236
244
  # Dispatch enter and leave events for CapturePatternNode nodes and continue
237
245
  # walking the tree.
238
246
  def visit_capture_pattern_node(node)
@@ -609,6 +617,14 @@ module Prism
609
617
  listeners[:on_implicit_node_leave]&.each { |listener| listener.on_implicit_node_leave(node) }
610
618
  end
611
619
 
620
+ # Dispatch enter and leave events for ImplicitRestNode nodes and continue
621
+ # walking the tree.
622
+ def visit_implicit_rest_node(node)
623
+ listeners[:on_implicit_rest_node_enter]&.each { |listener| listener.on_implicit_rest_node_enter(node) }
624
+ super
625
+ listeners[:on_implicit_rest_node_leave]&.each { |listener| listener.on_implicit_rest_node_leave(node) }
626
+ end
627
+
612
628
  # Dispatch enter and leave events for InNode nodes and continue
613
629
  # walking the tree.
614
630
  def visit_in_node(node)
@@ -641,6 +657,14 @@ module Prism
641
657
  listeners[:on_index_or_write_node_leave]&.each { |listener| listener.on_index_or_write_node_leave(node) }
642
658
  end
643
659
 
660
+ # Dispatch enter and leave events for IndexTargetNode nodes and continue
661
+ # walking the tree.
662
+ def visit_index_target_node(node)
663
+ listeners[:on_index_target_node_enter]&.each { |listener| listener.on_index_target_node_enter(node) }
664
+ super
665
+ listeners[:on_index_target_node_leave]&.each { |listener| listener.on_index_target_node_leave(node) }
666
+ end
667
+
644
668
  # Dispatch enter and leave events for InstanceVariableAndWriteNode nodes and continue
645
669
  # walking the tree.
646
670
  def visit_instance_variable_and_write_node(node)
@@ -897,6 +921,14 @@ module Prism
897
921
  listeners[:on_no_keywords_parameter_node_leave]&.each { |listener| listener.on_no_keywords_parameter_node_leave(node) }
898
922
  end
899
923
 
924
+ # Dispatch enter and leave events for NumberedParametersNode nodes and continue
925
+ # walking the tree.
926
+ def visit_numbered_parameters_node(node)
927
+ listeners[:on_numbered_parameters_node_enter]&.each { |listener| listener.on_numbered_parameters_node_enter(node) }
928
+ super
929
+ listeners[:on_numbered_parameters_node_leave]&.each { |listener| listener.on_numbered_parameters_node_leave(node) }
930
+ end
931
+
900
932
  # Dispatch enter and leave events for NumberedReferenceReadNode nodes and continue
901
933
  # walking the tree.
902
934
  def visit_numbered_reference_read_node(node)
@@ -1350,6 +1382,12 @@ module Prism
1350
1382
  listeners[:on_call_or_write_node_leave]&.each { |listener| listener.on_call_or_write_node_leave(node) }
1351
1383
  end
1352
1384
 
1385
+ # Dispatch enter and leave events for CallTargetNode nodes.
1386
+ def visit_call_target_node(node)
1387
+ listeners[:on_call_target_node_enter]&.each { |listener| listener.on_call_target_node_enter(node) }
1388
+ listeners[:on_call_target_node_leave]&.each { |listener| listener.on_call_target_node_leave(node) }
1389
+ end
1390
+
1353
1391
  # Dispatch enter and leave events for CapturePatternNode nodes.
1354
1392
  def visit_capture_pattern_node(node)
1355
1393
  listeners[:on_capture_pattern_node_enter]&.each { |listener| listener.on_capture_pattern_node_enter(node) }
@@ -1632,6 +1670,12 @@ module Prism
1632
1670
  listeners[:on_implicit_node_leave]&.each { |listener| listener.on_implicit_node_leave(node) }
1633
1671
  end
1634
1672
 
1673
+ # Dispatch enter and leave events for ImplicitRestNode nodes.
1674
+ def visit_implicit_rest_node(node)
1675
+ listeners[:on_implicit_rest_node_enter]&.each { |listener| listener.on_implicit_rest_node_enter(node) }
1676
+ listeners[:on_implicit_rest_node_leave]&.each { |listener| listener.on_implicit_rest_node_leave(node) }
1677
+ end
1678
+
1635
1679
  # Dispatch enter and leave events for InNode nodes.
1636
1680
  def visit_in_node(node)
1637
1681
  listeners[:on_in_node_enter]&.each { |listener| listener.on_in_node_enter(node) }
@@ -1656,6 +1700,12 @@ module Prism
1656
1700
  listeners[:on_index_or_write_node_leave]&.each { |listener| listener.on_index_or_write_node_leave(node) }
1657
1701
  end
1658
1702
 
1703
+ # Dispatch enter and leave events for IndexTargetNode nodes.
1704
+ def visit_index_target_node(node)
1705
+ listeners[:on_index_target_node_enter]&.each { |listener| listener.on_index_target_node_enter(node) }
1706
+ listeners[:on_index_target_node_leave]&.each { |listener| listener.on_index_target_node_leave(node) }
1707
+ end
1708
+
1659
1709
  # Dispatch enter and leave events for InstanceVariableAndWriteNode nodes.
1660
1710
  def visit_instance_variable_and_write_node(node)
1661
1711
  listeners[:on_instance_variable_and_write_node_enter]&.each { |listener| listener.on_instance_variable_and_write_node_enter(node) }
@@ -1848,6 +1898,12 @@ module Prism
1848
1898
  listeners[:on_no_keywords_parameter_node_leave]&.each { |listener| listener.on_no_keywords_parameter_node_leave(node) }
1849
1899
  end
1850
1900
 
1901
+ # Dispatch enter and leave events for NumberedParametersNode nodes.
1902
+ def visit_numbered_parameters_node(node)
1903
+ listeners[:on_numbered_parameters_node_enter]&.each { |listener| listener.on_numbered_parameters_node_enter(node) }
1904
+ listeners[:on_numbered_parameters_node_leave]&.each { |listener| listener.on_numbered_parameters_node_leave(node) }
1905
+ end
1906
+
1851
1907
  # Dispatch enter and leave events for NumberedReferenceReadNode nodes.
1852
1908
  def visit_numbered_reference_read_node(node)
1853
1909
  listeners[:on_numbered_reference_read_node_enter]&.each { |listener| listener.on_numbered_reference_read_node_enter(node) }