cpp
[m6w6/ext-psi] / src / token.c
index 4860afd3a43cae9276ca4ec6ed7e96fc46bbd2e8..ec8c4e93ca764c182ad38e73d9be1fa9a1cb1e2f 100644 (file)
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/
 
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#else
-# include "php_config.h"
-#endif
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <inttypes.h>
-#include <sys/param.h>
-
-#include "parser_proc.h"
+#include "php_psi_stdinc.h"
+
+#include <ctype.h>
+
 #include "token.h"
 #include "parser.h"
 
@@ -54,7 +45,7 @@ struct psi_token *psi_token_alloc(struct psi_parser *P) {
 
        token_typ = P->num;
        token_len = P->cur - P->tok;
-       fname_len = strlen(P->psi.file.fn);
+       fname_len = strlen(P->file.fn);
 
        T = calloc(1, psi_token_alloc_size(token_len, fname_len));
        T->type = token_typ;
@@ -65,11 +56,20 @@ struct psi_token *psi_token_alloc(struct psi_parser *P) {
        T->col = P->col;
 
        memcpy(T->text, P->tok, token_len);
-       memcpy(T->file, P->psi.file.fn, fname_len);
+       memcpy(T->file, P->file.fn, fname_len);
 
        return T;
 }
 
+void psi_token_free(struct psi_token **token_ptr) {
+       if (*token_ptr) {
+               struct psi_token *token = *token_ptr;
+
+               *token_ptr = NULL;
+               free(token);
+       }
+}
+
 struct psi_token *psi_token_copy(struct psi_token *src) {
        size_t strct_len = psi_token_alloc_size(src->size, strlen(src->file));
        struct psi_token *ptr = malloc(strct_len);
@@ -82,6 +82,10 @@ struct psi_token *psi_token_copy(struct psi_token *src) {
        return ptr;
 }
 
+void psi_token_copy_ctor(struct psi_token **tok) {
+       *tok = psi_token_copy(*tok);
+}
+
 struct psi_token *psi_token_cat(unsigned argc, ...) {
        va_list argv;
        unsigned i;
@@ -93,8 +97,16 @@ 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));
+
+                       if (tmp) {
+                               T = tmp;
+                       } else {
+                               free(T);
+                               va_end(argv);
+                               return NULL;
+                       }
 
-                       T = realloc(T, psi_token_alloc_size(T->size += arg->size + 1, fname_len));
                        T->text = &T->buf[0];
                        T->file = &T->buf[T->size + 1];
                        T->buf[token_len] = ' ';
@@ -139,29 +151,75 @@ struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to)
 
 static inline uint64_t psi_hash(char *digest_buf, ...)
 {
-    uint64_t hash = 5381;
-    uint8_t c;
-    const uint8_t *ptr;
-    va_list argv;
+       uint64_t hash = 5381;
+       uint8_t c;
+       const uint8_t *ptr;
+       va_list argv;
 
-    va_start(argv, digest_buf);
-    while ((ptr = va_arg(argv, const uint8_t *))) {
+       va_start(argv, digest_buf);
+       while ((ptr = va_arg(argv, const uint8_t *))) {
                while ((c = *ptr++)) {
                        hash = ((hash << 5) + hash) + c;
                }
-    }
-    va_end(argv);
+       }
+       va_end(argv);
 
-    if (digest_buf) {
-       sprintf(digest_buf, "%" PRIx64, hash);
-    }
+       if (digest_buf) {
+               sprintf(digest_buf, "%" PRIx64, hash);
+       }
 
-    return hash;
+       return hash;
 }
 
 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf) {
        char loc_buf[48];
 
        sprintf(loc_buf, "%u%u", t->line, t->col);
-       return psi_hash(digest_buf, t->file, loc_buf, NULL);
+       return psi_hash(digest_buf, t->file, loc_buf, (char *) NULL);
+}
+
+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]);
+                       }
+                       break;
+               }
+       }
+       dprintf(fd, "\" at col %u in %s on line %u\n", t->col, t->file, t->line);
 }