readme-thing/token.c

99 lines
1.9 KiB
C
Raw Permalink Normal View History

2021-07-14 08:15:46 -07:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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'",
2021-07-14 08:15:46 -07:00
t->line,
t->column,
TokenTypeString(t->type),
printable
2021-07-14 08:15:46 -07:00
);
free(printable);
2021-07-14 08:15:46 -07:00
return stringBuff;
}
Token*
FreeToken(Token* t)
{
Token* next = t->next;
if (t->type != TT_TRIPLEBACKTICK)
free(t->literal);
free(t);
return next;
}
2021-07-14 08:15:46 -07:00
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";
2021-07-14 08:15:46 -07:00
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";
2021-07-14 08:15:46 -07:00
}
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;
}