#include "data.h"
#ifndef PSI_CPP_DEBUG
-# define PSI_CPP_DEBUG 0
+# define PSI_CPP_DEBUG 1
#endif
struct psi_cpp_data {
fprintf(stderr, "PSI: CPP del_range -> index=%zu, offset=%zu, num_eles=%zu\n", cpp->index, offset, num_eles);
#endif
- deleted = psi_plist_del_range(cpp->tokens, offset, num_eles, (void *) ptr);
+ deleted = psi_plist_del_r(cpp->tokens, offset, num_eles, (void *) ptr);
if (deleted) {
if (cpp->index >= psi_plist_count(cpp->tokens)) {
bool psi_cpp_tokiter_ins_range(struct psi_cpp_data *cpp, size_t offset,
size_t num_eles, void **eles)
{
- struct psi_plist *tokens = psi_plist_ins_range(cpp->tokens, offset,
+ struct psi_plist *tokens = psi_plist_ins_r(cpp->tokens, offset,
num_eles, eles);
#if PSI_CPP_DEBUG
void psi_cpp_tokiter_expand_tokens(struct psi_cpp_data *cpp, struct psi_plist *tokens)
{
if (tokens && psi_plist_count(tokens)) {
- size_t i;
- struct psi_token **exp_tokens = calloc(psi_plist_count(tokens), sizeof(*exp_tokens));
+ size_t i = 0, n = 0;
+ bool stringify = false, paste = false;
+ struct psi_token *tok, **exp_tokens = calloc(psi_plist_count(tokens), sizeof(*exp_tokens));
- for (i = 0; psi_plist_get(tokens, i, &exp_tokens[i]); ++i) {
- exp_tokens[i] = psi_token_copy(exp_tokens[i]);
+ while (psi_plist_get(tokens, i++, &tok)) {
+ struct psi_token *new_tok;
+
+ if (tok->type == PSI_T_HASH) {
+ if (stringify) {
+ stringify = false;
+ paste = true;
+ } else {
+ stringify = true;
+ }
+ continue;
+ }
+
+ if (paste && n > 0 && exp_tokens[n - 1] &&
+ (new_tok = psi_token_cat(NULL, 2, exp_tokens[n - 1], tok))) {
+ free(exp_tokens[n - 1]);
+ exp_tokens[n - 1] = new_tok;
+ } else {
+ struct psi_token *cpy = psi_token_copy(tok);
+
+ if (stringify) {
+ cpy = psi_token_append(NULL,
+ psi_token_prepend(NULL, cpy, 1, "\""), 1, "\"");
+ cpy->type = PSI_T_QUOTED_STRING;
+ }
+ exp_tokens[n++] = cpy;
+ }
#if PSI_CPP_DEBUG
fprintf(stderr, "PSI: CPP expand > ");
- psi_token_dump(2, exp_tokens[i]);
+ psi_token_dump(2, tok);
#endif
+ paste = false;
+ stringify = false;
}
- psi_cpp_tokiter_ins_range(cpp, psi_cpp_tokiter_index(cpp), i, (void *) exp_tokens);
+ psi_cpp_tokiter_ins_range(cpp, psi_cpp_tokiter_index(cpp), n, (void *) exp_tokens);
free(exp_tokens);
}
}
{
size_t i;
struct psi_token *tok;
+ struct psi_plist *tokens = psi_plist_init(NULL);
for (i = 0; psi_plist_get(macro->tokens, i, &tok); ++i) {
struct psi_plist *arg_tokens = NULL;
}
if (arg_tokens) {
- size_t tok_count = psi_plist_count(arg_tokens);
-
- if (tok_count) {
- psi_cpp_tokiter_expand_tokens(cpp, arg_tokens);
- psi_cpp_tokiter_seek(cpp, psi_cpp_tokiter_index(cpp) + tok_count);
- }
+ tokens = psi_plist_add_r(tokens, psi_plist_count(arg_tokens), psi_plist_eles(arg_tokens));
} else {
- psi_cpp_tokiter_ins_cur(cpp, psi_token_copy(tok));
- psi_cpp_tokiter_next(cpp);
+ tokens = psi_plist_add(tokens, &tok);
}
}
+
+ psi_cpp_tokiter_expand_tokens(cpp, tokens);
+ psi_cpp_tokiter_seek(cpp, psi_cpp_tokiter_index(cpp) + psi_plist_count(tokens));
+ psi_plist_free(tokens);
+
}
static bool psi_cpp_tokiter_expand_call(struct psi_cpp_data *cpp,
} else {
char digest[17];
psi_token_hash(yymsp[-1].minor.yy0, digest);
- yylhsminor.yy0 = psi_token_translit(psi_token_append(yymsp[-1].minor.yy0, 1, digest), " ", "@");
+ yylhsminor.yy0 = psi_token_append("@", yymsp[-1].minor.yy0, 1, digest);
}
}
#line 2445 "src/parser_proc.c"
#line 712 "src/parser_proc.y"
{
if (yymsp[0].minor.yy0) {
- yylhsminor.yy0 = psi_token_cat(2, yymsp[-1].minor.yy0, yymsp[0].minor.yy0);
+ yylhsminor.yy0 = psi_token_cat(" ", 2, yymsp[-1].minor.yy0, yymsp[0].minor.yy0);
free(yymsp[-1].minor.yy0);
free(yymsp[0].minor.yy0);
} else {
case 105: /* decl_type ::= SIGNED decl_scalar_type */ yytestcase(yyruleno==105);
#line 760 "src/parser_proc.y"
{
- struct psi_token *T = psi_token_cat(2, yymsp[-1].minor.yy0, yymsp[0].minor.yy0);
+ struct psi_token *T = psi_token_cat(" ", 2, yymsp[-1].minor.yy0, yymsp[0].minor.yy0);
yylhsminor.yy172 = psi_decl_type_init(T->type, T->text);
yylhsminor.yy172->token = T;
free(yymsp[-1].minor.yy0);
} else {
char digest[17];
psi_token_hash(E, digest);
- n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
+ n = psi_token_append("@", E, 1, digest);
}
}
struct_name(n) ::= STRUCT(S) optional_name(N). {
} else {
char digest[17];
psi_token_hash(S, digest);
- n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
+ n = psi_token_append("@", S, 1, digest);
}
}
union_name(n) ::= UNION(U) optional_name(N). {
} else {
char digest[17];
psi_token_hash(U, digest);
- n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
+ n = psi_token_append("@", U, 1, digest);
}
}
decl_enum(e) ::= enum_name(N) LBRACE decl_enum_items(list) RBRACE. {
}
decl_scalar_type(type_) ::= SHORT(S) decl_scalar_type_short(s). {
if (s) {
- type_ = psi_token_cat(2, S, s);
+ type_ = psi_token_cat(" ", 2, S, s);
free(S);
free(s);
} else {
}
decl_scalar_type(type_) ::= LONG(L) decl_scalar_type_long(l). {
if (l) {
- type_ = psi_token_cat(2, L, l);
+ type_ = psi_token_cat(" ", 2, L, l);
free(L);
free(l);
} else {
}
decl_scalar_type_long(l) ::= LONG(L) decl_scalar_type_long_long(ll). {
if (ll) {
- l = psi_token_cat(2, L, ll);
+ l = psi_token_cat(" ", 2, L, ll);
free(L);
free(ll);
} else {
ll = I;
}
decl_type(type_) ::= UNSIGNED(U) decl_scalar_type(N). {
- struct psi_token *T = psi_token_cat(2, U, N);
+ struct psi_token *T = psi_token_cat(" ", 2, U, N);
type_ = psi_decl_type_init(T->type, T->text);
type_->token = T;
free(U);
free(N);
}
decl_type(type_) ::= SIGNED(S) decl_scalar_type(N). {
- struct psi_token *T = psi_token_cat(2, S, N);
+ struct psi_token *T = psi_token_cat(" ", 2, S, N);
type_ = psi_decl_type_init(T->type, T->text);
type_->token = T;
free(S);
char digest[17];
psi_token_hash(E, digest);
- n = psi_token_translit(psi_token_append(E, 1, digest), " ", "@");
+ n = psi_token_append("@", E, 1, digest);
}
}
char digest[17];
psi_token_hash(S, digest);
- n = psi_token_translit(psi_token_append(S, 1, digest), " ", "@");
+ n = psi_token_append("@", S, 1, digest);
}
}
char digest[17];
psi_token_hash(U, digest);
- n = psi_token_translit(psi_token_append(U, 1, digest), " ", "@");
+ n = psi_token_append("@", U, 1, digest);
}
}
NAMED(SHORT, S)
NAMED(decl_scalar_type_short, s)) {
if (s) {
- type_ = psi_token_cat(2, S, s);
+ type_ = psi_token_cat(" ", 2, S, s);
free(S);
free(s);
} else {
NAMED(LONG, L)
NAMED(decl_scalar_type_long, l)) {
if (l) {
- type_ = psi_token_cat(2, L, l);
+ type_ = psi_token_cat(" ", 2, L, l);
free(L);
free(l);
} else {
NAMED(LONG, L)
NAMED(decl_scalar_type_long_long, ll)) {
if (ll) {
- l = psi_token_cat(2, L, ll);
+ l = psi_token_cat(" ", 2, L, ll);
free(L);
free(ll);
} else {
PARSE_TYPED(decl_type, type_,
NAMED(UNSIGNED, U)
NAMED(decl_scalar_type, N)) {
- struct psi_token *T = psi_token_cat(2, U, N);
+ struct psi_token *T = psi_token_cat(" ", 2, U, N);
type_ = psi_decl_type_init(T->type, T->text);
type_->token = T;
free(U);
PARSE_TYPED(decl_type, type_,
NAMED(SIGNED, S)
NAMED(decl_scalar_type, N)) {
- struct psi_token *T = psi_token_cat(2, S, N);
+ struct psi_token *T = psi_token_cat(" ", 2, S, N);
type_ = psi_decl_type_init(T->type, T->text);
type_->token = T;
free(S);
return list;
}
+struct psi_plist *psi_plist_add_r(struct psi_plist *list, size_t num_eles, void **eles) {
+ if (list && list->count) {
+ list = realloc(list, sizeof(*list) + list->size + (num_eles + list->count) * list->size);
+ }
+ if (list) {
+ memcpy(PLIST_ELE(list, list->count), eles, num_eles * list->size);
+ list->count += num_eles;
+ }
+ return list;
+}
+
bool psi_plist_get(struct psi_plist *list, size_t index, void *ptr) {
if (list && list->count > index) {
PLIST_CPY(list, ptr, PLIST_ELE(list, index));
return false;
}
-bool psi_plist_del_range(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles) {
+bool psi_plist_del_r(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles) {
if (list) {
size_t offset_end = offset_start + num_eles - 1;
return list;
}
-struct psi_plist *psi_plist_ins_range(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles) {
+struct psi_plist *psi_plist_ins_r(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles) {
size_t new_count = MAX(offset_start, list->count) + num_eles;
if (list && new_count) {
void **psi_plist_eles(struct psi_plist *list);
struct psi_plist *psi_plist_add(struct psi_plist *list, void *ptr);
+struct psi_plist *psi_plist_add_r(struct psi_plist *list, size_t num_eles, void **eles);
bool psi_plist_get(struct psi_plist *list, size_t index, void *ptr);
bool psi_plist_del(struct psi_plist *list, size_t index, void *ptr);
-bool psi_plist_del_range(struct psi_plist *list, size_t offset_start, size_t offset_end, void **eles);
+bool psi_plist_del_r(struct psi_plist *list, size_t offset_start, size_t offset_end, void **eles);
struct psi_plist *psi_plist_ins(struct psi_plist *list, size_t index, void *ptr);
-struct psi_plist *psi_plist_ins_range(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles);
+struct psi_plist *psi_plist_ins_r(struct psi_plist *list, size_t offset_start, size_t num_eles, void **eles);
bool psi_plist_shift(struct psi_plist *list, void *ptr);
bool psi_plist_pop(struct psi_plist *list, void *ptr);
*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);
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;
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);
+ memmove(&T->buf[T->size + 1], &T->buf[token_len + sep_len], fname_len + 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;
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 + 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);
+ memmove(&T->buf[str_len + sep_len], &T->buf[0], T->size + 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 + sep_len, fname_len));
+ T->text = &T->buf[0];
+ T->file = &T->buf[T->size + 1];
+ memmove(&T->buf[T->size + 1], &T->buf[token_len + sep_len], fname_len + 1);
+ memcpy(&T->buf[token_len], sep, sep_len);
+ memcpy(&T->buf[token_len + sep_len], str, str_len + 1);
}
va_end(argv);
size_t psi_token_alloc_size(size_t token_len, size_t fname_len);
struct psi_token *psi_token_copy(struct psi_token *src);
void psi_token_copy_ctor(struct psi_token **src);
-struct psi_token *psi_token_cat(unsigned argc, ...);
-struct psi_token *psi_token_append(struct psi_token *T, unsigned argc, ...);
+struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...);
+struct psi_token *psi_token_prepend(const char *sep, struct psi_token *T, unsigned argc, ...);
+struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...);
struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to);
uint64_t psi_token_hash(struct psi_token *t, char *digest_buf);
void psi_token_dump(int fd, struct psi_token *t);
}
dprintf(fd, ")");
}
+
+ if (macro->exp) {
+ dprintf(fd, " ");
+ psi_num_exp_dump(fd, macro->exp);
+ } else if (macro->tokens) {
+ size_t i = 0;
+ struct psi_token *tok;
+
+ while (psi_plist_get(macro->tokens, i++, &tok)) {
+ dprintf(fd, " %s", tok->text);
+ }
+ }
}