#include "token.h"
#include "str.h"
-struct token *token_new(void)
+struct token *token_new(const token_type_t type, const int line, const int col)
{
struct token *tok;
if(tok) {
memset(tok, 0, sizeof(*tok));
+
+ tok->type = type;
+ tok->line = line;
+ tok->column = col;
}
return(tok);
}
-struct token *token_new2(const int line, const int col, const char *val)
+struct token *token_new_from_str(const token_type_t type, const int line, const int col, const char *val)
{
struct token *tok;
- tok = token_new();
+ tok = token_new(type, line, col);
if(tok) {
- token_setpos(tok, line, col);
assert(token_setvalue(tok, val) == 0);
}
return(tok);
}
-struct token *token_new_from_char(const int line, const int col, const char chr)
+struct token *token_new_from_char(const token_type_t type, const int line, const int col, const char chr)
{
struct token *tok;
- tok = token_new();
+ tok = token_new(type, line, col);
if(tok) {
char val[2];
val[0] = chr;
val[1] = 0;
- token_setpos(tok, line, col);
assert(token_setvalue(tok, val) == 0);
}
return;
}
-void token_setpos(struct token *tok, int line, int col)
+int token_setvalue(struct token *tok, const char *str)
{
- tok->line = line;
- tok->column = col;
- return;
+ int ret_val;
+
+ if(!tok->value) {
+ tok->value = str_new();
+ }
+
+ if(tok->value) {
+ ret_val = str_set(tok->value, str);
+ } else {
+ ret_val = -ENOMEM;
+ }
+
+ return(ret_val);
}
-int token_setvalue(struct token *tok, const char *str)
+int token_append_str(struct token *tok, const char *str)
{
int ret_val;
}
if(tok->value) {
- ret_val = str_set(tok->value, str);
+ ret_val = str_appends(tok->value, str);
+ } else {
+ ret_val = -ENOMEM;
+ }
+
+ return(ret_val);
+}
+
+int token_append_char(struct token *tok, const char chr)
+{
+ int ret_val;
+
+ if(!tok->value) {
+ tok->value = str_new();
+ }
+
+ if(tok->value) {
+ ret_val = str_appendc(tok->value, chr);
} else {
ret_val = -ENOMEM;
}
#include "str.h"
+typedef enum {
+ TOKEN_INVALID = 0,
+ TOKEN_ADD,
+ TOKEN_AND,
+ TOKEN_ASSIGN,
+ TOKEN_ASSIGN_ADD,
+ TOKEN_ASSIGN_AND,
+ TOKEN_ASSIGN_MOD,
+ TOKEN_ASSIGN_MUL,
+ TOKEN_ASSIGN_OR,
+ TOKEN_ASSIGN_SHL,
+ TOKEN_ASSIGN_SHR,
+ TOKEN_ASSIGN_SUB,
+ TOKEN_ASSIGN_XOR,
+ TOKEN_CHAR,
+ TOKEN_COLON,
+ TOKEN_COMMA,
+ TOKEN_COMMENT,
+ TOKEN_COMPLEMENT,
+ TOKEN_DEC,
+ TOKEN_DIV,
+ TOKEN_DIV_ASSIGN,
+ TOKEN_DMEMBER,
+ TOKEN_EQ,
+ TOKEN_GE,
+ TOKEN_GT,
+ TOKEN_IDENTIFIER,
+ TOKEN_INC,
+ TOKEN_LAND,
+ TOKEN_LBRACE,
+ TOKEN_LBRACKET,
+ TOKEN_LE,
+ TOKEN_LOR,
+ TOKEN_LPAREN,
+ TOKEN_LT,
+ TOKEN_MEMBER,
+ TOKEN_MOD,
+ TOKEN_MUL,
+ TOKEN_NEQ,
+ TOKEN_NOT,
+ TOKEN_NUMBER,
+ TOKEN_OR,
+ TOKEN_QMARK,
+ TOKEN_RBRACE,
+ TOKEN_RBRACKET,
+ TOKEN_RPAREN,
+ TOKEN_SEMICOLON,
+ TOKEN_SHL,
+ TOKEN_SHR,
+ TOKEN_STRING,
+ TOKEN_SUB
+} token_type_t;
+
struct token {
- int line;
- int column;
- str_t *value;
- size_t len;
+ int line;
+ int column;
+ token_type_t type;
+ str_t *value;
+ size_t len;
};
-struct token *token_new2(const int, const int, const char*);
-struct token *token_new(void);
-struct token *token_new_from_char(const int, const int, const char);
+struct token *token_new_from_str(const token_type_t, const int, const int, const char*);
+struct token *token_new_from_char(const token_type_t, const int, const int, const char);
+
void token_free(struct token*);
-void token_setpos(struct token*, const int, const int);
int token_setvalue(struct token*, const char*);
+int token_append_str(struct token*, const char*);
+int token_append_char(struct token*, const char);
#endif /* TOKEN_H */