herb 0.7.4-aarch64-linux-gnu → 0.8.0-aarch64-linux-gnu

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 (176) hide show
  1. checksums.yaml +4 -4
  2. data/Makefile +8 -5
  3. data/config.yml +40 -20
  4. data/ext/herb/error_helpers.c +57 -3
  5. data/ext/herb/error_helpers.h +1 -1
  6. data/ext/herb/extconf.rb +1 -0
  7. data/ext/herb/extension.c +10 -24
  8. data/ext/herb/extension_helpers.c +12 -18
  9. data/ext/herb/extension_helpers.h +4 -4
  10. data/ext/herb/nodes.c +72 -37
  11. data/herb.gemspec +0 -2
  12. data/lib/herb/3.0/herb.so +0 -0
  13. data/lib/herb/3.1/herb.so +0 -0
  14. data/lib/herb/3.2/herb.so +0 -0
  15. data/lib/herb/3.3/herb.so +0 -0
  16. data/lib/herb/3.4/herb.so +0 -0
  17. data/lib/herb/ast/helpers.rb +11 -0
  18. data/lib/herb/ast/node.rb +15 -6
  19. data/lib/herb/ast/nodes.rb +609 -392
  20. data/lib/herb/cli.rb +31 -0
  21. data/lib/herb/colors.rb +82 -0
  22. data/lib/herb/engine/compiler.rb +140 -14
  23. data/lib/herb/engine/debug_visitor.rb +1 -5
  24. data/lib/herb/engine/parser_error_overlay.rb +1 -1
  25. data/lib/herb/engine.rb +18 -20
  26. data/lib/herb/errors.rb +166 -56
  27. data/lib/herb/location.rb +2 -2
  28. data/lib/herb/project.rb +86 -21
  29. data/lib/herb/token.rb +14 -2
  30. data/lib/herb/version.rb +1 -1
  31. data/lib/herb.rb +1 -0
  32. data/sig/herb/ast/helpers.rbs +3 -0
  33. data/sig/herb/ast/node.rbs +12 -5
  34. data/sig/herb/ast/nodes.rbs +124 -62
  35. data/sig/herb/colors.rbs +35 -0
  36. data/sig/herb/engine/compiler.rbs +23 -1
  37. data/sig/herb/errors.rbs +74 -20
  38. data/sig/herb/token.rbs +8 -0
  39. data/sig/herb_c_extension.rbs +1 -1
  40. data/sig/serialized_ast_errors.rbs +8 -0
  41. data/src/analyze.c +461 -249
  42. data/src/analyze_helpers.c +5 -0
  43. data/src/analyze_missing_end.c +147 -0
  44. data/src/analyze_transform.c +196 -0
  45. data/src/analyzed_ruby.c +23 -2
  46. data/src/ast_node.c +14 -17
  47. data/src/ast_nodes.c +179 -181
  48. data/src/ast_pretty_print.c +232 -232
  49. data/src/element_source.c +7 -6
  50. data/src/errors.c +272 -152
  51. data/src/extract.c +92 -34
  52. data/src/herb.c +37 -49
  53. data/src/html_util.c +34 -96
  54. data/src/include/analyze.h +10 -2
  55. data/src/include/analyze_helpers.h +3 -0
  56. data/src/include/analyzed_ruby.h +4 -2
  57. data/src/include/ast_node.h +4 -4
  58. data/src/include/ast_nodes.h +68 -67
  59. data/src/include/ast_pretty_print.h +2 -2
  60. data/src/include/element_source.h +3 -1
  61. data/src/include/errors.h +42 -26
  62. data/src/include/extract.h +4 -4
  63. data/src/include/herb.h +6 -7
  64. data/src/include/html_util.h +4 -5
  65. data/src/include/lexer.h +1 -3
  66. data/src/include/lexer_peek_helpers.h +21 -19
  67. data/src/include/lexer_struct.h +12 -10
  68. data/src/include/location.h +10 -13
  69. data/src/include/macros.h +4 -0
  70. data/src/include/parser.h +12 -6
  71. data/src/include/parser_helpers.h +26 -16
  72. data/src/include/position.h +3 -14
  73. data/src/include/pretty_print.h +38 -28
  74. data/src/include/prism_helpers.h +1 -1
  75. data/src/include/range.h +4 -13
  76. data/src/include/token.h +5 -11
  77. data/src/include/token_struct.h +2 -2
  78. data/src/include/utf8.h +3 -2
  79. data/src/include/util/hb_arena.h +31 -0
  80. data/src/include/util/hb_arena_debug.h +8 -0
  81. data/src/include/util/hb_array.h +33 -0
  82. data/src/include/util/hb_buffer.h +34 -0
  83. data/src/include/util/hb_string.h +29 -0
  84. data/src/include/util/hb_system.h +9 -0
  85. data/src/include/util.h +3 -14
  86. data/src/include/version.h +1 -1
  87. data/src/include/visitor.h +1 -1
  88. data/src/io.c +7 -4
  89. data/src/lexer.c +62 -88
  90. data/src/lexer_peek_helpers.c +42 -38
  91. data/src/location.c +9 -37
  92. data/src/main.c +19 -23
  93. data/src/parser.c +373 -313
  94. data/src/parser_helpers.c +60 -54
  95. data/src/parser_match_tags.c +316 -0
  96. data/src/pretty_print.c +88 -117
  97. data/src/prism_helpers.c +7 -7
  98. data/src/range.c +2 -35
  99. data/src/token.c +36 -87
  100. data/src/utf8.c +4 -4
  101. data/src/util/hb_arena.c +179 -0
  102. data/src/util/hb_arena_debug.c +237 -0
  103. data/src/{array.c → util/hb_array.c} +26 -27
  104. data/src/util/hb_buffer.c +203 -0
  105. data/src/util/hb_string.c +85 -0
  106. data/src/util/hb_system.c +30 -0
  107. data/src/util.c +29 -99
  108. data/src/visitor.c +54 -54
  109. data/templates/ext/herb/error_helpers.c.erb +3 -3
  110. data/templates/ext/herb/error_helpers.h.erb +1 -1
  111. data/templates/ext/herb/nodes.c.erb +11 -6
  112. data/templates/java/error_helpers.c.erb +75 -0
  113. data/templates/java/error_helpers.h.erb +20 -0
  114. data/templates/java/nodes.c.erb +97 -0
  115. data/templates/java/nodes.h.erb +23 -0
  116. data/templates/java/org/herb/ast/Errors.java.erb +121 -0
  117. data/templates/java/org/herb/ast/NodeVisitor.java.erb +14 -0
  118. data/templates/java/org/herb/ast/Nodes.java.erb +220 -0
  119. data/templates/java/org/herb/ast/Visitor.java.erb +56 -0
  120. data/templates/javascript/packages/core/src/visitor.ts.erb +29 -1
  121. data/templates/javascript/packages/node/extension/error_helpers.cpp.erb +8 -8
  122. data/templates/javascript/packages/node/extension/error_helpers.h.erb +1 -1
  123. data/templates/javascript/packages/node/extension/nodes.cpp.erb +9 -9
  124. data/templates/javascript/packages/node/extension/nodes.h.erb +1 -1
  125. data/templates/lib/herb/ast/nodes.rb.erb +28 -16
  126. data/templates/lib/herb/errors.rb.erb +17 -12
  127. data/templates/rust/src/ast/nodes.rs.erb +220 -0
  128. data/templates/rust/src/errors.rs.erb +216 -0
  129. data/templates/rust/src/nodes.rs.erb +374 -0
  130. data/templates/src/analyze_missing_end.c.erb +36 -0
  131. data/templates/src/analyze_transform.c.erb +24 -0
  132. data/templates/src/ast_nodes.c.erb +14 -16
  133. data/templates/src/ast_pretty_print.c.erb +36 -36
  134. data/templates/src/errors.c.erb +36 -38
  135. data/templates/src/include/ast_nodes.h.erb +11 -10
  136. data/templates/src/include/ast_pretty_print.h.erb +2 -2
  137. data/templates/src/include/errors.h.erb +9 -9
  138. data/templates/src/parser_match_tags.c.erb +38 -0
  139. data/templates/src/visitor.c.erb +4 -4
  140. data/templates/template.rb +22 -3
  141. data/templates/wasm/error_helpers.cpp.erb +9 -9
  142. data/templates/wasm/error_helpers.h.erb +1 -1
  143. data/templates/wasm/nodes.cpp.erb +9 -9
  144. data/templates/wasm/nodes.h.erb +1 -1
  145. data/vendor/prism/Rakefile +4 -1
  146. data/vendor/prism/config.yml +2 -1
  147. data/vendor/prism/include/prism/ast.h +31 -1
  148. data/vendor/prism/include/prism/diagnostic.h +1 -0
  149. data/vendor/prism/include/prism/version.h +3 -3
  150. data/vendor/prism/src/diagnostic.c +3 -1
  151. data/vendor/prism/src/prism.c +130 -71
  152. data/vendor/prism/src/util/pm_string.c +6 -8
  153. data/vendor/prism/templates/include/prism/ast.h.erb +2 -0
  154. data/vendor/prism/templates/java/org/prism/Loader.java.erb +2 -2
  155. data/vendor/prism/templates/javascript/src/deserialize.js.erb +2 -2
  156. data/vendor/prism/templates/lib/prism/serialize.rb.erb +2 -2
  157. data/vendor/prism/templates/sig/prism.rbs.erb +4 -0
  158. data/vendor/prism/templates/src/diagnostic.c.erb +1 -0
  159. metadata +34 -21
  160. data/lib/herb/libherb/array.rb +0 -51
  161. data/lib/herb/libherb/ast_node.rb +0 -50
  162. data/lib/herb/libherb/buffer.rb +0 -56
  163. data/lib/herb/libherb/extract_result.rb +0 -20
  164. data/lib/herb/libherb/lex_result.rb +0 -32
  165. data/lib/herb/libherb/libherb.rb +0 -52
  166. data/lib/herb/libherb/parse_result.rb +0 -20
  167. data/lib/herb/libherb/token.rb +0 -46
  168. data/lib/herb/libherb.rb +0 -35
  169. data/src/buffer.c +0 -232
  170. data/src/include/array.h +0 -33
  171. data/src/include/buffer.h +0 -39
  172. data/src/include/json.h +0 -28
  173. data/src/include/memory.h +0 -12
  174. data/src/json.c +0 -205
  175. data/src/memory.c +0 -53
  176. data/src/position.c +0 -33
