|
| 1 | +/** |
| 2 | + * @file merve_c.h |
| 3 | + * @brief Includes the C definitions for merve. This is a C file, not C++. |
| 4 | + */ |
| 5 | +#ifndef MERVE_C_H |
| 6 | +#define MERVE_C_H |
| 7 | + |
| 8 | +#include <stdbool.h> |
| 9 | +#include <stddef.h> |
| 10 | +#include <stdint.h> |
| 11 | + |
| 12 | +/** |
| 13 | + * @brief Non-owning string reference. |
| 14 | + * |
| 15 | + * The data pointer is NOT null-terminated. Always use the length field. |
| 16 | + * |
| 17 | + * The data is valid as long as: |
| 18 | + * - The merve_analysis handle that produced it has not been freed. |
| 19 | + * - For string_view-backed exports: the original source buffer is alive. |
| 20 | + */ |
| 21 | +typedef struct { |
| 22 | + const char* data; |
| 23 | + size_t length; |
| 24 | +} merve_string; |
| 25 | + |
| 26 | +/** |
| 27 | + * @brief Opaque handle to a CommonJS parse result. |
| 28 | + * |
| 29 | + * Created by merve_parse_commonjs(). Must be freed with merve_free(). |
| 30 | + */ |
| 31 | +typedef void* merve_analysis; |
| 32 | + |
| 33 | +/** |
| 34 | + * @brief Version number components. |
| 35 | + */ |
| 36 | +typedef struct { |
| 37 | + int major; |
| 38 | + int minor; |
| 39 | + int revision; |
| 40 | +} merve_version_components; |
| 41 | + |
| 42 | +/* Error codes corresponding to lexer::lexer_error values. */ |
| 43 | +#define MERVE_ERROR_TODO 0 |
| 44 | +#define MERVE_ERROR_UNEXPECTED_PAREN 1 |
| 45 | +#define MERVE_ERROR_UNEXPECTED_BRACE 2 |
| 46 | +#define MERVE_ERROR_UNTERMINATED_PAREN 3 |
| 47 | +#define MERVE_ERROR_UNTERMINATED_BRACE 4 |
| 48 | +#define MERVE_ERROR_UNTERMINATED_TEMPLATE_STRING 5 |
| 49 | +#define MERVE_ERROR_UNTERMINATED_STRING_LITERAL 6 |
| 50 | +#define MERVE_ERROR_UNTERMINATED_REGEX_CHARACTER_CLASS 7 |
| 51 | +#define MERVE_ERROR_UNTERMINATED_REGEX 8 |
| 52 | +#define MERVE_ERROR_UNEXPECTED_ESM_IMPORT_META 9 |
| 53 | +#define MERVE_ERROR_UNEXPECTED_ESM_IMPORT 10 |
| 54 | +#define MERVE_ERROR_UNEXPECTED_ESM_EXPORT 11 |
| 55 | +#define MERVE_ERROR_TEMPLATE_NEST_OVERFLOW 12 |
| 56 | + |
| 57 | +#ifdef __cplusplus |
| 58 | +extern "C" { |
| 59 | +#endif |
| 60 | + |
| 61 | +/** |
| 62 | + * Parse CommonJS source code and extract export information. |
| 63 | + * |
| 64 | + * The source buffer must remain valid while accessing string_view-backed |
| 65 | + * export names from the returned handle. |
| 66 | + * |
| 67 | + * You must call merve_free() on the returned handle when done. |
| 68 | + * |
| 69 | + * @param input Pointer to the JavaScript source (need not be null-terminated). |
| 70 | + * NULL is treated as an empty string. |
| 71 | + * @param length Length of the input in bytes. |
| 72 | + * @return A handle to the parse result, or NULL on out-of-memory. |
| 73 | + * Use merve_is_valid() to check if parsing succeeded. |
| 74 | + */ |
| 75 | +merve_analysis merve_parse_commonjs(const char* input, size_t length); |
| 76 | + |
| 77 | +/** |
| 78 | + * Check whether the parse result is valid (parsing succeeded). |
| 79 | + * |
| 80 | + * @param result Handle returned by merve_parse_commonjs(). NULL returns false. |
| 81 | + * @return true if parsing succeeded, false otherwise. |
| 82 | + */ |
| 83 | +bool merve_is_valid(merve_analysis result); |
| 84 | + |
| 85 | +/** |
| 86 | + * Free a parse result and all associated memory. |
| 87 | + * |
| 88 | + * @param result Handle returned by merve_parse_commonjs(). NULL is a no-op. |
| 89 | + */ |
| 90 | +void merve_free(merve_analysis result); |
| 91 | + |
| 92 | +/** |
| 93 | + * Get the number of named exports found. |
| 94 | + * |
| 95 | + * @param result A parse result handle. NULL returns 0. |
| 96 | + * @return Number of exports, or 0 if result is NULL or invalid. |
| 97 | + */ |
| 98 | +size_t merve_get_exports_count(merve_analysis result); |
| 99 | + |
| 100 | +/** |
| 101 | + * Get the number of re-export module specifiers found. |
| 102 | + * |
| 103 | + * @param result A parse result handle. NULL returns 0. |
| 104 | + * @return Number of re-exports, or 0 if result is NULL or invalid. |
| 105 | + */ |
| 106 | +size_t merve_get_reexports_count(merve_analysis result); |
| 107 | + |
| 108 | +/** |
| 109 | + * Get the name of an export at the given index. |
| 110 | + * |
| 111 | + * @param result A valid parse result handle. |
| 112 | + * @param index Zero-based index (must be < merve_get_exports_count()). |
| 113 | + * @return Non-owning string reference. Returns {NULL, 0} on error. |
| 114 | + */ |
| 115 | +merve_string merve_get_export_name(merve_analysis result, size_t index); |
| 116 | + |
| 117 | +/** |
| 118 | + * Get the 1-based source line number of an export. |
| 119 | + * |
| 120 | + * @param result A valid parse result handle. |
| 121 | + * @param index Zero-based index (must be < merve_get_exports_count()). |
| 122 | + * @return 1-based line number, or 0 on error. |
| 123 | + */ |
| 124 | +uint32_t merve_get_export_line(merve_analysis result, size_t index); |
| 125 | + |
| 126 | +/** |
| 127 | + * Get the module specifier of a re-export at the given index. |
| 128 | + * |
| 129 | + * @param result A valid parse result handle. |
| 130 | + * @param index Zero-based index (must be < merve_get_reexports_count()). |
| 131 | + * @return Non-owning string reference. Returns {NULL, 0} on error. |
| 132 | + */ |
| 133 | +merve_string merve_get_reexport_name(merve_analysis result, size_t index); |
| 134 | + |
| 135 | +/** |
| 136 | + * Get the 1-based source line number of a re-export. |
| 137 | + * |
| 138 | + * @param result A valid parse result handle. |
| 139 | + * @param index Zero-based index (must be < merve_get_reexports_count()). |
| 140 | + * @return 1-based line number, or 0 on error. |
| 141 | + */ |
| 142 | +uint32_t merve_get_reexport_line(merve_analysis result, size_t index); |
| 143 | + |
| 144 | +/** |
| 145 | + * Get the error code from the last merve_parse_commonjs() call. |
| 146 | + * |
| 147 | + * @return One of the MERVE_ERROR_* constants, or -1 if the last parse |
| 148 | + * succeeded. |
| 149 | + * @note This is global state, overwritten by each merve_parse_commonjs() call. |
| 150 | + */ |
| 151 | +int merve_get_last_error(void); |
| 152 | + |
| 153 | +/** |
| 154 | + * Get the merve library version string. |
| 155 | + * |
| 156 | + * @return Null-terminated version string (e.g. "1.0.1"). Never NULL. |
| 157 | + */ |
| 158 | +const char* merve_get_version(void); |
| 159 | + |
| 160 | +/** |
| 161 | + * Get the merve library version as individual components. |
| 162 | + * |
| 163 | + * @return Struct with major, minor, and revision fields. |
| 164 | + */ |
| 165 | +merve_version_components merve_get_version_components(void); |
| 166 | + |
| 167 | +#ifdef __cplusplus |
| 168 | +} /* extern "C" */ |
| 169 | +#endif |
| 170 | + |
| 171 | +#endif /* MERVE_C_H */ |
0 commit comments