travis trusty
[m6w6/ext-psi] / src / token.c
1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #else
29 # include "php_config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <inttypes.h>
36 #include <sys/param.h>
37
38 #include "parser_proc.h"
39 #include "token.h"
40 #include "parser.h"
41
42 size_t psi_token_alloc_size(size_t token_len, size_t fname_len) {
43 return sizeof(struct psi_token) + token_len + fname_len + 2;
44 }
45
46 struct psi_token *psi_token_alloc(struct psi_parser *P) {
47 struct psi_token *T;
48 size_t token_len, fname_len;
49 token_t token_typ;
50
51 if (P->cur < P->tok) {
52 return NULL;
53 }
54
55 token_typ = P->num;
56 token_len = P->cur - P->tok;
57 fname_len = strlen(P->psi.file.fn);
58
59 T = calloc(1, psi_token_alloc_size(token_len, fname_len));
60 T->type = token_typ;
61 T->size = token_len;
62 T->text = &T->buf[0];
63 T->file = &T->buf[token_len + 1];
64 T->line = P->line;
65 T->col = P->col;
66
67 memcpy(T->text, P->tok, token_len);
68 memcpy(T->file, P->psi.file.fn, fname_len);
69
70 return T;
71 }
72
73 struct psi_token *psi_token_copy(struct psi_token *src) {
74 size_t strct_len = psi_token_alloc_size(src->size, strlen(src->file));
75 struct psi_token *ptr = malloc(strct_len);
76
77 memcpy(ptr, src, strct_len);
78
79 ptr->text = &ptr->buf[0];
80 ptr->file = &ptr->buf[ptr->size + 1];
81
82 return ptr;
83 }
84
85 struct psi_token *psi_token_cat(unsigned argc, ...) {
86 va_list argv;
87 unsigned i;
88 struct psi_token *T = NULL;
89
90 va_start(argv, argc);
91 for (i = 0; i < argc; ++i) {
92 struct psi_token *arg = va_arg(argv, struct psi_token *);
93
94 if (T) {
95 size_t token_len = T->size, fname_len = strlen(T->file);
96
97 T = realloc(T, psi_token_alloc_size(T->size += arg->size + 1, fname_len));
98 T->text = &T->buf[0];
99 T->file = &T->buf[T->size + 1];
100 T->buf[token_len] = ' ';
101 memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1);
102 memcpy(&T->buf[token_len + 1], arg->text, arg->size + 1);
103 } else {
104 T = psi_token_copy(arg);
105 T->type = PSI_T_NAME;
106 }
107 }
108 va_end(argv);
109
110 return T;
111 }
112
113 struct psi_token *psi_token_append(struct psi_token *T, unsigned argc, ...) {
114 va_list argv;
115 unsigned i;
116
117 va_start(argv, argc);
118 for (i = 0; i < argc; ++i) {
119 char *str = va_arg(argv, char *);
120 size_t str_len = strlen(str), token_len = T->size, fname_len = strlen(T->file);
121
122 T = realloc(T, psi_token_alloc_size(T->size += str_len + 1, fname_len));
123 T->text = &T->buf[0];
124 T->file = &T->buf[T->size + 1];
125 T->buf[token_len] = ' ';
126 memmove(&T->buf[T->size + 1], &T->buf[token_len + 1], fname_len + 1);
127 memcpy(&T->buf[token_len + 1], str, str_len + 1);
128 }
129 va_end(argv);
130
131 return T;
132 }
133
134 char *php_strtr(char *str, size_t len, char *str_from, char *str_to, size_t trlen);
135 struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to) {
136 php_strtr(T->text, T->size, from, to, MIN(strlen(from), strlen(to)));
137 return T;
138 }
139
140 static inline uint64_t psi_hash(char *digest_buf, ...)
141 {
142 uint64_t hash = 5381;
143 uint8_t c;
144 const uint8_t *ptr;
145 va_list argv;
146
147 va_start(argv, digest_buf);
148 while ((ptr = va_arg(argv, const uint8_t *))) {
149 while ((c = *ptr++)) {
150 hash = ((hash << 5) + hash) + c;
151 }
152 }
153 va_end(argv);
154
155 if (digest_buf) {
156 sprintf(digest_buf, "%" PRIx64, hash);
157 }
158
159 return hash;
160 }
161
162 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf) {
163 char loc_buf[48];
164
165 sprintf(loc_buf, "%u%u", t->line, t->col);
166 return psi_hash(digest_buf, t->file, loc_buf, NULL);
167 }