#include #include #include #include "token.h" #define STRING_BUFF_SIZE 1024 static char stringBuff[STRING_BUFF_SIZE]; char* printableOnly(char* input); TokenList* TokenListAdd(TokenList* current, Token* next) { if (current->token == NULL) { printf("current->token == null\n"); current->token = next; return current; } TokenList* nl = malloc(sizeof(TokenList)); nl->token = next; current->next = nl; return nl; } char* TokenString(Token* t) { //char* str = malloc(sizeof(char) * 1000); snprintf(stringBuff, 1000, "[%d:%d] Type: %s Literal: '%s'", t->line, t->column, TokenTypeString(t->type), printableOnly(t->literal) ); return stringBuff; } 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_WHITESPACE: return "TT_WHITESPACE"; case TT_NEWLINE: return "TT_NEWLINE"; case TT_WORD: return "TT_WORD"; case TT_NUMBER: return "TT_NUMBER"; } 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; }