prism 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -1
  3. data/Makefile +2 -1
  4. data/README.md +1 -0
  5. data/config.yml +273 -37
  6. data/docs/parser_translation.md +8 -23
  7. data/docs/releasing.md +1 -1
  8. data/docs/ripper_translation.md +1 -1
  9. data/docs/ruby_api.md +1 -1
  10. data/ext/prism/api_node.c +1816 -1303
  11. data/ext/prism/extension.c +244 -110
  12. data/ext/prism/extension.h +4 -4
  13. data/include/prism/ast.h +291 -49
  14. data/include/prism/defines.h +4 -1
  15. data/include/prism/diagnostic.h +4 -0
  16. data/include/prism/options.h +89 -3
  17. data/include/prism/regexp.h +2 -2
  18. data/include/prism/util/pm_buffer.h +18 -0
  19. data/include/prism/util/pm_integer.h +4 -0
  20. data/include/prism/util/pm_list.h +6 -0
  21. data/include/prism/util/pm_string.h +12 -2
  22. data/include/prism/version.h +2 -2
  23. data/include/prism.h +41 -16
  24. data/lib/prism/compiler.rb +456 -151
  25. data/lib/prism/desugar_compiler.rb +1 -0
  26. data/lib/prism/dispatcher.rb +16 -0
  27. data/lib/prism/dot_visitor.rb +21 -1
  28. data/lib/prism/dsl.rb +13 -2
  29. data/lib/prism/ffi.rb +62 -34
  30. data/lib/prism/inspect_visitor.rb +5 -1
  31. data/lib/prism/lex_compat.rb +1 -0
  32. data/lib/prism/mutation_compiler.rb +3 -0
  33. data/lib/prism/node.rb +554 -345
  34. data/lib/prism/node_ext.rb +4 -1
  35. data/lib/prism/pack.rb +2 -0
  36. data/lib/prism/parse_result/comments.rb +1 -0
  37. data/lib/prism/parse_result/errors.rb +1 -0
  38. data/lib/prism/parse_result/newlines.rb +2 -1
  39. data/lib/prism/parse_result.rb +53 -0
  40. data/lib/prism/pattern.rb +1 -0
  41. data/lib/prism/polyfill/append_as_bytes.rb +15 -0
  42. data/lib/prism/polyfill/scan_byte.rb +14 -0
  43. data/lib/prism/polyfill/warn.rb +42 -0
  44. data/lib/prism/reflection.rb +5 -2
  45. data/lib/prism/relocation.rb +1 -0
  46. data/lib/prism/serialize.rb +1275 -783
  47. data/lib/prism/string_query.rb +1 -0
  48. data/lib/prism/translation/parser/builder.rb +62 -0
  49. data/lib/prism/translation/parser/compiler.rb +230 -152
  50. data/lib/prism/translation/parser/lexer.rb +446 -64
  51. data/lib/prism/translation/parser.rb +64 -4
  52. data/lib/prism/translation/parser33.rb +1 -0
  53. data/lib/prism/translation/parser34.rb +1 -0
  54. data/lib/prism/translation/parser35.rb +13 -0
  55. data/lib/prism/translation/parser_current.rb +24 -0
  56. data/lib/prism/translation/ripper/sexp.rb +1 -0
  57. data/lib/prism/translation/ripper.rb +30 -4
  58. data/lib/prism/translation/ruby_parser.rb +291 -7
  59. data/lib/prism/translation.rb +3 -0
  60. data/lib/prism/visitor.rb +457 -152
  61. data/lib/prism.rb +5 -3
  62. data/prism.gemspec +9 -1
  63. data/rbi/prism/dsl.rbi +9 -6
  64. data/rbi/prism/node.rbi +43 -16
  65. data/rbi/prism/parse_result.rbi +17 -0
  66. data/rbi/prism/translation/parser35.rbi +6 -0
  67. data/rbi/prism.rbi +39 -36
  68. data/sig/prism/dispatcher.rbs +3 -0
  69. data/sig/prism/dsl.rbs +7 -5
  70. data/sig/prism/node.rbs +461 -37
  71. data/sig/prism/node_ext.rbs +84 -17
  72. data/sig/prism/parse_result/comments.rbs +38 -0
  73. data/sig/prism/parse_result.rbs +14 -0
  74. data/sig/prism/reflection.rbs +1 -1
  75. data/sig/prism/serialize.rbs +4 -2
  76. data/sig/prism.rbs +22 -1
  77. data/src/diagnostic.c +9 -3
  78. data/src/node.c +23 -0
  79. data/src/options.c +33 -2
  80. data/src/prettyprint.c +32 -0
  81. data/src/prism.c +620 -242
  82. data/src/serialize.c +8 -0
  83. data/src/token_type.c +36 -34
  84. data/src/util/pm_buffer.c +40 -0
  85. data/src/util/pm_constant_pool.c +6 -2
  86. data/src/util/pm_strncasecmp.c +13 -1
  87. metadata +11 -7
