#include #include #include #include void Token::init(TokenType type, char *lexeme, unsigned int line) { this->type = type; this->lexeme = lexeme; this->line = line; this->string = NULL; } char * Token::to_string(void) { unsigned int line_length = snprintf(NULL, 0, "%ul", line) - 1; /* 2: ": ", 1: "\0" */ unsigned int final_size = strlen(lexeme) + line_length + 2 + 1; char *result = (char *) calloc(1, final_size); snprintf(result, final_size, "%d: %s", line, lexeme); string = result; return result; } void Token::clean(void) { if (string != NULL) free(string); }