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
data/src/serialize.c ADDED
@@ -0,0 +1,1554 @@
1
+ /******************************************************************************/
2
+ /* This file is generated by the templates/template.rb script and should not */
3
+ /* be modified manually. See */
4
+ /* templates/src/serialize.c.erb */
5
+ /* if you are looking to modify the */
6
+ /* template */
7
+ /******************************************************************************/
8
+ #include "prism.h"
9
+
10
+ #include <stdio.h>
11
+
12
+ static inline uint32_t
13
+ pm_ptrdifft_to_u32(ptrdiff_t value) {
14
+ assert(value >= 0 && ((unsigned long) value) < UINT32_MAX);
15
+ return (uint32_t) value;
16
+ }
17
+
18
+ static inline uint32_t
19
+ pm_sizet_to_u32(size_t value) {
20
+ assert(value < UINT32_MAX);
21
+ return (uint32_t) value;
22
+ }
23
+
24
+ static void
25
+ pm_serialize_location(const pm_parser_t *parser, const pm_location_t *location, pm_buffer_t *buffer) {
26
+ assert(location->start);
27
+ assert(location->end);
28
+ assert(location->start <= location->end);
29
+
30
+ pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(location->start - parser->start));
31
+ pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(location->end - location->start));
32
+ }
33
+
34
+ static void
35
+ pm_serialize_string(pm_parser_t *parser, pm_string_t *string, pm_buffer_t *buffer) {
36
+ switch (string->type) {
37
+ case PM_STRING_SHARED: {
38
+ pm_buffer_append_byte(buffer, 1);
39
+ pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(pm_string_source(string) - parser->start));
40
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_string_length(string)));
41
+ break;
42
+ }
43
+ case PM_STRING_OWNED:
44
+ case PM_STRING_CONSTANT: {
45
+ uint32_t length = pm_sizet_to_u32(pm_string_length(string));
46
+ pm_buffer_append_byte(buffer, 2);
47
+ pm_buffer_append_varuint(buffer, length);
48
+ pm_buffer_append_bytes(buffer, pm_string_source(string), length);
49
+ break;
50
+ }
51
+ case PM_STRING_MAPPED:
52
+ assert(false && "Cannot serialize mapped strings.");
53
+ break;
54
+ }
55
+ }
56
+
57
+ static void
58
+ pm_serialize_node(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
59
+ pm_buffer_append_byte(buffer, (uint8_t) PM_NODE_TYPE(node));
60
+
61
+ size_t offset = buffer->length;
62
+
63
+ pm_serialize_location(parser, &node->location, buffer);
64
+
65
+ switch (PM_NODE_TYPE(node)) {
66
+ // We do not need to serialize a ScopeNode ever as
67
+ // it is not part of the AST
68
+ case PM_SCOPE_NODE:
69
+ return;
70
+ case PM_ALIAS_GLOBAL_VARIABLE_NODE: {
71
+ pm_serialize_node(parser, (pm_node_t *)((pm_alias_global_variable_node_t *)node)->new_name, buffer);
72
+ pm_serialize_node(parser, (pm_node_t *)((pm_alias_global_variable_node_t *)node)->old_name, buffer);
73
+ break;
74
+ }
75
+ case PM_ALIAS_METHOD_NODE: {
76
+ pm_serialize_node(parser, (pm_node_t *)((pm_alias_method_node_t *)node)->new_name, buffer);
77
+ pm_serialize_node(parser, (pm_node_t *)((pm_alias_method_node_t *)node)->old_name, buffer);
78
+ break;
79
+ }
80
+ case PM_ALTERNATION_PATTERN_NODE: {
81
+ pm_serialize_node(parser, (pm_node_t *)((pm_alternation_pattern_node_t *)node)->left, buffer);
82
+ pm_serialize_node(parser, (pm_node_t *)((pm_alternation_pattern_node_t *)node)->right, buffer);
83
+ break;
84
+ }
85
+ case PM_AND_NODE: {
86
+ pm_serialize_node(parser, (pm_node_t *)((pm_and_node_t *)node)->left, buffer);
87
+ pm_serialize_node(parser, (pm_node_t *)((pm_and_node_t *)node)->right, buffer);
88
+ break;
89
+ }
90
+ case PM_ARGUMENTS_NODE: {
91
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
92
+ uint32_t arguments_size = pm_sizet_to_u32(((pm_arguments_node_t *)node)->arguments.size);
93
+ pm_buffer_append_varuint(buffer, arguments_size);
94
+ for (uint32_t index = 0; index < arguments_size; index++) {
95
+ pm_serialize_node(parser, (pm_node_t *) ((pm_arguments_node_t *)node)->arguments.nodes[index], buffer);
96
+ }
97
+ break;
98
+ }
99
+ case PM_ARRAY_NODE: {
100
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
101
+ uint32_t elements_size = pm_sizet_to_u32(((pm_array_node_t *)node)->elements.size);
102
+ pm_buffer_append_varuint(buffer, elements_size);
103
+ for (uint32_t index = 0; index < elements_size; index++) {
104
+ pm_serialize_node(parser, (pm_node_t *) ((pm_array_node_t *)node)->elements.nodes[index], buffer);
105
+ }
106
+ break;
107
+ }
108
+ case PM_ARRAY_PATTERN_NODE: {
109
+ if (((pm_array_pattern_node_t *)node)->constant == NULL) {
110
+ pm_buffer_append_byte(buffer, 0);
111
+ } else {
112
+ pm_serialize_node(parser, (pm_node_t *)((pm_array_pattern_node_t *)node)->constant, buffer);
113
+ }
114
+ uint32_t requireds_size = pm_sizet_to_u32(((pm_array_pattern_node_t *)node)->requireds.size);
115
+ pm_buffer_append_varuint(buffer, requireds_size);
116
+ for (uint32_t index = 0; index < requireds_size; index++) {
117
+ pm_serialize_node(parser, (pm_node_t *) ((pm_array_pattern_node_t *)node)->requireds.nodes[index], buffer);
118
+ }
119
+ if (((pm_array_pattern_node_t *)node)->rest == NULL) {
120
+ pm_buffer_append_byte(buffer, 0);
121
+ } else {
122
+ pm_serialize_node(parser, (pm_node_t *)((pm_array_pattern_node_t *)node)->rest, buffer);
123
+ }
124
+ uint32_t posts_size = pm_sizet_to_u32(((pm_array_pattern_node_t *)node)->posts.size);
125
+ pm_buffer_append_varuint(buffer, posts_size);
126
+ for (uint32_t index = 0; index < posts_size; index++) {
127
+ pm_serialize_node(parser, (pm_node_t *) ((pm_array_pattern_node_t *)node)->posts.nodes[index], buffer);
128
+ }
129
+ break;
130
+ }
131
+ case PM_ASSOC_NODE: {
132
+ pm_serialize_node(parser, (pm_node_t *)((pm_assoc_node_t *)node)->key, buffer);
133
+ pm_serialize_node(parser, (pm_node_t *)((pm_assoc_node_t *)node)->value, buffer);
134
+ break;
135
+ }
136
+ case PM_ASSOC_SPLAT_NODE: {
137
+ if (((pm_assoc_splat_node_t *)node)->value == NULL) {
138
+ pm_buffer_append_byte(buffer, 0);
139
+ } else {
140
+ pm_serialize_node(parser, (pm_node_t *)((pm_assoc_splat_node_t *)node)->value, buffer);
141
+ }
142
+ break;
143
+ }
144
+ case PM_BACK_REFERENCE_READ_NODE: {
145
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_back_reference_read_node_t *)node)->name));
146
+ break;
147
+ }
148
+ case PM_BEGIN_NODE: {
149
+ if (((pm_begin_node_t *)node)->statements == NULL) {
150
+ pm_buffer_append_byte(buffer, 0);
151
+ } else {
152
+ pm_serialize_node(parser, (pm_node_t *)((pm_begin_node_t *)node)->statements, buffer);
153
+ }
154
+ if (((pm_begin_node_t *)node)->rescue_clause == NULL) {
155
+ pm_buffer_append_byte(buffer, 0);
156
+ } else {
157
+ pm_serialize_node(parser, (pm_node_t *)((pm_begin_node_t *)node)->rescue_clause, buffer);
158
+ }
159
+ if (((pm_begin_node_t *)node)->else_clause == NULL) {
160
+ pm_buffer_append_byte(buffer, 0);
161
+ } else {
162
+ pm_serialize_node(parser, (pm_node_t *)((pm_begin_node_t *)node)->else_clause, buffer);
163
+ }
164
+ if (((pm_begin_node_t *)node)->ensure_clause == NULL) {
165
+ pm_buffer_append_byte(buffer, 0);
166
+ } else {
167
+ pm_serialize_node(parser, (pm_node_t *)((pm_begin_node_t *)node)->ensure_clause, buffer);
168
+ }
169
+ break;
170
+ }
171
+ case PM_BLOCK_ARGUMENT_NODE: {
172
+ if (((pm_block_argument_node_t *)node)->expression == NULL) {
173
+ pm_buffer_append_byte(buffer, 0);
174
+ } else {
175
+ pm_serialize_node(parser, (pm_node_t *)((pm_block_argument_node_t *)node)->expression, buffer);
176
+ }
177
+ break;
178
+ }
179
+ case PM_BLOCK_LOCAL_VARIABLE_NODE: {
180
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
181
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_block_local_variable_node_t *)node)->name));
182
+ break;
183
+ }
184
+ case PM_BLOCK_NODE: {
185
+ uint32_t locals_size = pm_sizet_to_u32(((pm_block_node_t *)node)->locals.size);
186
+ pm_buffer_append_varuint(buffer, locals_size);
187
+ for (uint32_t index = 0; index < locals_size; index++) {
188
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_block_node_t *)node)->locals.ids[index]));
189
+ }
190
+ if (((pm_block_node_t *)node)->parameters == NULL) {
191
+ pm_buffer_append_byte(buffer, 0);
192
+ } else {
193
+ pm_serialize_node(parser, (pm_node_t *)((pm_block_node_t *)node)->parameters, buffer);
194
+ }
195
+ if (((pm_block_node_t *)node)->body == NULL) {
196
+ pm_buffer_append_byte(buffer, 0);
197
+ } else {
198
+ pm_serialize_node(parser, (pm_node_t *)((pm_block_node_t *)node)->body, buffer);
199
+ }
200
+ break;
201
+ }
202
+ case PM_BLOCK_PARAMETER_NODE: {
203
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
204
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_block_parameter_node_t *)node)->name));
205
+ break;
206
+ }
207
+ case PM_BLOCK_PARAMETERS_NODE: {
208
+ if (((pm_block_parameters_node_t *)node)->parameters == NULL) {
209
+ pm_buffer_append_byte(buffer, 0);
210
+ } else {
211
+ pm_serialize_node(parser, (pm_node_t *)((pm_block_parameters_node_t *)node)->parameters, buffer);
212
+ }
213
+ uint32_t locals_size = pm_sizet_to_u32(((pm_block_parameters_node_t *)node)->locals.size);
214
+ pm_buffer_append_varuint(buffer, locals_size);
215
+ for (uint32_t index = 0; index < locals_size; index++) {
216
+ pm_serialize_node(parser, (pm_node_t *) ((pm_block_parameters_node_t *)node)->locals.nodes[index], buffer);
217
+ }
218
+ break;
219
+ }
220
+ case PM_BREAK_NODE: {
221
+ if (((pm_break_node_t *)node)->arguments == NULL) {
222
+ pm_buffer_append_byte(buffer, 0);
223
+ } else {
224
+ pm_serialize_node(parser, (pm_node_t *)((pm_break_node_t *)node)->arguments, buffer);
225
+ }
226
+ break;
227
+ }
228
+ case PM_CALL_AND_WRITE_NODE: {
229
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
230
+ if (((pm_call_and_write_node_t *)node)->receiver == NULL) {
231
+ pm_buffer_append_byte(buffer, 0);
232
+ } else {
233
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_and_write_node_t *)node)->receiver, buffer);
234
+ }
235
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_and_write_node_t *)node)->read_name));
236
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_and_write_node_t *)node)->write_name));
237
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_and_write_node_t *)node)->value, buffer);
238
+ break;
239
+ }
240
+ case PM_CALL_NODE: {
241
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
242
+ if (((pm_call_node_t *)node)->receiver == NULL) {
243
+ pm_buffer_append_byte(buffer, 0);
244
+ } else {
245
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_node_t *)node)->receiver, buffer);
246
+ }
247
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_node_t *)node)->name));
248
+ if (((pm_call_node_t *)node)->arguments == NULL) {
249
+ pm_buffer_append_byte(buffer, 0);
250
+ } else {
251
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_node_t *)node)->arguments, buffer);
252
+ }
253
+ if (((pm_call_node_t *)node)->block == NULL) {
254
+ pm_buffer_append_byte(buffer, 0);
255
+ } else {
256
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_node_t *)node)->block, buffer);
257
+ }
258
+ break;
259
+ }
260
+ case PM_CALL_OPERATOR_WRITE_NODE: {
261
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
262
+ if (((pm_call_operator_write_node_t *)node)->receiver == NULL) {
263
+ pm_buffer_append_byte(buffer, 0);
264
+ } else {
265
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_operator_write_node_t *)node)->receiver, buffer);
266
+ }
267
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_operator_write_node_t *)node)->read_name));
268
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_operator_write_node_t *)node)->write_name));
269
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_operator_write_node_t *)node)->operator));
270
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_operator_write_node_t *)node)->value, buffer);
271
+ break;
272
+ }
273
+ case PM_CALL_OR_WRITE_NODE: {
274
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
275
+ if (((pm_call_or_write_node_t *)node)->receiver == NULL) {
276
+ pm_buffer_append_byte(buffer, 0);
277
+ } else {
278
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_or_write_node_t *)node)->receiver, buffer);
279
+ }
280
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_or_write_node_t *)node)->read_name));
281
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_or_write_node_t *)node)->write_name));
282
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_or_write_node_t *)node)->value, buffer);
283
+ break;
284
+ }
285
+ case PM_CALL_TARGET_NODE: {
286
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
287
+ pm_serialize_node(parser, (pm_node_t *)((pm_call_target_node_t *)node)->receiver, buffer);
288
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_call_target_node_t *)node)->name));
289
+ break;
290
+ }
291
+ case PM_CAPTURE_PATTERN_NODE: {
292
+ pm_serialize_node(parser, (pm_node_t *)((pm_capture_pattern_node_t *)node)->value, buffer);
293
+ pm_serialize_node(parser, (pm_node_t *)((pm_capture_pattern_node_t *)node)->target, buffer);
294
+ break;
295
+ }
296
+ case PM_CASE_MATCH_NODE: {
297
+ if (((pm_case_match_node_t *)node)->predicate == NULL) {
298
+ pm_buffer_append_byte(buffer, 0);
299
+ } else {
300
+ pm_serialize_node(parser, (pm_node_t *)((pm_case_match_node_t *)node)->predicate, buffer);
301
+ }
302
+ uint32_t conditions_size = pm_sizet_to_u32(((pm_case_match_node_t *)node)->conditions.size);
303
+ pm_buffer_append_varuint(buffer, conditions_size);
304
+ for (uint32_t index = 0; index < conditions_size; index++) {
305
+ pm_serialize_node(parser, (pm_node_t *) ((pm_case_match_node_t *)node)->conditions.nodes[index], buffer);
306
+ }
307
+ if (((pm_case_match_node_t *)node)->consequent == NULL) {
308
+ pm_buffer_append_byte(buffer, 0);
309
+ } else {
310
+ pm_serialize_node(parser, (pm_node_t *)((pm_case_match_node_t *)node)->consequent, buffer);
311
+ }
312
+ break;
313
+ }
314
+ case PM_CASE_NODE: {
315
+ if (((pm_case_node_t *)node)->predicate == NULL) {
316
+ pm_buffer_append_byte(buffer, 0);
317
+ } else {
318
+ pm_serialize_node(parser, (pm_node_t *)((pm_case_node_t *)node)->predicate, buffer);
319
+ }
320
+ uint32_t conditions_size = pm_sizet_to_u32(((pm_case_node_t *)node)->conditions.size);
321
+ pm_buffer_append_varuint(buffer, conditions_size);
322
+ for (uint32_t index = 0; index < conditions_size; index++) {
323
+ pm_serialize_node(parser, (pm_node_t *) ((pm_case_node_t *)node)->conditions.nodes[index], buffer);
324
+ }
325
+ if (((pm_case_node_t *)node)->consequent == NULL) {
326
+ pm_buffer_append_byte(buffer, 0);
327
+ } else {
328
+ pm_serialize_node(parser, (pm_node_t *)((pm_case_node_t *)node)->consequent, buffer);
329
+ }
330
+ break;
331
+ }
332
+ case PM_CLASS_NODE: {
333
+ uint32_t locals_size = pm_sizet_to_u32(((pm_class_node_t *)node)->locals.size);
334
+ pm_buffer_append_varuint(buffer, locals_size);
335
+ for (uint32_t index = 0; index < locals_size; index++) {
336
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_node_t *)node)->locals.ids[index]));
337
+ }
338
+ pm_serialize_node(parser, (pm_node_t *)((pm_class_node_t *)node)->constant_path, buffer);
339
+ if (((pm_class_node_t *)node)->superclass == NULL) {
340
+ pm_buffer_append_byte(buffer, 0);
341
+ } else {
342
+ pm_serialize_node(parser, (pm_node_t *)((pm_class_node_t *)node)->superclass, buffer);
343
+ }
344
+ if (((pm_class_node_t *)node)->body == NULL) {
345
+ pm_buffer_append_byte(buffer, 0);
346
+ } else {
347
+ pm_serialize_node(parser, (pm_node_t *)((pm_class_node_t *)node)->body, buffer);
348
+ }
349
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_node_t *)node)->name));
350
+ break;
351
+ }
352
+ case PM_CLASS_VARIABLE_AND_WRITE_NODE: {
353
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_variable_and_write_node_t *)node)->name));
354
+ pm_serialize_node(parser, (pm_node_t *)((pm_class_variable_and_write_node_t *)node)->value, buffer);
355
+ break;
356
+ }
357
+ case PM_CLASS_VARIABLE_OPERATOR_WRITE_NODE: {
358
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_variable_operator_write_node_t *)node)->name));
359
+ pm_serialize_node(parser, (pm_node_t *)((pm_class_variable_operator_write_node_t *)node)->value, buffer);
360
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_variable_operator_write_node_t *)node)->operator));
361
+ break;
362
+ }
363
+ case PM_CLASS_VARIABLE_OR_WRITE_NODE: {
364
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_variable_or_write_node_t *)node)->name));
365
+ pm_serialize_node(parser, (pm_node_t *)((pm_class_variable_or_write_node_t *)node)->value, buffer);
366
+ break;
367
+ }
368
+ case PM_CLASS_VARIABLE_READ_NODE: {
369
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_variable_read_node_t *)node)->name));
370
+ break;
371
+ }
372
+ case PM_CLASS_VARIABLE_TARGET_NODE: {
373
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_variable_target_node_t *)node)->name));
374
+ break;
375
+ }
376
+ case PM_CLASS_VARIABLE_WRITE_NODE: {
377
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_class_variable_write_node_t *)node)->name));
378
+ pm_serialize_node(parser, (pm_node_t *)((pm_class_variable_write_node_t *)node)->value, buffer);
379
+ break;
380
+ }
381
+ case PM_CONSTANT_AND_WRITE_NODE: {
382
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_and_write_node_t *)node)->name));
383
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_and_write_node_t *)node)->value, buffer);
384
+ break;
385
+ }
386
+ case PM_CONSTANT_OPERATOR_WRITE_NODE: {
387
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_operator_write_node_t *)node)->name));
388
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_operator_write_node_t *)node)->value, buffer);
389
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_operator_write_node_t *)node)->operator));
390
+ break;
391
+ }
392
+ case PM_CONSTANT_OR_WRITE_NODE: {
393
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_or_write_node_t *)node)->name));
394
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_or_write_node_t *)node)->value, buffer);
395
+ break;
396
+ }
397
+ case PM_CONSTANT_PATH_AND_WRITE_NODE: {
398
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_and_write_node_t *)node)->target, buffer);
399
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_and_write_node_t *)node)->value, buffer);
400
+ break;
401
+ }
402
+ case PM_CONSTANT_PATH_NODE: {
403
+ if (((pm_constant_path_node_t *)node)->parent == NULL) {
404
+ pm_buffer_append_byte(buffer, 0);
405
+ } else {
406
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_node_t *)node)->parent, buffer);
407
+ }
408
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_node_t *)node)->child, buffer);
409
+ break;
410
+ }
411
+ case PM_CONSTANT_PATH_OPERATOR_WRITE_NODE: {
412
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_operator_write_node_t *)node)->target, buffer);
413
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_operator_write_node_t *)node)->value, buffer);
414
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_path_operator_write_node_t *)node)->operator));
415
+ break;
416
+ }
417
+ case PM_CONSTANT_PATH_OR_WRITE_NODE: {
418
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_or_write_node_t *)node)->target, buffer);
419
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_or_write_node_t *)node)->value, buffer);
420
+ break;
421
+ }
422
+ case PM_CONSTANT_PATH_TARGET_NODE: {
423
+ if (((pm_constant_path_target_node_t *)node)->parent == NULL) {
424
+ pm_buffer_append_byte(buffer, 0);
425
+ } else {
426
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_target_node_t *)node)->parent, buffer);
427
+ }
428
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_target_node_t *)node)->child, buffer);
429
+ break;
430
+ }
431
+ case PM_CONSTANT_PATH_WRITE_NODE: {
432
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_write_node_t *)node)->target, buffer);
433
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_path_write_node_t *)node)->value, buffer);
434
+ break;
435
+ }
436
+ case PM_CONSTANT_READ_NODE: {
437
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_read_node_t *)node)->name));
438
+ break;
439
+ }
440
+ case PM_CONSTANT_TARGET_NODE: {
441
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_target_node_t *)node)->name));
442
+ break;
443
+ }
444
+ case PM_CONSTANT_WRITE_NODE: {
445
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_constant_write_node_t *)node)->name));
446
+ pm_serialize_node(parser, (pm_node_t *)((pm_constant_write_node_t *)node)->value, buffer);
447
+ break;
448
+ }
449
+ case PM_DEF_NODE: {
450
+ // serialize length
451
+ // encoding of location u32s make us need to save this offset.
452
+ size_t length_offset = buffer->length;
453
+ pm_buffer_append_string(buffer, "\0\0\0\0", 4); /* consume 4 bytes, updated below */
454
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_def_node_t *)node)->name));
455
+ if (((pm_def_node_t *)node)->receiver == NULL) {
456
+ pm_buffer_append_byte(buffer, 0);
457
+ } else {
458
+ pm_serialize_node(parser, (pm_node_t *)((pm_def_node_t *)node)->receiver, buffer);
459
+ }
460
+ if (((pm_def_node_t *)node)->parameters == NULL) {
461
+ pm_buffer_append_byte(buffer, 0);
462
+ } else {
463
+ pm_serialize_node(parser, (pm_node_t *)((pm_def_node_t *)node)->parameters, buffer);
464
+ }
465
+ if (((pm_def_node_t *)node)->body == NULL) {
466
+ pm_buffer_append_byte(buffer, 0);
467
+ } else {
468
+ pm_serialize_node(parser, (pm_node_t *)((pm_def_node_t *)node)->body, buffer);
469
+ }
470
+ uint32_t locals_size = pm_sizet_to_u32(((pm_def_node_t *)node)->locals.size);
471
+ pm_buffer_append_varuint(buffer, locals_size);
472
+ for (uint32_t index = 0; index < locals_size; index++) {
473
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_def_node_t *)node)->locals.ids[index]));
474
+ }
475
+ // serialize length
476
+ uint32_t length = pm_sizet_to_u32(buffer->length - offset - sizeof(uint32_t));
477
+ memcpy(buffer->value + length_offset, &length, sizeof(uint32_t));
478
+ break;
479
+ }
480
+ case PM_DEFINED_NODE: {
481
+ pm_serialize_node(parser, (pm_node_t *)((pm_defined_node_t *)node)->value, buffer);
482
+ break;
483
+ }
484
+ case PM_ELSE_NODE: {
485
+ if (((pm_else_node_t *)node)->statements == NULL) {
486
+ pm_buffer_append_byte(buffer, 0);
487
+ } else {
488
+ pm_serialize_node(parser, (pm_node_t *)((pm_else_node_t *)node)->statements, buffer);
489
+ }
490
+ break;
491
+ }
492
+ case PM_EMBEDDED_STATEMENTS_NODE: {
493
+ if (((pm_embedded_statements_node_t *)node)->statements == NULL) {
494
+ pm_buffer_append_byte(buffer, 0);
495
+ } else {
496
+ pm_serialize_node(parser, (pm_node_t *)((pm_embedded_statements_node_t *)node)->statements, buffer);
497
+ }
498
+ break;
499
+ }
500
+ case PM_EMBEDDED_VARIABLE_NODE: {
501
+ pm_serialize_node(parser, (pm_node_t *)((pm_embedded_variable_node_t *)node)->variable, buffer);
502
+ break;
503
+ }
504
+ case PM_ENSURE_NODE: {
505
+ if (((pm_ensure_node_t *)node)->statements == NULL) {
506
+ pm_buffer_append_byte(buffer, 0);
507
+ } else {
508
+ pm_serialize_node(parser, (pm_node_t *)((pm_ensure_node_t *)node)->statements, buffer);
509
+ }
510
+ break;
511
+ }
512
+ case PM_FALSE_NODE: {
513
+ break;
514
+ }
515
+ case PM_FIND_PATTERN_NODE: {
516
+ if (((pm_find_pattern_node_t *)node)->constant == NULL) {
517
+ pm_buffer_append_byte(buffer, 0);
518
+ } else {
519
+ pm_serialize_node(parser, (pm_node_t *)((pm_find_pattern_node_t *)node)->constant, buffer);
520
+ }
521
+ pm_serialize_node(parser, (pm_node_t *)((pm_find_pattern_node_t *)node)->left, buffer);
522
+ uint32_t requireds_size = pm_sizet_to_u32(((pm_find_pattern_node_t *)node)->requireds.size);
523
+ pm_buffer_append_varuint(buffer, requireds_size);
524
+ for (uint32_t index = 0; index < requireds_size; index++) {
525
+ pm_serialize_node(parser, (pm_node_t *) ((pm_find_pattern_node_t *)node)->requireds.nodes[index], buffer);
526
+ }
527
+ pm_serialize_node(parser, (pm_node_t *)((pm_find_pattern_node_t *)node)->right, buffer);
528
+ break;
529
+ }
530
+ case PM_FLIP_FLOP_NODE: {
531
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
532
+ if (((pm_flip_flop_node_t *)node)->left == NULL) {
533
+ pm_buffer_append_byte(buffer, 0);
534
+ } else {
535
+ pm_serialize_node(parser, (pm_node_t *)((pm_flip_flop_node_t *)node)->left, buffer);
536
+ }
537
+ if (((pm_flip_flop_node_t *)node)->right == NULL) {
538
+ pm_buffer_append_byte(buffer, 0);
539
+ } else {
540
+ pm_serialize_node(parser, (pm_node_t *)((pm_flip_flop_node_t *)node)->right, buffer);
541
+ }
542
+ break;
543
+ }
544
+ case PM_FLOAT_NODE: {
545
+ break;
546
+ }
547
+ case PM_FOR_NODE: {
548
+ pm_serialize_node(parser, (pm_node_t *)((pm_for_node_t *)node)->index, buffer);
549
+ pm_serialize_node(parser, (pm_node_t *)((pm_for_node_t *)node)->collection, buffer);
550
+ if (((pm_for_node_t *)node)->statements == NULL) {
551
+ pm_buffer_append_byte(buffer, 0);
552
+ } else {
553
+ pm_serialize_node(parser, (pm_node_t *)((pm_for_node_t *)node)->statements, buffer);
554
+ }
555
+ break;
556
+ }
557
+ case PM_FORWARDING_ARGUMENTS_NODE: {
558
+ break;
559
+ }
560
+ case PM_FORWARDING_PARAMETER_NODE: {
561
+ break;
562
+ }
563
+ case PM_FORWARDING_SUPER_NODE: {
564
+ if (((pm_forwarding_super_node_t *)node)->block == NULL) {
565
+ pm_buffer_append_byte(buffer, 0);
566
+ } else {
567
+ pm_serialize_node(parser, (pm_node_t *)((pm_forwarding_super_node_t *)node)->block, buffer);
568
+ }
569
+ break;
570
+ }
571
+ case PM_GLOBAL_VARIABLE_AND_WRITE_NODE: {
572
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_global_variable_and_write_node_t *)node)->name));
573
+ pm_serialize_node(parser, (pm_node_t *)((pm_global_variable_and_write_node_t *)node)->value, buffer);
574
+ break;
575
+ }
576
+ case PM_GLOBAL_VARIABLE_OPERATOR_WRITE_NODE: {
577
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_global_variable_operator_write_node_t *)node)->name));
578
+ pm_serialize_node(parser, (pm_node_t *)((pm_global_variable_operator_write_node_t *)node)->value, buffer);
579
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_global_variable_operator_write_node_t *)node)->operator));
580
+ break;
581
+ }
582
+ case PM_GLOBAL_VARIABLE_OR_WRITE_NODE: {
583
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_global_variable_or_write_node_t *)node)->name));
584
+ pm_serialize_node(parser, (pm_node_t *)((pm_global_variable_or_write_node_t *)node)->value, buffer);
585
+ break;
586
+ }
587
+ case PM_GLOBAL_VARIABLE_READ_NODE: {
588
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_global_variable_read_node_t *)node)->name));
589
+ break;
590
+ }
591
+ case PM_GLOBAL_VARIABLE_TARGET_NODE: {
592
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_global_variable_target_node_t *)node)->name));
593
+ break;
594
+ }
595
+ case PM_GLOBAL_VARIABLE_WRITE_NODE: {
596
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_global_variable_write_node_t *)node)->name));
597
+ pm_serialize_node(parser, (pm_node_t *)((pm_global_variable_write_node_t *)node)->value, buffer);
598
+ break;
599
+ }
600
+ case PM_HASH_NODE: {
601
+ uint32_t elements_size = pm_sizet_to_u32(((pm_hash_node_t *)node)->elements.size);
602
+ pm_buffer_append_varuint(buffer, elements_size);
603
+ for (uint32_t index = 0; index < elements_size; index++) {
604
+ pm_serialize_node(parser, (pm_node_t *) ((pm_hash_node_t *)node)->elements.nodes[index], buffer);
605
+ }
606
+ break;
607
+ }
608
+ case PM_HASH_PATTERN_NODE: {
609
+ if (((pm_hash_pattern_node_t *)node)->constant == NULL) {
610
+ pm_buffer_append_byte(buffer, 0);
611
+ } else {
612
+ pm_serialize_node(parser, (pm_node_t *)((pm_hash_pattern_node_t *)node)->constant, buffer);
613
+ }
614
+ uint32_t elements_size = pm_sizet_to_u32(((pm_hash_pattern_node_t *)node)->elements.size);
615
+ pm_buffer_append_varuint(buffer, elements_size);
616
+ for (uint32_t index = 0; index < elements_size; index++) {
617
+ pm_serialize_node(parser, (pm_node_t *) ((pm_hash_pattern_node_t *)node)->elements.nodes[index], buffer);
618
+ }
619
+ if (((pm_hash_pattern_node_t *)node)->rest == NULL) {
620
+ pm_buffer_append_byte(buffer, 0);
621
+ } else {
622
+ pm_serialize_node(parser, (pm_node_t *)((pm_hash_pattern_node_t *)node)->rest, buffer);
623
+ }
624
+ break;
625
+ }
626
+ case PM_IF_NODE: {
627
+ pm_serialize_node(parser, (pm_node_t *)((pm_if_node_t *)node)->predicate, buffer);
628
+ if (((pm_if_node_t *)node)->statements == NULL) {
629
+ pm_buffer_append_byte(buffer, 0);
630
+ } else {
631
+ pm_serialize_node(parser, (pm_node_t *)((pm_if_node_t *)node)->statements, buffer);
632
+ }
633
+ if (((pm_if_node_t *)node)->consequent == NULL) {
634
+ pm_buffer_append_byte(buffer, 0);
635
+ } else {
636
+ pm_serialize_node(parser, (pm_node_t *)((pm_if_node_t *)node)->consequent, buffer);
637
+ }
638
+ break;
639
+ }
640
+ case PM_IMAGINARY_NODE: {
641
+ pm_serialize_node(parser, (pm_node_t *)((pm_imaginary_node_t *)node)->numeric, buffer);
642
+ break;
643
+ }
644
+ case PM_IMPLICIT_NODE: {
645
+ pm_serialize_node(parser, (pm_node_t *)((pm_implicit_node_t *)node)->value, buffer);
646
+ break;
647
+ }
648
+ case PM_IMPLICIT_REST_NODE: {
649
+ break;
650
+ }
651
+ case PM_IN_NODE: {
652
+ pm_serialize_node(parser, (pm_node_t *)((pm_in_node_t *)node)->pattern, buffer);
653
+ if (((pm_in_node_t *)node)->statements == NULL) {
654
+ pm_buffer_append_byte(buffer, 0);
655
+ } else {
656
+ pm_serialize_node(parser, (pm_node_t *)((pm_in_node_t *)node)->statements, buffer);
657
+ }
658
+ break;
659
+ }
660
+ case PM_INDEX_AND_WRITE_NODE: {
661
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
662
+ if (((pm_index_and_write_node_t *)node)->receiver == NULL) {
663
+ pm_buffer_append_byte(buffer, 0);
664
+ } else {
665
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_and_write_node_t *)node)->receiver, buffer);
666
+ }
667
+ if (((pm_index_and_write_node_t *)node)->arguments == NULL) {
668
+ pm_buffer_append_byte(buffer, 0);
669
+ } else {
670
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_and_write_node_t *)node)->arguments, buffer);
671
+ }
672
+ if (((pm_index_and_write_node_t *)node)->block == NULL) {
673
+ pm_buffer_append_byte(buffer, 0);
674
+ } else {
675
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_and_write_node_t *)node)->block, buffer);
676
+ }
677
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_and_write_node_t *)node)->value, buffer);
678
+ break;
679
+ }
680
+ case PM_INDEX_OPERATOR_WRITE_NODE: {
681
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
682
+ if (((pm_index_operator_write_node_t *)node)->receiver == NULL) {
683
+ pm_buffer_append_byte(buffer, 0);
684
+ } else {
685
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_operator_write_node_t *)node)->receiver, buffer);
686
+ }
687
+ if (((pm_index_operator_write_node_t *)node)->arguments == NULL) {
688
+ pm_buffer_append_byte(buffer, 0);
689
+ } else {
690
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_operator_write_node_t *)node)->arguments, buffer);
691
+ }
692
+ if (((pm_index_operator_write_node_t *)node)->block == NULL) {
693
+ pm_buffer_append_byte(buffer, 0);
694
+ } else {
695
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_operator_write_node_t *)node)->block, buffer);
696
+ }
697
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_index_operator_write_node_t *)node)->operator));
698
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_operator_write_node_t *)node)->value, buffer);
699
+ break;
700
+ }
701
+ case PM_INDEX_OR_WRITE_NODE: {
702
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
703
+ if (((pm_index_or_write_node_t *)node)->receiver == NULL) {
704
+ pm_buffer_append_byte(buffer, 0);
705
+ } else {
706
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_or_write_node_t *)node)->receiver, buffer);
707
+ }
708
+ if (((pm_index_or_write_node_t *)node)->arguments == NULL) {
709
+ pm_buffer_append_byte(buffer, 0);
710
+ } else {
711
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_or_write_node_t *)node)->arguments, buffer);
712
+ }
713
+ if (((pm_index_or_write_node_t *)node)->block == NULL) {
714
+ pm_buffer_append_byte(buffer, 0);
715
+ } else {
716
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_or_write_node_t *)node)->block, buffer);
717
+ }
718
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_or_write_node_t *)node)->value, buffer);
719
+ break;
720
+ }
721
+ case PM_INDEX_TARGET_NODE: {
722
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
723
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_target_node_t *)node)->receiver, buffer);
724
+ if (((pm_index_target_node_t *)node)->arguments == NULL) {
725
+ pm_buffer_append_byte(buffer, 0);
726
+ } else {
727
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_target_node_t *)node)->arguments, buffer);
728
+ }
729
+ if (((pm_index_target_node_t *)node)->block == NULL) {
730
+ pm_buffer_append_byte(buffer, 0);
731
+ } else {
732
+ pm_serialize_node(parser, (pm_node_t *)((pm_index_target_node_t *)node)->block, buffer);
733
+ }
734
+ break;
735
+ }
736
+ case PM_INSTANCE_VARIABLE_AND_WRITE_NODE: {
737
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_instance_variable_and_write_node_t *)node)->name));
738
+ pm_serialize_node(parser, (pm_node_t *)((pm_instance_variable_and_write_node_t *)node)->value, buffer);
739
+ break;
740
+ }
741
+ case PM_INSTANCE_VARIABLE_OPERATOR_WRITE_NODE: {
742
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_instance_variable_operator_write_node_t *)node)->name));
743
+ pm_serialize_node(parser, (pm_node_t *)((pm_instance_variable_operator_write_node_t *)node)->value, buffer);
744
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_instance_variable_operator_write_node_t *)node)->operator));
745
+ break;
746
+ }
747
+ case PM_INSTANCE_VARIABLE_OR_WRITE_NODE: {
748
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_instance_variable_or_write_node_t *)node)->name));
749
+ pm_serialize_node(parser, (pm_node_t *)((pm_instance_variable_or_write_node_t *)node)->value, buffer);
750
+ break;
751
+ }
752
+ case PM_INSTANCE_VARIABLE_READ_NODE: {
753
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_instance_variable_read_node_t *)node)->name));
754
+ break;
755
+ }
756
+ case PM_INSTANCE_VARIABLE_TARGET_NODE: {
757
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_instance_variable_target_node_t *)node)->name));
758
+ break;
759
+ }
760
+ case PM_INSTANCE_VARIABLE_WRITE_NODE: {
761
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_instance_variable_write_node_t *)node)->name));
762
+ pm_serialize_node(parser, (pm_node_t *)((pm_instance_variable_write_node_t *)node)->value, buffer);
763
+ break;
764
+ }
765
+ case PM_INTEGER_NODE: {
766
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
767
+ break;
768
+ }
769
+ case PM_INTERPOLATED_MATCH_LAST_LINE_NODE: {
770
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
771
+ uint32_t parts_size = pm_sizet_to_u32(((pm_interpolated_match_last_line_node_t *)node)->parts.size);
772
+ pm_buffer_append_varuint(buffer, parts_size);
773
+ for (uint32_t index = 0; index < parts_size; index++) {
774
+ pm_serialize_node(parser, (pm_node_t *) ((pm_interpolated_match_last_line_node_t *)node)->parts.nodes[index], buffer);
775
+ }
776
+ break;
777
+ }
778
+ case PM_INTERPOLATED_REGULAR_EXPRESSION_NODE: {
779
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
780
+ uint32_t parts_size = pm_sizet_to_u32(((pm_interpolated_regular_expression_node_t *)node)->parts.size);
781
+ pm_buffer_append_varuint(buffer, parts_size);
782
+ for (uint32_t index = 0; index < parts_size; index++) {
783
+ pm_serialize_node(parser, (pm_node_t *) ((pm_interpolated_regular_expression_node_t *)node)->parts.nodes[index], buffer);
784
+ }
785
+ break;
786
+ }
787
+ case PM_INTERPOLATED_STRING_NODE: {
788
+ uint32_t parts_size = pm_sizet_to_u32(((pm_interpolated_string_node_t *)node)->parts.size);
789
+ pm_buffer_append_varuint(buffer, parts_size);
790
+ for (uint32_t index = 0; index < parts_size; index++) {
791
+ pm_serialize_node(parser, (pm_node_t *) ((pm_interpolated_string_node_t *)node)->parts.nodes[index], buffer);
792
+ }
793
+ break;
794
+ }
795
+ case PM_INTERPOLATED_SYMBOL_NODE: {
796
+ uint32_t parts_size = pm_sizet_to_u32(((pm_interpolated_symbol_node_t *)node)->parts.size);
797
+ pm_buffer_append_varuint(buffer, parts_size);
798
+ for (uint32_t index = 0; index < parts_size; index++) {
799
+ pm_serialize_node(parser, (pm_node_t *) ((pm_interpolated_symbol_node_t *)node)->parts.nodes[index], buffer);
800
+ }
801
+ break;
802
+ }
803
+ case PM_INTERPOLATED_X_STRING_NODE: {
804
+ uint32_t parts_size = pm_sizet_to_u32(((pm_interpolated_x_string_node_t *)node)->parts.size);
805
+ pm_buffer_append_varuint(buffer, parts_size);
806
+ for (uint32_t index = 0; index < parts_size; index++) {
807
+ pm_serialize_node(parser, (pm_node_t *) ((pm_interpolated_x_string_node_t *)node)->parts.nodes[index], buffer);
808
+ }
809
+ break;
810
+ }
811
+ case PM_KEYWORD_HASH_NODE: {
812
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
813
+ uint32_t elements_size = pm_sizet_to_u32(((pm_keyword_hash_node_t *)node)->elements.size);
814
+ pm_buffer_append_varuint(buffer, elements_size);
815
+ for (uint32_t index = 0; index < elements_size; index++) {
816
+ pm_serialize_node(parser, (pm_node_t *) ((pm_keyword_hash_node_t *)node)->elements.nodes[index], buffer);
817
+ }
818
+ break;
819
+ }
820
+ case PM_KEYWORD_REST_PARAMETER_NODE: {
821
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
822
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_keyword_rest_parameter_node_t *)node)->name));
823
+ break;
824
+ }
825
+ case PM_LAMBDA_NODE: {
826
+ uint32_t locals_size = pm_sizet_to_u32(((pm_lambda_node_t *)node)->locals.size);
827
+ pm_buffer_append_varuint(buffer, locals_size);
828
+ for (uint32_t index = 0; index < locals_size; index++) {
829
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_lambda_node_t *)node)->locals.ids[index]));
830
+ }
831
+ if (((pm_lambda_node_t *)node)->parameters == NULL) {
832
+ pm_buffer_append_byte(buffer, 0);
833
+ } else {
834
+ pm_serialize_node(parser, (pm_node_t *)((pm_lambda_node_t *)node)->parameters, buffer);
835
+ }
836
+ if (((pm_lambda_node_t *)node)->body == NULL) {
837
+ pm_buffer_append_byte(buffer, 0);
838
+ } else {
839
+ pm_serialize_node(parser, (pm_node_t *)((pm_lambda_node_t *)node)->body, buffer);
840
+ }
841
+ break;
842
+ }
843
+ case PM_LOCAL_VARIABLE_AND_WRITE_NODE: {
844
+ pm_serialize_node(parser, (pm_node_t *)((pm_local_variable_and_write_node_t *)node)->value, buffer);
845
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_local_variable_and_write_node_t *)node)->name));
846
+ pm_buffer_append_varuint(buffer, ((pm_local_variable_and_write_node_t *)node)->depth);
847
+ break;
848
+ }
849
+ case PM_LOCAL_VARIABLE_OPERATOR_WRITE_NODE: {
850
+ pm_serialize_node(parser, (pm_node_t *)((pm_local_variable_operator_write_node_t *)node)->value, buffer);
851
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_local_variable_operator_write_node_t *)node)->name));
852
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_local_variable_operator_write_node_t *)node)->operator));
853
+ pm_buffer_append_varuint(buffer, ((pm_local_variable_operator_write_node_t *)node)->depth);
854
+ break;
855
+ }
856
+ case PM_LOCAL_VARIABLE_OR_WRITE_NODE: {
857
+ pm_serialize_node(parser, (pm_node_t *)((pm_local_variable_or_write_node_t *)node)->value, buffer);
858
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_local_variable_or_write_node_t *)node)->name));
859
+ pm_buffer_append_varuint(buffer, ((pm_local_variable_or_write_node_t *)node)->depth);
860
+ break;
861
+ }
862
+ case PM_LOCAL_VARIABLE_READ_NODE: {
863
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_local_variable_read_node_t *)node)->name));
864
+ pm_buffer_append_varuint(buffer, ((pm_local_variable_read_node_t *)node)->depth);
865
+ break;
866
+ }
867
+ case PM_LOCAL_VARIABLE_TARGET_NODE: {
868
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_local_variable_target_node_t *)node)->name));
869
+ pm_buffer_append_varuint(buffer, ((pm_local_variable_target_node_t *)node)->depth);
870
+ break;
871
+ }
872
+ case PM_LOCAL_VARIABLE_WRITE_NODE: {
873
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_local_variable_write_node_t *)node)->name));
874
+ pm_buffer_append_varuint(buffer, ((pm_local_variable_write_node_t *)node)->depth);
875
+ pm_serialize_node(parser, (pm_node_t *)((pm_local_variable_write_node_t *)node)->value, buffer);
876
+ break;
877
+ }
878
+ case PM_MATCH_LAST_LINE_NODE: {
879
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
880
+ pm_serialize_string(parser, &((pm_match_last_line_node_t *)node)->unescaped, buffer);
881
+ break;
882
+ }
883
+ case PM_MATCH_PREDICATE_NODE: {
884
+ pm_serialize_node(parser, (pm_node_t *)((pm_match_predicate_node_t *)node)->value, buffer);
885
+ pm_serialize_node(parser, (pm_node_t *)((pm_match_predicate_node_t *)node)->pattern, buffer);
886
+ break;
887
+ }
888
+ case PM_MATCH_REQUIRED_NODE: {
889
+ pm_serialize_node(parser, (pm_node_t *)((pm_match_required_node_t *)node)->value, buffer);
890
+ pm_serialize_node(parser, (pm_node_t *)((pm_match_required_node_t *)node)->pattern, buffer);
891
+ break;
892
+ }
893
+ case PM_MATCH_WRITE_NODE: {
894
+ pm_serialize_node(parser, (pm_node_t *)((pm_match_write_node_t *)node)->call, buffer);
895
+ uint32_t targets_size = pm_sizet_to_u32(((pm_match_write_node_t *)node)->targets.size);
896
+ pm_buffer_append_varuint(buffer, targets_size);
897
+ for (uint32_t index = 0; index < targets_size; index++) {
898
+ pm_serialize_node(parser, (pm_node_t *) ((pm_match_write_node_t *)node)->targets.nodes[index], buffer);
899
+ }
900
+ break;
901
+ }
902
+ case PM_MISSING_NODE: {
903
+ break;
904
+ }
905
+ case PM_MODULE_NODE: {
906
+ uint32_t locals_size = pm_sizet_to_u32(((pm_module_node_t *)node)->locals.size);
907
+ pm_buffer_append_varuint(buffer, locals_size);
908
+ for (uint32_t index = 0; index < locals_size; index++) {
909
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_module_node_t *)node)->locals.ids[index]));
910
+ }
911
+ pm_serialize_node(parser, (pm_node_t *)((pm_module_node_t *)node)->constant_path, buffer);
912
+ if (((pm_module_node_t *)node)->body == NULL) {
913
+ pm_buffer_append_byte(buffer, 0);
914
+ } else {
915
+ pm_serialize_node(parser, (pm_node_t *)((pm_module_node_t *)node)->body, buffer);
916
+ }
917
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_module_node_t *)node)->name));
918
+ break;
919
+ }
920
+ case PM_MULTI_TARGET_NODE: {
921
+ uint32_t lefts_size = pm_sizet_to_u32(((pm_multi_target_node_t *)node)->lefts.size);
922
+ pm_buffer_append_varuint(buffer, lefts_size);
923
+ for (uint32_t index = 0; index < lefts_size; index++) {
924
+ pm_serialize_node(parser, (pm_node_t *) ((pm_multi_target_node_t *)node)->lefts.nodes[index], buffer);
925
+ }
926
+ if (((pm_multi_target_node_t *)node)->rest == NULL) {
927
+ pm_buffer_append_byte(buffer, 0);
928
+ } else {
929
+ pm_serialize_node(parser, (pm_node_t *)((pm_multi_target_node_t *)node)->rest, buffer);
930
+ }
931
+ uint32_t rights_size = pm_sizet_to_u32(((pm_multi_target_node_t *)node)->rights.size);
932
+ pm_buffer_append_varuint(buffer, rights_size);
933
+ for (uint32_t index = 0; index < rights_size; index++) {
934
+ pm_serialize_node(parser, (pm_node_t *) ((pm_multi_target_node_t *)node)->rights.nodes[index], buffer);
935
+ }
936
+ break;
937
+ }
938
+ case PM_MULTI_WRITE_NODE: {
939
+ uint32_t lefts_size = pm_sizet_to_u32(((pm_multi_write_node_t *)node)->lefts.size);
940
+ pm_buffer_append_varuint(buffer, lefts_size);
941
+ for (uint32_t index = 0; index < lefts_size; index++) {
942
+ pm_serialize_node(parser, (pm_node_t *) ((pm_multi_write_node_t *)node)->lefts.nodes[index], buffer);
943
+ }
944
+ if (((pm_multi_write_node_t *)node)->rest == NULL) {
945
+ pm_buffer_append_byte(buffer, 0);
946
+ } else {
947
+ pm_serialize_node(parser, (pm_node_t *)((pm_multi_write_node_t *)node)->rest, buffer);
948
+ }
949
+ uint32_t rights_size = pm_sizet_to_u32(((pm_multi_write_node_t *)node)->rights.size);
950
+ pm_buffer_append_varuint(buffer, rights_size);
951
+ for (uint32_t index = 0; index < rights_size; index++) {
952
+ pm_serialize_node(parser, (pm_node_t *) ((pm_multi_write_node_t *)node)->rights.nodes[index], buffer);
953
+ }
954
+ pm_serialize_node(parser, (pm_node_t *)((pm_multi_write_node_t *)node)->value, buffer);
955
+ break;
956
+ }
957
+ case PM_NEXT_NODE: {
958
+ if (((pm_next_node_t *)node)->arguments == NULL) {
959
+ pm_buffer_append_byte(buffer, 0);
960
+ } else {
961
+ pm_serialize_node(parser, (pm_node_t *)((pm_next_node_t *)node)->arguments, buffer);
962
+ }
963
+ break;
964
+ }
965
+ case PM_NIL_NODE: {
966
+ break;
967
+ }
968
+ case PM_NO_KEYWORDS_PARAMETER_NODE: {
969
+ break;
970
+ }
971
+ case PM_NUMBERED_PARAMETERS_NODE: {
972
+ pm_buffer_append_byte(buffer, ((pm_numbered_parameters_node_t *)node)->maximum);
973
+ break;
974
+ }
975
+ case PM_NUMBERED_REFERENCE_READ_NODE: {
976
+ pm_buffer_append_varuint(buffer, ((pm_numbered_reference_read_node_t *)node)->number);
977
+ break;
978
+ }
979
+ case PM_OPTIONAL_KEYWORD_PARAMETER_NODE: {
980
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
981
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_optional_keyword_parameter_node_t *)node)->name));
982
+ pm_serialize_node(parser, (pm_node_t *)((pm_optional_keyword_parameter_node_t *)node)->value, buffer);
983
+ break;
984
+ }
985
+ case PM_OPTIONAL_PARAMETER_NODE: {
986
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
987
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_optional_parameter_node_t *)node)->name));
988
+ pm_serialize_node(parser, (pm_node_t *)((pm_optional_parameter_node_t *)node)->value, buffer);
989
+ break;
990
+ }
991
+ case PM_OR_NODE: {
992
+ pm_serialize_node(parser, (pm_node_t *)((pm_or_node_t *)node)->left, buffer);
993
+ pm_serialize_node(parser, (pm_node_t *)((pm_or_node_t *)node)->right, buffer);
994
+ break;
995
+ }
996
+ case PM_PARAMETERS_NODE: {
997
+ uint32_t requireds_size = pm_sizet_to_u32(((pm_parameters_node_t *)node)->requireds.size);
998
+ pm_buffer_append_varuint(buffer, requireds_size);
999
+ for (uint32_t index = 0; index < requireds_size; index++) {
1000
+ pm_serialize_node(parser, (pm_node_t *) ((pm_parameters_node_t *)node)->requireds.nodes[index], buffer);
1001
+ }
1002
+ uint32_t optionals_size = pm_sizet_to_u32(((pm_parameters_node_t *)node)->optionals.size);
1003
+ pm_buffer_append_varuint(buffer, optionals_size);
1004
+ for (uint32_t index = 0; index < optionals_size; index++) {
1005
+ pm_serialize_node(parser, (pm_node_t *) ((pm_parameters_node_t *)node)->optionals.nodes[index], buffer);
1006
+ }
1007
+ if (((pm_parameters_node_t *)node)->rest == NULL) {
1008
+ pm_buffer_append_byte(buffer, 0);
1009
+ } else {
1010
+ pm_serialize_node(parser, (pm_node_t *)((pm_parameters_node_t *)node)->rest, buffer);
1011
+ }
1012
+ uint32_t posts_size = pm_sizet_to_u32(((pm_parameters_node_t *)node)->posts.size);
1013
+ pm_buffer_append_varuint(buffer, posts_size);
1014
+ for (uint32_t index = 0; index < posts_size; index++) {
1015
+ pm_serialize_node(parser, (pm_node_t *) ((pm_parameters_node_t *)node)->posts.nodes[index], buffer);
1016
+ }
1017
+ uint32_t keywords_size = pm_sizet_to_u32(((pm_parameters_node_t *)node)->keywords.size);
1018
+ pm_buffer_append_varuint(buffer, keywords_size);
1019
+ for (uint32_t index = 0; index < keywords_size; index++) {
1020
+ pm_serialize_node(parser, (pm_node_t *) ((pm_parameters_node_t *)node)->keywords.nodes[index], buffer);
1021
+ }
1022
+ if (((pm_parameters_node_t *)node)->keyword_rest == NULL) {
1023
+ pm_buffer_append_byte(buffer, 0);
1024
+ } else {
1025
+ pm_serialize_node(parser, (pm_node_t *)((pm_parameters_node_t *)node)->keyword_rest, buffer);
1026
+ }
1027
+ if (((pm_parameters_node_t *)node)->block == NULL) {
1028
+ pm_buffer_append_byte(buffer, 0);
1029
+ } else {
1030
+ pm_serialize_node(parser, (pm_node_t *)((pm_parameters_node_t *)node)->block, buffer);
1031
+ }
1032
+ break;
1033
+ }
1034
+ case PM_PARENTHESES_NODE: {
1035
+ if (((pm_parentheses_node_t *)node)->body == NULL) {
1036
+ pm_buffer_append_byte(buffer, 0);
1037
+ } else {
1038
+ pm_serialize_node(parser, (pm_node_t *)((pm_parentheses_node_t *)node)->body, buffer);
1039
+ }
1040
+ break;
1041
+ }
1042
+ case PM_PINNED_EXPRESSION_NODE: {
1043
+ pm_serialize_node(parser, (pm_node_t *)((pm_pinned_expression_node_t *)node)->expression, buffer);
1044
+ break;
1045
+ }
1046
+ case PM_PINNED_VARIABLE_NODE: {
1047
+ pm_serialize_node(parser, (pm_node_t *)((pm_pinned_variable_node_t *)node)->variable, buffer);
1048
+ break;
1049
+ }
1050
+ case PM_POST_EXECUTION_NODE: {
1051
+ if (((pm_post_execution_node_t *)node)->statements == NULL) {
1052
+ pm_buffer_append_byte(buffer, 0);
1053
+ } else {
1054
+ pm_serialize_node(parser, (pm_node_t *)((pm_post_execution_node_t *)node)->statements, buffer);
1055
+ }
1056
+ break;
1057
+ }
1058
+ case PM_PRE_EXECUTION_NODE: {
1059
+ if (((pm_pre_execution_node_t *)node)->statements == NULL) {
1060
+ pm_buffer_append_byte(buffer, 0);
1061
+ } else {
1062
+ pm_serialize_node(parser, (pm_node_t *)((pm_pre_execution_node_t *)node)->statements, buffer);
1063
+ }
1064
+ break;
1065
+ }
1066
+ case PM_PROGRAM_NODE: {
1067
+ uint32_t locals_size = pm_sizet_to_u32(((pm_program_node_t *)node)->locals.size);
1068
+ pm_buffer_append_varuint(buffer, locals_size);
1069
+ for (uint32_t index = 0; index < locals_size; index++) {
1070
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_program_node_t *)node)->locals.ids[index]));
1071
+ }
1072
+ pm_serialize_node(parser, (pm_node_t *)((pm_program_node_t *)node)->statements, buffer);
1073
+ break;
1074
+ }
1075
+ case PM_RANGE_NODE: {
1076
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1077
+ if (((pm_range_node_t *)node)->left == NULL) {
1078
+ pm_buffer_append_byte(buffer, 0);
1079
+ } else {
1080
+ pm_serialize_node(parser, (pm_node_t *)((pm_range_node_t *)node)->left, buffer);
1081
+ }
1082
+ if (((pm_range_node_t *)node)->right == NULL) {
1083
+ pm_buffer_append_byte(buffer, 0);
1084
+ } else {
1085
+ pm_serialize_node(parser, (pm_node_t *)((pm_range_node_t *)node)->right, buffer);
1086
+ }
1087
+ break;
1088
+ }
1089
+ case PM_RATIONAL_NODE: {
1090
+ pm_serialize_node(parser, (pm_node_t *)((pm_rational_node_t *)node)->numeric, buffer);
1091
+ break;
1092
+ }
1093
+ case PM_REDO_NODE: {
1094
+ break;
1095
+ }
1096
+ case PM_REGULAR_EXPRESSION_NODE: {
1097
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1098
+ pm_serialize_string(parser, &((pm_regular_expression_node_t *)node)->unescaped, buffer);
1099
+ break;
1100
+ }
1101
+ case PM_REQUIRED_KEYWORD_PARAMETER_NODE: {
1102
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1103
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_required_keyword_parameter_node_t *)node)->name));
1104
+ break;
1105
+ }
1106
+ case PM_REQUIRED_PARAMETER_NODE: {
1107
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1108
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_required_parameter_node_t *)node)->name));
1109
+ break;
1110
+ }
1111
+ case PM_RESCUE_MODIFIER_NODE: {
1112
+ pm_serialize_node(parser, (pm_node_t *)((pm_rescue_modifier_node_t *)node)->expression, buffer);
1113
+ pm_serialize_node(parser, (pm_node_t *)((pm_rescue_modifier_node_t *)node)->rescue_expression, buffer);
1114
+ break;
1115
+ }
1116
+ case PM_RESCUE_NODE: {
1117
+ uint32_t exceptions_size = pm_sizet_to_u32(((pm_rescue_node_t *)node)->exceptions.size);
1118
+ pm_buffer_append_varuint(buffer, exceptions_size);
1119
+ for (uint32_t index = 0; index < exceptions_size; index++) {
1120
+ pm_serialize_node(parser, (pm_node_t *) ((pm_rescue_node_t *)node)->exceptions.nodes[index], buffer);
1121
+ }
1122
+ if (((pm_rescue_node_t *)node)->reference == NULL) {
1123
+ pm_buffer_append_byte(buffer, 0);
1124
+ } else {
1125
+ pm_serialize_node(parser, (pm_node_t *)((pm_rescue_node_t *)node)->reference, buffer);
1126
+ }
1127
+ if (((pm_rescue_node_t *)node)->statements == NULL) {
1128
+ pm_buffer_append_byte(buffer, 0);
1129
+ } else {
1130
+ pm_serialize_node(parser, (pm_node_t *)((pm_rescue_node_t *)node)->statements, buffer);
1131
+ }
1132
+ if (((pm_rescue_node_t *)node)->consequent == NULL) {
1133
+ pm_buffer_append_byte(buffer, 0);
1134
+ } else {
1135
+ pm_serialize_node(parser, (pm_node_t *)((pm_rescue_node_t *)node)->consequent, buffer);
1136
+ }
1137
+ break;
1138
+ }
1139
+ case PM_REST_PARAMETER_NODE: {
1140
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1141
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_rest_parameter_node_t *)node)->name));
1142
+ break;
1143
+ }
1144
+ case PM_RETRY_NODE: {
1145
+ break;
1146
+ }
1147
+ case PM_RETURN_NODE: {
1148
+ if (((pm_return_node_t *)node)->arguments == NULL) {
1149
+ pm_buffer_append_byte(buffer, 0);
1150
+ } else {
1151
+ pm_serialize_node(parser, (pm_node_t *)((pm_return_node_t *)node)->arguments, buffer);
1152
+ }
1153
+ break;
1154
+ }
1155
+ case PM_SELF_NODE: {
1156
+ break;
1157
+ }
1158
+ case PM_SINGLETON_CLASS_NODE: {
1159
+ uint32_t locals_size = pm_sizet_to_u32(((pm_singleton_class_node_t *)node)->locals.size);
1160
+ pm_buffer_append_varuint(buffer, locals_size);
1161
+ for (uint32_t index = 0; index < locals_size; index++) {
1162
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(((pm_singleton_class_node_t *)node)->locals.ids[index]));
1163
+ }
1164
+ pm_serialize_node(parser, (pm_node_t *)((pm_singleton_class_node_t *)node)->expression, buffer);
1165
+ if (((pm_singleton_class_node_t *)node)->body == NULL) {
1166
+ pm_buffer_append_byte(buffer, 0);
1167
+ } else {
1168
+ pm_serialize_node(parser, (pm_node_t *)((pm_singleton_class_node_t *)node)->body, buffer);
1169
+ }
1170
+ break;
1171
+ }
1172
+ case PM_SOURCE_ENCODING_NODE: {
1173
+ break;
1174
+ }
1175
+ case PM_SOURCE_FILE_NODE: {
1176
+ pm_serialize_string(parser, &((pm_source_file_node_t *)node)->filepath, buffer);
1177
+ break;
1178
+ }
1179
+ case PM_SOURCE_LINE_NODE: {
1180
+ break;
1181
+ }
1182
+ case PM_SPLAT_NODE: {
1183
+ if (((pm_splat_node_t *)node)->expression == NULL) {
1184
+ pm_buffer_append_byte(buffer, 0);
1185
+ } else {
1186
+ pm_serialize_node(parser, (pm_node_t *)((pm_splat_node_t *)node)->expression, buffer);
1187
+ }
1188
+ break;
1189
+ }
1190
+ case PM_STATEMENTS_NODE: {
1191
+ uint32_t body_size = pm_sizet_to_u32(((pm_statements_node_t *)node)->body.size);
1192
+ pm_buffer_append_varuint(buffer, body_size);
1193
+ for (uint32_t index = 0; index < body_size; index++) {
1194
+ pm_serialize_node(parser, (pm_node_t *) ((pm_statements_node_t *)node)->body.nodes[index], buffer);
1195
+ }
1196
+ break;
1197
+ }
1198
+ case PM_STRING_NODE: {
1199
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1200
+ pm_serialize_string(parser, &((pm_string_node_t *)node)->unescaped, buffer);
1201
+ break;
1202
+ }
1203
+ case PM_SUPER_NODE: {
1204
+ if (((pm_super_node_t *)node)->arguments == NULL) {
1205
+ pm_buffer_append_byte(buffer, 0);
1206
+ } else {
1207
+ pm_serialize_node(parser, (pm_node_t *)((pm_super_node_t *)node)->arguments, buffer);
1208
+ }
1209
+ if (((pm_super_node_t *)node)->block == NULL) {
1210
+ pm_buffer_append_byte(buffer, 0);
1211
+ } else {
1212
+ pm_serialize_node(parser, (pm_node_t *)((pm_super_node_t *)node)->block, buffer);
1213
+ }
1214
+ break;
1215
+ }
1216
+ case PM_SYMBOL_NODE: {
1217
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1218
+ pm_serialize_string(parser, &((pm_symbol_node_t *)node)->unescaped, buffer);
1219
+ break;
1220
+ }
1221
+ case PM_TRUE_NODE: {
1222
+ break;
1223
+ }
1224
+ case PM_UNDEF_NODE: {
1225
+ uint32_t names_size = pm_sizet_to_u32(((pm_undef_node_t *)node)->names.size);
1226
+ pm_buffer_append_varuint(buffer, names_size);
1227
+ for (uint32_t index = 0; index < names_size; index++) {
1228
+ pm_serialize_node(parser, (pm_node_t *) ((pm_undef_node_t *)node)->names.nodes[index], buffer);
1229
+ }
1230
+ break;
1231
+ }
1232
+ case PM_UNLESS_NODE: {
1233
+ pm_serialize_node(parser, (pm_node_t *)((pm_unless_node_t *)node)->predicate, buffer);
1234
+ if (((pm_unless_node_t *)node)->statements == NULL) {
1235
+ pm_buffer_append_byte(buffer, 0);
1236
+ } else {
1237
+ pm_serialize_node(parser, (pm_node_t *)((pm_unless_node_t *)node)->statements, buffer);
1238
+ }
1239
+ if (((pm_unless_node_t *)node)->consequent == NULL) {
1240
+ pm_buffer_append_byte(buffer, 0);
1241
+ } else {
1242
+ pm_serialize_node(parser, (pm_node_t *)((pm_unless_node_t *)node)->consequent, buffer);
1243
+ }
1244
+ break;
1245
+ }
1246
+ case PM_UNTIL_NODE: {
1247
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1248
+ pm_serialize_node(parser, (pm_node_t *)((pm_until_node_t *)node)->predicate, buffer);
1249
+ if (((pm_until_node_t *)node)->statements == NULL) {
1250
+ pm_buffer_append_byte(buffer, 0);
1251
+ } else {
1252
+ pm_serialize_node(parser, (pm_node_t *)((pm_until_node_t *)node)->statements, buffer);
1253
+ }
1254
+ break;
1255
+ }
1256
+ case PM_WHEN_NODE: {
1257
+ uint32_t conditions_size = pm_sizet_to_u32(((pm_when_node_t *)node)->conditions.size);
1258
+ pm_buffer_append_varuint(buffer, conditions_size);
1259
+ for (uint32_t index = 0; index < conditions_size; index++) {
1260
+ pm_serialize_node(parser, (pm_node_t *) ((pm_when_node_t *)node)->conditions.nodes[index], buffer);
1261
+ }
1262
+ if (((pm_when_node_t *)node)->statements == NULL) {
1263
+ pm_buffer_append_byte(buffer, 0);
1264
+ } else {
1265
+ pm_serialize_node(parser, (pm_node_t *)((pm_when_node_t *)node)->statements, buffer);
1266
+ }
1267
+ break;
1268
+ }
1269
+ case PM_WHILE_NODE: {
1270
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1271
+ pm_serialize_node(parser, (pm_node_t *)((pm_while_node_t *)node)->predicate, buffer);
1272
+ if (((pm_while_node_t *)node)->statements == NULL) {
1273
+ pm_buffer_append_byte(buffer, 0);
1274
+ } else {
1275
+ pm_serialize_node(parser, (pm_node_t *)((pm_while_node_t *)node)->statements, buffer);
1276
+ }
1277
+ break;
1278
+ }
1279
+ case PM_X_STRING_NODE: {
1280
+ pm_buffer_append_varuint(buffer, (uint32_t)(node->flags & ~PM_NODE_FLAG_COMMON_MASK));
1281
+ pm_serialize_string(parser, &((pm_x_string_node_t *)node)->unescaped, buffer);
1282
+ break;
1283
+ }
1284
+ case PM_YIELD_NODE: {
1285
+ if (((pm_yield_node_t *)node)->arguments == NULL) {
1286
+ pm_buffer_append_byte(buffer, 0);
1287
+ } else {
1288
+ pm_serialize_node(parser, (pm_node_t *)((pm_yield_node_t *)node)->arguments, buffer);
1289
+ }
1290
+ break;
1291
+ }
1292
+ }
1293
+ }
1294
+
1295
+ static void
1296
+ pm_serialize_newline_list(pm_newline_list_t *list, pm_buffer_t *buffer) {
1297
+ uint32_t size = pm_sizet_to_u32(list->size);
1298
+ pm_buffer_append_varuint(buffer, size);
1299
+
1300
+ for (uint32_t i = 0; i < size; i++) {
1301
+ uint32_t offset = pm_sizet_to_u32(list->offsets[i]);
1302
+ pm_buffer_append_varuint(buffer, offset);
1303
+ }
1304
+ }
1305
+
1306
+ static void
1307
+ pm_serialize_comment(pm_parser_t *parser, pm_comment_t *comment, pm_buffer_t *buffer) {
1308
+ // serialize type
1309
+ pm_buffer_append_byte(buffer, (uint8_t) comment->type);
1310
+
1311
+ // serialize location
1312
+ pm_serialize_location(parser, &comment->location, buffer);
1313
+ }
1314
+
1315
+ /**
1316
+ * Serialize the given list of comments to the given buffer.
1317
+ */
1318
+ void
1319
+ pm_serialize_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer) {
1320
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_list_size(list)));
1321
+
1322
+ pm_comment_t *comment;
1323
+ for (comment = (pm_comment_t *) list->head; comment != NULL; comment = (pm_comment_t *) comment->node.next) {
1324
+ pm_serialize_comment(parser, comment, buffer);
1325
+ }
1326
+ }
1327
+
1328
+ static void
1329
+ pm_serialize_magic_comment(pm_parser_t *parser, pm_magic_comment_t *magic_comment, pm_buffer_t *buffer) {
1330
+ // serialize key location
1331
+ pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(magic_comment->key_start - parser->start));
1332
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(magic_comment->key_length));
1333
+
1334
+ // serialize value location
1335
+ pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(magic_comment->value_start - parser->start));
1336
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(magic_comment->value_length));
1337
+ }
1338
+
1339
+ static void
1340
+ pm_serialize_magic_comment_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer) {
1341
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_list_size(list)));
1342
+
1343
+ pm_magic_comment_t *magic_comment;
1344
+ for (magic_comment = (pm_magic_comment_t *) list->head; magic_comment != NULL; magic_comment = (pm_magic_comment_t *) magic_comment->node.next) {
1345
+ pm_serialize_magic_comment(parser, magic_comment, buffer);
1346
+ }
1347
+ }
1348
+
1349
+ static void
1350
+ pm_serialize_data_loc(const pm_parser_t *parser, pm_buffer_t *buffer) {
1351
+ if (parser->data_loc.end == NULL) {
1352
+ pm_buffer_append_byte(buffer, 0);
1353
+ } else {
1354
+ pm_buffer_append_byte(buffer, 1);
1355
+ pm_serialize_location(parser, &parser->data_loc, buffer);
1356
+ }
1357
+ }
1358
+
1359
+ static void
1360
+ pm_serialize_diagnostic(pm_parser_t *parser, pm_diagnostic_t *diagnostic, pm_buffer_t *buffer) {
1361
+ // serialize message
1362
+ size_t message_length = strlen(diagnostic->message);
1363
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(message_length));
1364
+ pm_buffer_append_string(buffer, diagnostic->message, message_length);
1365
+
1366
+ // serialize location
1367
+ pm_serialize_location(parser, &diagnostic->location, buffer);
1368
+
1369
+ pm_buffer_append_byte(buffer, diagnostic->level);
1370
+ }
1371
+
1372
+ static void
1373
+ pm_serialize_diagnostic_list(pm_parser_t *parser, pm_list_t *list, pm_buffer_t *buffer) {
1374
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(pm_list_size(list)));
1375
+
1376
+ pm_diagnostic_t *diagnostic;
1377
+ for (diagnostic = (pm_diagnostic_t *) list->head; diagnostic != NULL; diagnostic = (pm_diagnostic_t *) diagnostic->node.next) {
1378
+ pm_serialize_diagnostic(parser, diagnostic, buffer);
1379
+ }
1380
+ }
1381
+
1382
+ /**
1383
+ * Serialize the name of the encoding to the buffer.
1384
+ */
1385
+ void
1386
+ pm_serialize_encoding(const pm_encoding_t *encoding, pm_buffer_t *buffer) {
1387
+ size_t encoding_length = strlen(encoding->name);
1388
+ pm_buffer_append_varuint(buffer, pm_sizet_to_u32(encoding_length));
1389
+ pm_buffer_append_string(buffer, encoding->name, encoding_length);
1390
+ }
1391
+
1392
+ static void
1393
+ pm_serialize_metadata(pm_parser_t *parser, pm_buffer_t *buffer) {
1394
+ pm_serialize_encoding(parser->encoding, buffer);
1395
+ pm_buffer_append_varsint(buffer, parser->start_line);
1396
+ pm_serialize_newline_list(&parser->newline_list, buffer);
1397
+ pm_serialize_magic_comment_list(parser, &parser->magic_comment_list, buffer);
1398
+ pm_serialize_data_loc(parser, buffer);
1399
+ pm_serialize_diagnostic_list(parser, &parser->error_list, buffer);
1400
+ pm_serialize_diagnostic_list(parser, &parser->warning_list, buffer);
1401
+ }
1402
+
1403
+ #line 243 "serialize.c.erb"
1404
+ /**
1405
+ * Serialize the metadata, nodes, and constant pool.
1406
+ */
1407
+ void
1408
+ pm_serialize_content(pm_parser_t *parser, pm_node_t *node, pm_buffer_t *buffer) {
1409
+ pm_serialize_metadata(parser, buffer);
1410
+
1411
+ // Here we're going to leave space for the offset of the constant pool in
1412
+ // the buffer.
1413
+ size_t offset = buffer->length;
1414
+ pm_buffer_append_zeroes(buffer, 4);
1415
+
1416
+ // Next, encode the length of the constant pool.
1417
+ pm_buffer_append_varuint(buffer, parser->constant_pool.size);
1418
+
1419
+ // Now we're going to serialize the content of the node.
1420
+ pm_serialize_node(parser, node, buffer);
1421
+
1422
+ // Now we're going to serialize the offset of the constant pool back where
1423
+ // we left space for it.
1424
+ uint32_t length = pm_sizet_to_u32(buffer->length);
1425
+ memcpy(buffer->value + offset, &length, sizeof(uint32_t));
1426
+
1427
+ // Now we're going to serialize the constant pool.
1428
+ offset = buffer->length;
1429
+ pm_buffer_append_zeroes(buffer, parser->constant_pool.size * 8);
1430
+
1431
+ for (uint32_t index = 0; index < parser->constant_pool.capacity; index++) {
1432
+ pm_constant_pool_bucket_t *bucket = &parser->constant_pool.buckets[index];
1433
+
1434
+ // If we find a constant at this index, serialize it at the correct
1435
+ // index in the buffer.
1436
+ if (bucket->id != 0) {
1437
+ pm_constant_t *constant = &parser->constant_pool.constants[bucket->id - 1];
1438
+ size_t buffer_offset = offset + ((((size_t)bucket->id) - 1) * 8);
1439
+
1440
+ if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED || bucket->type == PM_CONSTANT_POOL_BUCKET_CONSTANT) {
1441
+ // Since this is an owned or constant constant, we are going to
1442
+ // write its contents into the buffer after the constant pool.
1443
+ // So effectively in place of the source offset, we have a
1444
+ // buffer offset. We will add a leading 1 to indicate that this
1445
+ // is a buffer offset.
1446
+ uint32_t content_offset = pm_sizet_to_u32(buffer->length);
1447
+ uint32_t owned_mask = (uint32_t) (1 << 31);
1448
+
1449
+ assert(content_offset < owned_mask);
1450
+ content_offset |= owned_mask;
1451
+
1452
+ memcpy(buffer->value + buffer_offset, &content_offset, 4);
1453
+ pm_buffer_append_bytes(buffer, constant->start, constant->length);
1454
+ } else {
1455
+ // Since this is a shared constant, we are going to write its
1456
+ // source offset directly into the buffer.
1457
+ uint32_t source_offset = pm_ptrdifft_to_u32(constant->start - parser->start);
1458
+ memcpy(buffer->value + buffer_offset, &source_offset, 4);
1459
+ }
1460
+
1461
+ // Now we can write the length of the constant into the buffer.
1462
+ uint32_t constant_length = pm_sizet_to_u32(constant->length);
1463
+ memcpy(buffer->value + buffer_offset + 4, &constant_length, 4);
1464
+ }
1465
+ }
1466
+ }
1467
+
1468
+ static void
1469
+ serialize_token(void *data, pm_parser_t *parser, pm_token_t *token) {
1470
+ pm_buffer_t *buffer = (pm_buffer_t *) data;
1471
+
1472
+ pm_buffer_append_varuint(buffer, token->type);
1473
+ pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(token->start - parser->start));
1474
+ pm_buffer_append_varuint(buffer, pm_ptrdifft_to_u32(token->end - token->start));
1475
+ pm_buffer_append_varuint(buffer, parser->lex_state);
1476
+ }
1477
+
1478
+ /**
1479
+ * Lex the given source and serialize to the given buffer.
1480
+ */
1481
+ PRISM_EXPORTED_FUNCTION void
1482
+ pm_serialize_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data) {
1483
+ pm_options_t options = { 0 };
1484
+ pm_options_read(&options, data);
1485
+
1486
+ pm_parser_t parser;
1487
+ pm_parser_init(&parser, source, size, &options);
1488
+
1489
+ pm_lex_callback_t lex_callback = (pm_lex_callback_t) {
1490
+ .data = (void *) buffer,
1491
+ .callback = serialize_token,
1492
+ };
1493
+
1494
+ parser.lex_callback = &lex_callback;
1495
+ pm_node_t *node = pm_parse(&parser);
1496
+
1497
+ // Append 0 to mark end of tokens.
1498
+ pm_buffer_append_byte(buffer, 0);
1499
+
1500
+ pm_serialize_metadata(&parser, buffer);
1501
+
1502
+ pm_node_destroy(&parser, node);
1503
+ pm_parser_free(&parser);
1504
+ pm_options_free(&options);
1505
+ }
1506
+
1507
+ /**
1508
+ * Parse and serialize both the AST and the tokens represented by the given
1509
+ * source to the given buffer.
1510
+ */
1511
+ PRISM_EXPORTED_FUNCTION void
1512
+ pm_serialize_parse_lex(pm_buffer_t *buffer, const uint8_t *source, size_t size, const char *data) {
1513
+ pm_options_t options = { 0 };
1514
+ pm_options_read(&options, data);
1515
+
1516
+ pm_parser_t parser;
1517
+ pm_parser_init(&parser, source, size, &options);
1518
+
1519
+ pm_lex_callback_t lex_callback = (pm_lex_callback_t) {
1520
+ .data = (void *) buffer,
1521
+ .callback = serialize_token,
1522
+ };
1523
+
1524
+ parser.lex_callback = &lex_callback;
1525
+ pm_node_t *node = pm_parse(&parser);
1526
+
1527
+ pm_buffer_append_byte(buffer, 0);
1528
+ pm_serialize(&parser, node, buffer);
1529
+
1530
+ pm_node_destroy(&parser, node);
1531
+ pm_parser_free(&parser);
1532
+ pm_options_free(&options);
1533
+ }
1534
+
1535
+ /**
1536
+ * Parse the source and return true if it parses without errors or warnings.
1537
+ */
1538
+ PRISM_EXPORTED_FUNCTION bool
1539
+ pm_parse_success_p(const uint8_t *source, size_t size, const char *data) {
1540
+ pm_options_t options = { 0 };
1541
+ pm_options_read(&options, data);
1542
+
1543
+ pm_parser_t parser;
1544
+ pm_parser_init(&parser, source, size, &options);
1545
+
1546
+ pm_node_t *node = pm_parse(&parser);
1547
+ pm_node_destroy(&parser, node);
1548
+
1549
+ bool result = parser.error_list.size == 0 && parser.warning_list.size == 0;
1550
+ pm_parser_free(&parser);
1551
+ pm_options_free(&options);
1552
+
1553
+ return result;
1554
+ }