Debian Codegraph @

include/prism/util/pm_newline_list.h

raw symbols SCIP
1 /**
2 * @file pm_newline_list.h
3 *
4 * A list of byte offsets of newlines in a string.
5 *
6 * When compiling the syntax tree, it's necessary to know the line and column
7 * of many nodes. This is necessary to support things like error messages,
8 * tracepoints, etc.
9 *
10 * It's possible that we could store the start line, start column, end line, and
11 * end column on every node in addition to the offsets that we already store,
12 * but that would be quite a lot of memory overhead.
13 */
14 #ifndef PRISM_NEWLINE_LIST_H
15 #define PRISM_NEWLINE_LIST_H
16
17 #include "prism/defines.h"
18
19 #include <assert.h>
20 #include <stdbool.h>
21 #include <stddef.h>
22 #include <stdlib.h>
23
24 /**
25 * A list of offsets of newlines in a string. The offsets are assumed to be
26 * sorted/inserted in ascending order.
27 */
28 typedef struct {
29 /** A pointer to the start of the source string. */
30 const uint8_t *start;
31
32 /** The number of offsets in the list. */
33 size_t size;
34
35 /** The capacity of the list that has been allocated. */
36 size_t capacity;
37
38 /** The list of offsets. */
39 size_t *offsets;
40 } pm_newline_list_t;
41
42 /**
43 * A line and column in a string.
44 */
45 typedef struct {
46 /** The line number. */
47 int32_t line;
48
49 /** The column number. */
50 uint32_t column;
51 } pm_line_column_t;
52
53 /**
54 * Initialize a new newline list with the given capacity. Returns true if the
55 * allocation of the offsets succeeds, otherwise returns false.
56 *
57 * @param list The list to initialize.
58 * @param start A pointer to the start of the source string.
59 * @param capacity The initial capacity of the list.
60 * @return True if the allocation of the offsets succeeds, otherwise false.
61 */
62 bool pm_newline_list_init(pm_newline_list_t *list, const uint8_t *start, size_t capacity);
63
64 /**
65 * Clear out the newlines that have been appended to the list.
66 *
67 * @param list The list to clear.
68 */
69 void
70 pm_newline_list_clear(pm_newline_list_t *list);
71
72 /**
73 * Append a new offset to the newline list. Returns true if the reallocation of
74 * the offsets succeeds (if one was necessary), otherwise returns false.
75 *
76 * @param list The list to append to.
77 * @param cursor A pointer to the offset to append.
78 * @return True if the reallocation of the offsets succeeds (if one was
79 * necessary), otherwise false.
80 */
81 bool pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor);
82
83 /**
84 * Returns the line of the given offset. If the offset is not in the list, the
85 * line of the closest offset less than the given offset is returned.
86 *
87 * @param list The list to search.
88 * @param cursor A pointer to the offset to search for.
89 * @param start_line The line to start counting from.
90 * @return The line of the given offset.
91 */
92 int32_t pm_newline_list_line(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line);
93
94 /**
95 * Returns the line and column of the given offset. If the offset is not in the
96 * list, the line and column of the closest offset less than the given offset
97 * are returned.
98 *
99 * @param list The list to search.
100 * @param cursor A pointer to the offset to search for.
101 * @param start_line The line to start counting from.
102 * @return The line and column of the given offset.
103 */
104 pm_line_column_t pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line);
105
106 /**
107 * Free the internal memory allocated for the newline list.
108 *
109 * @param list The list to free.
110 */
111 void pm_newline_list_free(pm_newline_list_t *list);
112
113 #endif