1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 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: tokenizer.c 277761 2009-03-25 15:23:58Z dmitry $ */
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 :
38 : /* {{{ arginfo */
39 : ZEND_BEGIN_ARG_INFO_EX(arginfo_token_get_all, 0, 0, 1)
40 : ZEND_ARG_INFO(0, source)
41 : ZEND_END_ARG_INFO()
42 :
43 : ZEND_BEGIN_ARG_INFO_EX(arginfo_token_name, 0, 0, 1)
44 : ZEND_ARG_INFO(0, token)
45 : ZEND_END_ARG_INFO()
46 : /* }}} */
47 :
48 : /* {{{ tokenizer_functions[]
49 : *
50 : * Every user visible function must have an entry in tokenizer_functions[].
51 : */
52 : const zend_function_entry tokenizer_functions[] = {
53 : PHP_FE(token_get_all, arginfo_token_get_all)
54 : PHP_FE(token_name, arginfo_token_name)
55 : {NULL, NULL, NULL} /* Must be the last line in tokenizer_functions[] */
56 : };
57 : /* }}} */
58 :
59 : /* {{{ tokenizer_module_entry
60 : */
61 : zend_module_entry tokenizer_module_entry = {
62 : #if ZEND_MODULE_API_NO >= 20010901
63 : STANDARD_MODULE_HEADER,
64 : #endif
65 : "tokenizer",
66 : tokenizer_functions,
67 : PHP_MINIT(tokenizer),
68 : NULL,
69 : NULL,
70 : NULL,
71 : PHP_MINFO(tokenizer),
72 : #if ZEND_MODULE_API_NO >= 20010901
73 : "0.1", /* Replace with version number for your extension */
74 : #endif
75 : STANDARD_MODULE_PROPERTIES
76 : };
77 : /* }}} */
78 :
79 : #ifdef COMPILE_DL_TOKENIZER
80 : ZEND_GET_MODULE(tokenizer)
81 : #endif
82 :
83 : /* {{{ PHP_MINIT_FUNCTION
84 : */
85 : PHP_MINIT_FUNCTION(tokenizer)
86 17633 : {
87 17633 : tokenizer_register_constants(INIT_FUNC_ARGS_PASSTHRU);
88 17633 : return SUCCESS;
89 : }
90 : /* }}} */
91 :
92 : /* {{{ PHP_MINFO_FUNCTION
93 : */
94 : PHP_MINFO_FUNCTION(tokenizer)
95 42 : {
96 42 : php_info_print_table_start();
97 42 : php_info_print_table_row(2, "Tokenizer Support", "enabled");
98 42 : php_info_print_table_end();
99 42 : }
100 : /* }}} */
101 :
102 : static void tokenize(zval *return_value TSRMLS_DC)
103 67 : {
104 : zval token;
105 : zval *keyword;
106 : int token_type;
107 : zend_bool destroy;
108 67 : int token_line = 1;
109 :
110 67 : array_init(return_value);
111 :
112 67 : ZVAL_NULL(&token);
113 1762 : while ((token_type = lex_scan(&token TSRMLS_CC))) {
114 1628 : destroy = 1;
115 1628 : switch (token_type) {
116 : case T_CLOSE_TAG:
117 40 : if (zendtext[zendleng - 1] != '>') {
118 0 : CG(zend_lineno)++;
119 : }
120 : case T_OPEN_TAG:
121 : case T_OPEN_TAG_WITH_ECHO:
122 : case T_WHITESPACE:
123 : case T_COMMENT:
124 : case T_DOC_COMMENT:
125 670 : destroy = 0;
126 : break;
127 : }
128 :
129 1628 : if (token_type >= 256) {
130 1206 : MAKE_STD_ZVAL(keyword);
131 1206 : array_init(keyword);
132 1206 : add_next_index_long(keyword, token_type);
133 1206 : if (token_type == T_END_HEREDOC) {
134 3 : if (CG(increment_lineno)) {
135 3 : token_line = ++CG(zend_lineno);
136 3 : CG(increment_lineno) = 0;
137 : }
138 3 : add_next_index_stringl(keyword, Z_STRVAL(token), Z_STRLEN(token), 1);
139 3 : efree(Z_STRVAL(token));
140 : } else {
141 1203 : add_next_index_stringl(keyword, (char *)zendtext, zendleng, 1);
142 : }
143 1206 : add_next_index_long(keyword, token_line);
144 1206 : add_next_index_zval(return_value, keyword);
145 : } else {
146 422 : add_next_index_stringl(return_value, (char *)zendtext, zendleng, 1);
147 : }
148 1628 : if (destroy && Z_TYPE(token) != IS_NULL) {
149 350 : zval_dtor(&token);
150 : }
151 1628 : ZVAL_NULL(&token);
152 :
153 1628 : token_line = CG(zend_lineno);
154 : }
155 67 : }
156 :
157 : /* {{{ proto array token_get_all(string source)
158 : */
159 : PHP_FUNCTION(token_get_all)
160 77 : {
161 77 : char *source = NULL;
162 77 : int argc = ZEND_NUM_ARGS();
163 : int source_len;
164 : zval source_z;
165 : zend_lex_state original_lex_state;
166 :
167 77 : if (zend_parse_parameters(argc TSRMLS_CC, "s", &source, &source_len) == FAILURE)
168 10 : return;
169 :
170 67 : ZVAL_STRINGL(&source_z, source, source_len, 1);
171 67 : zend_save_lexical_state(&original_lex_state TSRMLS_CC);
172 :
173 67 : if (zend_prepare_string_for_scanning(&source_z, "" TSRMLS_CC) == FAILURE) {
174 0 : zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
175 0 : RETURN_EMPTY_STRING();
176 : }
177 :
178 67 : LANG_SCNG(yy_state) = yycINITIAL;
179 :
180 67 : tokenize(return_value TSRMLS_CC);
181 :
182 67 : zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
183 67 : zval_dtor(&source_z);
184 : }
185 : /* }}} */
186 :
187 : /* {{{ proto string token_name(int type)
188 : */
189 : PHP_FUNCTION(token_name)
190 121 : {
191 121 : int argc = ZEND_NUM_ARGS();
192 : long type;
193 :
194 121 : if (zend_parse_parameters(argc TSRMLS_CC, "l", &type) == FAILURE) {
195 2 : return;
196 : }
197 119 : RETVAL_STRING(get_token_type_name(type), 1);
198 : }
199 : /* }}} */
200 :
201 : /*
202 : * Local variables:
203 : * tab-width: 4
204 : * c-basic-offset: 4
205 : * End:
206 : * vim600: noet sw=4 ts=4 fdm=marker
207 : * vim<600: noet sw=4 ts=4
208 : */
|