#include "parser.h" #include #include Module* parser_parse(TokenStream* ts) { Token t = tokenstream_next(ts); if (t.token != TOKEN_MODULE) { return NULL; } t = tokenstream_next(ts); if (t.token != TOKEN_IDENTIFIER) { return NULL; } Module* module = (Module*)malloc(sizeof(Module)); if (module == NULL) return NULL; module->name = (char*)malloc(t.text.length + 1); if (module->name == NULL) { free(module); return NULL; } memcpy(module->name, t.text.data, t.text.length); module->name[t.text.length] = '\0'; t = tokenstream_next(ts); if (t.token != TOKEN_SEMICOLON) { free(module->name); free(module); return NULL; } return module; } void parser_free(Module* module) { if (module == NULL) return; free(module->name); free(module); }