#include #include #include #include "token.h" #define STRING_BUFF_SIZE 1024 static char stringBuff[STRING_BUFF_SIZE]; char* printableOnly(char* input); char* TokenString(Token* t) { char* printable = printableOnly(t->literal); snprintf(stringBuff, STRING_BUFF_SIZE, "[%d:%d] Type: %s Literal: '%s'", t->line, t->column, TokenTypeString(t->type), printable ); free(printable); return stringBuff; } Token* FreeToken(Token* t) { Token* next = t->next; if (t->type != TT_TRIPLEBACKTICK) free(t->literal); free(t); return next; } char* TokenTypeString(TokenType tt) { switch (tt) { case TT_ILLEGAL: return "TT_ILLEGAL"; case TT_EOF: return "TT_EOF"; case TT_HASH: return "TT_HASH"; case TT_ASTERISK: return "TT_ASTERISK"; case TT_UNDERSCORE: return "TT_UNDERSCORE"; case TT_DASH: return "TT_DASH"; case TT_PERIOD: return "TT_PERIOD"; case TT_BACKTICK: return "TT_BACKTICK"; case TT_TRIPLEBACKTICK: return "TT_TRIPLEBACKTICK"; case TT_WHITESPACE: return "TT_WHITESPACE"; case TT_NEWLINE: return "TT_NEWLINE"; case TT_WORD: return "TT_WORD"; case TT_NUMBER: return "TT_NUMBER"; case TT_GT: return "TT_GT"; } return "\0"; } char* printableOnly(char* input) { char *str = malloc(sizeof(char) * ((strlen(input)*4)+1)); int i, j; int len = strlen(input); for(i = 0, j = 0; i < len; i++, j++) { if(input[i] < 0x20 || input[i] > 0x7F) { // hex notation snprintf(&str[j], 5, "\\x%02X", input[i]); j+=3; } else { str[j] = input[i]; } } str[j] = '\0'; return str; }