feat: handle more complex types

NOTE: doesn't handles union and arrays, considere attributes part of the type
This commit is contained in:
2026-01-25 19:49:22 +01:00
parent b6c57c2741
commit b4e080c731

43
cmeta.h
View File

@@ -78,6 +78,11 @@ void sb_append(String_Builder* sb, const char* data) {
sb->data[sb->length] = '\0';
}
void sb_append_ch(String_Builder* sb, char ch) {
char buf[2] = {ch, '\0'};
sb_append(sb, buf);
}
#define da_append(da, item) \
do { \
if ((da)->count + 1 > (da)->capacity) { \
@@ -142,6 +147,7 @@ bool parse_struct(Parsed_Struct_Info* info) {
char* name = NULL;
Parsed_Field_Infos fields = {0};
String_Builder field = {0};
if (!lexer_expect_keyword("typedef")) goto fail;
if (!lexer_expect_keyword("struct")) goto fail;
@@ -157,19 +163,36 @@ bool parse_struct(Parsed_Struct_Info* info) {
if (lexer.token == '}') break;
lexer.parse_point = mark;
if (!lexer_expect(CLEX_id, "field type")) goto fail;
char* field_type = strdup(lexer.string);
field.length = 0;
while (stb_c_lexer_get_token(&lexer) && lexer.token != ';') {
if (lexer.token <= 255) {
// TODO: parse arrays
if(lexer.token == '[') goto fail;
if (!lexer_expect(CLEX_id, "field name")) goto fail;
char* field_name = strdup(lexer.string);
sb_append_ch(&field, (char)lexer.token);
sb_append_ch(&field, ' ');
} else {
// TODO: parse unions
if(strcmp(lexer.string, "union") == 0) goto fail;
Parsed_Field_Info field = {
// TODO: parse attributes
sb_append(&field, lexer.string);
sb_append_ch(&field, ' ');
}
}
field.data[field.length - 1] = '\0';
char* last_space = strrchr(field.data, ' ');
char* field_name = strdup(last_space + 1);
field.data[last_space - field.data] = '\0';
char* field_type = strdup(field.data);
da_append(&fields, ((Parsed_Field_Info) {
.type = field_type,
.name = field_name,
};
da_append(&fields, field);
if(!lexer_expect(';', NULL)) goto fail;
}));
}
if (!lexer_expect(CLEX_id, "type name")) goto fail;
@@ -185,6 +208,8 @@ bool parse_struct(Parsed_Struct_Info* info) {
result = true;
fail:
free(field.data);
if(!result) {
free(name);
for(size_t i = 0; i < fields.count; i += 1) {