@@ -39,8 +39,26 @@ typedef struct pm_options_scope {
39
39
 
40
40
  /** The names of the locals in the scope. */
41
41
  pm_string_t *locals;
42
+
43
+ /** Flags for the set of forwarding parameters in this scope. */
44
+ uint8_t forwarding;
42
45
  } pm_options_scope_t;
43
46
 
47
+ /** The default value for parameters. */
48
+ static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_NONE = 0x0;
49
+
50
+ /** When the scope is fowarding with the * parameter. */
51
+ static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_POSITIONALS = 0x1;
52
+
53
+ /** When the scope is fowarding with the ** parameter. */
54
+ static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_KEYWORDS = 0x2;
55
+
56
+ /** When the scope is fowarding with the & parameter. */
57
+ static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_BLOCK = 0x4;
58
+
59
+ /** When the scope is fowarding with the ... parameter. */
60
+ static const uint8_t PM_OPTIONS_SCOPE_FORWARDING_ALL = 0x8;
61
+
44
62
  // Forward declaration needed by the callback typedef.
45
63
  struct pm_options;
46
64
 
@@ -64,11 +82,20 @@ typedef void (*pm_options_shebang_callback_t)(struct pm_options *options, const
64
82
  * parse in the same way as a specific version of CRuby would have.
65
83
  */
66
84
  typedef enum {
67
- /** The current version of prism. */
68
- PM_OPTIONS_VERSION_LATEST = 0,
85
+ /** If an explicit version is not provided, the current version of prism will be used. */
86
+ PM_OPTIONS_VERSION_UNSET = 0,
69
87
 
70
88
  /** The vendored version of prism in CRuby 3.3.x. */
71
- PM_OPTIONS_VERSION_CRUBY_3_3 = 1
89
+ PM_OPTIONS_VERSION_CRUBY_3_3 = 1,
90
+
91
+ /** The vendored version of prism in CRuby 3.4.x. */
92
+ PM_OPTIONS_VERSION_CRUBY_3_4 = 2,
93
+
94
+ /** The vendored version of prism in CRuby 3.5.x. */
95
+ PM_OPTIONS_VERSION_CRUBY_3_5 = 3,
96
+
97
+ /** The current version of prism. */
98
+ PM_OPTIONS_VERSION_LATEST = PM_OPTIONS_VERSION_CRUBY_3_5
72
99
  } pm_options_version_t;
73
100
 
74
101
  /**
@@ -157,6 +184,13 @@ typedef struct pm_options {
157
184
  * inside another script.
158
185
  */
159
186
  bool partial_script;
187
+
188
+ /**
189
+ * Whether or not the parser should freeze the nodes that it creates. This
190
+ * makes it possible to have a deeply frozen AST that is safe to share
191
+ * between concurrency primitives.
192
+ */
193
+ bool freeze;
160
194
  } pm_options_t;
161
195
 
