1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 6 |
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 277841 2009-03-26 20:02:53Z felipe $ */
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 17007 : {
87 17007 : tokenizer_register_constants(INIT_FUNC_ARGS_PASSTHRU);
88 17007 : return SUCCESS;
89 : }
90 : /* }}} */
91 :
92 : /* {{{ PHP_MINFO_FUNCTION
93 : */
94 : PHP_MINFO_FUNCTION(tokenizer)
95 43 : {
96 43 : php_info_print_table_start();
97 43 : php_info_print_table_row(2, "Tokenizer Support", "enabled");
98 43 : php_info_print_table_end();
99 43 : }
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 : CG(literal_type) = IS_UNICODE;
111 :
112 67 : array_init(return_value);
113 :
114 67 : ZVAL_NULL(&token);
115 1762 : while ((token_type = lex_scan(&token TSRMLS_CC))) {
116 1628 : destroy = 1;
117 1628 : switch (token_type) {
118 : case T_CLOSE_TAG:
119 40 : if (zendtext[zendleng - 1] != '>') {
120 0 : CG(zend_lineno)++;
121 : }
122 : case T_OPEN_TAG:
123 : case T_OPEN_TAG_WITH_ECHO:
124 : case T_WHITESPACE:
125 : case T_COMMENT:
126 : case T_DOC_COMMENT:
127 670 : destroy = 0;
128 : break;
129 : }
130 :
131 1628 : if (token_type >= 256) {
132 1206 : MAKE_STD_ZVAL(keyword);
133 1206 : array_init(keyword);
134 1206 : add_next_index_long(keyword, token_type);
135 1206 : if (token_type == T_END_HEREDOC) {
136 3 : if (CG(increment_lineno)) {
137 3 : token_line = ++CG(zend_lineno);
138 3 : CG(increment_lineno) = 0;
139 : }
140 3 : add_next_index_stringl(keyword, Z_STRVAL(token), Z_STRLEN(token), 1);
141 3 : efree(Z_STRVAL(token));
142 : } else {
143 1203 : add_next_index_stringl(keyword, (char *)zendtext, zendleng, 1);
144 : }
145 1206 : add_next_index_long(keyword, token_line);
146 1206 : add_next_index_zval(return_value, keyword);
147 : } else {
148 422 : add_next_index_stringl(return_value, (char *)zendtext, zendleng, 1);
149 : }
150 1628 : if (destroy && Z_TYPE(token) != IS_NULL) {
151 350 : zval_dtor(&token);
152 : }
153 1628 : ZVAL_NULL(&token);
154 :
155 1628 : token_line = CG(zend_lineno);
156 : }
157 67 : }
158 :
159 : /* {{{ proto array token_get_all(string source)
160 : */
161 : PHP_FUNCTION(token_get_all)
162 77 : {
163 77 : char *source = NULL;
164 77 : int argc = ZEND_NUM_ARGS();
165 : int source_len;
166 : zval source_z;
167 : zend_lex_state original_lex_state;
168 :
169 77 : if (zend_parse_parameters(argc TSRMLS_CC, "s", &source, &source_len) == FAILURE)
170 10 : return;
171 :
172 67 : ZVAL_STRINGL(&source_z, source, source_len, 1);
173 67 : zend_save_lexical_state(&original_lex_state TSRMLS_CC);
174 :
175 67 : if (zend_prepare_string_for_scanning(&source_z, "" TSRMLS_CC) == FAILURE) {
176 0 : zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
177 0 : RETURN_EMPTY_STRING();
178 : }
179 :
180 67 : LANG_SCNG(yy_state) = yycINITIAL;
181 :
182 67 : tokenize(return_value TSRMLS_CC);
183 :
184 67 : zend_restore_lexical_state(&original_lex_state TSRMLS_CC);
185 67 : zval_dtor(&source_z);
186 : }
187 : /* }}} */
188 :
189 : /* {{{ proto string token_name(int type)
190 : */
191 : PHP_FUNCTION(token_name)
192 121 : {
193 121 : int argc = ZEND_NUM_ARGS();
194 : long type;
195 :
196 121 : if (zend_parse_parameters(argc TSRMLS_CC, "l", &type) == FAILURE) {
197 2 : return;
198 : }
199 119 : RETVAL_STRING(get_token_type_name(type), 1);
200 : }
201 : /* }}} */
202 :
203 : /*
204 : * Local variables:
205 : * tab-width: 4
206 : * c-basic-offset: 4
207 : * End:
208 : * vim600: noet sw=4 ts=4 fdm=marker
209 : * vim<600: noet sw=4 ts=4
210 : */
|