fix leak
[m6w6/ext-psi] / src / token.h
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 #ifndef PSI_TOKEN_H
27 #define PSI_TOKEN_H
28
29 #include <stddef.h>
30 #include <stdarg.h>
31 #include <stdint.h>
32 #include <assert.h>
33 #include <sys/param.h>
34
35 static inline size_t psi_align(size_t s, size_t a) {
36 return ((s - 1) | (a - 1)) + 1;
37 }
38
39 static inline size_t psi_offset_padding(size_t diff, size_t alignment) {
40 if (diff && diff <= psi_align(diff, alignment)) {
41 diff = 0;
42 }
43
44 return diff;
45 }
46
47 #include "parser_proc.h"
48 #undef YYDEBUG
49
50 #define PSI_T_CAST PSI_T_EQUALS
51 #define PSI_T_POINTER PSI_T_ASTERISK
52 #define PSI_T_LONG_DOUBLE (PSI_T_DOUBLE << 16)
53 #define PSI_T_BSLASH (PSI_T_SLASH << 16)
54 #define PSI_T_WHITESPACE -PSI_T_NO_WHITESPACE
55
56 typedef int token_t;
57
58 static inline size_t psi_t_alignment(token_t t)
59 {
60 #define PSI_ALIGNOF(T) case PSI_T_## T: return ALIGNOF_## T ##_T;
61 switch (t) {
62 PSI_ALIGNOF(INT8);
63 PSI_ALIGNOF(UINT8);
64 PSI_ALIGNOF(INT16);
65 PSI_ALIGNOF(UINT16);
66 PSI_ALIGNOF(INT32);
67 PSI_ALIGNOF(UINT32);
68 PSI_ALIGNOF(INT64);
69 PSI_ALIGNOF(UINT64);
70 case PSI_T_FLOAT:
71 return ALIGNOF_FLOAT;
72 case PSI_T_DOUBLE:
73 return ALIGNOF_DOUBLE;
74 case PSI_T_POINTER:
75 case PSI_T_FUNCTION:
76 return ALIGNOF_VOID_P;
77 case PSI_T_ENUM:
78 return ALIGNOF_INT;
79 #ifdef HAVE_LONG_DOUBLE
80 case PSI_T_LONG_DOUBLE:
81 return ALIGNOF_LONG_DOUBLE;
82 #endif
83 default:
84 assert(0);
85 }
86 return 0;
87 }
88
89 static inline size_t psi_t_size(token_t t)
90 {
91 #define PSI_SIZEOF(T) case PSI_T_## T : return SIZEOF_## T ##_T;
92 switch (t) {
93 PSI_SIZEOF(INT8);
94 PSI_SIZEOF(UINT8);
95 PSI_SIZEOF(INT16);
96 PSI_SIZEOF(UINT16);
97 PSI_SIZEOF(INT32);
98 PSI_SIZEOF(UINT32);
99 PSI_SIZEOF(INT64);
100 PSI_SIZEOF(UINT64);
101 case PSI_T_FLOAT:
102 return SIZEOF_FLOAT;
103 case PSI_T_DOUBLE:
104 return SIZEOF_DOUBLE;
105 case PSI_T_VOID:
106 case PSI_T_POINTER:
107 case PSI_T_FUNCTION:
108 return SIZEOF_VOID_P;
109 case PSI_T_ENUM:
110 return SIZEOF_INT;
111 #ifdef HAVE_LONG_DOUBLE
112 case PSI_T_LONG_DOUBLE:
113 return SIZEOF_LONG_DOUBLE;
114 #endif
115 default:
116 assert(!t);
117 }
118 return 0;
119 }
120
121 static inline const char *psi_t_indent(unsigned level) {
122 static const char indent[] =
123 "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
124 return &indent[32 - MIN(32, level)];
125 }
126
127 static inline const char *psi_t_indirection(unsigned pointer_level) {
128 static const char indir[] = "********************************";
129 return &indir[32 - MIN(32, pointer_level)];
130 }
131
132 struct psi_token {
133 token_t type;
134 unsigned size, line, col, flags;
135 char *text, *file;
136 char buf[1];
137 };
138
139 struct psi_parser;
140
141 struct psi_token *psi_token_init(token_t token_typ, const char *token_txt,
142 size_t token_len, unsigned col, unsigned line, const char *file);
143 size_t psi_token_alloc_size(size_t token_len, size_t fname_len);
144 struct psi_token *psi_token_copy(struct psi_token *src);
145 void psi_token_copy_ctor(struct psi_token **src);
146 struct psi_token *psi_token_cat(const char *sep, unsigned argc, ...);
147 struct psi_token *psi_token_prepend(const char *sep, struct psi_token *T, unsigned argc, ...);
148 struct psi_token *psi_token_append(const char *sep, struct psi_token *T, unsigned argc, ...);
149 struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to);
150 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf);
151 void psi_token_dump(int fd, struct psi_token *t);
152 void psi_token_free(struct psi_token **token);
153
154 #endif