162
196
  /**
@@ -203,6 +237,8 @@ static const uint8_t PM_OPTIONS_COMMAND_LINE_X = 0x20;
203
237
  * @param shebang_callback The shebang callback to set.
204
238
  * @param shebang_callback_data Any additional data that should be passed along
205
239
  * to the callback.
240
+ *
241
+ * \public \memberof pm_options
206
242
  */
207
243
  PRISM_EXPORTED_FUNCTION void pm_options_shebang_callback_set(pm_options_t *options, pm_options_shebang_callback_t shebang_callback, void *shebang_callback_data);
208
244
 
@@ -211,6 +247,8 @@ PRISM_EXPORTED_FUNCTION void pm_options_shebang_callback_set(pm_options_t *optio
211
247
  *
212
248
  * @param options The options struct to set the filepath on.
213
249
  * @param filepath The filepath to set.
250
+ *
251
+ * \public \memberof pm_options
214
252
  */
215
253
  PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, const char *filepath);
216
254
 
@@ -219,6 +257,8 @@ PRISM_EXPORTED_FUNCTION void pm_options_filepath_set(pm_options_t *options, cons
219
257
  *
220
258
  * @param options The options struct to set the line on.
221
259
  * @param line The line to set.
260
+ *
261
+ * \public \memberof pm_options
222
262
  */
223
263
  PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, int32_t line);
224
264
 
@@ -227,6 +267,8 @@ PRISM_EXPORTED_FUNCTION void pm_options_line_set(pm_options_t *options, int32_t
227
267
  *
228
268
  * @param options The options struct to set the encoding on.
229
269
  * @param encoding The encoding to set.
270
+ *
271
+ * \public \memberof pm_options
230
272
  */
231
273
  PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, const char *encoding);
232
274
 
