prism 1.9.0 → 2.0.0.rc2

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 (244) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +88 -0
  3. data/CODE_OF_CONDUCT.md +1 -1
  4. data/LICENSE.md +1 -1
  5. data/Makefile +41 -12
  6. data/README.md +35 -97
  7. data/config.yml +174 -161
  8. data/docs/build_system.md +2 -2
  9. data/docs/configuration.md +5 -4
  10. data/docs/encoding.md +2 -2
  11. data/docs/fuzzing.md +4 -11
  12. data/docs/javascript.md +19 -4
  13. data/docs/mapping.md +1 -1
  14. data/docs/releasing.md +77 -5
  15. data/docs/relocation.md +2 -4
  16. data/docs/serialization.md +22 -24
  17. data/docs/testing.md +46 -29
  18. data/ext/prism/api_node.c +1461 -1401
  19. data/ext/prism/extconf.rb +3 -4
  20. data/ext/prism/extension.c +796 -422
  21. data/ext/prism/extension.h +3 -3
  22. data/include/prism/arena.h +37 -0
  23. data/include/prism/ast.h +2727 -610
  24. data/include/prism/buffer.h +52 -0
  25. data/include/prism/comments.h +55 -0
  26. data/include/prism/compiler/accel.h +26 -0
  27. data/include/prism/compiler/align.h +36 -0
  28. data/include/prism/compiler/assume.h +25 -0
  29. data/include/prism/compiler/bswap.h +46 -0
  30. data/include/prism/compiler/endianness.h +34 -0
  31. data/include/prism/compiler/exported.h +24 -0
  32. data/include/prism/compiler/fallthrough.h +22 -0
  33. data/include/prism/compiler/filesystem.h +32 -0
  34. data/include/prism/compiler/flex_array.h +19 -0
  35. data/include/prism/compiler/force_inline.h +21 -0
  36. data/include/prism/compiler/format.h +25 -0
  37. data/include/prism/compiler/inline.h +17 -0
  38. data/include/prism/compiler/nodiscard.h +22 -0
  39. data/include/prism/compiler/nonnull.h +18 -0
  40. data/include/prism/compiler/unused.h +18 -0
  41. data/include/prism/constant_pool.h +81 -0
  42. data/include/prism/diagnostic.h +38 -403
  43. data/include/prism/errors_format.h +53 -0
  44. data/include/prism/excludes.h +29 -0
  45. data/include/prism/integer.h +41 -0
  46. data/include/prism/internal/allocator.h +68 -0
  47. data/include/prism/internal/allocator_debug.h +88 -0
  48. data/include/prism/internal/arena.h +108 -0
  49. data/include/prism/internal/bit.h +42 -0
  50. data/include/prism/internal/buffer.h +94 -0
  51. data/include/prism/internal/char.h +145 -0
  52. data/include/prism/internal/comments.h +53 -0
  53. data/include/prism/internal/constant_pool.h +152 -0
  54. data/include/prism/internal/diagnostic.h +389 -0
  55. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  56. data/include/prism/internal/integer.h +68 -0
  57. data/include/prism/internal/isinf.h +16 -0
  58. data/include/prism/internal/line_offset_list.h +34 -0
  59. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  60. data/include/prism/internal/magic_comments.h +23 -0
  61. data/include/prism/internal/memchr.h +15 -0
  62. data/include/prism/internal/node.h +38 -0
  63. data/include/prism/internal/options.h +232 -0
  64. data/include/prism/internal/parser.h +1004 -0
  65. data/include/prism/internal/regexp.h +26 -0
  66. data/include/prism/internal/serialize.h +39 -0
  67. data/include/prism/internal/source.h +72 -0
  68. data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
  69. data/include/prism/internal/stringy.h +35 -0
  70. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  71. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  72. data/include/prism/internal/tokens.h +11 -0
  73. data/include/prism/internal/xxh3.h +16 -0
  74. data/include/prism/json.h +32 -0
  75. data/include/prism/line_offset_list.h +61 -0
  76. data/include/prism/magic_comments.h +35 -0
  77. data/include/prism/node.h +18 -53
  78. data/include/prism/options.h +146 -280
  79. data/include/prism/parser.h +356 -867
  80. data/include/prism/prettyprint.h +6 -9
  81. data/include/prism/serialize.h +115 -0
  82. data/include/prism/source.h +155 -0
  83. data/include/prism/stream.h +28 -0
  84. data/include/prism/string_query.h +74 -0
  85. data/include/prism/stringy.h +72 -0
  86. data/include/prism/version.h +12 -3
  87. data/include/prism.h +43 -320
  88. data/lib/prism/compiler.rb +319 -305
  89. data/lib/prism/desugar_compiler.rb +105 -34
  90. data/lib/prism/dispatcher.rb +646 -473
  91. data/lib/prism/dot_visitor.rb +592 -511
  92. data/lib/prism/dsl.rb +391 -25
  93. data/lib/prism/ffi.rb +258 -90
  94. data/lib/prism/inspect_visitor.rb +357 -325
  95. data/lib/prism/lex_compat.rb +254 -212
  96. data/lib/prism/mutation_compiler.rb +313 -305
  97. data/lib/prism/node.rb +20966 -6992
  98. data/lib/prism/node_ext.rb +180 -187
  99. data/lib/prism/node_find.rb +215 -0
  100. data/lib/prism/parse_result/errors.rb +7 -1
  101. data/lib/prism/parse_result/newlines.rb +276 -8
  102. data/lib/prism/parse_result.rb +468 -139
  103. data/lib/prism/pattern.rb +65 -20
  104. data/lib/prism/reflection.rb +23 -12
  105. data/lib/prism/relocation.rb +149 -87
  106. data/lib/prism/serialize.rb +3460 -539
  107. data/lib/prism/string_query.rb +24 -1
  108. data/lib/prism/translation/parser/builder.rb +10 -2
  109. data/lib/prism/translation/parser/compiler.rb +44 -52
  110. data/lib/prism/translation/parser/lexer.rb +41 -92
  111. data/lib/prism/translation/parser.rb +1 -1
  112. data/lib/prism/translation/parser_current.rb +1 -1
  113. data/lib/prism/translation/ripper/lexer.rb +8 -10
  114. data/lib/prism/translation/ripper/sexp.rb +2 -10
  115. data/lib/prism/translation/ripper/shim.rb +2 -0
  116. data/lib/prism/translation/ripper.rb +967 -209
  117. data/lib/prism/translation/ruby_parser.rb +73 -316
  118. data/lib/prism/translation.rb +2 -0
  119. data/lib/prism/visitor.rb +632 -5
  120. data/lib/prism.rb +79 -9
  121. data/prism.gemspec +128 -62
  122. data/rbi/generated/prism/compiler.rbi +489 -0
  123. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  124. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  125. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  126. data/rbi/generated/prism/dsl.rbi +751 -0
  127. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  128. data/rbi/generated/prism/lex_compat.rbi +162 -0
  129. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  130. data/rbi/generated/prism/node.rbi +14407 -0
  131. data/rbi/generated/prism/node_ext.rbi +259 -0
  132. data/rbi/generated/prism/node_find.rbi +79 -0
  133. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  134. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  135. data/rbi/generated/prism/parse_result.rbi +776 -0
  136. data/rbi/generated/prism/pattern.rbi +134 -0
  137. data/rbi/generated/prism/reflection.rbi +89 -0
  138. data/rbi/generated/prism/relocation.rbi +329 -0
  139. data/rbi/generated/prism/serialize.rbi +184 -0
  140. data/rbi/generated/prism/string_query.rbi +43 -0
  141. data/rbi/generated/prism/translation.rbi +7 -0
  142. data/rbi/generated/prism/visitor.rbi +650 -0
  143. data/rbi/generated/prism.rbi +86 -0
  144. data/rbi/prism/translation/parser_versions.rbi +5 -0
  145. data/rbi/rubyvm/node_find.rbi +14 -0
  146. data/sig/generated/prism/compiler.rbs +492 -0
  147. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  148. data/sig/generated/prism/dispatcher.rbs +1292 -0
  149. data/sig/generated/prism/dot_visitor.rbs +635 -0
  150. data/sig/generated/prism/dsl.rbs +928 -0
  151. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  152. data/sig/generated/prism/lex_compat.rbs +166 -0
  153. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  154. data/sig/generated/prism/node.rbs +20896 -0
  155. data/sig/generated/prism/node_ext.rbs +290 -0
  156. data/sig/generated/prism/node_find.rbs +90 -0
  157. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  158. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  159. data/sig/generated/prism/parse_result.rbs +871 -0
  160. data/sig/generated/prism/pattern.rbs +156 -0
  161. data/sig/generated/prism/reflection.rbs +101 -0
  162. data/sig/generated/prism/relocation.rbs +428 -0
  163. data/sig/generated/prism/serialize.rbs +198 -0
  164. data/sig/generated/prism/string_query.rbs +43 -0
  165. data/sig/generated/prism/translation.rbs +8 -0
  166. data/sig/generated/prism/visitor.rbs +1111 -0
  167. data/sig/generated/prism.rbs +84 -0
  168. data/sig/manifest.yaml +2 -0
  169. data/src/arena.c +117 -0
  170. data/src/{util/pm_buffer.c → buffer.c} +53 -21
  171. data/src/{util/pm_char.c → char.c} +28 -57
  172. data/src/comments.c +454 -0
  173. data/src/constant_pool.c +406 -0
  174. data/src/diagnostic.c +97 -65
  175. data/src/encoding.c +8 -3
  176. data/src/errors_format.c +519 -0
  177. data/src/{util/pm_integer.c → integer.c} +33 -22
  178. data/src/json.c +5733 -0
  179. data/src/line_offset_list.c +100 -0
  180. data/src/list.c +24 -0
  181. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  182. data/src/node.c +3504 -7674
  183. data/src/options.c +162 -49
  184. data/src/parser.c +367 -0
  185. data/src/prettyprint.c +421 -354
  186. data/src/prism.c +8402 -7161
  187. data/src/regexp.c +961 -45
  188. data/src/serialize.c +460 -424
  189. data/src/source.c +518 -0
  190. data/src/static_literals.c +111 -43
  191. data/src/string_query.c +180 -0
  192. data/src/stringy.c +91 -0
  193. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  194. data/src/strpbrk.c +447 -0
  195. data/src/{token_type.c → tokens.c} +39 -18
  196. data/src/xxh3.c +292 -0
  197. metadata +128 -63
  198. data/ext/prism/api_pack.c +0 -276
  199. data/include/prism/defines.h +0 -260
  200. data/include/prism/pack.h +0 -163
  201. data/include/prism/regexp.h +0 -43
  202. data/include/prism/util/pm_buffer.h +0 -236
  203. data/include/prism/util/pm_char.h +0 -204
  204. data/include/prism/util/pm_constant_pool.h +0 -218
  205. data/include/prism/util/pm_integer.h +0 -130
  206. data/include/prism/util/pm_memchr.h +0 -29
  207. data/include/prism/util/pm_newline_list.h +0 -113
  208. data/include/prism/util/pm_string.h +0 -200
  209. data/lib/prism/pack.rb +0 -230
  210. data/lib/prism/parse_result/comments.rb +0 -188
  211. data/rbi/prism/compiler.rbi +0 -12
  212. data/rbi/prism/dsl.rbi +0 -524
  213. data/rbi/prism/inspect_visitor.rbi +0 -12
  214. data/rbi/prism/node.rbi +0 -8750
  215. data/rbi/prism/node_ext.rbi +0 -107
  216. data/rbi/prism/parse_result.rbi +0 -404
  217. data/rbi/prism/reflection.rbi +0 -58
  218. data/rbi/prism/string_query.rbi +0 -12
  219. data/rbi/prism/visitor.rbi +0 -473
  220. data/rbi/prism.rbi +0 -63
  221. data/sig/prism/compiler.rbs +0 -9
  222. data/sig/prism/dispatcher.rbs +0 -19
  223. data/sig/prism/dot_visitor.rbs +0 -6
  224. data/sig/prism/dsl.rbs +0 -351
  225. data/sig/prism/inspect_visitor.rbs +0 -22
  226. data/sig/prism/lex_compat.rbs +0 -10
  227. data/sig/prism/node.rbs +0 -4042
  228. data/sig/prism/node_ext.rbs +0 -149
  229. data/sig/prism/pack.rbs +0 -43
  230. data/sig/prism/parse_result/comments.rbs +0 -38
  231. data/sig/prism/parse_result.rbs +0 -197
  232. data/sig/prism/pattern.rbs +0 -13
  233. data/sig/prism/reflection.rbs +0 -50
  234. data/sig/prism/relocation.rbs +0 -185
  235. data/sig/prism/serialize.rbs +0 -8
  236. data/sig/prism/string_query.rbs +0 -11
  237. data/sig/prism/visitor.rbs +0 -169
  238. data/sig/prism.rbs +0 -272
  239. data/src/pack.c +0 -509
  240. data/src/util/pm_constant_pool.c +0 -342
  241. data/src/util/pm_list.c +0 -49
  242. data/src/util/pm_newline_list.c +0 -125
  243. data/src/util/pm_string.c +0 -381
  244. data/src/util/pm_strpbrk.c +0 -206
