X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Ftoken.c;h=08a6802826cb70572afcae87bf6816990cf4b7ea;hp=ec8c4e93ca764c182ad38e73d9be1fa9a1cb1e2f;hb=8d1d1243a8a42b0046ac670808913b3bb06c994b;hpb=6509a2053456d0e63b6f383b757289d3016ed1a5 diff --git a/src/token.c b/src/token.c index ec8c4e9..08a6802 100644 --- a/src/token.c +++ b/src/token.c @@ -34,29 +34,22 @@ size_t psi_token_alloc_size(size_t token_len, size_t fname_len) { return sizeof(struct psi_token) + token_len + fname_len + 2; } -struct psi_token *psi_token_alloc(struct psi_parser *P) { +struct psi_token *psi_token_init(token_t token_typ, const char *token_txt, + size_t token_len, unsigned col, unsigned line, const char *file) +{ struct psi_token *T; - size_t token_len, fname_len; - token_t token_typ; - - if (P->cur < P->tok) { - return NULL; - } + size_t file_len = strlen(file); - token_typ = P->num; - token_len = P->cur - P->tok; - fname_len = strlen(P->file.fn); - - T = calloc(1, psi_token_alloc_size(token_len, fname_len)); + T = calloc(1, psi_token_alloc_size(token_len, file_len)); T->type = token_typ; T->size = token_len; T->text = &T->buf[0]; T->file = &T->buf[token_len + 1]; - T->line = P->line; - T->col = P->col; + T->line = line; + T->col = col; - memcpy(T->text, P->tok, token_len); - memcpy(T->file, P->file.fn, fname_len); + memcpy(T->text, token_txt, token_len); + memcpy(T->file, file, file_len); return T; } @@ -86,9 +79,10 @@ void psi_token_copy_ctor(struct psi_token **tok) { *tok = psi_token_copy(*tok); } -struct psi_token *psi_token_cat(unsigned argc, ...) { +struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...) { va_list argv; unsigned i; + size_t sep_len = sep ? strlen(sep) : 0; struct psi_token *T = NULL; va_start(argv, argc); @@ -97,7 +91,7 @@ struct psi_token *psi_token_cat(unsigned argc, ...) { if (T) { size_t token_len = T->size, fname_len = strlen(T->file); - struct psi_token *tmp = realloc(T, psi_token_alloc_size(T->size += arg->size + 1, fname_len)); + struct psi_token *tmp = realloc(T, psi_token_alloc_size(T->size += arg->size + sep_len, fname_len)); if (tmp) { T = tmp; @@ -109,9 +103,9 @@ struct psi_token *psi_token_cat(unsigned argc, ...) { T->text = &T->buf[0]; T->file = &T->buf[T->size + 1]; - T->buf[token_len] = ' '; memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1); - memcpy(&T->buf[token_len + 1], arg->text, arg->size + 1); + memcpy(&T->buf[token_len], sep, sep_len); + memcpy(&T->buf[token_len + sep_len], arg->text, arg->size + 1); } else { T = psi_token_copy(arg); T->type = PSI_T_NAME; @@ -122,21 +116,44 @@ struct psi_token *psi_token_cat(unsigned argc, ...) { return T; } -struct psi_token *psi_token_append(struct psi_token *T, unsigned argc, ...) { +struct psi_token *psi_token_prepend(const char *sep, struct psi_token *T, unsigned argc, ...) { + va_list argv; + unsigned i; + size_t sep_len = sep ? strlen(sep) : 0; + + va_start(argv, argc); + for (i = 0; i < argc; ++i) { + char *str = va_arg(argv, char *); + size_t str_len = strlen(str), token_len = T->size, fname_len = strlen(T->file); + + T = realloc(T, psi_token_alloc_size(T->size += str_len + sep_len, fname_len)); + T->text = &T->buf[0]; + T->file = &T->buf[T->size + 1]; + memmove(&T->buf[str_len + sep_len], &T->buf[0], token_len + 1 + fname_len + 1); + memcpy(&T->buf[0], str, str_len); + memcpy(&T->buf[str_len], sep, sep_len); + T->buf[T->size] = '\0'; + } + va_end(argv); + + return T; +} +struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...) { va_list argv; unsigned i; + size_t sep_len = sep ? strlen(sep) : 0; va_start(argv, argc); for (i = 0; i < argc; ++i) { char *str = va_arg(argv, char *); size_t str_len = strlen(str), token_len = T->size, fname_len = strlen(T->file); - T = realloc(T, psi_token_alloc_size(T->size += str_len + 1, fname_len)); + T = realloc(T, psi_token_alloc_size(T->size += str_len + sep_len, fname_len)); T->text = &T->buf[0]; T->file = &T->buf[T->size + 1]; - T->buf[token_len] = ' '; memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1); - memcpy(&T->buf[token_len + 1], str, str_len + 1); + memcpy(&T->buf[token_len], sep, sep_len); + memcpy(&T->buf[token_len + sep_len], str, str_len + 1); } va_end(argv); @@ -182,44 +199,50 @@ void psi_token_dump(int fd, struct psi_token *t) { size_t i; - dprintf(fd, "TOKEN %p (%d) \"", t, t->type); - for (i = 0; i < MIN(t->size, 16); ++i) { - switch (t->text[i]) { - case '\0': - dprintf(fd, "\\0"); - break; - case '\a': - dprintf(fd, "\\a"); - break; - case '\b': - dprintf(fd, "\\b"); - break; - case '\f': - dprintf(fd, "\\f"); - break; - case '\n': - dprintf(fd, "\\n"); - break; - case '\r': - dprintf(fd, "\\r"); - break; - case '\t': - dprintf(fd, "\\t"); - break; - case '\v': - dprintf(fd, "\\v"); - break; - case '"': - dprintf(fd, "\\\""); - break; - default: - if (isprint(t->text[i])) { - dprintf(fd, "%c", t->text[i]); - } else { - dprintf(fd, "\\%03hho", t->text[i]); + dprintf(fd, "TOKEN %p (%u) ", t, t->type); + if (t->type == PSI_T_EOF) { + dprintf(fd, "EOF"); + } else { + dprintf(fd, "\""); + for (i = 0; i < t->size; ++i) { + switch (t->text[i]) { + case '\0': + dprintf(fd, "\\0"); + break; + case '\a': + dprintf(fd, "\\a"); + break; + case '\b': + dprintf(fd, "\\b"); + break; + case '\f': + dprintf(fd, "\\f"); + break; + case '\n': + dprintf(fd, "\\n"); + break; + case '\r': + dprintf(fd, "\\r"); + break; + case '\t': + dprintf(fd, "\\t"); + break; + case '\v': + dprintf(fd, "\\v"); + break; + case '"': + dprintf(fd, "\\\""); + break; + default: + if (isprint(t->text[i])) { + dprintf(fd, "%c", t->text[i]); + } else { + dprintf(fd, "\\x%02hhX", t->text[i]); + } + break; } - break; } + dprintf(fd, "\""); } - dprintf(fd, "\" at col %u in %s on line %u\n", t->col, t->file, t->line); + dprintf(fd, " at col %u in %s on line %u\n", t->col, t->file, t->line); }