@@ -235,6 +277,8 @@ PRISM_EXPORTED_FUNCTION void pm_options_encoding_set(pm_options_t *options, cons
235
277
  *
236
278
  * @param options The options struct to set the encoding_locked value on.
237
279
  * @param encoding_locked The encoding_locked value to set.
280
+ *
281
+ * \public \memberof pm_options
238
282
  */
239
283
  PRISM_EXPORTED_FUNCTION void pm_options_encoding_locked_set(pm_options_t *options, bool encoding_locked);
240
284
 
@@ -243,6 +287,8 @@ PRISM_EXPORTED_FUNCTION void pm_options_encoding_locked_set(pm_options_t *option
243
287
  *
244
288
  * @param options The options struct to set the frozen string literal value on.
245
289
  * @param frozen_string_literal The frozen string literal value to set.
290
+ *
291
+ * \public \memberof pm_options
246
292
  */
247
293
  PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *options, bool frozen_string_literal);
248
294
 
@@ -251,6 +297,8 @@ PRISM_EXPORTED_FUNCTION void pm_options_frozen_string_literal_set(pm_options_t *
251
297
  *
252
298
  * @param options The options struct to set the command line option on.
253
299
  * @param command_line The command_line value to set.
300
+ *
301
+ * \public \memberof pm_options
254
302
  */
255
303
  PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options, uint8_t command_line);
256
304
 
@@ -263,6 +311,8 @@ PRISM_EXPORTED_FUNCTION void pm_options_command_line_set(pm_options_t *options,
263
311
  * @param version The version to set.
264
312
  * @param length The length of the version string.
265
313
  * @return Whether or not the version was parsed successfully.
314
+ *
315
+ * \public \memberof pm_options
266
316
  */
267
317
  PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const char *version, size_t length);
268
318
 
@@ -271,6 +321,8 @@ PRISM_EXPORTED_FUNCTION bool pm_options_version_set(pm_options_t *options, const
271
321
  *
272
322
  * @param options The options struct to set the main script value on.
273
323
  * @param main_script The main script value to set.
324
+ *
325
+ * \public \memberof pm_options
274
326
  */
275
327
  PRISM_EXPORTED_FUNCTION void pm_options_main_script_set(pm_options_t *options, bool main_script);
276
328
 
@@ -279,15 +331,29 @@ PRISM_EXPORTED_FUNCTION void pm_options_main_script_set(pm_options_t *options, b
279
331
  *
280
332
  * @param options The options struct to set the partial script value on.
281
333
  * @param partial_script The partial script value to set.
334
+ *
335
+ * \public \memberof pm_options
282
336
  */
283
337
  PRISM_EXPORTED_FUNCTION void pm_options_partial_script_set(pm_options_t *options, bool partial_script);
284
338
 
339
+ /**
340
+ * Set the freeze option on the given options struct.
341
+ *
342
+ * @param options The options struct to set the freeze value on.
343
+ * @param freeze The freeze value to set.
344
+ *
345
+ * \public \memberof pm_options
346
+ */
347
+ PRISM_EXPORTED_FUNCTION void pm_options_freeze_set(pm_options_t *options, bool freeze);
348
+
285
349
  /**
286
350
  * Allocate and zero out the scopes array on the given options struct.
287
351
  *
288
352
  * @param options The options struct to initialize the scopes array on.
289
353
  * @param scopes_count The number of scopes to allocate.
290
354
  * @return Whether or not the scopes array was initialized successfully.
355
+ *
356
+ * \public \memberof pm_options
291
357
  */
292
358
  PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_t scopes_count);
293
359
 
@@ -297,6 +363,8 @@ PRISM_EXPORTED_FUNCTION bool pm_options_scopes_init(pm_options_t *options, size_
297
363
  * @param options The options struct to get the scope from.
298
364
  * @param index The index of the scope to get.
299
365
  * @return A pointer to the scope at the given index.
366
+ *
367
+ * \public \memberof pm_options
300
368
  */
301
369
  PRISM_EXPORTED_FUNCTION const pm_options_scope_t * pm_options_scope_get(const pm_options_t *options, size_t index);
302
370
 
@@ -307,6 +375,8 @@ PRISM_EXPORTED_FUNCTION const pm_options_scope_t * pm_options_scope_get(const pm
307
375
  * @param scope The scope struct to initialize.
308
376
  * @param locals_count The number of locals to allocate.
309
377
  * @return Whether or not the scope was initialized successfully.
378
+ *
379
+ * \public \memberof pm_options
310
380
  */
311
381
  PRISM_EXPORTED_FUNCTION bool pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count);
312
382
 
@@ -316,13 +386,27 @@ PRISM_EXPORTED_FUNCTION bool pm_options_scope_init(pm_options_scope_t *scope, si
316
386
  * @param scope The scope struct to get the local from.
317
387
  * @param index The index of the local to get.
318
388
  * @return A pointer to the local at the given index.
389
+ *
390
+ * \public \memberof pm_options
319
391
  */
320
392
  PRISM_EXPORTED_FUNCTION const pm_string_t * pm_options_scope_local_get(const pm_options_scope_t *scope, size_t index);
321
393
 
394
+ /**
395
+ * Set the forwarding option on the given scope struct.
396
+ *
397
+ * @param scope The scope struct to set the forwarding on.
398
+ * @param forwarding The forwarding value to set.
399
+ *
400
+ * \public \memberof pm_options
401
+ */
402
+ PRISM_EXPORTED_FUNCTION void pm_options_scope_forwarding_set(pm_options_scope_t *scope, uint8_t forwarding);
403
+
322
404
  /**
323
405
  * Free the internal memory associated with the options.
324
406
  *
325
407
  * @param options The options struct whose internal memory should be freed.
408
+ *
409
+ * \public \memberof pm_options
326
410
  */
327
411
  PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
328
412
 
@@ -352,6 +436,7 @@ PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
352
436
  * | `1` | encoding locked |
353
437
  * | `1` | main script |
354
438
  * | `1` | partial script |
439
+ * | `1` | freeze |
355
440
  * | `4` | the number of scopes |
356
441
  * | ... | the scopes |
357
442
  *
@@ -367,6 +452,7 @@ PRISM_EXPORTED_FUNCTION void pm_options_free(pm_options_t *options);
367
452
  * | # bytes | field |
368
453
  * | ------- | -------------------------- |
369
454
  * | `4` | the number of locals |
455
+ * | `1` | the forwarding flags |
370
456
  * | ... | the locals |
371
457
  *
372
458
  * Each local is laid out as follows:
@@ -17,12 +17,12 @@
17
17
  #include <string.h>
18
18
 
19
19
  /**
20
- * This callback is called when a named capture group is found.
20
+ * This callback is called by pm_regexp_parse() when a named capture group is found.
21
21
  */
22
22
  typedef void (*pm_regexp_name_callback_t)(const pm_string_t *name, void *data);
23
23
 
24
24
  /**
25
- * This callback is called when a parse error is found.
25
+ * This callback is called by pm_regexp_parse() when a parse error is found.
26
26
  */
27
27
  typedef void (*pm_regexp_error_callback_t)(const uint8_t *start, const uint8_t *end, const char *message, void *data);
28
28
 
@@ -51,6 +51,8 @@ bool pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity);
51
51
  *
52
52
  * @param buffer The buffer to initialize.
53
53
  * @returns True if the buffer was initialized successfully, false otherwise.
54
+ *
55
+ * \public \memberof pm_buffer_t
54
56
  */
55
57
  PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer);
56
58
 
@@ -59,6 +61,8 @@ PRISM_EXPORTED_FUNCTION bool pm_buffer_init(pm_buffer_t *buffer);
59
61
  *
60
62
  * @param buffer The buffer to get the value of.
61
63
  * @returns The value of the buffer.
64
+ *
65
+ * \public \memberof pm_buffer_t
62
66
  */
63
67
  PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer);
64
68
 
@@ -67,6 +71,8 @@ PRISM_EXPORTED_FUNCTION char * pm_buffer_value(const pm_buffer_t *buffer);
67
71
  *
68
72
  * @param buffer The buffer to get the length of.
69
73
  * @returns The length of the buffer.
74
+ *
75
+ * \public \memberof pm_buffer_t
70
76
  */
71
77
  PRISM_EXPORTED_FUNCTION size_t pm_buffer_length(const pm_buffer_t *buffer);
72
78
 
@@ -137,6 +143,16 @@ void pm_buffer_append_varsint(pm_buffer_t *buffer, int32_t value);
137
143
  */
138
144
  void pm_buffer_append_double(pm_buffer_t *buffer, double value);
139
145
 
146
+ /**
147
+ * Append a unicode codepoint to the buffer.
148
+ *
149
+ * @param buffer The buffer to append to.
150
+ * @param value The character to append.
151
+ * @returns True if the codepoint was valid and appended successfully, false
152
+ * otherwise.
153
+ */
154
+ bool pm_buffer_append_unicode_codepoint(pm_buffer_t *buffer, uint32_t value);
155
+
140
156
  /**
141
157
  * The different types of escaping that can be performed by the buffer when
142
158
  * appending a slice of Ruby source code.
@@ -212,6 +228,8 @@ void pm_buffer_insert(pm_buffer_t *buffer, size_t index, const char *value, size
212
228
  * Free the memory associated with the buffer.
213
229
  *
214
230
  * @param buffer The buffer to free.
231
+ *
232
+ * \public \memberof pm_buffer_t
215
233
  */
216
234
  PRISM_EXPORTED_FUNCTION void pm_buffer_free(pm_buffer_t *buffer);
217
235
 
@@ -112,6 +112,8 @@ void pm_integers_reduce(pm_integer_t *numerator, pm_integer_t *denominator);
112
112
  *
113
113
  * @param buffer The buffer to append the string to.
114
114
  * @param integer The integer to convert to a string.
115
+ *
116
+ * \public \memberof pm_integer_t
115
117
  */
116
118
  PRISM_EXPORTED_FUNCTION void pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer);
