* threaded parser
[m6w6/ext-psi] / src / parser.c
index e747ed269babd32eb53b8da7d949b30700c76f40..cf06f6af7694c2ddfc1f26342cee51cb9114d550 100644 (file)
@@ -33,7 +33,8 @@
 
 #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 +48,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;
@@ -96,12 +98,13 @@ 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->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;
 
@@ -116,12 +119,13 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P, const char
        memset(sb->buffer + length, 0, psi_parser_maxfill());
 
        sb->length = length;
-       sb->file = zend_string_init_interned("<stdin>", strlen("<stdin>"), 1);
+       sb->file = psi_string_init_interned("<stdin>", strlen("<stdin>"), 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)) {
                return *tokens;
@@ -129,7 +133,8 @@ struct psi_plist *psi_parser_preprocess(struct psi_parser *P, struct psi_plist *
        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);
@@ -145,27 +150,44 @@ static inline zend_string *macro_to_constant(struct psi_parser *parser,
        size_t i = 0;
        struct psi_token *tok;
 
-       smart_str_append_printf(&str, "\nconst 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);
+#if HAVE_ASPRINTF
+       int persistent = 1;
 
-               switch (typ) {
-               CASE_IMPLVAL_NUM_PRINTF(smart_str_append_printf, &str, res);
-               default:
-                       assert(0);
-               }
+       smart_str_appendl_ex(&str, ZEND_STRL("const psi\\"), 1);
+       smart_str_append_ex(&str, name, 1);
+       smart_str_appendl_ex(&str, ZEND_STRL(" = "), 1);
+#else
+       int persistent = 0;
+
+       smart_str_append_printf(&str, "const psi\\%s = ", name->val);
+#endif
+       if (scope->macro->exp) {
+#if HAVE_ASPRINTF
+               char *astr = NULL;
+               struct psi_dump dump = {{.hn = &astr},
+                               .fun = (psi_dump_cb) asprintf};
+#else
+               struct psi_dump dump = {{.hn = &str},
+                               .fun = (psi_dump_cb) smart_str_append_printf};
+#endif
+
+               psi_num_exp_dump(&dump, scope->macro->exp);
+
+#if HAVE_ASPRINTF
+               smart_str_appends_ex(&str, astr, 1);
+               free(astr);
+#endif
        } 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 +201,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)
        {
@@ -208,7 +230,7 @@ void psi_parser_postprocess(struct psi_parser *P)
                if (!cnst) {
                        continue;
                }
-
+//fprintf(stderr, "PARSE: %s", ZSTR_VAL(cnst));
                I = psi_parser_open_string(P, ZSTR_VAL(cnst), ZSTR_LEN(cnst));
                zend_string_release(cnst);