Coverage Report

Created: 2026-11-10 14:14

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
src/parser.c
Line
Count
Source
1
#include "prism/internal/parser.h"
2
3
#include "prism/internal/allocator.h"
4
#include "prism/internal/assert.h"
5
#include "prism/internal/comments.h"
6
#include "prism/internal/diagnostic.h"
7
#include "prism/internal/encoding.h"
8
#include "prism/internal/magic_comments.h"
9
#include "prism/internal/xxh3.h"
10
11
#include <stdlib.h>
12
#include <string.h>
13
14
/**
15
 * Register a callback that will be called whenever prism changes the encoding
16
 * it is using to parse based on the magic comment.
17
 */
18
void
19
pm_parser_encoding_changed_callback_set(pm_parser_t *parser, pm_encoding_changed_callback_t callback) {
20
    parser->encoding_changed_callback = callback;
21
}
22
23
/**
24
 * Register a callback that will be called whenever a token is lexed.
25
 */
26
void
27
pm_parser_lex_callback_set(pm_parser_t *parser, pm_lex_callback_t callback, void *data) {
28
    parser->lex_callback.callback = callback;
29
    parser->lex_callback.data = data;
30
}
31
32
/**
33
 * Returns the opaque data that is passed to the lex callback when it is called.
34
 */
35
void *
36
pm_parser_lex_callback_data(const pm_parser_t *parser) {
37
    return parser->lex_callback.data;
38
}
39
40
/**
41
 * Returns the raw pointer to the start of the source that is being parsed.
42
 */
43
const uint8_t *
44
pm_parser_start(const pm_parser_t *parser) {
45
    return parser->start;
46
}
47
48
/**
49
 * Returns the raw pointer to the end of the source that is being parsed.
50
 */
51
const uint8_t *
52
pm_parser_end(const pm_parser_t *parser) {
53
    return parser->end;
54
}
55
56
/**
57
 * Returns the line that the parser was considered to have started on.
58
 *
59
 * @param parser the parser whose start line we want to get
60
 * @return the line that the parser was considered to have started on
61
 */
62
int32_t
63
pm_parser_start_line(const pm_parser_t *parser) {
64
    return parser->start_line;
65
}
66
67
/**
68
 * Returns the name of the encoding that is being used to parse the source.
69
 */
70
const char *
71
pm_parser_encoding_name(const pm_parser_t *parser) {
72
    return parser->encoding->name;
73
}
74
75
/**
76
 * Returns the width of the character at the given pointer in the encoding that
77
 * is being used to parse the source.
78
 */
79
size_t
80
15.2k
pm_parser_encoding_char_width(const pm_parser_t *parser, const uint8_t *start, ptrdiff_t remaining) {
81
15.2k
    return parser->encoding->char_width(start, remaining);
82
15.2k
}
83
84
/**
85
 * Returns whether or not the parser is using the US-ASCII encoding.
86
 */
87
bool
88
pm_parser_encoding_us_ascii(const pm_parser_t *parser) {
89
    return parser->encoding == PM_ENCODING_US_ASCII_ENTRY;
90
}
91
92
/**
93
 * Returns the filepath that is being used to parse the source.
94
 */
95
const pm_string_t *
96
pm_parser_filepath(const pm_parser_t *parser) {
97
    return &parser->filepath;
98
}
99
100
/**
101
 * Find a constant in the parser's constant pool. Returns the id of the
102
 * constant, or 0 if the constant is not found.
103
 */
104
pm_constant_id_t
105
pm_parser_constant_find(const pm_parser_t *parser, const uint8_t *start, size_t length) {
106
    return pm_constant_pool_find(&parser->constant_pool, start, length);
107
}
108
109
/**
110
 * Add a constant to the parser's constant pool, copying its bytes into the
111
 * parser's arena only if the constant is not already there.
112
 */
113
pm_constant_id_t
114
pm_parser_constant_insert(pm_parser_t *parser, const uint8_t *start, size_t length) {
115
    pm_constant_id_t id = pm_constant_pool_find(&parser->constant_pool, start, length);
116
    if (id != PM_CONSTANT_ID_UNSET) return id;
117
118
    const uint8_t *copy = (const uint8_t *) pm_arena_memdup(&parser->metadata_arena, start, length, PRISM_ALIGNOF(uint8_t));
119
    return pm_constant_pool_insert_constant(&parser->metadata_arena, &parser->constant_pool, copy, length);
120
}
121
122
/**
123
 * Returns the frozen string literal value of the parser.
124
 */