data/src/parser.c ADDED
@@ -0,0 +1,367 @@
1
+ #include "prism/internal/parser.h"
2
+
3
+ #include "prism/internal/allocator.h"
4
+ #include "prism/internal/comments.h"
5
+ #include "prism/internal/diagnostic.h"
6
+ #include "prism/internal/encoding.h"
7
+ #include "prism/internal/magic_comments.h"
8
+ #include "prism/internal/xxh3.h"
9
+
10
+ #include <stdlib.h>
11
+ #include <string.h>
12
+
13
+ /**
14
+ * Register a callback that will be called whenever prism changes the encoding
15
+ * it is using to parse based on the magic comment.
16
+ */
17
+ void
18
+ pm_parser_encoding_changed_callback_set(pm_parser_t *parser, pm_encoding_changed_callback_t callback) {
19
+ parser->encoding_changed_callback = callback;
20
+ }
21
+
22
+ /**
23
+ * Register a callback that will be called whenever a token is lexed.
24
+ */
25
+ void
26
+ pm_parser_lex_callback_set(pm_parser_t *parser, pm_lex_callback_t callback, void *data) {
27
+ parser->lex_callback.callback = callback;
28
+ parser->lex_callback.data = data;
29
+ }
30
+
31
+ /**
32
+ * Returns the opaque data that is passed to the lex callback when it is called.
33
+ */
34
+ void *
35
+ pm_parser_lex_callback_data(const pm_parser_t *parser) {
36
+ return parser->lex_callback.data;
37
+ }
38
+
39
+ /**
40
+ * Returns the raw pointer to the start of the source that is being parsed.
41
+ */
42
+ const uint8_t *
43
+ pm_parser_start(const pm_parser_t *parser) {
44
+ return parser->start;
45
+ }
46
+
47
+ /**
48
+ * Returns the raw pointer to the end of the source that is being parsed.
49
+ */
50
+ const uint8_t *
51
+ pm_parser_end(const pm_parser_t *parser) {
52
+ return parser->end;
53
+ }
54
+
55
+ /**
56
+ * Returns the line that the parser was considered to have started on.
57
+ *
58
+ * @param parser the parser whose start line we want to get
59
+ * @return the line that the parser was considered to have started on
60
+ */
61
+ int32_t
62
+ pm_parser_start_line(const pm_parser_t *parser) {
63
+ return parser->start_line;
64
+ }
65
+
66
+ /**
67
+ * Returns the name of the encoding that is being used to parse the source.
68
+ */
69
+ const char *
70
+ pm_parser_encoding_name(const pm_parser_t *parser) {
71
+ return parser->encoding->name;
72
+ }
73
+
74
+ /**
75
+ * Returns the width of the character at the given pointer in the encoding that
76
+ * is being used to parse the source.
77
+ */
78
+ size_t
79
+ pm_parser_encoding_char_width(const pm_parser_t *parser, const uint8_t *start, ptrdiff_t remaining) {
80
+ return parser->encoding->char_width(start, remaining);
81
+ }
82
+
83
+ /**
84
+ * Returns whether or not the parser is using the US-ASCII encoding.
85
+ */
86
+ bool
87
+ pm_parser_encoding_us_ascii(const pm_parser_t *parser) {
88
+ return parser->encoding == PM_ENCODING_US_ASCII_ENTRY;
89
+ }
90
+
91
+ /**
92
+ * Returns the filepath that is being used to parse the source.
93
+ */
94
+ const pm_string_t *
95
+ pm_parser_filepath(const pm_parser_t *parser) {
96
+ return &parser->filepath;
97
+ }
98
+
99
+ /**
100
+ * Find a constant in the parser's constant pool. Returns the id of the
101
+ * constant, or 0 if the constant is not found.
102
+ */
103
+ pm_constant_id_t
104
+ pm_parser_constant_find(const pm_parser_t *parser, const uint8_t *start, size_t length) {
105
+ return pm_constant_pool_find(&parser->constant_pool, start, length);
106
+ }
107
+
108
+ /**
109
+ * Returns the frozen string literal value of the parser.
110
+ */
111
+ int8_t
112
+ pm_parser_frozen_string_literal(const pm_parser_t *parser) {
113
+ return parser->frozen_string_literal;
114
+ }
115
+
116
+ /**
117
+ * Returns the line offsets that are associated with the given parser.
118
+ *
119
+ * @param parser the parser whose line offsets we want to get
120
+ * @return the line offsets that are associated with the given parser
121
+ */
122
+ const pm_line_offset_list_t *
123
+ pm_parser_line_offsets(const pm_parser_t *parser) {
124
+ return &parser->line_offsets;
125
+ }
126
+
127
+ /**
128
+ * Returns the location of the __DATA__ section that is associated with the
129
+ * given parser, if it exists.
130
+ */
131
+ const pm_location_t *
132
+ pm_parser_data_loc(const pm_parser_t *parser) {
133
+ return &parser->data_loc;
134
+ }
135
+
136
+ /**
137
+ * Returns the checksum of the source code that was parsed.
138
+ */
139
+ uint64_t
140
+ pm_parser_checksum(const pm_parser_t *parser) {
141
+ const uint8_t *end = parser->end;
142
+
143
+ if (parser->data_loc.length != 0) {
144
+ const uint8_t *cursor = parser->start + parser->data_loc.start;
145
+ const uint8_t *newline = memchr(cursor, '\n', (size_t) (end - cursor));
146
+ if (newline != NULL) end = newline + 1;
147
+ }
148
+
149
+ return pm_xxh3(parser->start, (size_t) (end - parser->start));
150
+ }
151
+
152
+ /**
153
+ * Returns whether the given parser is continuable, meaning that it could become
154
+ * valid if more input were appended, as opposed to being definitively invalid.
155
+ */
156
+ bool
157
+ pm_parser_continuable(const pm_parser_t *parser) {
158
+ return parser->continuable;
159
+ }
160
+
161
+ /**
162
+ * Returns the lex state of the parser. Note that this is an internal detail,
163
+ * and we are purposefully not returning an instance of the internal enum that
164
+ * we use to track this. This is only exposed because we need it for some very
165
+ * niche use cases. Most consumers should avoid this function.
166
+ */
167
+ int
168
+ pm_parser_lex_state(const pm_parser_t *parser) {
169
+ return (int) parser->lex_state;
170
+ }
171
+
172
+ /**
173
+ * Returns the location associated with the given comment.
174
+ */
175
+ pm_location_t
176
+ pm_comment_location(const pm_comment_t *comment) {
177
+ return comment->location;
178
+ }
179
+
180
+ /**
181
+ * Returns the type associated with the given comment.
182
+ */
183
+ pm_comment_type_t
184
+ pm_comment_type(const pm_comment_t *comment) {
185
+ return comment->type;
186
+ }
187
+
188
+ /**
189
+ * Returns the number of comments associated with the given parser.
190
+ */
191
+ size_t
192
+ pm_parser_comments_size(const pm_parser_t *parser) {
193
+ return parser->comment_list.size;
194
+ }
195
+
196
+ /**
197
+ * Iterates over the comments associated with the given parser and calls the
198
+ * given callback for each comment.
199
+ */
200
+ void
201
+ pm_parser_comments_each(const pm_parser_t *parser, pm_comment_callback_t callback, void *data) {
202
+ const pm_list_node_t *current = parser->comment_list.head;
203
+ while (current != NULL) {
204
+ const pm_comment_t *comment = (const pm_comment_t *) current;
205
+ callback(comment, data);
206
+ current = current->next;
207
+ }
208
+ }
209
+
210
+ /**
211
+ * Returns the number of comments owned by the given node.
212
+ */
213
+ uint32_t
214
+ pm_node_comments_size(const pm_parser_t *parser, const pm_node_t *node) {
215
+ return pm_parser_comment_range(parser, node->node_id).length;
216
+ }
217
+
218
+ /**
219
+ * Returns the comment at the given index among the comments owned by the given
220
+ * node.
221
+ */
222
+ const pm_comment_t *
223
+ pm_node_comment(const pm_parser_t *parser, const pm_node_t *node, uint32_t index) {
224
+ pm_comment_range_t range = pm_parser_comment_range(parser, node->node_id);
225
+ if (index >= range.length) return NULL;
226
+
227
+ return parser->comment_attachments.comments[parser->comment_attachments.attachments[range.index + index]];
228
+ }
229
+
230
+ /**
231
+ * Returns the index, among the comments associated with the given parser, of
232
+ * the comment at the given index among the comment attachments.
233
+ */
234
+ uint32_t
235
+ pm_parser_comment_attachment(const pm_parser_t *parser, uint32_t index) {
236
+ if (parser->comment_attachments.attachments == NULL || index >= parser->comment_list.size) return UINT32_MAX;
237
+ return parser->comment_attachments.attachments[index];
238
+ }
239
+
240
+ /**
241
+ * Returns the number of node ids that have a comment range.
242
+ */
243
+ uint32_t
244
+ pm_parser_comment_ranges_size(const pm_parser_t *parser) {
245
+ return parser->comment_attachments.ranges_size;
246
+ }
247
+
248
+ /**
249
+ * Returns the range of comment attachments owned by the node with the given id.
250
+ */
251
+ pm_comment_range_t
252
+ pm_parser_comment_range(const pm_parser_t *parser, uint32_t node_id) {
253
+ if (node_id >= parser->comment_attachments.ranges_size) return (pm_comment_range_t) { .index = 0, .length = 0 };
254
+ return parser->comment_attachments.ranges[node_id];
255
+ }
256
+
257
+ /**
258
+ * Returns the location associated with the given magic comment key.
259
+ */
260
+ pm_location_t
261
+ pm_magic_comment_key(const pm_magic_comment_t *magic_comment) {
262
+ return magic_comment->key;
263
+ }
264
+
265
+ /**
266
+ * Returns the location associated with the given magic comment value.
267
+ */
268
+ pm_location_t
269
+ pm_magic_comment_value(const pm_magic_comment_t *magic_comment) {
270
+ return magic_comment->value;
271
+ }
272
+
273
+ /**
274
+ * Returns the number of magic comments associated with the given parser.
275
+ */
276
+ size_t
277
+ pm_parser_magic_comments_size(const pm_parser_t *parser) {
278
+ return parser->magic_comment_list.size;
279
+ }
280
+
281
+ /**
282
+ * Iterates over the magic comments associated with the given parser and calls
283
+ * the given callback for each magic comment.
284
+ */
285
+ void
286
+ pm_parser_magic_comments_each(const pm_parser_t *parser, pm_magic_comment_callback_t callback, void *data) {
287
+ const pm_list_node_t *current = parser->magic_comment_list.head;
288
+ while (current != NULL) {
289
+ const pm_magic_comment_t *magic_comment = (const pm_magic_comment_t *) current;
290
+ callback(magic_comment, data);
291
+ current = current->next;
292
+ }
293
+ }
294
+
295
+ /**
296
+ * Returns the number of errors associated with the given parser.
297
+ */
298
+ size_t
299
+ pm_parser_errors_size(const pm_parser_t *parser) {
300
+ return parser->error_list.size;
301
+ }
302
+
303
+ /**
304
+ * Returns the number of warnings associated with the given parser.
305
+ */
306
+ size_t
307
+ pm_parser_warnings_size(const pm_parser_t *parser) {
308
+ return parser->warning_list.size;
309
+ }
310
+
311
+ static inline void
312
+ pm_parser_diagnostics_each(const pm_list_t *list, pm_diagnostic_callback_t callback, void *data) {
313
+ const pm_list_node_t *current = list->head;
314
+ while (current != NULL) {
315
+ const pm_diagnostic_t *diagnostic = (const pm_diagnostic_t *) current;
316
+ callback(diagnostic, data);
317
+ current = current->next;
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Iterates over the errors associated with the given parser and calls the
323
+ * given callback for each error.
324
+ */
325
+ void
326
+ pm_parser_errors_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) {
327
+ pm_parser_diagnostics_each(&parser->error_list, callback, data);
328
+ }
329
+
330
+ /**
331
+ * Iterates over the warnings associated with the given parser and calls the
332
+ * given callback for each warning.
333
+ */
334
+ void
335
+ pm_parser_warnings_each(const pm_parser_t *parser, pm_diagnostic_callback_t callback, void *data) {
336
+ pm_parser_diagnostics_each(&parser->warning_list, callback, data);
337
+ }
338
+
339
+ /**
340
+ * Returns the number of constants in the constant pool associated with the
341
+ * given parser.
342
+ */
343
+ size_t
344
+ pm_parser_constants_size(const pm_parser_t *parser) {
345
+ return parser->constant_pool.size;
346
+ }
347
+
348
+ /**
349
+ * Iterates over the constants in the constant pool associated with the given
350
+ * parser and calls the given callback for each constant.
351
+ */
352
+ void
353
+ pm_parser_constants_each(const pm_parser_t *parser, pm_constant_callback_t callback, void *data) {
354
+ for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
355
+ const pm_constant_t *constant = &parser->constant_pool.constants[index];
356
+ callback(constant, data);
357
+ }
358
+ }
359
+
360
+ /**
361
+ * Returns a pointer to the constant at the given id in the constant pool
362
+ * associated with the given parser.
363
+ */
364
+ const pm_constant_t *
365
+ pm_parser_constant(const pm_parser_t *parser, pm_constant_id_t constant_id) {
366
+ return pm_constant_pool_id_to_constant(&parser->constant_pool, constant_id);
367
+ }