117
119
 
@@ -120,6 +122,8 @@ PRISM_EXPORTED_FUNCTION void pm_integer_string(pm_buffer_t *buffer, const pm_int
120
122
  * the integer exceeds the size of a single node in the linked list.
121
123
  *
122
124
  * @param integer The integer to free.
125
+ *
126
+ * \public \memberof pm_integer_t
123
127
  */
124
128
  PRISM_EXPORTED_FUNCTION void pm_integer_free(pm_integer_t *integer);
125
129
 
@@ -68,6 +68,8 @@ typedef struct {
68
68
  *
69
69
  * @param list The list to check.
70
70
  * @return True if the given list is empty, otherwise false.
71
+ *
72
+ * \public \memberof pm_list_t
71
73
  */
72
74
  PRISM_EXPORTED_FUNCTION bool pm_list_empty_p(pm_list_t *list);
73
75
 
@@ -76,6 +78,8 @@ PRISM_EXPORTED_FUNCTION bool pm_list_empty_p(pm_list_t *list);
76
78
  *
77
79
  * @param list The list to check.
78
80
  * @return The size of the list.
81
+ *
82
+ * \public \memberof pm_list_t
79
83
  */
80
84
  PRISM_EXPORTED_FUNCTION size_t pm_list_size(pm_list_t *list);
81
85
 
@@ -91,6 +95,8 @@ void pm_list_append(pm_list_t *list, pm_list_node_t *node);
91
95
  * Deallocate the internal state of the given list.
92
96
  *
93
97
  * @param list The list to free.
98
+ *
99
+ * \public \memberof pm_list_t
94
100
  */
95
101
  PRISM_EXPORTED_FUNCTION void pm_list_free(pm_list_t *list);
96
102
 
@@ -45,11 +45,11 @@ typedef struct {
45
45
  /** This is a slice of another string, and should not be freed. */
46
46
  PM_STRING_SHARED,
47
47
 
48
- /** This string owns its memory, and should be freed using `pm_string_free`. */
48
+ /** This string owns its memory, and should be freed using `pm_string_free()`. */
49
49
  PM_STRING_OWNED,
50
50
 
51
51
  #ifdef PRISM_HAS_MMAP
52
- /** This string is a memory-mapped file, and should be freed using `pm_string_free`. */
52
+ /** This string is a memory-mapped file, and should be freed using `pm_string_free()`. */
53
53
  PM_STRING_MAPPED
54
54
  #endif
55
55
  } type;
@@ -130,6 +130,8 @@ typedef enum {
130
130
  * @param string The string to initialize.
131
131
  * @param filepath The filepath to read.
132
132
  * @return The success of the read, indicated by the value of the enum.
133
+ *
134
+ * \public \memberof pm_string_t
133
135
  */
134
136
  PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init(pm_string_t *string, const char *filepath);
135
137
 
@@ -141,6 +143,8 @@ PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_mapped_init(pm_string_
141
143
  * @param string The string to initialize.
142
144
  * @param filepath The filepath to read.
143
145
  * @return The success of the read, indicated by the value of the enum.
146
+ *
147
+ * \public \memberof pm_string_t
144
148
  */
145
149
  PRISM_EXPORTED_FUNCTION pm_string_init_result_t pm_string_file_init(pm_string_t *string, const char *filepath);
146
150
 
@@ -169,6 +173,8 @@ int pm_string_compare(const pm_string_t *left, const pm_string_t *right);
169
173
  *
170
174
  * @param string The string to get the length of.
171
175
  * @return The length of the string.
176
+ *
177
+ * \public \memberof pm_string_t
172
178
  */
173
179
  PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string);
174
180
 
@@ -177,6 +183,8 @@ PRISM_EXPORTED_FUNCTION size_t pm_string_length(const pm_string_t *string);
177
183
  *
178
184
  * @param string The string to get the start pointer of.
179
185
  * @return The start pointer of the string.
186
+ *
187
+ * \public \memberof pm_string_t
180
188
  */
181
189
  PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *string);
182
190
 
@@ -184,6 +192,8 @@ PRISM_EXPORTED_FUNCTION const uint8_t * pm_string_source(const pm_string_t *stri
184
192
  * Free the associated memory of the given string.
185
193
  *
186
194
  * @param string The string to free.
195
+ *
196
+ * \public \memberof pm_string_t
187
197
  */
188
198
  PRISM_EXPORTED_FUNCTION void pm_string_free(pm_string_t *string);
189
199
 
@@ -14,7 +14,7 @@
14
14
  /**
15
15
  * The minor version of the Prism library as an int.
16
16
  */
17
- #define PRISM_VERSION_MINOR 3
17
+ #define PRISM_VERSION_MINOR 5
18
18
 
19
19
  /**
20
20
  * The patch version of the Prism library as an int.
@@ -24,6 +24,6 @@
24
24
  /**
25
25
  * The version of the Prism library as a constant string.
26
26
  */
27
- #define PRISM_VERSION "1.3.0"
27
+ #define PRISM_VERSION "1.5.0"
28
28
 
29
29
  #endif
data/include/prism.h CHANGED
@@ -49,10 +49,15 @@ PRISM_EXPORTED_FUNCTION const char * pm_version(void);
49
49
  /**
50
50
  * Initialize a parser with the given start and end pointers.
51
51
  *
52
+ * The resulting parser must eventually be freed with `pm_parser_free()`.
53
+ *
52
54
  * @param parser The parser to initialize.
53
55
  * @param source The source to parse.
54
56
  * @param size The size of the source.
55
- * @param options The optional options to use when parsing.
57
+ * @param options The optional options to use when parsing. These options must
58
+ * live for the whole lifetime of this parser.
59
+ *
60
+ * \public \memberof pm_parser
56
61
  */
57
62
  PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm_options_t *options);
58
63
 
@@ -62,13 +67,20 @@ PRISM_EXPORTED_FUNCTION void pm_parser_init(pm_parser_t *parser, const uint8_t *
62
67
  *
63
68
  * @param parser The parser to register the callback with.
64
69
  * @param callback The callback to register.
70
+ *
71
+ * \public \memberof pm_parser
65
72
  */
66
73
  PRISM_EXPORTED_FUNCTION void pm_parser_register_encoding_changed_callback(pm_parser_t *parser, pm_encoding_changed_callback_t callback);
67
74
 
68
75
  /**
69
76
  * Free any memory associated with the given parser.
70
77
  *
78
+ * This does not free the `pm_options_t` object that was used to initialize the
79
+ * parser.
80
+ *
71
81
  * @param parser The parser to free.
82
+ *
83
+ * \public \memberof pm_parser
72
84
  */
73
85
  PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser);
74
86
 
@@ -77,27 +89,39 @@ PRISM_EXPORTED_FUNCTION void pm_parser_free(pm_parser_t *parser);
77
89
  *
78
90
  * @param parser The parser to use.
79
91
  * @return The AST representing the source.
92
+ *
93
+ * \public \memberof pm_parser
80
94
  */
81
95
  PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse(pm_parser_t *parser);
82
96
 
83
97
  /**
84
- * This function is used in pm_parse_stream to retrieve a line of input from a
98
+ * This function is used in pm_parse_stream() to retrieve a line of input from a
85
99
  * stream. It closely mirrors that of fgets so that fgets can be used as the
86
100
  * default implementation.
87
101
  */
88
102
  typedef char * (pm_parse_stream_fgets_t)(char *string, int size, void *stream);
89
103
 
104
+ /**
105
+ * This function is used in pm_parse_stream to check whether a stream is EOF.
106
+ * It closely mirrors that of feof so that feof can be used as the
107
+ * default implementation.
108
+ */
109
+ typedef int (pm_parse_stream_feof_t)(void *stream);
110
+
90
111
  /**
91
112
  * Parse a stream of Ruby source and return the tree.
92
113
  *
93
114
  * @param parser The parser to use.
94
115
  * @param buffer The buffer to use.
95
116
  * @param stream The stream to parse.
96
- * @param fgets The function to use to read from the stream.
117
+ * @param stream_fgets The function to use to read from the stream.
118
+ * @param stream_feof The function to use to determine if the stream has hit eof.
97
119
  * @param options The optional options to use when parsing.
98
120
  * @return The AST representing the source.
121
+ *
122
+ * \public \memberof pm_parser
99
123
  */
100
- PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *fgets, const pm_options_t *options);
124
+ PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *stream_fgets, pm_parse_stream_feof_t *stream_feof, const pm_options_t *options);
101
125
 