125
int8_t
126
pm_parser_frozen_string_literal(const pm_parser_t *parser) {
127
    return parser->frozen_string_literal;
128
}
129
130
/**
131
 * Returns the line offsets that are associated with the given parser.
132
 *
133
 * @param parser the parser whose line offsets we want to get
134
 * @return the line offsets that are associated with the given parser
135
 */
136
const pm_line_offset_list_t *
137
pm_parser_line_offsets(const pm_parser_t *parser) {
138
    return &parser->line_offsets;
139
}
140
141
/**
142
 * Returns the location of the __DATA__ section that is associated with the
143
 * given parser, if it exists.
144
 */
145
const pm_location_t *
146
pm_parser_data_loc(const pm_parser_t *parser) {
147
    return &parser->data_loc;
148
}
149
150
/**
151
 * Returns the checksum of the source code that was parsed.
152
 */
153
uint64_t
154
4.88k
pm_parser_checksum(const pm_parser_t *parser) {
155
4.88k
    const uint8_t *end = parser->end;
156
157
4.88k
    if (parser->data_loc.length != 0) {
  Branch (157:9): [True: 2, False: 4.88k]
158
2
        const uint8_t *cursor = parser->start + parser->data_loc.start;
159
2
        const uint8_t *newline = memchr(cursor, '\n', (size_t) (end - cursor));
160
2
        if (newline != NULL) 
end = newline + 11
;
  Branch (160:13): [True: 1, False: 1]
161
2
    }
162
163
4.88k
    return pm_xxh3(parser->start, (size_t) (end - parser->start));
164
4.88k
}
165
166
/**
167
 * Returns whether the given parser is continuable, meaning that it could become
168
 * valid if more input were appended, as opposed to being definitively invalid.
169
 */
170
bool
171
pm_parser_continuable(const pm_parser_t *parser) {
172
    return parser->continuable;
173
}
174
175
/**
176
 * Returns the lex state of the parser. Note that this is an internal detail,
177
 * and we are purposefully not returning an instance of the internal enum that
178
 * we use to track this. This is only exposed because we need it for some very
179
 * niche use cases. Most consumers should avoid this function.
180
 */
181
int
182
pm_parser_lex_state(const pm_parser_t *parser) {
183
    return (int) parser->lex_state;
184
}
185
186
/**
187
 * Returns the location associated with the given comment.
188
 */
189
pm_location_t
190
pm_comment_location(const pm_comment_t *comment) {
191
    return comment->location;
192
}
193
194
/**
195
 * Returns the type associated with the given comment.
196
 */
197
pm_comment_type_t
198
pm_comment_type(const pm_comment_t *comment) {
199
    return comment->type;
200
}
201
202
/**
203
 * Returns the comment that follows the given comment.
204
 */
205
const pm_comment_t *
206
pm_comment_next(const pm_comment_t *comment) {
207
    return (const pm_comment_t *) comment->node.next;
208
}
209
210
/**
211
 * Returns the number of comments associated with the given parser.
212
 */
213
size_t
214
pm_parser_comments_size(const pm_parser_t *parser) {
215
    return parser->comment_list.size;
216
}
217
218
/**
219
 * Returns the first comment associated with the given parser.
220
 */
221
const pm_comment_t *
222
pm_parser_comments_first(const pm_parser_t *parser) {
223
    return (const pm_comment_t *) parser->comment_list.head;
224
}
225
226
/**
227
 * Returns the number of comments owned by the given node.
228
 */
229
uint32_t
230
pm_node_comments_size(const pm_parser_t *parser, const pm_node_t *node) {
231
    return pm_parser_comment_range(parser, node->node_id).length;
232
}
233
234
/**
235
 * Returns the comment at the given index among the comments owned by the given
236
 * node.
237
 */
238
const pm_comment_t *
239
pm_node_comment(const pm_parser_t *parser, const pm_node_t *node, uint32_t index) {
240
    pm_comment_range_t range = pm_parser_comment_range(parser, node->node_id);
241
    if (index >= range.length) return NULL;
242
243
    return parser->comment_attachments.comments[parser->comment_attachments.attachments[range.index + index]];
244
}
245
246
/**
247
 * Returns the index, among the comments associated with the given parser, of
248
 * the comment at the given index among the comment attachments.
249
 */
