jruby-prism-parser 0.23.0.pre.SNAPSHOT-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +401 -0
  3. data/CODE_OF_CONDUCT.md +76 -0
  4. data/CONTRIBUTING.md +62 -0
  5. data/LICENSE.md +7 -0
  6. data/Makefile +101 -0
  7. data/README.md +98 -0
  8. data/config.yml +2902 -0
  9. data/docs/build_system.md +91 -0
  10. data/docs/configuration.md +64 -0
  11. data/docs/cruby_compilation.md +27 -0
  12. data/docs/design.md +53 -0
  13. data/docs/encoding.md +121 -0
  14. data/docs/fuzzing.md +88 -0
  15. data/docs/heredocs.md +36 -0
  16. data/docs/javascript.md +118 -0
  17. data/docs/local_variable_depth.md +229 -0
  18. data/docs/mapping.md +117 -0
  19. data/docs/parser_translation.md +34 -0
  20. data/docs/parsing_rules.md +19 -0
  21. data/docs/releasing.md +98 -0
  22. data/docs/ripper.md +36 -0
  23. data/docs/ruby_api.md +43 -0
  24. data/docs/ruby_parser_translation.md +19 -0
  25. data/docs/serialization.md +209 -0
  26. data/docs/testing.md +55 -0
  27. data/ext/prism/api_node.c +5098 -0
  28. data/ext/prism/api_pack.c +267 -0
  29. data/ext/prism/extconf.rb +110 -0
  30. data/ext/prism/extension.c +1155 -0
  31. data/ext/prism/extension.h +18 -0
  32. data/include/prism/ast.h +5807 -0
  33. data/include/prism/defines.h +102 -0
  34. data/include/prism/diagnostic.h +339 -0
  35. data/include/prism/encoding.h +265 -0
  36. data/include/prism/node.h +57 -0
  37. data/include/prism/options.h +230 -0
  38. data/include/prism/pack.h +152 -0
  39. data/include/prism/parser.h +732 -0
  40. data/include/prism/prettyprint.h +26 -0
  41. data/include/prism/regexp.h +33 -0
  42. data/include/prism/util/pm_buffer.h +155 -0
  43. data/include/prism/util/pm_char.h +205 -0
  44. data/include/prism/util/pm_constant_pool.h +209 -0
  45. data/include/prism/util/pm_list.h +97 -0
  46. data/include/prism/util/pm_memchr.h +29 -0
  47. data/include/prism/util/pm_newline_list.h +93 -0
  48. data/include/prism/util/pm_state_stack.h +42 -0
  49. data/include/prism/util/pm_string.h +150 -0
  50. data/include/prism/util/pm_string_list.h +44 -0
  51. data/include/prism/util/pm_strncasecmp.h +32 -0
  52. data/include/prism/util/pm_strpbrk.h +46 -0
  53. data/include/prism/version.h +29 -0
  54. data/include/prism.h +289 -0
  55. data/jruby-prism.jar +0 -0
  56. data/lib/prism/compiler.rb +486 -0
  57. data/lib/prism/debug.rb +206 -0
  58. data/lib/prism/desugar_compiler.rb +207 -0
  59. data/lib/prism/dispatcher.rb +2150 -0
  60. data/lib/prism/dot_visitor.rb +4634 -0
  61. data/lib/prism/dsl.rb +785 -0
  62. data/lib/prism/ffi.rb +346 -0
  63. data/lib/prism/lex_compat.rb +908 -0
  64. data/lib/prism/mutation_compiler.rb +753 -0
  65. data/lib/prism/node.rb +17864 -0
  66. data/lib/prism/node_ext.rb +212 -0
  67. data/lib/prism/node_inspector.rb +68 -0
  68. data/lib/prism/pack.rb +224 -0
  69. data/lib/prism/parse_result/comments.rb +177 -0
  70. data/lib/prism/parse_result/newlines.rb +64 -0
  71. data/lib/prism/parse_result.rb +498 -0
  72. data/lib/prism/pattern.rb +250 -0
  73. data/lib/prism/serialize.rb +1354 -0
  74. data/lib/prism/translation/parser/compiler.rb +1838 -0
  75. data/lib/prism/translation/parser/lexer.rb +335 -0
  76. data/lib/prism/translation/parser/rubocop.rb +37 -0
  77. data/lib/prism/translation/parser.rb +178 -0
  78. data/lib/prism/translation/ripper.rb +577 -0
  79. data/lib/prism/translation/ruby_parser.rb +1521 -0
  80. data/lib/prism/translation.rb +11 -0
  81. data/lib/prism/version.rb +3 -0
  82. data/lib/prism/visitor.rb +495 -0
  83. data/lib/prism.rb +99 -0
  84. data/prism.gemspec +135 -0
  85. data/rbi/prism.rbi +7767 -0
  86. data/rbi/prism_static.rbi +207 -0
  87. data/sig/prism.rbs +4773 -0
  88. data/sig/prism_static.rbs +201 -0
  89. data/src/diagnostic.c +400 -0
  90. data/src/encoding.c +5132 -0
  91. data/src/node.c +2786 -0
  92. data/src/options.c +213 -0
  93. data/src/pack.c +493 -0
  94. data/src/prettyprint.c +8881 -0
  95. data/src/prism.c +18406 -0
  96. data/src/regexp.c +638 -0
  97. data/src/serialize.c +1554 -0
  98. data/src/token_type.c +700 -0
  99. data/src/util/pm_buffer.c +190 -0
  100. data/src/util/pm_char.c +318 -0
  101. data/src/util/pm_constant_pool.c +322 -0
  102. data/src/util/pm_list.c +49 -0
  103. data/src/util/pm_memchr.c +35 -0
  104. data/src/util/pm_newline_list.c +84 -0
  105. data/src/util/pm_state_stack.c +25 -0
  106. data/src/util/pm_string.c +203 -0
  107. data/src/util/pm_string_list.c +28 -0
  108. data/src/util/pm_strncasecmp.c +24 -0
  109. data/src/util/pm_strpbrk.c +180 -0
  110. metadata +156 -0
