PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/tokenizer - tokenizer.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 102 112 91.1 %
Date: 2015-06-27 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | Copyright (c) 1997-2015 The PHP Group                                |
       6             :    +----------------------------------------------------------------------+
       7             :    | This source file is subject to version 3.01 of the PHP license,      |
       8             :    | that is bundled with this package in the file LICENSE, and is        |
       9             :    | available through the world-wide-web at the following url:           |
      10             :    | http://www.php.net/license/3_01.txt                                  |
      11             :    | If you did not receive a copy of the PHP license and are unable to   |
      12             :    | obtain it through the world-wide-web, please send a note to          |
      13             :    | license@php.net so we can mail you a copy immediately.               |
      14             :    +----------------------------------------------------------------------+
      15             :    | Author: Andrei Zmievski <andrei@php.net>                             |
      16             :    +----------------------------------------------------------------------+
      17             : */
      18             : 
      19             : /* $Id$ */
      20             : 
      21             : #ifdef HAVE_CONFIG_H
      22             : #include "config.h"
      23             : #endif
      24             : 
      25             : #include "php.h"
      26             : #include "php_ini.h"
      27             : #include "ext/standard/info.h"
      28             : #include "php_tokenizer.h"
      29             : 
      30             : #include "zend.h"
      31             : #include "zend_language_scanner.h"
      32             : #include "zend_language_scanner_defs.h"
      33             : #include <zend_language_parser.h>
      34             : 
      35             : #define zendtext   LANG_SCNG(yy_text)
      36             : #define zendleng   LANG_SCNG(yy_leng)
      37             : #define zendcursor LANG_SCNG(yy_cursor)
      38             : #define zendlimit  LANG_SCNG(yy_limit)
      39             : 
      40             : #define TOKEN_PARSE                             1
      41             : 
      42       21049 : void tokenizer_token_get_all_register_constants(INIT_FUNC_ARGS) {
      43       21049 :         REGISTER_LONG_CONSTANT("TOKEN_PARSE", TOKEN_PARSE, CONST_CS|CONST_PERSISTENT);
      44       21049 : }
      45             : 
      46             : /* {{{ arginfo */
      47             : ZEND_BEGIN_ARG_INFO_EX(arginfo_token_get_all, 0, 0, 1)
      48             :         ZEND_ARG_INFO(0, source)
      49             : ZEND_END_ARG_INFO()
      50             : 
      51             : ZEND_BEGIN_ARG_INFO_EX(arginfo_token_name, 0, 0, 1)
      52             :         ZEND_ARG_INFO(0, token)
      53             : ZEND_END_ARG_INFO()
      54             : /* }}} */
      55             : 
      56             : /* {{{ tokenizer_functions[]
      57             :  *
      58             :  * Every user visible function must have an entry in tokenizer_functions[].
      59             :  */
      60             : const zend_function_entry tokenizer_functions[] = {
      61             :         PHP_FE(token_get_all,   arginfo_token_get_all)
      62             :         PHP_FE(token_name,              arginfo_token_name)
      63             :         PHP_FE_END
      64             : };
      65             : /* }}} */
      66             : 
      67             : /* {{{ tokenizer_module_entry
      68             :  */
      69             : zend_module_entry tokenizer_module_entry = {
      70             :         STANDARD_MODULE_HEADER,
      71             :         "tokenizer",
      72             :         tokenizer_functions,
      73             :         PHP_MINIT(tokenizer),
      74             :         NULL,
      75             :         NULL,
      76             :         NULL,
      77             :         PHP_MINFO(tokenizer),
      78             :         PHP_TOKENIZER_VERSION,
      79             :         STANDARD_MODULE_PROPERTIES
      80             : };
      81             : /* }}} */
      82             : 
      83             : #ifdef COMPILE_DL_TOKENIZER
      84             : ZEND_GET_MODULE(tokenizer)
      85             : #endif
      86             : 
      87             : /* {{{ PHP_MINIT_FUNCTION
      88             :  */
      89       21049 : PHP_MINIT_FUNCTION(tokenizer)
      90             : {
      91       21049 :         tokenizer_register_constants(INIT_FUNC_ARGS_PASSTHRU);
      92       21049 :         tokenizer_token_get_all_register_constants(INIT_FUNC_ARGS_PASSTHRU);
      93       21049 :         return SUCCESS;
      94             : }
      95             : /* }}} */
      96             : 
      97             : /* {{{ PHP_MINFO_FUNCTION
      98             :  */
      99         142 : PHP_MINFO_FUNCTION(tokenizer)
     100             : {
     101         142 :         php_info_print_table_start();
     102         142 :         php_info_print_table_row(2, "Tokenizer Support", "enabled");
     103         142 :         php_info_print_table_end();
     104         142 : }
     105             : /* }}} */
     106             : 
     107          82 : static zend_bool tokenize(zval *return_value, zend_string *source)
     108             : {
     109             :         zval source_zval;
     110             :         zend_lex_state original_lex_state;
     111             :         zval token;
     112             :         zval keyword;
     113             :         int token_type;
     114             :         zend_bool destroy;
     115          82 :         int token_line = 1;
     116          82 :         int need_tokens = -1; /* for __halt_compiler lexing. -1 = disabled */
     117             : 
     118          82 :         ZVAL_STR_COPY(&source_zval, source);
     119          82 :         zend_save_lexical_state(&original_lex_state);
     120             : 
     121          82 :         if (zend_prepare_string_for_scanning(&source_zval, "") == FAILURE) {
     122           0 :                 zend_restore_lexical_state(&original_lex_state);
     123           0 :                 return 0;
     124             :         }
     125             : 
     126          82 :         LANG_SCNG(yy_state) = yycINITIAL;
     127          82 :         array_init(return_value);
     128             : 
     129          82 :         ZVAL_NULL(&token);
     130        1858 :         while ((token_type = lex_scan(&token))) {
     131             : 
     132        1702 :                 if(token_type == T_ERROR) break;
     133             : 
     134        1698 :                 destroy = 1;
     135        1698 :                 switch (token_type) {
     136             :                         case T_CLOSE_TAG:
     137          40 :                                 if (zendtext[zendleng - 1] != '>') {
     138           0 :                                         CG(zend_lineno)++;
     139             :                                 }
     140             :                         case T_OPEN_TAG:
     141             :                         case T_OPEN_TAG_WITH_ECHO:
     142             :                         case T_WHITESPACE:
     143             :                         case T_COMMENT:
     144             :                         case T_DOC_COMMENT:
     145         697 :                                 destroy = 0;
     146             :                                 break;
     147             :                 }
     148             : 
     149        1698 :                 if (token_type >= 256) {
     150        1257 :                         array_init(&keyword);
     151        1257 :                         add_next_index_long(&keyword, token_type);
     152        1257 :                         if (token_type == T_END_HEREDOC) {
     153           5 :                                 if (CG(increment_lineno)) {
     154           4 :                                         token_line = ++CG(zend_lineno);
     155           4 :                                         CG(increment_lineno) = 0;
     156             :                                 }
     157             :                         }
     158        1257 :                         add_next_index_stringl(&keyword, (char *)zendtext, zendleng);
     159        1257 :                         add_next_index_long(&keyword, token_line);
     160        1257 :                         add_next_index_zval(return_value, &keyword);
     161             :                 } else {
     162         441 :                         add_next_index_stringl(return_value, (char *)zendtext, zendleng);
     163             :                 }
     164        2699 :                 if (destroy && Z_TYPE(token) != IS_NULL) {
     165             :                         zval_dtor(&token);
     166             :                 }
     167        1698 :                 ZVAL_NULL(&token);
     168             : 
     169             :                 /* after T_HALT_COMPILER collect the next three non-dropped tokens */
     170        1698 :                 if (need_tokens != -1) {
     171          19 :                         if (token_type != T_WHITESPACE && token_type != T_OPEN_TAG
     172             :                                 && token_type != T_COMMENT && token_type != T_DOC_COMMENT
     173             :                                 && --need_tokens == 0
     174             :                         ) {
     175             :                                 /* fetch the rest into a T_INLINE_HTML */
     176           4 :                                 if (zendcursor != zendlimit) {
     177           3 :                                         array_init(&keyword);
     178           3 :                                         add_next_index_long(&keyword, T_INLINE_HTML);
     179           3 :                                         add_next_index_stringl(&keyword, (char *)zendcursor, zendlimit - zendcursor);
     180           3 :                                         add_next_index_long(&keyword, token_line);
     181           3 :                                         add_next_index_zval(return_value, &keyword);
     182             :                                 }
     183           4 :                                 break;
     184             :                         }
     185        1679 :                 } else if (token_type == T_HALT_COMPILER) {
     186           6 :                         need_tokens = 3;
     187             :                 }
     188             : 
     189        1694 :                 token_line = CG(zend_lineno);
     190             :         }
     191             : 
     192             :         zval_dtor(&source_zval);
     193          82 :         zend_restore_lexical_state(&original_lex_state);
     194             : 
     195          82 :         return 1;
     196             : }
     197             : 
     198             : zval token_stream;
     199             : 
     200         147 : void on_event(zend_php_scanner_event event, int token, int line)
     201             : {
     202             :         zval keyword;
     203             :         HashTable *tokens_ht;
     204             :         zval *token_zv;
     205             : 
     206         147 :         switch(event) {
     207             :                 case ON_TOKEN:
     208         137 :                         if (token == T_ERROR || token == END) break;
     209         135 :                         if (token >= 256) {
     210         100 :                                 array_init(&keyword);
     211         100 :                                 add_next_index_long(&keyword, token);
     212         100 :                                 add_next_index_stringl(&keyword, (char *)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
     213         100 :                                 add_next_index_long(&keyword, line);
     214         100 :                                 add_next_index_zval(&token_stream, &keyword);
     215             :                         } else {
     216          35 :                                 add_next_index_stringl(&token_stream, (char *)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
     217             :                         }
     218         135 :                         break;
     219             :                 case ON_FEEDBACK:
     220          10 :                         tokens_ht = Z_ARRVAL(token_stream);
     221          10 :                         token_zv = zend_hash_index_find(tokens_ht, zend_hash_num_elements(tokens_ht) - 1);
     222          20 :                         if (token_zv && Z_TYPE_P(token_zv) == IS_ARRAY) {
     223          10 :                                 ZVAL_LONG(zend_hash_index_find(Z_ARRVAL_P(token_zv), 0), token);
     224             :                         }
     225          10 :                         break;
     226             :                 case ON_STOP:
     227           0 :                         if (LANG_SCNG(yy_cursor) != LANG_SCNG(yy_limit)) {
     228           0 :                                 array_init(&keyword);
     229           0 :                                 add_next_index_long(&keyword, T_INLINE_HTML);
     230           0 :                                 add_next_index_stringl(&keyword,
     231           0 :                                         (char *)LANG_SCNG(yy_cursor), LANG_SCNG(yy_limit) - LANG_SCNG(yy_cursor));
     232           0 :                                 add_next_index_long(&keyword, CG(zend_lineno));
     233           0 :                                 add_next_index_zval(&token_stream, &keyword);
     234             :                         }
     235             :                         break;
     236             :         }
     237         147 : }
     238             : 
     239           3 : static zend_bool tokenize_parse(zval *return_value, zend_string *source)
     240             : {
     241             :         zval source_zval;
     242             :         zend_lex_state original_lex_state;
     243             :         zend_bool original_in_compilation;
     244             :         zend_bool success;
     245             : 
     246           3 :         ZVAL_STR_COPY(&source_zval, source);
     247             : 
     248           3 :         original_in_compilation = CG(in_compilation);
     249           3 :         CG(in_compilation) = 1;
     250           3 :         zend_save_lexical_state(&original_lex_state);
     251             : 
     252           3 :         if ((success = (zend_prepare_string_for_scanning(&source_zval, "") == SUCCESS))) {
     253           3 :                 CG(ast) = NULL;
     254           3 :                 CG(ast_arena) = zend_arena_create(1024 * 32);
     255           3 :                 LANG_SCNG(yy_state) = yycINITIAL;
     256           3 :                 LANG_SCNG(on_event) = on_event;
     257             : 
     258           3 :                 array_init(&token_stream);
     259           3 :                 if((success = (zendparse() == SUCCESS))) {
     260           2 :                         ZVAL_COPY_VALUE(return_value, &token_stream);
     261             :                 } else {
     262           1 :                         zval_ptr_dtor(&token_stream);
     263             :                 }
     264             : 
     265           3 :                 zend_ast_destroy(CG(ast));
     266           3 :                 zend_arena_destroy(CG(ast_arena));
     267             :         }
     268             : 
     269             :         /* restore compiler and scanner global states */
     270           3 :         zend_restore_lexical_state(&original_lex_state);
     271           3 :         CG(in_compilation) = original_in_compilation;
     272             : 
     273             :         zval_dtor(&source_zval);
     274             : 
     275           3 :         return success;
     276             : }
     277             : 
     278             : /* }}} */
     279             : 
     280             : /* {{{ proto array token_get_all(string source)
     281             :  */
     282          95 : PHP_FUNCTION(token_get_all)
     283             : {
     284             :         zend_string *source;
     285          95 :         zend_long flags = 0;
     286             :         zend_bool success;
     287             : 
     288          95 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|l", &source, &flags) == FAILURE) {
     289          10 :                 return;
     290             :         }
     291             : 
     292          85 :         if (flags & TOKEN_PARSE) {
     293           3 :                 success = tokenize_parse(return_value, source);
     294             :         } else {
     295          82 :                 success = tokenize(return_value, source);
     296             :         }
     297             : 
     298          85 :         if (!success) RETURN_FALSE;
     299             : }
     300             : /* }}} */
     301             : 
     302             : /* {{{ proto string token_name(int type)
     303             :  */
     304         219 : PHP_FUNCTION(token_name)
     305             : {
     306             :         zend_long type;
     307             : 
     308         219 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type) == FAILURE) {
     309           2 :                 return;
     310             :         }
     311             : 
     312         434 :         RETVAL_STRING(get_token_type_name(type));
     313             : }
     314             : /* }}} */
     315             : 
     316             : /*
     317             :  * Local variables:
     318             :  * tab-width: 4
     319             :  * c-basic-offset: 4
     320             :  * End:
     321             :  * vim600: noet sw=4 ts=4 fdm=marker
     322             :  * vim<600: noet sw=4 ts=4
     323             :  */

Generated by: LCOV version 1.10

Generated at Sat, 27 Jun 2015 09:41:21 +0000 (5 days ago)

Copyright © 2005-2015 The PHP Group
All rights reserved.