| 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_Hcxx . . $ `include/prism/util/pm_newline_list.h:15:9`! |
| 16 | |
| 17 | #include "prism/defines.h"cxx . . $ `<file>/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 structcxx . . $ $anonymous_type_9901d064858912af_0# { |
| 29 | /** A pointer to the start of the source string. */ |
| 30 | const uint8_tcxx . . $ uint8_t# *startcxx . . $ $anonymous_type_9901d064858912af_0#start.; |
| 31 | |
| 32 | /** The number of offsets in the list. */ |
| 33 | size_t sizecxx . . $ $anonymous_type_9901d064858912af_0#size.; |
| 34 | |
| 35 | /** The capacity of the list that has been allocated. */ |
| 36 | size_t capacitycxx . . $ $anonymous_type_9901d064858912af_0#capacity.; |
| 37 | |
| 38 | /** The list of offsets. */ |
| 39 | size_t *offsetscxx . . $ $anonymous_type_9901d064858912af_0#offsets.; |
| 40 | } pm_newline_list_tcxx . . $ pm_newline_list_t#; |
| 41 | |
| 42 | /** |
| 43 | * A line and column in a string. |
| 44 | */ |
| 45 | typedef structcxx . . $ $anonymous_type_9901d064858912af_1# { |
| 46 | /** The line number. */ |
| 47 | int32_tcxx . . $ int32_t# linecxx . . $ $anonymous_type_9901d064858912af_1#line.; |
| 48 | |
| 49 | /** The column number. */ |
| 50 | uint32_tcxx . . $ uint32_t# columncxx . . $ $anonymous_type_9901d064858912af_1#column.; |
| 51 | } pm_line_column_tcxx . . $ 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 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! pm_newline_list_initcxx . . $ pm_newline_list_init(340008c22afd583).(pm_newline_list_tcxx . . $ pm_newline_list_t# *list, const uint8_tcxx . . $ 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_clearcxx . . $ pm_newline_list_clear(6eb44ba19cf4754d).(pm_newline_list_tcxx . . $ 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 | boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! pm_newline_list_appendcxx . . $ pm_newline_list_append(93623494a75c4b1f).(pm_newline_list_tcxx . . $ pm_newline_list_t# *list, const uint8_tcxx . . $ 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_tcxx . . $ int32_t# pm_newline_list_linecxx . . $ pm_newline_list_line(5c8a19f31b9c22e2).(const pm_newline_list_tcxx . . $ pm_newline_list_t# *list, const uint8_tcxx . . $ uint8_t# *cursor, int32_tcxx . . $ 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_tcxx . . $ pm_line_column_t# pm_newline_list_line_columncxx . . $ pm_newline_list_line_column(1a8b391d550aa6bd).(const pm_newline_list_tcxx . . $ pm_newline_list_t# *list, const uint8_tcxx . . $ uint8_t# *cursor, int32_tcxx . . $ 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_freecxx . . $ pm_newline_list_free(6eb44ba19cf4754d).(pm_newline_list_tcxx . . $ pm_newline_list_t# *list); |
| 112 | |
| 113 | #endif |