1 /*******************************************************************************
2 Copyright (c) 2016, Michael Wallner <mike@php.net>.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
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.
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 *******************************************************************************/
29 # include "php_config.h"
36 #include <Zend/zend_smart_str.h>
40 struct psi_parser
*psi_parser_init(struct psi_parser
*P
, psi_error_cb error
,
44 P
= pemalloc(sizeof(*P
), 1);
46 memset(P
, 0, sizeof(*P
));
48 psi_data_ctor_with_dtors(PSI_DATA(P
), error
, flags
);
50 P
->preproc
= psi_cpp_init(P
);
55 struct psi_parser_input
*psi_parser_open_file(struct psi_parser
*P
,
56 const char *filename
, bool report_errors
)
60 struct psi_parser_input
*fb
;
62 if (stat(filename
, &sb
)) {
64 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
65 "Could not stat '%s': %s",
66 filename
, strerror(errno
));
71 if (!(fb
= pemalloc(sizeof(*fb
) + sb
.st_size
+ psi_parser_maxfill(), 1))) {
73 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
74 "Could not allocate %zu bytes for reading '%s': %s",
75 sb
.st_size
+ psi_parser_maxfill(), filename
, strerror(errno
));
80 if (!(fp
= fopen(filename
, "r"))) {
83 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
84 "Could not open '%s' for reading: %s",
85 filename
, strerror(errno
));
90 if (sb
.st_size
!= fread(fb
->buffer
, 1, sb
.st_size
, fp
)) {
94 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
95 "Could not read %zu bytes from '%s': %s",
96 sb
.st_size
+ psi_parser_maxfill(), filename
, strerror(errno
));
102 memset(fb
->buffer
+ sb
.st_size
, 0, psi_parser_maxfill());
104 fb
->length
= sb
.st_size
;
105 fb
->file
= psi_string_init_interned(filename
, strlen(filename
), 1);
110 struct psi_parser_input
*psi_parser_open_string(struct psi_parser
*P
,
111 const char *string
, size_t length
)
113 struct psi_parser_input
*sb
;
115 if (!(sb
= pemalloc(sizeof(*sb
) + length
+ psi_parser_maxfill(), 1))) {
116 P
->error(PSI_DATA(P
), NULL
, PSI_WARNING
,
117 "Could not allocate %zu bytes: %s",
118 length
+ psi_parser_maxfill(), strerror(errno
));
122 memcpy(sb
->buffer
, string
, length
);
123 memset(sb
->buffer
+ length
, 0, psi_parser_maxfill());
126 sb
->file
= psi_string_init_interned("<stdin>", strlen("<stdin>"), 1);
131 struct psi_plist
*psi_parser_preprocess(struct psi_parser
*P
,
132 struct psi_plist
**tokens
)
134 if (psi_cpp_process(P
->preproc
, tokens
)) {
140 bool psi_parser_process(struct psi_parser
*P
, struct psi_plist
*tokens
,
143 if (psi_plist_count(tokens
)) {
144 return 0 == psi_parser_proc_parse(P
, tokens
, processed
);
149 #if PSI_THREADED_PARSER
150 static void psi_smart_str_printf(smart_str
*ss
, const char *fmt
, ...)
157 len
= vasprintf(&buf
, fmt
, argv
);
161 smart_str_appendl_ex(ss
, buf
, len
, 1);
166 # define psi_smart_str_printf smart_str_append_printf
169 static inline zend_string
*macro_to_constant(struct psi_parser
*parser
,
170 zend_string
*name
, struct psi_validate_scope
*scope
)
174 struct psi_token
*tok
;
175 #if PSI_THREADED_PARSER
181 psi_smart_str_printf(&str
, "const psi\\%s = ", name
->val
);
182 if (scope
->macro
->exp
) {
183 struct psi_dump dump
= {{.hn
= &str
},
184 .fun
= (psi_dump_cb
) psi_smart_str_printf
};
186 psi_num_exp_dump(&dump
, scope
->macro
->exp
);
188 } else while (psi_plist_get(scope
->macro
->tokens
, i
++, &tok
)) {
189 if (tok
->type
== PSI_T_QUOTED_STRING
) {
190 smart_str_appendc_ex(&str
, '"', persistent
);
192 smart_str_append_ex(&str
, tok
->text
, persistent
);
193 if (tok
->type
== PSI_T_QUOTED_STRING
) {
194 smart_str_appendc_ex(&str
, '"', persistent
);
196 smart_str_appendc_ex(&str
, ' ', persistent
);
198 smart_str_appendl_ex(&str
, ";\n", 2, persistent
);
199 return smart_str_extract(&str
);
202 void psi_parser_postprocess(struct psi_parser
*P
)
206 struct psi_validate_scope scope
= {0};
208 psi_validate_scope_ctor(&scope
);
209 scope
.cpp
= P
->preproc
;
212 P
->flags
|= PSI_SILENT
;
214 ZEND_HASH_FOREACH_STR_KEY_PTR(&P
->preproc
->defs
, name
, scope
.macro
)
217 size_t processed
= 0;
218 struct psi_plist
*scanned
, *preproc
;
219 struct psi_parser_input
*I
;
222 if (scope
.macro
->sig
) {
224 } else if (!scope
.macro
->exp
) {
225 struct psi_token
*tok
;
226 if (psi_plist_count(scope
.macro
->tokens
) != 1) {
228 } else if (!psi_plist_get(scope
.macro
->tokens
, 0, &tok
)) {
230 } else if (tok
->type
!= PSI_T_QUOTED_STRING
) {
233 } else if (!psi_num_exp_validate(PSI_DATA(P
), scope
.macro
->exp
, &scope
)) {
237 cnst
= macro_to_constant(P
, name
, &scope
);
241 //fprintf(stderr, "PARSE: %s", ZSTR_VAL(cnst));
242 I
= psi_parser_open_string(P
, ZSTR_VAL(cnst
), ZSTR_LEN(cnst
));
243 zend_string_release(cnst
);
245 if (!(scanned
= psi_parser_scan(P
, I
))) {
246 psi_parser_input_free(&I
);
249 psi_parser_input_free(&I
);
251 if (!(preproc
= psi_parser_preprocess(P
, &scanned
))) {
252 psi_plist_free(scanned
);
256 parsed
= psi_parser_process(P
, preproc
, &processed
);
257 PSI_DEBUG_PRINT(PSI_DATA(P
), "PSI: processed=%zu success=%d\n",
258 processed
, (int) parsed
);
259 psi_plist_free(preproc
);
261 ZEND_HASH_FOREACH_END();
265 psi_validate_scope_dtor(&scope
);
268 bool psi_parser_parse(struct psi_parser
*P
, struct psi_parser_input
*I
)
270 struct psi_plist
*scanned
, *preproc
;
271 size_t processed
= 0;
273 if (!(scanned
= psi_parser_scan(P
, I
))) {
277 if (!(preproc
= psi_parser_preprocess(P
, &scanned
))) {
278 psi_plist_free(scanned
);
282 if (!psi_parser_process(P
, preproc
, &processed
)) {
283 psi_plist_free(preproc
);
287 psi_parser_postprocess(P
);
289 psi_plist_free(preproc
);
293 void psi_parser_dtor(struct psi_parser
*P
)
295 psi_cpp_free(&P
->preproc
);
296 psi_data_dtor(PSI_DATA(P
));
298 memset(P
, 0, sizeof(*P
));
301 void psi_parser_free(struct psi_parser
**P
)