flush
[m6w6/ext-psi] / src / parser.c
index 24028166df0c3eb310bb7185e12a6b4cc8b09d1a..2a89068e74d2ca6ed65ccf2b7d35a32c4f5c2e89 100644 (file)
  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 <sys/mman.h>
 #include <assert.h>
 #include <errno.h>
@@ -98,7 +102,7 @@ struct psi_parser_input *psi_parser_open_file(struct psi_parser *P,
        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;
 }
@@ -119,7 +123,7 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P,
        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;
 }
@@ -127,7 +131,7 @@ struct psi_parser_input *psi_parser_open_string(struct psi_parser *P,
 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;
@@ -142,31 +146,56 @@ bool psi_parser_process(struct psi_parser *P, struct psi_plist *tokens,
        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, "const psi\\%s = ", name->val);
+       psi_smart_str_printf(&str, "const psi\\%s = ", name->val);
        if (scope->macro->exp) {
                struct psi_dump dump = {{.hn = &str},
-                               .fun = (psi_dump_cb) smart_str_append_printf};
+                               .fun = (psi_dump_cb) psi_smart_str_printf};
 
                psi_num_exp_dump(&dump, scope->macro->exp);
+
        } 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);
 }
 
@@ -209,7 +238,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);