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: 58 61 95.1 %
Date: 2015-05-21 Functions: 5 5 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             : /* {{{ arginfo */
      41             : ZEND_BEGIN_ARG_INFO_EX(arginfo_token_get_all, 0, 0, 1)
      42             :         ZEND_ARG_INFO(0, source)
      43             : ZEND_END_ARG_INFO()
      44             : 
      45             : ZEND_BEGIN_ARG_INFO_EX(arginfo_token_name, 0, 0, 1)
      46             :         ZEND_ARG_INFO(0, token)
      47             : ZEND_END_ARG_INFO()
      48             : /* }}} */
      49             : 
      50             : /* {{{ tokenizer_functions[]
      51             :  *
      52             :  * Every user visible function must have an entry in tokenizer_functions[].
      53             :  */
      54             : const zend_function_entry tokenizer_functions[] = {
      55             :         PHP_FE(token_get_all,   arginfo_token_get_all)
      56             :         PHP_FE(token_name,              arginfo_token_name)
      57             :         PHP_FE_END
      58             : };
      59             : /* }}} */
      60             : 
      61             : /* {{{ tokenizer_module_entry
      62             :  */
      63             : zend_module_entry tokenizer_module_entry = {
      64             :         STANDARD_MODULE_HEADER,
      65             :         "tokenizer",
      66             :         tokenizer_functions,
      67             :         PHP_MINIT(tokenizer),
      68             :         NULL,
      69             :         NULL,
      70             :         NULL,
      71             :         PHP_MINFO(tokenizer),
      72             :         PHP_TOKENIZER_VERSION,
      73             :         STANDARD_MODULE_PROPERTIES
      74             : };
      75             : /* }}} */
      76             : 
      77             : #ifdef COMPILE_DL_TOKENIZER
      78             : ZEND_GET_MODULE(tokenizer)
      79             : #endif
      80             : 
      81             : /* {{{ PHP_MINIT_FUNCTION
      82             :  */
      83       20916 : PHP_MINIT_FUNCTION(tokenizer)
      84             : {
      85       20916 :         tokenizer_register_constants(INIT_FUNC_ARGS_PASSTHRU);
      86       20916 :         return SUCCESS;
      87             : }
      88             : /* }}} */
      89             : 
      90             : /* {{{ PHP_MINFO_FUNCTION
      91             :  */
      92         142 : PHP_MINFO_FUNCTION(tokenizer)
      93             : {
      94         142 :         php_info_print_table_start();
      95         142 :         php_info_print_table_row(2, "Tokenizer Support", "enabled");
      96         142 :         php_info_print_table_end();
      97         142 : }
      98             : /* }}} */
      99             : 
     100          82 : static void tokenize(zval *return_value)
     101             : {
     102             :         zval token;
     103             :         zval keyword;
     104             :         int token_type;
     105             :         zend_bool destroy;
     106          82 :         int token_line = 1;
     107          82 :         int need_tokens = -1; // for __halt_compiler lexing. -1 = disabled
     108             : 
     109          82 :         array_init(return_value);
     110             : 
     111          82 :         ZVAL_NULL(&token);
     112        1858 :         while ((token_type = lex_scan(&token))) {
     113        1702 :                 destroy = 1;
     114        1702 :                 switch (token_type) {
     115             :                         case T_CLOSE_TAG:
     116          40 :                                 if (zendtext[zendleng - 1] != '>') {
     117           0 :                                         CG(zend_lineno)++;
     118             :                                 }
     119             :                         case T_OPEN_TAG:
     120             :                         case T_OPEN_TAG_WITH_ECHO:
     121             :                         case T_WHITESPACE:
     122             :                         case T_COMMENT:
     123             :                         case T_DOC_COMMENT:
     124         697 :                                 destroy = 0;
     125         697 :                                 break;
     126             :                         case T_ERROR:
     127           4 :                                 return;
     128             :                 }
     129             : 
     130        1698 :                 if (token_type >= 256) {
     131        1257 :                         array_init(&keyword);
     132        1257 :                         add_next_index_long(&keyword, token_type);
     133        1257 :                         if (token_type == T_END_HEREDOC) {
     134           5 :                                 if (CG(increment_lineno)) {
     135           4 :                                         token_line = ++CG(zend_lineno);
     136           4 :                                         CG(increment_lineno) = 0;
     137             :                                 }
     138             :                         }
     139        1257 :                         add_next_index_stringl(&keyword, (char *)zendtext, zendleng);
     140        1257 :                         add_next_index_long(&keyword, token_line);
     141        1257 :                         add_next_index_zval(return_value, &keyword);
     142             :                 } else {
     143         441 :                         add_next_index_stringl(return_value, (char *)zendtext, zendleng);
     144             :                 }
     145        2699 :                 if (destroy && Z_TYPE(token) != IS_NULL) {
     146             :                         zval_dtor(&token);
     147             :                 }
     148        1698 :                 ZVAL_NULL(&token);
     149             : 
     150             :                 // after T_HALT_COMPILER collect the next three non-dropped tokens
     151        1698 :                 if (need_tokens != -1) {
     152          19 :                         if (token_type != T_WHITESPACE && token_type != T_OPEN_TAG
     153             :                             && token_type != T_COMMENT && token_type != T_DOC_COMMENT
     154             :                             && --need_tokens == 0
     155             :                         ) {
     156             :                                 // fetch the rest into a T_INLINE_HTML
     157           4 :                                 if (zendcursor != zendlimit) {
     158           3 :                                         array_init(&keyword);
     159           3 :                                         add_next_index_long(&keyword, T_INLINE_HTML);
     160           3 :                                         add_next_index_stringl(&keyword, (char *)zendcursor, zendlimit - zendcursor);
     161           3 :                                         add_next_index_long(&keyword, token_line);
     162           3 :                                         add_next_index_zval(return_value, &keyword);
     163             :                                 }
     164           4 :                                 break;
     165             :                         }
     166        1679 :                 } else if (token_type == T_HALT_COMPILER) {
     167           6 :                         need_tokens = 3;
     168             :                 }
     169             : 
     170        1694 :                 token_line = CG(zend_lineno);
     171             :         }
     172             : }
     173             : 
     174             : /* {{{ proto array token_get_all(string source)
     175             :  */
     176          92 : PHP_FUNCTION(token_get_all)
     177             : {
     178             :         zend_string *source;
     179             :         zval source_zval;
     180             :         zend_lex_state original_lex_state;
     181             : 
     182          92 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "S", &source) == FAILURE) {
     183          10 :                 return;
     184             :         }
     185             : 
     186          82 :         ZVAL_STR_COPY(&source_zval, source);
     187          82 :         zend_save_lexical_state(&original_lex_state);
     188             : 
     189          82 :         if (zend_prepare_string_for_scanning(&source_zval, "") == FAILURE) {
     190           0 :                 zend_restore_lexical_state(&original_lex_state);
     191           0 :                 RETURN_FALSE;
     192             :         }
     193             : 
     194          82 :         LANG_SCNG(yy_state) = yycINITIAL;
     195             : 
     196          82 :         tokenize(return_value);
     197             : 
     198          82 :         zend_restore_lexical_state(&original_lex_state);
     199             :         zval_dtor(&source_zval);
     200             : }
     201             : /* }}} */
     202             : 
     203             : /* {{{ proto string token_name(int type)
     204             :  */
     205         123 : PHP_FUNCTION(token_name)
     206             : {
     207             :         zend_long type;
     208             : 
     209         123 :         if (zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type) == FAILURE) {
     210           2 :                 return;
     211             :         }
     212             : 
     213         242 :         RETVAL_STRING(get_token_type_name(type));
     214             : }
     215             : /* }}} */
     216             : 
     217             : /*
     218             :  * Local variables:
     219             :  * tab-width: 4
     220             :  * c-basic-offset: 4
     221             :  * End:
     222             :  * vim600: noet sw=4 ts=4 fdm=marker
     223             :  * vim<600: noet sw=4 ts=4
     224             :  */

Generated by: LCOV version 1.10

Generated at Thu, 21 May 2015 19:59:06 +0000 (14 hours ago)

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