102
126
  // We optionally support serializing to a binary string. For systems that don't
103
127
  // want or need this functionality, it can be turned off with the
@@ -110,10 +134,11 @@ PRISM_EXPORTED_FUNCTION pm_node_t * pm_parse_stream(pm_parser_t *parser, pm_buff
110
134
  *
111
135
  * @param buffer The buffer to serialize to.
112
136
  * @param stream The stream to parse.
113
- * @param fgets The function to use to read from the stream.
137
+ * @param stream_fgets The function to use to read from the stream.
138
+ * @param stream_feof The function to use to tell if the stream has hit eof.
114
139
  * @param data The optional data to pass to the parser.
115
140
  */
116
- PRISM_EXPORTED_FUNCTION void pm_serialize_parse_stream(pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *fgets, const char *data);
141
+ PRISM_EXPORTED_FUNCTION void pm_serialize_parse_stream(pm_buffer_t *buffer, void *stream, pm_parse_stream_fgets_t *stream_fgets, pm_parse_stream_feof_t *stream_feof, const char *data);
117
142
 
118
143
  /**
119
144
  * Serialize the given list of comments to the given buffer.
@@ -307,10 +332,10 @@ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint
307
332
  * to want to use and be aware of are:
308
333
  *
309
334
  * * `pm_parser_t` - the main parser structure
310
- * * `pm_parser_init` - initialize a parser
311
- * * `pm_parse` - parse and return the root node
312
- * * `pm_node_destroy` - deallocate the root node returned by `pm_parse`
313
- * * `pm_parser_free` - free the internal memory of the parser
335
+ * * `pm_parser_init()` - initialize a parser
336
+ * * `pm_parse()` - parse and return the root node
337
+ * * `pm_node_destroy()` - deallocate the root node returned by `pm_parse()`
338
+ * * `pm_parser_free()` - free the internal memory of the parser
314
339
  *
315
340
  * Putting all of this together would look something like:
316
341
  *
@@ -327,8 +352,8 @@ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint
327
352
  * }
328
353
  * ```
329
354
  *
330
- * All of the nodes "inherit" from `pm_node_t` by embedding those structures as
331
- * their first member. This means you can downcast and upcast any node in the
355
+ * All of the nodes "inherit" from `pm_node_t` by embedding those structures
356
+ * as their first member. This means you can downcast and upcast any node in the
332
357
  * tree to a `pm_node_t`.
333
358
  *
334
359
  * @section serializing Serializing
@@ -340,9 +365,9 @@ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint
340
365
  * use and be aware of are:
341
366
  *
342
367
  * * `pm_buffer_t` - a small buffer object that will hold the serialized AST
343
- * * `pm_buffer_free` - free the memory associated with the buffer
344
- * * `pm_serialize` - serialize the AST into a buffer
345
- * * `pm_serialize_parse` - parse and serialize the AST into a buffer
368
+ * * `pm_buffer_free()` - free the memory associated with the buffer
369
+ * * `pm_serialize()` - serialize the AST into a buffer
370
+ * * `pm_serialize_parse()` - parse and serialize the AST into a buffer
346
371
  *
347
372
  * Putting all of this together would look something like:
348
373
  *
@@ -360,7 +385,7 @@ PRISM_EXPORTED_FUNCTION pm_string_query_t pm_string_query_method_name(const uint
360
385
  * @section inspecting Inspecting
361
386
  *
362
387
  * Prism provides the ability to inspect the AST by pretty-printing nodes. You
363
- * can do this with the `pm_prettyprint` function, which you would use like:
388
+ * can do this with the `pm_prettyprint()` function, which you would use like:
364
389
  *
365
390
  * ```c
366
391
  * void prettyprint(const uint8_t *source, size_t length) {