data/src/buffer.c DELETED
@@ -1,232 +0,0 @@
1
- #include <stdint.h>
2
- #include <stdio.h>
3
- #include <string.h>
4
-
5
- #include "include/buffer.h"
6
- #include "include/macros.h"
7
- #include "include/memory.h"
8
- #include "include/util.h"
9
-
10
- bool buffer_init(buffer_T* buffer) {
11
- buffer->capacity = 1024;
12
- buffer->length = 0;
13
- buffer->value = nullable_safe_malloc((buffer->capacity + 1) * sizeof(char));
14
-
15
- if (!buffer->value) {
16
- fprintf(stderr, "Error: Failed to initialize buffer with capacity of %zu.\n", buffer->capacity);
17
- return false;
18
- }
19
-
20
- buffer->value[0] = '\0';
21
-
22
- return true;
23
- }
24
-
25
- buffer_T buffer_new(void) {
26
- buffer_T buffer;
27
- buffer_init(&buffer);
28
- return buffer;
29
- }
30
-
31
- char* buffer_value(const buffer_T* buffer) {
32
- return buffer->value;
33
- }
34
-
35
- size_t buffer_length(const buffer_T* buffer) {
36
- return buffer->length;
37
- }
38
-
39
- size_t buffer_capacity(const buffer_T* buffer) {
40
- return buffer->capacity;
41
- }
42
-
43
- size_t buffer_sizeof(void) {
44
- return sizeof(buffer_T);
45
- }
46
-
47
- /**
48
- * Increases the capacity of the buffer if needed to accommodate additional content.
49
- * This function only handles memory allocation and does not modify the buffer content
50
- * or null termination.
51
- *
52
- * @param buffer The buffer to increase capacity for
53
- * @param additional_capacity The additional length needed beyond current buffer capacity
54
- * @return true if capacity was increased, false if reallocation failed
55
- */
56
- bool buffer_increase_capacity(buffer_T* buffer, const size_t additional_capacity) {
57
- if (additional_capacity + 1 >= SIZE_MAX) {
58
- fprintf(stderr, "Error: Buffer capacity would overflow system limits.\n");
59
- exit(1);
60
- }
61
-
62
- const size_t new_capacity = buffer->capacity + additional_capacity;
63
-
64
- return buffer_resize(buffer, new_capacity);
65
- }
66
-
67
- /**
68
- * Resizes the capacity of the buffer to the specified new capacity.
69
- *
70
- * @param buffer The buffer to resize
71
- * @param new_capacity The new capacity to resize the buffer to
72
- * @return true if capacity was resized, false if reallocation failed
73
- */
74
- bool buffer_resize(buffer_T* buffer, const size_t new_capacity) {
75
- if (new_capacity + 1 >= SIZE_MAX) {
76
- fprintf(stderr, "Error: Buffer capacity would overflow system limits.\n");
77
- exit(1);
78
- }
79
-
80
- char* new_value = nullable_safe_realloc(buffer->value, new_capacity + 1);
81
-
82
- if (unlikely(new_value == NULL)) {
83
- fprintf(stderr, "Error: Failed to resize buffer to %zu.\n", new_capacity);
84
- exit(1);
85
- }
86
-
87
- buffer->value = new_value;
88
- buffer->capacity = new_capacity;
89
-
90
- return true;
91
- }
92
-
93
- /**
94
- * Expands the capacity of the buffer by doubling its current capacity.
95
- * This function is a convenience function that calls buffer_increase_capacity
96
- * with a factor of 2.
97
- *
98
- * @param buffer The buffer to expand capacity for
99
- * @return true if capacity was increased, false if reallocation failed
100
- */
101
- bool buffer_expand_capacity(buffer_T* buffer) {
102
- return buffer_resize(buffer, buffer->capacity * 2);
103
- }
104
-
105
- /**
106
- * Expands the capacity of the buffer if needed to accommodate additional content.
107
- * This function is a convenience function that calls buffer_has_capacity and
108
- * buffer_expand_capacity.
109
- *
110
- * @param buffer The buffer to expand capacity for
111
- * @param required_length The additional length needed beyond current buffer capacity
112
- * @return true if capacity was increased, false if reallocation failed
113
- */
114
- bool buffer_expand_if_needed(buffer_T* buffer, const size_t required_length) {
115
- if (buffer_has_capacity(buffer, required_length)) { return true; }
116
-
117
- return buffer_resize(buffer, buffer->capacity + (required_length * 2));
118
- }
119
-
120
- /**
121
- * Appends a null-terminated string to the buffer.
122
- * @note This function requires that 'text' is a properly null-terminated string.
123
- * When reading data from files or other non-string sources, ensure the data is
124
- * null-terminated before calling this function, or use buffer_append_with_length instead.
125
- *
126
- * @param buffer The buffer to append to
127
- * @param text A null-terminated string to append
128
- * @return void
129
- */
130
- void buffer_append(buffer_T* buffer, const char* text) {
131
- if (!buffer || !text) { return; }
132
- if (text[0] == '\0') { return; }
133
-
134
- size_t text_length = strlen(text);
135
-
136
- if (!buffer_expand_if_needed(buffer, text_length)) { return; }
137
-
138
- memcpy(buffer->value + buffer->length, text, text_length);
139
- buffer->length += text_length;
140
- buffer->value[buffer->length] = '\0';
141
- }
142
-
143
- /**
144
- * Appends a string of specified length to the buffer.
145
- * Unlike buffer_append(), this function does not require the text to be
146
- * null-terminated as it uses the provided length instead of strlen().
147
- * This is particularly useful when working with data from files, network
148
- * buffers, or other non-null-terminated sources.
149
- *
150
- * @param buffer The buffer to append to
151
- * @param text The text to append (doesn't need to be null-terminated)
152
- * @param length The number of bytes to append from text
153
- * @return void
154
- */
155
- void buffer_append_with_length(buffer_T* buffer, const char* text, const size_t length) {
156
- if (!buffer || !text || length == 0) { return; }
157
- if (!buffer_expand_if_needed(buffer, length)) { return; }
158
-
159
- memcpy(buffer->value + buffer->length, text, length);
160
-
161
- buffer->length += length;
162
- buffer->value[buffer->length] = '\0';
163
- }
164
-
165
- void buffer_append_char(buffer_T* buffer, const char character) {
166
- static char string[2];
167
-
168
- string[0] = character;
169
- string[1] = '\0';
170
-
171
- buffer_append(buffer, string);
172
- }
173
-
174
- void buffer_append_repeated(buffer_T* buffer, const char character, size_t length) {
175
- if (length == 0) { return; }
176
-
177
- char* spaces = malloc(length + 1);
178
- if (!spaces) { return; }
179
-
180
- memset(spaces, character, length);
181
- spaces[length] = '\0';
182
-
183
- buffer_append(buffer, spaces);
184
-
185
- free(spaces);
186
- }
187
-
188
- void buffer_append_whitespace(buffer_T* buffer, const size_t length) {
189
- buffer_append_repeated(buffer, ' ', length);
190
- }
191
-
192
- void buffer_prepend(buffer_T* buffer, const char* text) {
193
- if (!buffer || !text) { return; }
194
- if (text[0] == '\0') { return; }
195
-
196
- size_t text_length = strlen(text);
197
-
198
- if (!buffer_expand_if_needed(buffer, text_length)) { return; }
199
-
200
- memmove(buffer->value + text_length, buffer->value, buffer->length + 1);
201
- memcpy(buffer->value, text, text_length);
202
-
203
- buffer->length += text_length;
204
- }
205
-
206
- void buffer_concat(buffer_T* destination, buffer_T* source) {
207
- if (source->length == 0) { return; }
208
- if (!buffer_expand_if_needed(destination, source->length)) { return; }
209
-
210
- memcpy(destination->value + destination->length, source->value, source->length);
211
-
212
- destination->length += source->length;
213
- destination->value[destination->length] = '\0';
214
- }
215
-
216
- bool buffer_has_capacity(buffer_T* buffer, const size_t required_length) {
217
- return (buffer->length + required_length <= buffer->capacity);
218
- }
219
-
220
- void buffer_clear(buffer_T* buffer) {
221
- buffer->length = 0;
222
- buffer->value[0] = '\0';
223
- }
224
-
225
- void buffer_free(buffer_T* buffer) {
226
- if (!buffer) { return; }
227
-
228
- if (buffer->value != NULL) { free(buffer->value); }
229
-
230
- buffer->value = NULL;
231
- buffer->length = buffer->capacity = 0;
232
- }
data/src/include/array.h DELETED
@@ -1,33 +0,0 @@
1
- #ifndef HERB_ARRAY_H
2
- #define HERB_ARRAY_H
3
-
4
- #include <stdlib.h>
5
-
6
- typedef struct ARRAY_STRUCT {
7
- void** items;
8
- size_t size;
9
- size_t capacity;
10
- } array_T;
11
-
12
- array_T* array_init(size_t capacity);
13
-
14
- void* array_get(const array_T* array, size_t index);
15
- void* array_first(array_T* array);
16
- void* array_last(array_T* array);
17
-
18
- void array_append(array_T* array, void* item);
19
- void array_set(const array_T* array, size_t index, void* item);
20
- void array_free(array_T** array);
21
- void array_remove(array_T* array, size_t index);
22
-
23
- size_t array_index_of(array_T* array, void* item);
24
- void array_remove_item(array_T* array, void* item);
25
-
26
- void array_push(array_T* array, void* item);
27
- void* array_pop(array_T* array);
28
-
29
- size_t array_capacity(const array_T* array);
30
- size_t array_size(const array_T* array);
31
- size_t array_sizeof(void);
32
-
33
- #endif
data/src/include/buffer.h DELETED
@@ -1,39 +0,0 @@
1
- #ifndef HERB_BUFFER_H
2
- #define HERB_BUFFER_H
3
-
4
- #include <stdbool.h>
5
- #include <stdlib.h>
6
-
7
- typedef struct BUFFER_STRUCT {
8
- char* value;
9
- size_t length;
10
- size_t capacity;
11
- } buffer_T;
12
-
13
- bool buffer_init(buffer_T* buffer);
14
- buffer_T buffer_new(void);
15
-
16
- bool buffer_increase_capacity(buffer_T* buffer, size_t additional_capacity);
17
- bool buffer_has_capacity(buffer_T* buffer, size_t required_length);
18
- bool buffer_expand_capacity(buffer_T* buffer);
19
- bool buffer_expand_if_needed(buffer_T* buffer, size_t required_length);
20
- bool buffer_resize(buffer_T* buffer, size_t new_capacity);
21
-
22
- void buffer_append(buffer_T* buffer, const char* text);
23
- void buffer_append_with_length(buffer_T* buffer, const char* text, size_t length);
24
- void buffer_append_char(buffer_T* buffer, char character);
25
- void buffer_append_repeated(buffer_T* buffer, char character, size_t length);
26
- void buffer_append_whitespace(buffer_T* buffer, size_t length);
27
- void buffer_prepend(buffer_T* buffer, const char* text);
28
- void buffer_concat(buffer_T* destination, buffer_T* source);
29
-
30
- char* buffer_value(const buffer_T* buffer);
31
-
32
- size_t buffer_length(const buffer_T* buffer);
33
- size_t buffer_capacity(const buffer_T* buffer);
34
- size_t buffer_sizeof(void);
35
-
36
- void buffer_clear(buffer_T* buffer);
37
- void buffer_free(buffer_T* buffer);
38
-
39
- #endif
data/src/include/json.h DELETED
@@ -1,28 +0,0 @@
1
- #ifndef HERB_JSON_H
2
- #define HERB_JSON_H
3
-
4
- #include "buffer.h"
5
-
6
- void json_start_root_object(buffer_T* json);
7
- void json_start_root_array(buffer_T* json);
8
-
9
- void json_escape_string(buffer_T* json, const char* string);
10
-
11
- void json_add_string(buffer_T* json, const char* key, const char* value);
12
- void json_add_int(buffer_T* json, const char* key, int value);
13
- void json_add_size_t(buffer_T* json, const char* key, size_t value);
14
- void json_add_double(buffer_T* json, const char* key, double value);
15
- void json_add_bool(buffer_T* json, const char* key, int value);
16
-
17
- void json_add_raw_string(buffer_T* json, const char* string);
18
-
19
- void json_start_object(buffer_T* json, const char* key);
20
- void json_end_object(buffer_T* json);
21
-
22
- void json_start_array(buffer_T* json, const char* key);
23
- void json_end_array(buffer_T* json);
24
-
25
- void json_double_to_string(double value, char* buffer);
26
- void json_int_to_string(int value, char* buffer);
27
-
28
- #endif
data/src/include/memory.h DELETED
@@ -1,12 +0,0 @@
1
- #ifndef HERB_MEMORY_H
2
- #define HERB_MEMORY_H
3
-
4
- #include <stddef.h>
5
-
6
- void* safe_malloc(size_t size);
7
- void* safe_realloc(void* pointer, size_t new_size);
8
-
9
- void* nullable_safe_malloc(size_t size);
10
- void* nullable_safe_realloc(void* pointer, size_t new_size);
11
-
12
- #endif
data/src/json.c DELETED
@@ -1,205 +0,0 @@
1
- #include "include/json.h"
2
- #include "include/buffer.h"
3
-
4
- void json_escape_string(buffer_T* json, const char* string) {
5
- if (!string) {
6
- buffer_append(json, "null");
7
- return;
8
- }
9
-
10
- buffer_append(json, "\"");
11
-
12
- while (*string) {
13
- switch (*string) {
14
- case '\"': buffer_append(json, "\\\""); break;
15
- case '\\': buffer_append(json, "\\\\"); break;
16
- case '\n': buffer_append(json, "\\n"); break;
17
- case '\t': buffer_append(json, "\\t"); break;
18
- default: buffer_append_char(json, *string); break;
19
- }
20
- string++;
21
- }
22
-
23
- buffer_append(json, "\"");
24
- }
25
-
26
- void json_int_to_string(const int value, char* buffer) {
27
- char string[20]; // Enough to hold all possible int values
28
- int i = 0;
29
-
30
- // Handle negative numbers
31
- unsigned int abs_value = (unsigned int) abs(value);
32
-
33
- do {
34
- string[i++] = (char) ((abs_value % 10) + '0');
35
- abs_value /= 10;
36
- } while (abs_value > 0);
37
-
38
- if (value < 0) { string[i++] = '-'; }
39
-
40
- int j = 0;
41
-
42
- while (i > 0) {
43
- buffer[j++] = string[--i];
44
- }
45
-
46
- buffer[j] = '\0';
47
- }
48
-
49
- void json_double_to_string(const double value, char* buffer) {
50
- const int int_part = (int) value;
51
- const double frac_part = value - (double) int_part;
52
- const int frac_as_int = (int) (frac_part * 100); // Keep 2 decimal places
53
-
54
- char int_buffer[20];
55
- char frac_buffer[5];
56
-
57
- json_int_to_string(int_part, int_buffer);
58
- json_int_to_string(frac_as_int < 0 ? -frac_as_int : frac_as_int, frac_buffer);
59
-
60
- char* pointer = buffer;
61
- for (const char* source = int_buffer; *source != '\0'; ++source) {
62
- *pointer++ = *source;
63
- }
64
-
65
- *pointer++ = '.';
66
-
67
- for (const char* source = frac_buffer; *source != '\0'; ++source) {
68
- *pointer++ = *source;
69
- }
70
-
71
- *pointer = '\0';
72
- }
73
-
74
- void json_add_string(buffer_T* json, const char* key, const char* value) {
75
- if (!json) { return; }
76
-
77
- if (json->length > 1) { buffer_append(json, ", "); }
78
-
79
- if (key) {
80
- json_escape_string(json, key);
81
- buffer_append(json, ": ");
82
- }
83
-
84
- json_escape_string(json, value);
85
- }
86
-
87
- void json_add_double(buffer_T* json, const char* key, const double value) {
88
- if (!json) { return; }
89
-
90
- char number[32];
91
- json_double_to_string(value, number);
92
-
93
- if (json->length > 1) { buffer_append(json, ", "); }
94
-
95
- if (key) {
96
- json_escape_string(json, key);
97
- buffer_append(json, ": ");
98
- }
99
-
100
- buffer_append(json, number);
101
- }
102
-
103
- void json_add_int(buffer_T* json, const char* key, const int value) {
104
- if (!json) { return; }
105
-
106
- char number[20];
107
- json_int_to_string(value, number);
108
-
109
- if (json->length > 1) { buffer_append(json, ", "); }
110
-
111
- if (key) {
112
- json_escape_string(json, key);
113
- buffer_append(json, ": ");
114
- }
115
-
116
- buffer_append(json, number);
117
- if (json->length == 1) { buffer_append(json, " "); }
118
- }
119
-
120
- void json_add_size_t(buffer_T* json, const char* key, size_t value) {
121
- if (!json) { return; }
122
-
123
- char number[32];
124
- char temp[32];
125
- int i = 0;
126
-
127
- do {
128
- temp[i++] = (char) ((value % 10) + '0');
129
- value /= 10;
130
- } while (value > 0);
131
-
132
- int j = 0;
133
- while (i > 0) {
134
- number[j++] = temp[--i];
135
- }
136
- number[j] = '\0';
137
-
138
- if (json->length > 1) { buffer_append(json, ", "); }
139
-
140
- if (key) {
141
- json_escape_string(json, key);
142
- buffer_append(json, ": ");
143
- }
144
-
145
- buffer_append(json, number);
146
- if (json->length == 1) { buffer_append(json, " "); }
147
- }
148
-
149
- void json_add_bool(buffer_T* json, const char* key, const int value) {
150
- if (!json) { return; }
151
-
152
- if (json->length > 1) { buffer_append(json, ", "); }
153
-
154
- if (key) {
155
- json_escape_string(json, key);
156
- buffer_append(json, ": ");
157
- }
158
-
159
- buffer_append(json, value ? "true" : "false");
160
- }
161
-
162
- void json_add_raw_string(buffer_T* json, const char* string) {
163
- if (!json) { return; }
164
-
165
- if (json->length > 1) { buffer_append(json, ", "); }
166
-
167
- buffer_append(json, string);
168
- }
169
-
170
- void json_start_root_object(buffer_T* json) {
171
- if (json) { buffer_append(json, "{"); }
172
- }
173
-
174
- void json_start_object(buffer_T* json, const char* key) {
175
- if (!json) { return; }
176
-
177
- if (json->length > 1) { buffer_append(json, ", "); }
178
-
179
- if (key) {
180
- json_escape_string(json, key);
181
- buffer_append(json, ": ");
182
- }
183
-
184
- buffer_append(json, "{");
185
- }
186
-
187
- void json_end_object(buffer_T* json) {
188
- if (json) { buffer_append(json, "}"); }
189
- }
190
-
191
- void json_start_root_array(buffer_T* json) {
192
- if (json) { buffer_append(json, "["); }
193
- }
194
-
195
- void json_start_array(buffer_T* json, const char* key) {
196
- if (!json) { return; }
197
-
198
- if (json->length > 1) { buffer_append(json, ", "); }
199
- json_escape_string(json, key);
200
- buffer_append(json, ": [");
201
- }
202
-
203
- void json_end_array(buffer_T* json) {
204
- if (json) { buffer_append(json, "]"); }
205
- }
data/src/memory.c DELETED
@@ -1,53 +0,0 @@
1
- #include "memory.h"
2
-
3
- #include <stdbool.h>
4
- #include <stdio.h>
5
- #include <stdlib.h>
6
-
7
- static void* safe_malloc_internal(const size_t size, const bool fail_fast) {
8
- if (size == 0) { return NULL; }
9
-
10
- void* pointer = malloc(size);
11
-
12
- if (!pointer) {
13
- fprintf(stderr, "Error: Failed to allocate %zu bytes.\n", size);
14
- fflush(stderr);
15
- if (fail_fast) { exit(1); }
16
- return NULL;
17
- }
18
-
19
- return pointer;
20
- }
21
-
22
- static void* safe_realloc_internal(void* pointer, const size_t new_size, const bool fail_fast) {
23
- if (new_size == 0) { return NULL; }
24
-
25
- if (!pointer) { return safe_malloc_internal(new_size, fail_fast); }
26
-
27
- void* new_pointer = realloc(pointer, new_size);
28
-
29
- if (!new_pointer) {
30
- fprintf(stderr, "Error: Memory reallocation failed (size: %zu bytes).\n", new_size);
31
- fflush(stderr);
32
- if (fail_fast) { exit(1); }
33
- return NULL;
34
- }
35
-
36
- return new_pointer;
37
- }
38
-
39
- void* safe_malloc(const size_t size) {
40
- return safe_malloc_internal(size, true);
41
- }
42
-
43
- void* nullable_safe_malloc(const size_t size) {
44
- return safe_malloc_internal(size, false);
45
- }
46
-
47
- void* safe_realloc(void* pointer, const size_t new_size) {
48
- return safe_realloc_internal(pointer, new_size, true);
49
- }
50
-
51
- void* nullable_safe_realloc(void* pointer, const size_t new_size) {
52
- return safe_realloc_internal(pointer, new_size, false);
53
- }
data/src/position.c DELETED
@@ -1,33 +0,0 @@
1
- #include "include/position.h"
2
- #include "include/memory.h"
3
-
4
- size_t position_sizeof(void) {
5
- return sizeof(position_T);
6
- }
7
-
8
- position_T* position_init(const size_t line, const size_t column) {
9
- position_T* position = safe_malloc(position_sizeof());
10
-
11
- position->line = line;
12
- position->column = column;
13
-
14
- return position;
15
- }
16
-
17
- size_t position_line(const position_T* position) {
18
- return position->line;
19
- }
20
-
21
- size_t position_column(const position_T* position) {
22
- return position->column;
23
- }
24
-
25
- position_T* position_copy(position_T* position) {
26
- if (position == NULL) { return NULL; }
27
-
28
- return position_init(position_line(position), position_column(position));
29
- }
30
-
31
- void position_free(position_T* position) {
32
- free(position);
33
- }