#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_tokenizer.h"
typedef struct yy_buffer_state *YY_BUFFER_STATE;
typedef unsigned int yy_size_t;
struct yy_buffer_state
{
FILE *yy_input_file;
char *yy_ch_buf;
char *yy_buf_pos;
yy_size_t yy_buf_size;
int yy_n_chars;
int yy_is_our_buffer;
int yy_is_interactive;
int yy_at_bol;
int yy_fill_buffer;
int yy_buffer_status;
#define YY_BUFFER_NEW 0
#define YY_BUFFER_NORMAL 1
#define YY_BUFFER_EOF_PENDING 2
};
#include "zend.h"
#include "zend_language_scanner.h"
#include <zend_language_parser.h>
#define zendtext LANG_SCNG(yy_text)
#define zendleng LANG_SCNG(yy_leng)
zend_function_entry tokenizer_functions[] = {
PHP_FE(token_get_all, NULL)
PHP_FE(token_name, NULL)
{NULL, NULL, NULL}
};
zend_module_entry tokenizer_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
"tokenizer",
tokenizer_functions,
PHP_MINIT(tokenizer),
NULL,
NULL,
NULL,
PHP_MINFO(tokenizer),
#if ZEND_MODULE_API_NO >= 20010901
"0.1",
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_TOKENIZER
ZEND_GET_MODULE(tokenizer)
#endif
PHP_MINIT_FUNCTION(tokenizer)
{
tokenizer_register_constants(INIT_FUNC_ARGS_PASSTHRU);
return SUCCESS;
}
PHP_MINFO_FUNCTION(tokenizer)
{
php_info_print_table_start();
php_info_print_table_row(2, "Tokenizer Support", "enabled");
php_info_print_table_end();
}
static void tokenize(zval *return_value TSRMLS_DC)
{
zval token;
zval *keyword;
int token_type;
zend_bool destroy;
int token_line = 1;
array_init(return_value);
ZVAL_NULL(&token);
while ((token_type = lex_scan(&token TSRMLS_CC))) {
destroy = 1;
switch (token_type) {
case T_CLOSE_TAG:
if (zendtext[zendleng - 1] != '>') {
CG(zend_lineno)++;
}
case T_OPEN_TAG:
case T_OPEN_TAG_WITH_ECHO:
case T_WHITESPACE:
case T_COMMENT:
case T_DOC_COMMENT:
destroy = 0;
break;
}
if (token_type >= 256) {
MAKE_STD_ZVAL(keyword);
array_init(keyword);
add_next_index_long(keyword, token_type);
if (token_type == T_END_HEREDOC) {
if (CG(increment_lineno)) {
token_line = ++CG(zend_lineno);
CG(increment_lineno) = 0;
}
add_next_index_stringl(keyword, Z_STRVAL(token), Z_STRLEN(token), 1);
efree(Z_STRVAL(token));
} else {
add_next_index_stringl(keyword, zendtext, zendleng, 1);
}
add_next_index_long(keyword, token_line);
add_next_index_zval(return_value, keyword);
} else {
add_next_index_stringl(return_value, zendtext, zendleng, 1);
}
if (destroy && Z_TYPE(token) != IS_NULL) {
zval_dtor(&token);
}
ZVAL_NULL(&token);
token_line = CG(zend_lineno);
}
}
PHP_FUNCTION(token_get_all)
{
char *source = NULL;
int argc = ZEND_NUM_ARGS();
int source_len;
zval source_z;
zend_lex_state original_lex_state;
if (zend_parse_parameters(argc TSRMLS_CC, "s", &source, &source_len) == FAILURE)
return;
ZVAL_STRINGL(&source_z, source, source_len, 1);
zend_save_lexical_state(&original_lex_state TSRMLS_CC);
if (zend_prepare_string_for_scanning(&source_z, "" TSRMLS_CC) == FAILURE) {
RETURN_EMPTY_STRING();
}
LANG_SCNG(start) = 1;
tokenize(return_value TSRMLS_CC);
zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
zval_dtor(&source_z);
}
PHP_FUNCTION(token_name)
{
int argc = ZEND_NUM_ARGS();
long type;
if (zend_parse_parameters(argc TSRMLS_CC, "l", &type) == FAILURE) {
return;
}
RETVAL_STRING(get_token_type_name(type), 1);
}