* optimized inlines like htonl
* real integral type of enums
-* #pragma lib "foo" instead of lib "foo";
+* #pragma PSI lib "foo" instead of lib "foo";
if (cpp->skip) {
if (!cpp->do_cpp) {
#if PSI_CPP_DEBUG
- psi_debug_lock(PSI_DATA(cpp->parser));
- PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP skip ");
- PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current);
- psi_debug_unlock(PSI_DATA(cpp->parser));
+ PSI_DEBUG_LOCK(cpp->parser,
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP skip ");
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current);
+ );
#endif
psi_cpp_tokiter_del_cur(cpp, true);
continue;
/* leave EOLs in the input stream, else we might end up
* with a hash not preceded with a new line after include */
+ psi_cpp_tokiter_add(cpp, current);
psi_plist_pop(cpp->tokens.exec, NULL);
psi_plist_clean(cpp->tokens.exec);
}
psi_plist_free(cpp->tokens.exec);
+ cpp->tokens.exec = NULL;
return true;
}
zend_hash_del(&cpp->expanding, expanding->text);
}
- if (cpp->tokens.next) {
- free(cpp->tokens.iter);
- cpp->tokens.iter = cpp->tokens.next;
- cpp->tokens.next = NULL;
+ *tokens = cpp->tokens.next;
+ psi_plist_free(cpp->tokens.iter);
+ if (cpp->tokens.exec) {
+ assert(!psi_plist_count(cpp->tokens.exec));
+ psi_plist_free(cpp->tokens.exec);
}
- *tokens = cpp->tokens.iter;
-
- if (temp.tokens.iter) {
- cpp->tokens.iter = temp.tokens.iter;
- cpp->tokens.next = temp.tokens.next;
- cpp->tokens.exec = temp.tokens.exec;
- }
+ cpp->tokens = temp.tokens;
cpp->index = temp.index;
cpp->skip = temp.skip;
cpp->level = temp.level;
if (psi_builtin_exists(tok->text)) {
defined = true;
} else if (!zend_hash_exists(&cpp->expanding, tok->text)) {
- struct psi_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text);
-
- if (macro) {
- defined = true;
- }
+ defined = zend_hash_exists(&cpp->defs, tok->text);
}
#if PSI_CPP_DEBUG
- psi_debug_lock(PSI_DATA(cpp->parser));
- PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false");
- if (defined) {
- struct psi_cpp_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text);
- if (macro) {
- PSI_DEBUG_PRINT(cpp->parser, " @ %s:%u ", macro->token->file->val, macro->token->line);
- }
- } else {
- zend_string *key;
+ PSI_DEBUG_LOCK(cpp->parser,
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false");
+ if (defined) {
+ struct psi_cpp_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text);
+ if (macro) {
+ PSI_DEBUG_PRINT(cpp->parser, " @ %s:%u ", macro->token->file->val, macro->token->line);
+ }
+ } else {
+ zend_string *key;
- PSI_DEBUG_PRINT(cpp->parser, " expanding=");
- ZEND_HASH_FOREACH_STR_KEY(&cpp->expanding, key)
- {
- PSI_DEBUG_PRINT(cpp->parser, "%s,", key->val);
- }
- ZEND_HASH_FOREACH_END();
- PSI_DEBUG_PRINT(cpp->parser, "\t");
- }
- PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok);
- psi_debug_unlock(PSI_DATA(cpp->parser));
+ PSI_DEBUG_PRINT(cpp->parser, " expanding=");
+ ZEND_HASH_FOREACH_STR_KEY(&cpp->expanding, key)
+ {
+ PSI_DEBUG_PRINT(cpp->parser, "%s,", key->val);
+ }
+ ZEND_HASH_FOREACH_END();
+ PSI_DEBUG_PRINT(cpp->parser, "\t");
+ }
+ PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok);
+ );
#endif
}
"'%s' previously defined", old->token->text->val);
}
#if PSI_CPP_DEBUG
- psi_debug_lock(PSI_DATA(cpp->parser));
- if (decl->exp) {
- PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> ");
- } else {
- PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl -> ");
- }
- PSI_DEBUG_DUMP(cpp->parser, psi_cpp_macro_decl_dump, decl);
- PSI_DEBUG_PRINT(cpp->parser, "\n");
- psi_debug_unlock(PSI_DATA(cpp->parser));
+ PSI_DEBUG_LOCK(cpp->parser,
+ if (decl->exp) {
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> ");
+ } else {
+ PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl -> ");
+ }
+ PSI_DEBUG_DUMP(cpp->parser, psi_cpp_macro_decl_dump, decl);
+ PSI_DEBUG_PRINT(cpp->parser, "\n");
+ );
#endif
zend_hash_update_ptr(&cpp->defs, decl->token->text, decl);
}
#include "data.h"
#ifndef PSI_CPP_DEBUG
-# define PSI_CPP_DEBUG 1
+# define PSI_CPP_DEBUG 0
#endif
struct psi_cpp {
void psi_cpp_tokiter_reset(struct psi_cpp *cpp)
{
#if PSI_CPP_DEBUG
- PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP reset -> iter.count=%zu, next.count=%zu\n",
+ PSI_DEBUG_PRINT(cpp->parser,
+ "PSI: CPP reset -> iter.count=%zu, next.count=%zu, exec.count=%zu\n",
psi_plist_count(cpp->tokens.iter),
- psi_plist_count(cpp->tokens.next));
+ psi_plist_count(cpp->tokens.next),
+ psi_plist_count(cpp->tokens.exec));
# if PSI_CPP_DEBUG > 1
PSI_DEBUG_DUMP(cpp->parser, psi_cpp_tokiter_dump, cpp);
# endif
cpp->do_cpp = false;
if (cpp->tokens.next) {
- free(cpp->tokens.iter);
+ psi_plist_free(cpp->tokens.iter);
cpp->tokens.iter = cpp->tokens.next;
}
cpp->tokens.next = psi_plist_init((psi_plist_dtor) psi_token_free);
- cpp->tokens.exec = psi_plist_init((psi_plist_dtor) psi_token_free);
+ if (cpp->tokens.exec) {
+ assert(!psi_plist_count(cpp->tokens.exec));
+ psi_plist_clean(cpp->tokens.exec);
+ } else {
+ cpp->tokens.exec = psi_plist_init((psi_plist_dtor) psi_token_free);
+ }
}
bool psi_cpp_tokiter_seek(struct psi_cpp *cpp, size_t index)
struct psi_token *cur = NULL;
if (psi_plist_get(cpp->tokens.iter, cpp->index, &cur)) {
+ psi_plist_unset(cpp->tokens.iter, cpp->index);
if (cpp->do_cpp) {
cpp->tokens.exec = psi_plist_add(cpp->tokens.exec, &cur);
} else {
bool psi_cpp_tokiter_add_range(struct psi_cpp *cpp, size_t num_eles, void **eles)
{
- struct psi_plist *tokens;
-
if (!num_eles) {
return true;
}
#endif
if (cpp->do_cpp) {
- if (!psi_plist_pop(cpp->tokens.exec, NULL)) {
+ if (!psi_plist_pop(cpp->tokens.exec, &cur)) {
return false;
}
- } else if (!psi_plist_pop(cpp->tokens.next, NULL)) {
- return false;
- }
-
- if (!psi_plist_get(cpp->tokens.iter, cpp->index - 1, &cur)) {
+ } else if (!psi_plist_pop(cpp->tokens.next, &cur)) {
return false;
}
- psi_plist_unset(cpp->tokens.iter, cpp->index - 1);
if (free_token && cur) {
psi_token_free(&cur);
}
#if PSI_CPP_DEBUG
PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, cur);
#endif
- psi_plist_unset(cpp->tokens.iter, cpp->index);
+ if (cpp->index == psi_plist_count(cpp->tokens.iter) - 1) {
+ psi_plist_pop(cpp->tokens.iter, NULL);
+ } else {
+ psi_plist_unset(cpp->tokens.iter, cpp->index);
+ }
if (free_token && cur) {
psi_token_free(&cur);
}
return true;
}
-bool psi_cpp_tokiter_ins_range(struct psi_cpp *cpp, size_t num_eles, void **eles)
-{
- struct psi_plist *tokens;
-
- if (!num_eles) {
- return true;
- }
-
- cpp->tokens.iter = psi_plist_ins_r(cpp->tokens.iter, cpp->index, num_eles, eles);
-
-#if PSI_CPP_DEBUG
- PSI_DEBUG_PRINT(cpp->parser,
- "PSI: CPP ins_range -> index=%zu, num_eles=%zu, iter.count=%zu, next.count=%zu, exec.count=%zu\n",
- cpp->index, num_eles, psi_plist_count(cpp->tokens.iter), psi_plist_count(cpp->tokens.next),
- psi_plist_count(cpp->tokens.exec));
-#endif
-
- return true;
-}
-
bool psi_cpp_tokiter_defined(struct psi_cpp *cpp)
{
if (psi_cpp_tokiter_valid(cpp)) {
/* replace with tokens from macro */
psi_cpp_tokiter_expand_tokens(cpp, target, tokens);
}
+ psi_plist_free(tokens);
++cpp->expanded;
}
# define psi_debug_unlock(ctx)
#endif
+#define PSI_DEBUG_LOCK(ctx, group_actions) do { \
+ if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
+ psi_debug_lock(PSI_DATA(ctx)); \
+ group_actions; \
+ psi_debug_unlock(PSI_DATA(ctx)); \
+ } \
+} while(0)
+
#define PSI_DEBUG_PRINT(ctx, ...) do { \
if ((ctx) && (PSI_DATA(ctx)->flags & PSI_DEBUG)) { \
psi_debug_lock(PSI_DATA(ctx)); \
#define PSI_DUMP(dump, ...) do { \
const struct psi_dump *_dump_ptr = dump; \
if (!_dump_ptr) { \
- _dump_ptr = &psi_dump_stdout; \
+ _dump_ptr = &psi_dump_stderr; \
} \
_dump_ptr->fun(_dump_ptr->ctx, __VA_ARGS__); \
} while(0)
va_end(argv);
va_start(argv, msg);
- psi_debug_lock(context);
- PSI_DEBUG_PRINTV(context, msg, argv);
- PSI_DEBUG_PRINT(context, "\n");
- psi_debug_unlock(context);
+ PSI_DEBUG_LOCK(context,
+ PSI_DEBUG_PRINTV(context, msg, argv);
+ PSI_DEBUG_PRINT(context, "\n");
+ );
va_end(argv);
if (context) {
#if PSI_DEBUG_LEX
PSI_DEBUG_PRINT(P, "PSI: LEX index %4zu ", *index);
#endif
- if (psi_plist_get(tokens, (*index)++, &token)) {
+ if (psi_plist_get(tokens, (*index)++, &token) && token) {
#if PSI_DEBUG_LEX
PSI_DEBUG_DUMP(P, psi_token_dump, token);
#endif
#if PSI_DEBUG_LEX
PSI_DEBUG_PRINT(P, "PSI: LEX index %4zu ", *index);
#endif
- if (psi_plist_get(tokens, (*index)++, &token)) {
+ if (psi_plist_get(tokens, (*index)++, &token) && token) {
#if PSI_DEBUG_LEX
PSI_DEBUG_DUMP(P, psi_token_dump, token);
#endif
-/* Generated by re2c 1.1.1 on Tue Dec 4 17:43:28 2018 */
+/* Generated by re2c 1.1.1 on Fri Dec 7 08:54:40 2018 */
#line 1 "src/parser_scan.re"
/*******************************************************************************
Copyright (c) 2016, Michael Wallner <mike@php.net>.
token = psi_token_init(t, tok, cur - tok, tok - eol + 1, I->lines, I->file); \
} \
tokens = psi_plist_add(tokens, &token); \
- psi_debug_lock(PSI_DATA(P)); \
- PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
- PSI_DEBUG_DUMP(P, psi_token_dump, token); \
- psi_debug_unlock(PSI_DATA(P)); \
+ PSI_DEBUG_LOCK(P, \
+ PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
+ PSI_DEBUG_DUMP(P, psi_token_dump, token); \
+ ); \
} while(0)
#define CHECKEOF() if (cur >= lim - YYMAXFILL) goto done
token = psi_token_init(t, tok, cur - tok, tok - eol + 1, I->lines, I->file); \
} \
tokens = psi_plist_add(tokens, &token); \
- psi_debug_lock(PSI_DATA(P)); \
- PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
- PSI_DEBUG_DUMP(P, psi_token_dump, token); \
- psi_debug_unlock(PSI_DATA(P)); \
+ PSI_DEBUG_LOCK(P, \
+ PSI_DEBUG_PRINT(P, "PSI: scanned < "); \
+ PSI_DEBUG_DUMP(P, psi_token_dump, token); \
+ ); \
} while(0)
#define CHECKEOF() if (cur >= lim - YYMAXFILL) goto done
T->file = zend_string_copy(file);
T->text = psi_string_init_interned(token_txt, token_len, 1);
#if PSI_DEBUG_TOKEN_ALLOC
- PSI_DEBUG_PRINT(cpp->parser, "PSI: token_init %p\n", T);
+ fprintf(stderr, "PSI: token_init %p\t", T);
+ psi_token_dump(NULL, T);
#endif
return T;
}
if (*token_ptr) {
struct psi_token *token = *token_ptr;
#if PSI_DEBUG_TOKEN_ALLOC
- PSI_DEBUG_PRINT(cpp->parser, "PSI: token_free %p\n", token);
+ fprintf(stderr, "PSI: token_free %p\t", token);
+ psi_token_dump(NULL, token);
#endif
*token_ptr = NULL;
zend_string_release(token->text);
*ptr = *src;
#if PSI_DEBUG_TOKEN_ALLOC
- PSI_DEBUG_PRINT(cpp->parser, "PSI: token_copy %p <= %p\n", ptr, src);
+ fprintf(stderr, "PSI: token_copy %p\t", ptr);
+ psi_token_dump(NULL, src);
#endif
ptr->text = zend_string_copy(ptr->text);
ptr->file = zend_string_copy(ptr->file);
T->text = smart_str_extract(&text);
#if PSI_DEBUG_TOKEN_ALLOC
- PSI_DEBUG_PRINT(cpp->parser, "PSI: token_cat %p\n", T);
+ fprintf(stderr, "PSI: token_cat %p\n", T);
#endif
return T;
}