xref: /PHP-7.4/ext/tokenizer/tokenizer.c (revision 7e0f7b76)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 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 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22 
23 #include "php.h"
24 #include "php_ini.h"
25 #include "ext/standard/info.h"
26 #include "php_tokenizer.h"
27 
28 #include "zend.h"
29 #include "zend_exceptions.h"
30 #include "zend_language_scanner.h"
31 #include "zend_language_scanner_defs.h"
32 #include <zend_language_parser.h>
33 
34 #define zendtext   LANG_SCNG(yy_text)
35 #define zendleng   LANG_SCNG(yy_leng)
36 #define zendcursor LANG_SCNG(yy_cursor)
37 #define zendlimit  LANG_SCNG(yy_limit)
38 
39 #define TOKEN_PARSE 				1
40 
tokenizer_token_get_all_register_constants(INIT_FUNC_ARGS)41 void tokenizer_token_get_all_register_constants(INIT_FUNC_ARGS) {
42 	REGISTER_LONG_CONSTANT("TOKEN_PARSE", TOKEN_PARSE, CONST_CS|CONST_PERSISTENT);
43 }
44 
45 /* {{{ arginfo */
46 ZEND_BEGIN_ARG_INFO_EX(arginfo_token_get_all, 0, 0, 1)
47 	ZEND_ARG_INFO(0, source)
48 	ZEND_ARG_INFO(0, flags)
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 static 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  */
PHP_MINIT_FUNCTION(tokenizer)89 PHP_MINIT_FUNCTION(tokenizer)
90 {
91 	tokenizer_register_constants(INIT_FUNC_ARGS_PASSTHRU);
92 	tokenizer_token_get_all_register_constants(INIT_FUNC_ARGS_PASSTHRU);
93 	return SUCCESS;
94 }
95 /* }}} */
96 
97 /* {{{ PHP_MINFO_FUNCTION
98  */
PHP_MINFO_FUNCTION(tokenizer)99 PHP_MINFO_FUNCTION(tokenizer)
100 {
101 	php_info_print_table_start();
102 	php_info_print_table_row(2, "Tokenizer Support", "enabled");
103 	php_info_print_table_end();
104 }
105 /* }}} */
106 
add_token(zval * return_value,int token_type,unsigned char * text,size_t leng,int lineno)107 static void add_token(zval *return_value, int token_type,
108 		unsigned char *text, size_t leng, int lineno) {
109 	if (token_type >= 256) {
110 		zval keyword;
111 		array_init(&keyword);
112 		add_next_index_long(&keyword, token_type);
113 		if (leng == 1) {
114 			add_next_index_str(&keyword, ZSTR_CHAR(text[0]));
115 		} else {
116 			add_next_index_stringl(&keyword, (char *) text, leng);
117 		}
118 		add_next_index_long(&keyword, lineno);
119 		add_next_index_zval(return_value, &keyword);
120 	} else {
121 		if (leng == 1) {
122 			add_next_index_str(return_value, ZSTR_CHAR(text[0]));
123 		} else {
124 			add_next_index_stringl(return_value, (char *) text, leng);
125 		}
126 	}
127 }
128 
tokenize(zval * return_value,zend_string * source)129 static zend_bool tokenize(zval *return_value, zend_string *source)
130 {
131 	zval source_zval;
132 	zend_lex_state original_lex_state;
133 	zval token;
134 	int token_type;
135 	int token_line = 1;
136 	int need_tokens = -1; /* for __halt_compiler lexing. -1 = disabled */
137 
138 	ZVAL_STR_COPY(&source_zval, source);
139 	zend_save_lexical_state(&original_lex_state);
140 
141 	if (zend_prepare_string_for_scanning(&source_zval, "") == FAILURE) {
142 		zend_restore_lexical_state(&original_lex_state);
143 		return 0;
144 	}
145 
146 	LANG_SCNG(yy_state) = yycINITIAL;
147 	array_init(return_value);
148 
149 	while ((token_type = lex_scan(&token, NULL))) {
150 		add_token(return_value, token_type, zendtext, zendleng, token_line);
151 
152 		if (Z_TYPE(token) != IS_UNDEF) {
153 			zval_ptr_dtor_nogc(&token);
154 			ZVAL_UNDEF(&token);
155 		}
156 
157 		/* after T_HALT_COMPILER collect the next three non-dropped tokens */
158 		if (need_tokens != -1) {
159 			if (token_type != T_WHITESPACE && token_type != T_OPEN_TAG
160 				&& token_type != T_COMMENT && token_type != T_DOC_COMMENT
161 				&& --need_tokens == 0
162 			) {
163 				/* fetch the rest into a T_INLINE_HTML */
164 				if (zendcursor != zendlimit) {
165 					add_token(return_value, T_INLINE_HTML,
166 						zendcursor, zendlimit - zendcursor, token_line);
167 				}
168 				break;
169 			}
170 		} else if (token_type == T_HALT_COMPILER) {
171 			need_tokens = 3;
172 		}
173 
174 		if (CG(increment_lineno)) {
175 			CG(zend_lineno)++;
176 			CG(increment_lineno) = 0;
177 		}
178 
179 		token_line = CG(zend_lineno);
180 	}
181 
182 	zval_ptr_dtor_str(&source_zval);
183 	zend_restore_lexical_state(&original_lex_state);
184 
185 	return 1;
186 }
187 
on_event(zend_php_scanner_event event,int token,int line,void * context)188 void on_event(zend_php_scanner_event event, int token, int line, void *context)
189 {
190 	zval *token_stream = (zval *) context;
191 	HashTable *tokens_ht;
192 	zval *token_zv;
193 
194 	switch (event) {
195 		case ON_TOKEN:
196 			{
197 				if (token == END) break;
198 				/* Special cases */
199 				if (token == ';' && LANG_SCNG(yy_leng) > 1) { /* ?> or ?>\n or ?>\r\n */
200 					token = T_CLOSE_TAG;
201 				} else if (token == T_ECHO && LANG_SCNG(yy_leng) == sizeof("<?=") - 1) {
202 					token = T_OPEN_TAG_WITH_ECHO;
203 				}
204 				add_token(token_stream, token, LANG_SCNG(yy_text), LANG_SCNG(yy_leng), line);
205 			}
206 			break;
207 		case ON_FEEDBACK:
208 			tokens_ht = Z_ARRVAL_P(token_stream);
209 			token_zv = zend_hash_index_find(tokens_ht, zend_hash_num_elements(tokens_ht) - 1);
210 			if (token_zv && Z_TYPE_P(token_zv) == IS_ARRAY) {
211 				ZVAL_LONG(zend_hash_index_find(Z_ARRVAL_P(token_zv), 0), token);
212 			}
213 			break;
214 		case ON_STOP:
215 			if (LANG_SCNG(yy_cursor) != LANG_SCNG(yy_limit)) {
216 				add_token(token_stream, T_INLINE_HTML, LANG_SCNG(yy_cursor),
217 					LANG_SCNG(yy_limit) - LANG_SCNG(yy_cursor), CG(zend_lineno));
218 			}
219 			break;
220 	}
221 }
222 
tokenize_parse(zval * return_value,zend_string * source)223 static zend_bool tokenize_parse(zval *return_value, zend_string *source)
224 {
225 	zval source_zval;
226 	zend_lex_state original_lex_state;
227 	zend_bool original_in_compilation;
228 	zend_bool success;
229 
230 	ZVAL_STR_COPY(&source_zval, source);
231 
232 	original_in_compilation = CG(in_compilation);
233 	CG(in_compilation) = 1;
234 	zend_save_lexical_state(&original_lex_state);
235 
236 	if ((success = (zend_prepare_string_for_scanning(&source_zval, "") == SUCCESS))) {
237 		zval token_stream;
238 		array_init(&token_stream);
239 
240 		CG(ast) = NULL;
241 		CG(ast_arena) = zend_arena_create(1024 * 32);
242 		LANG_SCNG(yy_state) = yycINITIAL;
243 		LANG_SCNG(on_event) = on_event;
244 		LANG_SCNG(on_event_context) = &token_stream;
245 
246 		if((success = (zendparse() == SUCCESS))) {
247 			ZVAL_COPY_VALUE(return_value, &token_stream);
248 		} else {
249 			zval_ptr_dtor(&token_stream);
250 		}
251 
252 		zend_ast_destroy(CG(ast));
253 		zend_arena_destroy(CG(ast_arena));
254 	}
255 
256 	/* restore compiler and scanner global states */
257 	zend_restore_lexical_state(&original_lex_state);
258 	CG(in_compilation) = original_in_compilation;
259 
260 	zval_ptr_dtor_str(&source_zval);
261 
262 	return success;
263 }
264 
265 /* }}} */
266 
267 /* {{{ proto array token_get_all(string source [, int flags])
268  */
PHP_FUNCTION(token_get_all)269 PHP_FUNCTION(token_get_all)
270 {
271 	zend_string *source;
272 	zend_long flags = 0;
273 	zend_bool success;
274 
275 	ZEND_PARSE_PARAMETERS_START(1, 2)
276 		Z_PARAM_STR(source)
277 		Z_PARAM_OPTIONAL
278 		Z_PARAM_LONG(flags)
279 	ZEND_PARSE_PARAMETERS_END();
280 
281 	if (flags & TOKEN_PARSE) {
282 		success = tokenize_parse(return_value, source);
283 	} else {
284 		success = tokenize(return_value, source);
285 		/* Normal token_get_all() should not throw. */
286 		zend_clear_exception();
287 	}
288 
289 	if (!success) RETURN_FALSE;
290 }
291 /* }}} */
292 
293 /* {{{ proto string token_name(int type)
294  */
PHP_FUNCTION(token_name)295 PHP_FUNCTION(token_name)
296 {
297 	zend_long type;
298 
299 	ZEND_PARSE_PARAMETERS_START(1, 1)
300 		Z_PARAM_LONG(type)
301 	ZEND_PARSE_PARAMETERS_END();
302 
303 	RETVAL_STRING(get_token_type_name(type));
304 }
305 /* }}} */
306