250
uint32_t
251
pm_parser_comment_attachment(const pm_parser_t *parser, uint32_t index) {
252
    if (parser->comment_attachments.attachments == NULL || index >= parser->comment_list.size) return UINT32_MAX;
253
    return parser->comment_attachments.attachments[index];
254
}
255
256
/**
257
 * Returns the number of node ids that have a comment range.
258
 */
259
uint32_t
260
pm_parser_comment_ranges_size(const pm_parser_t *parser) {
261
    return parser->comment_attachments.ranges_size;
262
}
263
264
/**
265
 * Returns the range of comment attachments owned by the node with the given id.
266
 */
267
pm_comment_range_t
268
pm_parser_comment_range(const pm_parser_t *parser, uint32_t node_id) {
269
    if (node_id >= parser->comment_attachments.ranges_size) return (pm_comment_range_t) { .index = 0, .length = 0 };
270
    return parser->comment_attachments.ranges[node_id];
271
}
272
273
/**
274
 * Returns the location associated with the given magic comment key.
275
 */
276
pm_location_t
277
pm_magic_comment_key(const pm_magic_comment_t *magic_comment) {
278
    return magic_comment->key;
279
}
280
281
/**
282
 * Returns the location associated with the given magic comment value.
283
 */
284
pm_location_t
285
pm_magic_comment_value(const pm_magic_comment_t *magic_comment) {
286
    return magic_comment->value;
287
}
288
289
/**
290
 * Returns the magic comment that follows the given magic comment.
291
 */
292
const pm_magic_comment_t *
293
pm_magic_comment_next(const pm_magic_comment_t *magic_comment) {
294
    return (const pm_magic_comment_t *) magic_comment->node.next;
295
}
296
297
/**
298
 * Returns the number of magic comments associated with the given parser.
299
 */
300
size_t
301
pm_parser_magic_comments_size(const pm_parser_t *parser) {
302
    return parser->magic_comment_list.size;
303
}
304
305
/**
306
 * Returns the first magic comment associated with the given parser.
307
 */
308
const pm_magic_comment_t *
309
pm_parser_magic_comments_first(const pm_parser_t *parser) {
310
    return (const pm_magic_comment_t *) parser->magic_comment_list.head;
311
}
312
313
/**
314
 * Returns the number of errors associated with the given parser.
315
 */
316
size_t
317
pm_parser_errors_size(const pm_parser_t *parser) {
318
    return parser->error_list.size;
319
}
320
321
/**
322
 * Returns the number of warnings associated with the given parser.
323
 */
324
size_t
325
pm_parser_warnings_size(const pm_parser_t *parser) {
326
    return parser->warning_list.size;
327
}
328
329
/**
330
 * Returns the first error associated with the given parser.
331
 */
332
const pm_diagnostic_t *
333
pm_parser_errors_first(const pm_parser_t *parser) {
334
    return (const pm_diagnostic_t *) parser->error_list.head;
335
}
336
337
/**
338
 * Returns the first warning associated with the given parser.
339
 */
340
const pm_diagnostic_t *
341
pm_parser_warnings_first(const pm_parser_t *parser) {
342
    return (const pm_diagnostic_t *) parser->warning_list.head;
343
}
344
345
/**
346
 * Returns the number of constants in the constant pool associated with the
347
 * given parser.
348
 */
349
size_t
350
pm_parser_constants_size(const pm_parser_t *parser) {
351
    return parser->constant_pool.size;
352
}
353
354
/**
355
 * Returns the constant at the given index in the constant pool associated with
356
 * the given parser.
357
 */
358
const pm_constant_t *
359
pm_parser_constant_at(const pm_parser_t *parser, size_t index) {
360
    PRISM_ASSERT(index < parser->constant_pool.size);
361
    return &parser->constant_pool.constants[index];
362
}
363
364
/**
365
 * Returns a pointer to the constant at the given id in the constant pool
366
 * associated with the given parser.
367
 */
368
const pm_constant_t *
369
pm_parser_constant(const pm_parser_t *parser, pm_constant_id_t constant_id) {
370
    return pm_constant_pool_id_to_constant(&parser->constant_pool, constant_id);
371
}