X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fparser.c;h=d133cfb475cd394bba4a3bf5953daa02ba4a57d7;hp=e747ed269babd32eb53b8da7d949b30700c76f40;hb=4bf8ff5e477da2fa4f82518bb947055c2c0374d6;hpb=a7ac1c0a3c855321f21682c127a4b707de33a303 diff --git a/src/parser.c b/src/parser.c index e747ed2..d133cfb 100644 --- a/src/parser.c +++ b/src/parser.c @@ -23,17 +23,23 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *******************************************************************************/ -#include "php_psi_stdinc.h" +#ifdef HAVE_CONFIG_H +# include "config.h" +#else +# include "php_config.h" +#endif #include #include #include #include +#include #include #include "parser.h" -struct psi_parser *psi_parser_init(struct psi_parser *P, psi_error_cb error, unsigned flags) +struct psi_parser *psi_parser_init(struct psi_parser *P, psi_error_cb error, + unsigned flags) { if (!P) { P = pemalloc(sizeof(*P), 1); @@ -47,7 +53,8 @@ struct psi_parser *psi_parser_init(struct psi_parser *P, psi_error_cb error, uns return P; } -struct psi_parser_input *psi_parser_open_file(struct psi_parser *P, const char *filename, bool report_errors) +struct psi_parser_input *psi_parser_open_file(struct psi_parser *P, + const char *filename, bool report_errors) { struct stat sb; FILE *fp; @@ -95,13 +102,15 @@ struct psi_parser_input *psi_parser_open_file(struct psi_parser *P, const char * memset(fb->buffer + sb.st_size, 0, psi_parser_maxfill()); + fb->lmod = sb.st_mtime; fb->length = sb.st_size; - fb->file = zend_string_init_interned(filename, strlen(filename), 1); + fb->file = psi_string_init_interned(filename, strlen(filename), 1); return fb; } -struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, const char *string, size_t length) +struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, + const char *string, size_t length) { struct psi_parser_input *sb; @@ -115,21 +124,24 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, const char memcpy(sb->buffer, string, length); memset(sb->buffer + length, 0, psi_parser_maxfill()); + sb->lmod = time(NULL); sb->length = length; - sb->file = zend_string_init_interned("", strlen(""), 1); + sb->file = psi_string_init_interned("", strlen(""), 1); return sb; } -struct psi_plist *psi_parser_preprocess(struct psi_parser *P, struct psi_plist **tokens) +struct psi_plist *psi_parser_preprocess(struct psi_parser *P, + struct psi_plist **tokens) { - if (psi_cpp_process(P->preproc, tokens)) { + if (psi_cpp_process(P->preproc, tokens, NULL)) { return *tokens; } return NULL; } -bool psi_parser_process(struct psi_parser *P, struct psi_plist *tokens, size_t *processed) +bool psi_parser_process(struct psi_parser *P, struct psi_plist *tokens, + size_t *processed) { if (psi_plist_count(tokens)) { return 0 == psi_parser_proc_parse(P, tokens, processed); @@ -137,35 +149,56 @@ bool psi_parser_process(struct psi_parser *P, struct psi_plist *tokens, size_t * return true; } +#if PSI_THREADED_PARSER +static void psi_smart_str_printf(smart_str *ss, const char *fmt, ...) +{ + va_list argv; + char *buf; + int len; + + va_start(argv, fmt); + len = vasprintf(&buf, fmt, argv); + va_end(argv); + + if (len != -1) { + smart_str_appendl_ex(ss, buf, len, 1); + free(buf); + } +} +#else +# define psi_smart_str_printf smart_str_append_printf +#endif + static inline zend_string *macro_to_constant(struct psi_parser *parser, zend_string *name, struct psi_validate_scope *scope) { smart_str str = {0}; - size_t i = 0; struct psi_token *tok; +#if PSI_THREADED_PARSER + int persistent = 1; +#else + int persistent = 0; +#endif - smart_str_append_printf(&str, "\nconst psi\\%s = ", name->val); + psi_smart_str_printf(&str, "const psi\\%s = ", name->val); if (scope->macro->exp) { - impl_val res = {0}; - token_t typ = psi_num_exp_exec(scope->macro->exp, &res, NULL, scope->cpp); + struct psi_dump dump = {{.hn = &str}, + .fun = (psi_dump_cb) psi_smart_str_printf}; + + psi_num_exp_dump(&dump, scope->macro->exp); - switch (typ) { - CASE_IMPLVAL_NUM_PRINTF(smart_str_append_printf, &str, res); - default: - assert(0); - } } else while (psi_plist_get(scope->macro->tokens, i++, &tok)) { if (tok->type == PSI_T_QUOTED_STRING) { - smart_str_appendc(&str, '"'); + smart_str_appendc_ex(&str, '"', persistent); } - smart_str_append(&str, tok->text); + smart_str_append_ex(&str, tok->text, persistent); if (tok->type == PSI_T_QUOTED_STRING) { - smart_str_appendc(&str, '"'); + smart_str_appendc_ex(&str, '"', persistent); } - smart_str_appendc(&str, ' '); + smart_str_appendc_ex(&str, ' ', persistent); } - smart_str_appendl(&str, ";\n", 2); + smart_str_appendl_ex(&str, ";\n", 2, persistent); return smart_str_extract(&str); } @@ -179,7 +212,7 @@ void psi_parser_postprocess(struct psi_parser *P) scope.cpp = P->preproc; flags = P->flags; - //P->flags |= PSI_SILENT; + P->flags |= PSI_SILENT; ZEND_HASH_FOREACH_STR_KEY_PTR(&P->preproc->defs, name, scope.macro) { @@ -240,16 +273,21 @@ bool psi_parser_parse(struct psi_parser *P, struct psi_parser_input *I) struct psi_plist *scanned, *preproc; size_t processed = 0; + P->input = I; + if (!(scanned = psi_parser_scan(P, I))) { + P->input = NULL; return false; } if (!(preproc = psi_parser_preprocess(P, &scanned))) { + P->input = NULL; psi_plist_free(scanned); return false; } if (!psi_parser_process(P, preproc, &processed)) { + P->input = NULL; psi_plist_free(preproc); return false; } @@ -257,6 +295,7 @@ bool psi_parser_parse(struct psi_parser *P, struct psi_parser_input *I) psi_parser_postprocess(P); psi_plist_free(preproc); + P->input = NULL; return true; }