aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lexer.c')
-rw-r--r--src/lexer.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/src/lexer.c b/src/lexer.c
index 407be25..b8897da 100644
--- a/src/lexer.c
+++ b/src/lexer.c
@@ -120,6 +120,43 @@ int token_value_string(struct token *token, size_t buf_sz, char *buf)
return 0;
}
+#define STR_ALLOC_COPY(dest, str) do { \
+ size_t len = strlen(str) + 1; \
+ dest = malloc(len); \
+ memcpy((dest), (str), len); \
+ } while(0)
+
+void token_clone(struct token *dest, struct token *src)
+{
+ dest->type = src->type;
+
+ switch(src->type) {
+ case TOKEN_ID:
+ STR_ALLOC_COPY(dest->value.id, src->value.id);
+ return;
+ case TOKEN_STR:
+ STR_ALLOC_COPY(dest->value.str, src->value.str);
+ return;
+ case TOKEN_INT:
+ dest->value.num = src->value.num;
+ return;
+ default: return;
+ }
+}
+
+void token_dealloc(struct token *token)
+{
+ switch(token->type) {
+ case TOKEN_ID:
+ free(token->value.id);
+ return;
+ case TOKEN_STR:
+ free(token->value.str);
+ return;
+ default: return;
+ }
+}
+
static int on_separator(lexer_t lexer, enum token_type type)
{
if(lexer->acc_idx > 0) return acc_empty(lexer);