@@ -0,0 +1,486 @@
1
+ # frozen_string_literal: true
2
+ =begin
3
+ This file is generated by the templates/template.rb script and should not be
4
+ modified manually. See templates/lib/prism/compiler.rb.erb
5
+ if you are looking to modify the template
6
+ =end
7
+
8
+ module Prism
9
+ # A compiler is a visitor that returns the value of each node as it visits.
10
+ # This is as opposed to a visitor which will only walk the tree. This can be
11
+ # useful when you are trying to compile a tree into a different format.
12
+ #
13
+ # For example, to build a representation of the tree as s-expressions, you
14
+ # could write:
15
+ #
16
+ # class SExpressions < Prism::Compiler
17
+ # def visit_arguments_node(node) = [:arguments, super]
18
+ # def visit_call_node(node) = [:call, super]
19
+ # def visit_integer_node(node) = [:integer]
20
+ # def visit_program_node(node) = [:program, super]
21
+ # end
22
+ #
23
+ # Prism.parse("1 + 2").value.accept(SExpressions.new)
24
+ # # => [:program, [[[:call, [[:integer], [:arguments, [[:integer]]]]]]]]
25
+ #
26
+ class Compiler
27
+ # Visit an individual node.
28
+ def visit(node)
29
+ node&.accept(self)
30
+ end
31
+
32
+ # Visit a list of nodes.
33
+ def visit_all(nodes)
34
+ nodes.map { |node| node&.accept(self) }
35
+ end
36
+
37
+ # Visit the child nodes of the given node.
38
+ def visit_child_nodes(node)
39
+ node.compact_child_nodes.map { |node| node.accept(self) }
40
+ end
41
+
42
+ # Compile a AliasGlobalVariableNode node
43
+ alias visit_alias_global_variable_node visit_child_nodes
44
+
45
+ # Compile a AliasMethodNode node
46
+ alias visit_alias_method_node visit_child_nodes
47
+
48
+ # Compile a AlternationPatternNode node
49
+ alias visit_alternation_pattern_node visit_child_nodes
50
+
51
+ # Compile a AndNode node
52
+ alias visit_and_node visit_child_nodes
53
+
54
+ # Compile a ArgumentsNode node
55
+ alias visit_arguments_node visit_child_nodes
56
+
57
+ # Compile a ArrayNode node
58
+ alias visit_array_node visit_child_nodes
59
+
60
+ # Compile a ArrayPatternNode node
61
+ alias visit_array_pattern_node visit_child_nodes
62
+
63
+ # Compile a AssocNode node
64
+ alias visit_assoc_node visit_child_nodes
65
+
66
+ # Compile a AssocSplatNode node
67
+ alias visit_assoc_splat_node visit_child_nodes
68
+
69
+ # Compile a BackReferenceReadNode node
70
+ alias visit_back_reference_read_node visit_child_nodes
71
+
72
+ # Compile a BeginNode node
73
+ alias visit_begin_node visit_child_nodes
74
+
75
+ # Compile a BlockArgumentNode node
76
+ alias visit_block_argument_node visit_child_nodes
77
+
78
+ # Compile a BlockLocalVariableNode node
79
+ alias visit_block_local_variable_node visit_child_nodes
80
+
81
+ # Compile a BlockNode node
82
+ alias visit_block_node visit_child_nodes
83
+
84
+ # Compile a BlockParameterNode node
85
+ alias visit_block_parameter_node visit_child_nodes
86
+
87
+ # Compile a BlockParametersNode node
88
+ alias visit_block_parameters_node visit_child_nodes
89
+
90
+ # Compile a BreakNode node
91
+ alias visit_break_node visit_child_nodes
92
+
93
+ # Compile a CallAndWriteNode node
94
+ alias visit_call_and_write_node visit_child_nodes
95
+
96
+ # Compile a CallNode node
97
+ alias visit_call_node visit_child_nodes
98
+
99
+ # Compile a CallOperatorWriteNode node
100
+ alias visit_call_operator_write_node visit_child_nodes
101
+
102
+ # Compile a CallOrWriteNode node
103
+ alias visit_call_or_write_node visit_child_nodes
104
+
105
+ # Compile a CallTargetNode node
106
+ alias visit_call_target_node visit_child_nodes
107
+
108
+ # Compile a CapturePatternNode node
109
+ alias visit_capture_pattern_node visit_child_nodes
110
+
111
+ # Compile a CaseMatchNode node
112
+ alias visit_case_match_node visit_child_nodes
113
+
114
+ # Compile a CaseNode node
115
+ alias visit_case_node visit_child_nodes
116
+
117
+ # Compile a ClassNode node
118
+ alias visit_class_node visit_child_nodes
119
+
120
+ # Compile a ClassVariableAndWriteNode node
121
+ alias visit_class_variable_and_write_node visit_child_nodes
122
+
123
+ # Compile a ClassVariableOperatorWriteNode node
124
+ alias visit_class_variable_operator_write_node visit_child_nodes
125
+
126
+ # Compile a ClassVariableOrWriteNode node
127
+ alias visit_class_variable_or_write_node visit_child_nodes
128
+
129
+ # Compile a ClassVariableReadNode node
130
+ alias visit_class_variable_read_node visit_child_nodes
131
+
132
+ # Compile a ClassVariableTargetNode node
133
+ alias visit_class_variable_target_node visit_child_nodes
134
+
135
+ # Compile a ClassVariableWriteNode node
136
+ alias visit_class_variable_write_node visit_child_nodes
137
+
138
+ # Compile a ConstantAndWriteNode node
139
+ alias visit_constant_and_write_node visit_child_nodes
140
+
141
+ # Compile a ConstantOperatorWriteNode node
142
+ alias visit_constant_operator_write_node visit_child_nodes
143
+
144
+ # Compile a ConstantOrWriteNode node
145
+ alias visit_constant_or_write_node visit_child_nodes
146
+
147
+ # Compile a ConstantPathAndWriteNode node
148
+ alias visit_constant_path_and_write_node visit_child_nodes
149
+
150
+ # Compile a ConstantPathNode node
151
+ alias visit_constant_path_node visit_child_nodes
152
+
153
+ # Compile a ConstantPathOperatorWriteNode node
154
+ alias visit_constant_path_operator_write_node visit_child_nodes
155
+
156
+ # Compile a ConstantPathOrWriteNode node
157
+ alias visit_constant_path_or_write_node visit_child_nodes
158
+
159
+ # Compile a ConstantPathTargetNode node
160
+ alias visit_constant_path_target_node visit_child_nodes
161
+
162
+ # Compile a ConstantPathWriteNode node
163
+ alias visit_constant_path_write_node visit_child_nodes
164
+
165
+ # Compile a ConstantReadNode node
166
+ alias visit_constant_read_node visit_child_nodes
167
+
168
+ # Compile a ConstantTargetNode node
169
+ alias visit_constant_target_node visit_child_nodes
170
+
171
+ # Compile a ConstantWriteNode node
172
+ alias visit_constant_write_node visit_child_nodes
173
+
174
+ # Compile a DefNode node
175
+ alias visit_def_node visit_child_nodes
176
+
177
+ # Compile a DefinedNode node
178
+ alias visit_defined_node visit_child_nodes
179
+
180
+ # Compile a ElseNode node
181
+ alias visit_else_node visit_child_nodes
182
+
183
+ # Compile a EmbeddedStatementsNode node
184
+ alias visit_embedded_statements_node visit_child_nodes
185
+
186
+ # Compile a EmbeddedVariableNode node
187
+ alias visit_embedded_variable_node visit_child_nodes
188
+
189
+ # Compile a EnsureNode node
190
+ alias visit_ensure_node visit_child_nodes
191
+
192
+ # Compile a FalseNode node
193
+ alias visit_false_node visit_child_nodes
194
+
195
+ # Compile a FindPatternNode node
196
+ alias visit_find_pattern_node visit_child_nodes
197
+
198
+ # Compile a FlipFlopNode node
199
+ alias visit_flip_flop_node visit_child_nodes
200
+
201
+ # Compile a FloatNode node
202
+ alias visit_float_node visit_child_nodes
203
+
204
+ # Compile a ForNode node
205
+ alias visit_for_node visit_child_nodes
206
+
207
+ # Compile a ForwardingArgumentsNode node
208
+ alias visit_forwarding_arguments_node visit_child_nodes
209
+
210
+ # Compile a ForwardingParameterNode node
211
+ alias visit_forwarding_parameter_node visit_child_nodes
212
+
213
+ # Compile a ForwardingSuperNode node
214
+ alias visit_forwarding_super_node visit_child_nodes
215
+
216
+ # Compile a GlobalVariableAndWriteNode node
217
+ alias visit_global_variable_and_write_node visit_child_nodes
218
+
219
+ # Compile a GlobalVariableOperatorWriteNode node
220
+ alias visit_global_variable_operator_write_node visit_child_nodes
221
+
222
+ # Compile a GlobalVariableOrWriteNode node
223
+ alias visit_global_variable_or_write_node visit_child_nodes
224
+
225
+ # Compile a GlobalVariableReadNode node
226
+ alias visit_global_variable_read_node visit_child_nodes
227
+
228
+ # Compile a GlobalVariableTargetNode node
229
+ alias visit_global_variable_target_node visit_child_nodes
230
+
231
+ # Compile a GlobalVariableWriteNode node
232
+ alias visit_global_variable_write_node visit_child_nodes
233
+
234
+ # Compile a HashNode node
235
+ alias visit_hash_node visit_child_nodes
236
+
237
+ # Compile a HashPatternNode node
238
+ alias visit_hash_pattern_node visit_child_nodes
239
+
240
+ # Compile a IfNode node
241
+ alias visit_if_node visit_child_nodes
242
+
243
+ # Compile a ImaginaryNode node
244
+ alias visit_imaginary_node visit_child_nodes
245
+
246
+ # Compile a ImplicitNode node
247
+ alias visit_implicit_node visit_child_nodes
248
+
249
+ # Compile a ImplicitRestNode node
250
+ alias visit_implicit_rest_node visit_child_nodes
251
+
252
+ # Compile a InNode node
253
+ alias visit_in_node visit_child_nodes
254
+
255
+ # Compile a IndexAndWriteNode node
256
+ alias visit_index_and_write_node visit_child_nodes
257
+
258
+ # Compile a IndexOperatorWriteNode node
259
+ alias visit_index_operator_write_node visit_child_nodes
260
+
261
+ # Compile a IndexOrWriteNode node
262
+ alias visit_index_or_write_node visit_child_nodes
263
+
264
+ # Compile a IndexTargetNode node
265
+ alias visit_index_target_node visit_child_nodes
266
+
267
+ # Compile a InstanceVariableAndWriteNode node
268
+ alias visit_instance_variable_and_write_node visit_child_nodes
269
+
270
+ # Compile a InstanceVariableOperatorWriteNode node
271
+ alias visit_instance_variable_operator_write_node visit_child_nodes
272
+
273
+ # Compile a InstanceVariableOrWriteNode node
274
+ alias visit_instance_variable_or_write_node visit_child_nodes
275
+
276
+ # Compile a InstanceVariableReadNode node
277
+ alias visit_instance_variable_read_node visit_child_nodes
278
+
279
+ # Compile a InstanceVariableTargetNode node
280
+ alias visit_instance_variable_target_node visit_child_nodes
281
+
282
+ # Compile a InstanceVariableWriteNode node
283
+ alias visit_instance_variable_write_node visit_child_nodes
284
+
285
+ # Compile a IntegerNode node
286
+ alias visit_integer_node visit_child_nodes
287
+
288
+ # Compile a InterpolatedMatchLastLineNode node
289
+ alias visit_interpolated_match_last_line_node visit_child_nodes
290
+
291
+ # Compile a InterpolatedRegularExpressionNode node
292
+ alias visit_interpolated_regular_expression_node visit_child_nodes
293
+
294
+ # Compile a InterpolatedStringNode node
295
+ alias visit_interpolated_string_node visit_child_nodes
296
+
297
+ # Compile a InterpolatedSymbolNode node
298
+ alias visit_interpolated_symbol_node visit_child_nodes
299
+
300
+ # Compile a InterpolatedXStringNode node
301
+ alias visit_interpolated_x_string_node visit_child_nodes
302
+
303
+ # Compile a KeywordHashNode node
304
+ alias visit_keyword_hash_node visit_child_nodes
305
+
306
+ # Compile a KeywordRestParameterNode node
307
+ alias visit_keyword_rest_parameter_node visit_child_nodes
308
+
309
+ # Compile a LambdaNode node
310
+ alias visit_lambda_node visit_child_nodes
311
+
312
+ # Compile a LocalVariableAndWriteNode node
313
+ alias visit_local_variable_and_write_node visit_child_nodes
314
+
315
+ # Compile a LocalVariableOperatorWriteNode node
316
+ alias visit_local_variable_operator_write_node visit_child_nodes
317
+
318
+ # Compile a LocalVariableOrWriteNode node
319
+ alias visit_local_variable_or_write_node visit_child_nodes
320
+
321
+ # Compile a LocalVariableReadNode node
322
+ alias visit_local_variable_read_node visit_child_nodes
323
+
324
+ # Compile a LocalVariableTargetNode node
325
+ alias visit_local_variable_target_node visit_child_nodes
326
+
327
+ # Compile a LocalVariableWriteNode node
328
+ alias visit_local_variable_write_node visit_child_nodes
329
+
330
+ # Compile a MatchLastLineNode node
331
+ alias visit_match_last_line_node visit_child_nodes
332
+
333
+ # Compile a MatchPredicateNode node
334
+ alias visit_match_predicate_node visit_child_nodes
335
+
336
+ # Compile a MatchRequiredNode node
337
+ alias visit_match_required_node visit_child_nodes
338
+
339
+ # Compile a MatchWriteNode node
340
+ alias visit_match_write_node visit_child_nodes
341
+
342
+ # Compile a MissingNode node
343
+ alias visit_missing_node visit_child_nodes
344
+
345
+ # Compile a ModuleNode node
346
+ alias visit_module_node visit_child_nodes
347
+
348
+ # Compile a MultiTargetNode node
349
+ alias visit_multi_target_node visit_child_nodes
350
+
351
+ # Compile a MultiWriteNode node
352
+ alias visit_multi_write_node visit_child_nodes
353
+
354
+ # Compile a NextNode node
355
+ alias visit_next_node visit_child_nodes
356
+
357
+ # Compile a NilNode node
358
+ alias visit_nil_node visit_child_nodes
359
+
360
+ # Compile a NoKeywordsParameterNode node
361
+ alias visit_no_keywords_parameter_node visit_child_nodes
362
+
363
+ # Compile a NumberedParametersNode node
364
+ alias visit_numbered_parameters_node visit_child_nodes
365
+
366
+ # Compile a NumberedReferenceReadNode node
367
+ alias visit_numbered_reference_read_node visit_child_nodes
368
+
369
+ # Compile a OptionalKeywordParameterNode node
370
+ alias visit_optional_keyword_parameter_node visit_child_nodes
371
+
372
+ # Compile a OptionalParameterNode node
373
+ alias visit_optional_parameter_node visit_child_nodes
374
+
375
+ # Compile a OrNode node
376
+ alias visit_or_node visit_child_nodes
377
+
378
+ # Compile a ParametersNode node
379
+ alias visit_parameters_node visit_child_nodes
380
+
381
+ # Compile a ParenthesesNode node
382
+ alias visit_parentheses_node visit_child_nodes
383
+
384
+ # Compile a PinnedExpressionNode node
385
+ alias visit_pinned_expression_node visit_child_nodes
386
+
387
+ # Compile a PinnedVariableNode node
388
+ alias visit_pinned_variable_node visit_child_nodes
389
+
390
+ # Compile a PostExecutionNode node
391
+ alias visit_post_execution_node visit_child_nodes
392
+
393
+ # Compile a PreExecutionNode node
394
+ alias visit_pre_execution_node visit_child_nodes
395
+
396
+ # Compile a ProgramNode node
397
+ alias visit_program_node visit_child_nodes
398
+
399
+ # Compile a RangeNode node
400
+ alias visit_range_node visit_child_nodes
401
+
402
+ # Compile a RationalNode node
403
+ alias visit_rational_node visit_child_nodes
404
+
405
+ # Compile a RedoNode node
406
+ alias visit_redo_node visit_child_nodes
407
+
408
+ # Compile a RegularExpressionNode node
409
+ alias visit_regular_expression_node visit_child_nodes
410
+
411
+ # Compile a RequiredKeywordParameterNode node
412
+ alias visit_required_keyword_parameter_node visit_child_nodes
413
+
414
+ # Compile a RequiredParameterNode node
415
+ alias visit_required_parameter_node visit_child_nodes
416
+
417
+ # Compile a RescueModifierNode node
418
+ alias visit_rescue_modifier_node visit_child_nodes
419
+
420
+ # Compile a RescueNode node
421
+ alias visit_rescue_node visit_child_nodes
422
+
423
+ # Compile a RestParameterNode node
424
+ alias visit_rest_parameter_node visit_child_nodes
425
+
426
+ # Compile a RetryNode node
427
+ alias visit_retry_node visit_child_nodes
428
+
429
+ # Compile a ReturnNode node
430
+ alias visit_return_node visit_child_nodes
431
+
432
+ # Compile a SelfNode node
433
+ alias visit_self_node visit_child_nodes
434
+
435
+ # Compile a SingletonClassNode node
436
+ alias visit_singleton_class_node visit_child_nodes
437
+
438
+ # Compile a SourceEncodingNode node
439
+ alias visit_source_encoding_node visit_child_nodes
440
+
441
+ # Compile a SourceFileNode node
442
+ alias visit_source_file_node visit_child_nodes
443
+
444
+ # Compile a SourceLineNode node
445
+ alias visit_source_line_node visit_child_nodes
446
+
447
+ # Compile a SplatNode node
448
+ alias visit_splat_node visit_child_nodes
449
+
450
+ # Compile a StatementsNode node
451
+ alias visit_statements_node visit_child_nodes
452
+
453
+ # Compile a StringNode node
454
+ alias visit_string_node visit_child_nodes
455
+
456
+ # Compile a SuperNode node
457
+ alias visit_super_node visit_child_nodes
458
+
459
+ # Compile a SymbolNode node
460
+ alias visit_symbol_node visit_child_nodes
461
+
462
+ # Compile a TrueNode node
463
+ alias visit_true_node visit_child_nodes
464
+
465
+ # Compile a UndefNode node
466
+ alias visit_undef_node visit_child_nodes
467
+
468
+ # Compile a UnlessNode node
469
+ alias visit_unless_node visit_child_nodes
470
+
471
+ # Compile a UntilNode node
472
+ alias visit_until_node visit_child_nodes
473
+
474
+ # Compile a WhenNode node
475
+ alias visit_when_node visit_child_nodes
476
+
477
+ # Compile a WhileNode node
478
+ alias visit_while_node visit_child_nodes
479
+
480
+ # Compile a XStringNode node
481
+ alias visit_x_string_node visit_child_nodes
482
+
483
+ # Compile a YieldNode node
484
+ alias visit_yield_node visit_child_nodes
485
+ end
486
+ end
@@ -0,0 +1,206 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Prism
4
+ # This module is used for testing and debugging and is not meant to be used by
5
+ # consumers of this library.
6
+ module Debug
7
+ # A wrapper around a RubyVM::InstructionSequence that provides a more
8
+ # convenient interface for accessing parts of the iseq.
9
+ class ISeq # :nodoc:
10
+ attr_reader :parts
11
+
12
+ def initialize(parts)
13
+ @parts = parts
14
+ end
15
+
16
+ def type
17
+ parts[0]
18
+ end
19
+
20
+ def local_table
21
+ parts[10]
22
+ end
23
+
24
+ def instructions
25
+ parts[13]
26
+ end
27
+
28
+ def each_child
29
+ instructions.each do |instruction|
30
+ # Only look at arrays. Other instructions are line numbers or
31
+ # tracepoint events.
32
+ next unless instruction.is_a?(Array)
33
+
34
+ instruction.each do |opnd|
35
+ # Only look at arrays. Other operands are literals.
36
+ next unless opnd.is_a?(Array)
37
+
38
+ # Only look at instruction sequences. Other operands are literals.
39
+ next unless opnd[0] == "YARVInstructionSequence/SimpleDataFormat"
40
+
41
+ yield ISeq.new(opnd)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ private_constant :ISeq
48
+
49
+ # :call-seq:
50
+ # Debug::cruby_locals(source) -> Array
51
+ #
52
+ # For the given source, compiles with CRuby and returns a list of all of the
53
+ # sets of local variables that were encountered.
54
+ def self.cruby_locals(source)
55
+ verbose, $VERBOSE = $VERBOSE, nil
56
+
57
+ begin
58
+ locals = []
59
+ stack = [ISeq.new(RubyVM::InstructionSequence.compile(source).to_a)]
60
+
61
+ while (iseq = stack.pop)
62
+ names = [*iseq.local_table]
63
+ names.map!.with_index do |name, index|
64
+ # When an anonymous local variable is present in the iseq's local
65
+ # table, it is represented as the stack offset from the top.
66
+ # However, when these are dumped to binary and read back in, they
67
+ # are replaced with the symbol :#arg_rest. To consistently handle
68
+ # this, we replace them here with their index.
69
+ if name == :"#arg_rest"
70
+ names.length - index + 1
71
+ else
72
+ name
73
+ end
74
+ end
75
+
76
+ locals << names
77
+ iseq.each_child { |child| stack << child }
78
+ end
79
+
80
+ locals
81
+ ensure
82
+ $VERBOSE = verbose
83
+ end
84
+ end
85
+
86
+ # Used to hold the place of a local that will be in the local table but
87
+ # cannot be accessed directly from the source code. For example, the
88
+ # iteration variable in a for loop or the positional parameter on a method
89
+ # definition that is destructured.
90
+ AnonymousLocal = Object.new
91
+ private_constant :AnonymousLocal
92
+
93
+ # :call-seq:
94
+ # Debug::prism_locals(source) -> Array
95
+ #
96
+ # For the given source, parses with prism and returns a list of all of the
97
+ # sets of local variables that were encountered.
98
+ def self.prism_locals(source)
99
+ locals = []
100
+ stack = [Prism.parse(source).value]
101
+
102
+ while (node = stack.pop)
103
+ case node
104
+ when BlockNode, DefNode, LambdaNode
105
+ names = node.locals
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
114
+
115
+ # prism places parameters in the same order that they appear in the
116
+ # source. CRuby places them in the order that they need to appear
117
+ # according to their own internal calling convention. We mimic that
118
+ # order here so that we can compare properly.
119
+ if params
120
+ sorted = [
121
+ *params.requireds.map do |required|
122
+ if required.is_a?(RequiredParameterNode)
123
+ required.name
124
+ else
125
+ AnonymousLocal
126
+ end
127
+ end,
128
+ *params.optionals.map(&:name),
129
+ *((params.rest.name || :*) if params.rest && !params.rest.is_a?(ImplicitRestNode)),
130
+ *params.posts.map do |post|
131
+ if post.is_a?(RequiredParameterNode)
132
+ post.name
133
+ else
134
+ AnonymousLocal
135
+ end
136
+ end,
137
+ *params.keywords.grep(RequiredKeywordParameterNode).map(&:name),
138
+ *params.keywords.grep(OptionalKeywordParameterNode).map(&:name),
139
+ ]
140
+
141
+ sorted << AnonymousLocal if params.keywords.any?
142
+
143
+ if params.keyword_rest.is_a?(ForwardingParameterNode)
144
+ sorted.push(:*, :**, :&, :"...")
145
+ elsif params.keyword_rest.is_a?(KeywordRestParameterNode)
146
+ sorted << (params.keyword_rest.name || :**)
147
+ end
148
+
149
+ # Recurse down the parameter tree to find any destructured
150
+ # parameters and add them after the other parameters.
151
+ param_stack = params.requireds.concat(params.posts).grep(MultiTargetNode).reverse
152
+ while (param = param_stack.pop)
153
+ case param
154
+ when MultiTargetNode
155
+ param_stack.concat(param.rights.reverse)
156
+ param_stack << param.rest if param.rest&.expression && !sorted.include?(param.rest.expression.name)
157
+ param_stack.concat(param.lefts.reverse)
158
+ when RequiredParameterNode
159
+ sorted << param.name
160
+ when SplatNode
161
+ sorted << param.expression.name
162
+ end
163
+ end
164
+
165
+ if params.block
166
+ sorted << (params.block.name || :&)
167
+ end
168
+
169
+ names = sorted.concat(names - sorted)
170
+ end
171
+
172
+ names.map!.with_index do |name, index|
173
+ if name == AnonymousLocal
174
+ names.length - index + 1
175
+ else
176
+ name
177
+ end
178
+ end
179
+
180
+ locals << names
181
+ when ClassNode, ModuleNode, ProgramNode, SingletonClassNode
182
+ locals << node.locals
183
+ when ForNode
184
+ locals << [2]
185
+ when PostExecutionNode
186
+ locals.push([], [])
187
+ when InterpolatedRegularExpressionNode
188
+ locals << [] if node.once?
189
+ end
190
+
191
+ stack.concat(node.compact_child_nodes)
192
+ end
193
+
194
+ locals
195
+ end
196
+
197
+ # :call-seq:
198
+ # Debug::newlines(source) -> Array
199
+ #
200
+ # For the given source string, return the byte offsets of every newline in
201
+ # the source.
202
+ def self.newlines(source)
203
+ Prism.parse(source).source.offsets
204
+ end
205
+ end
206
+ end