php-internal-docs 8.4.8
Unofficial docs for php/php-src
Loading...
Searching...
No Matches
zend_highlight.c
Go to the documentation of this file.
1/*
2 +----------------------------------------------------------------------+
3 | Zend Engine |
4 +----------------------------------------------------------------------+
5 | Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 2.00 of the Zend 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.zend.com/license/2_00.txt. |
11 | If you did not receive a copy of the Zend license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@zend.com so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
15 | Authors: Andi Gutmans <andi@php.net> |
16 | Zeev Suraski <zeev@php.net> |
17 +----------------------------------------------------------------------+
18*/
19
20#include "zend.h"
21#include <zend_language_parser.h>
22#include "zend_compile.h"
23#include "zend_highlight.h"
24#include "zend_ptr_stack.h"
25#include "zend_globals.h"
26#include "zend_exceptions.h"
27
29{
30 switch (c) {
31 case '<':
32 ZEND_PUTS("&lt;");
33 break;
34 case '>':
35 ZEND_PUTS("&gt;");
36 break;
37 case '&':
38 ZEND_PUTS("&amp;");
39 break;
40 case '\t':
41 ZEND_PUTS(" ");
42 break;
43 default:
44 ZEND_PUTC(c);
45 break;
46 }
47}
48
49
50ZEND_API void zend_html_puts(const char *s, size_t len)
51{
52 const unsigned char *ptr = (const unsigned char*)s, *end = ptr + len;
53 unsigned char *filtered = NULL;
54 size_t filtered_len;
55
56 if (LANG_SCNG(output_filter)) {
57 LANG_SCNG(output_filter)(&filtered, &filtered_len, ptr, len);
58 ptr = filtered;
59 end = filtered + filtered_len;
60 }
61
62 while (ptr<end) {
63 if (*ptr==' ') {
64 do {
66 } while ((++ptr < end) && (*ptr==' '));
67 } else {
69 }
70 }
71
72 if (LANG_SCNG(output_filter)) {
73 efree(filtered);
74 }
75}
76
77
79{
80 zval token;
81 int token_type;
82 char *last_color = syntax_highlighter_ini->highlight_html;
83 char *next_color;
84
85 zend_printf("<pre><code style=\"color: %s\">", last_color);
86 /* highlight stuff coming back from zendlex() */
87 while ((token_type=lex_scan(&token, NULL))) {
88 switch (token_type) {
89 case T_INLINE_HTML:
90 next_color = syntax_highlighter_ini->highlight_html;
91 break;
92 case T_COMMENT:
93 case T_DOC_COMMENT:
94 next_color = syntax_highlighter_ini->highlight_comment;
95 break;
96 case T_OPEN_TAG:
98 case T_CLOSE_TAG:
99 case T_LINE:
100 case T_FILE:
101 case T_DIR:
102 case T_TRAIT_C:
103 case T_METHOD_C:
104 case T_FUNC_C:
105 case T_PROPERTY_C:
106 case T_NS_C:
107 case T_CLASS_C:
108 next_color = syntax_highlighter_ini->highlight_default;
109 break;
110 case '"':
113 next_color = syntax_highlighter_ini->highlight_string;
114 break;
115 case T_WHITESPACE:
116 zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng)); /* no color needed */
117 ZVAL_UNDEF(&token);
118 continue;
119 break;
120 default:
121 if (Z_TYPE(token) == IS_UNDEF) {
122 next_color = syntax_highlighter_ini->highlight_keyword;
123 } else {
124 next_color = syntax_highlighter_ini->highlight_default;
125 }
126 break;
127 }
128
129 if (last_color != next_color) {
130 if (last_color != syntax_highlighter_ini->highlight_html) {
131 zend_printf("</span>");
132 }
133 last_color = next_color;
134 if (last_color != syntax_highlighter_ini->highlight_html) {
135 zend_printf("<span style=\"color: %s\">", last_color);
136 }
137 }
138
139 zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
140
141 if (Z_TYPE(token) == IS_STRING) {
142 switch (token_type) {
143 case T_OPEN_TAG:
145 case T_CLOSE_TAG:
146 case T_WHITESPACE:
147 case T_COMMENT:
148 case T_DOC_COMMENT:
149 break;
150 default:
151 zval_ptr_dtor_str(&token);
152 break;
153 }
154 }
155 ZVAL_UNDEF(&token);
156 }
157
158 if (last_color != syntax_highlighter_ini->highlight_html) {
159 zend_printf("</span>");
160 }
161 zend_printf("</code></pre>");
162
163 /* Discard parse errors thrown during tokenization */
165}
166
168{
169 zval token;
170 int token_type;
171 int prev_space = 0;
172
173 while ((token_type=lex_scan(&token, NULL))) {
174 switch (token_type) {
175 case T_WHITESPACE:
176 if (!prev_space) {
177 zend_write(" ", sizeof(" ") - 1);
178 prev_space = 1;
179 }
181 case T_COMMENT:
182 case T_DOC_COMMENT:
183 ZVAL_UNDEF(&token);
184 continue;
185
186 case T_END_HEREDOC:
187 zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
188 /* read the following character, either newline or ; */
189 if (lex_scan(&token, NULL) != T_WHITESPACE) {
190 zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
191 }
192 zend_write("\n", sizeof("\n") - 1);
193 prev_space = 1;
194 ZVAL_UNDEF(&token);
195 continue;
196
197 default:
198 zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
199 break;
200 }
201
202 if (Z_TYPE(token) == IS_STRING) {
203 switch (token_type) {
204 case T_OPEN_TAG:
206 case T_CLOSE_TAG:
207 case T_WHITESPACE:
208 case T_COMMENT:
209 case T_DOC_COMMENT:
210 break;
211
212 default:
213 zval_ptr_dtor_str(&token);
214 break;
215 }
216 }
217 prev_space = 0;
218 ZVAL_UNDEF(&token);
219 }
220
221 /* Discard parse errors thrown during tokenization */
223}
size_t len
Definition apprentice.c:174
char s[4]
Definition cdf.c:77
void * ptr
Definition ffi.c:3814
#define yy_text
Definition ffi_parser.c:31
#define NULL
Definition gdcache.h:45
unsigned const char * end
Definition php_ffi.h:51
const T_COMMENT
const T_OPEN_TAG
const T_END_HEREDOC
const T_CLASS_C
const T_FILE
const T_FUNC_C
const T_METHOD_C
const T_INLINE_HTML
const T_OPEN_TAG_WITH_ECHO
const T_DOC_COMMENT
const T_CONSTANT_ENCAPSED_STRING
const T_WHITESPACE
const T_PROPERTY_C
const T_CLOSE_TAG
const T_TRAIT_C
const T_DIR
const T_LINE
const T_NS_C
const T_ENCAPSED_AND_WHITESPACE
ZEND_API size_t(* zend_printf)(const char *format,...)
Definition zend.c:84
ZEND_API zend_write_func_t zend_write
Definition zend.c:85
#define ZEND_PUTS(str)
Definition zend.h:335
#define ZEND_PUTC(c)
Definition zend.h:337
#define efree(ptr)
Definition zend_alloc.h:155
struct _zval_struct zval
ZEND_API int ZEND_FASTCALL lex_scan(zval *zendlval, zend_parser_stack_elem *elem)
#define ZEND_API
ZEND_API void zend_clear_exception(void)
#define LANG_SCNG(v)
ZEND_API void zend_html_puts(const char *s, size_t len)
ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini)
ZEND_API void zend_html_putc(char c)
ZEND_API void zend_strip(void)
zend_syntax_highlighter_ini syntax_highlighter_ini
struct _zend_syntax_highlighter_ini zend_syntax_highlighter_ini
#define ZEND_FALLTHROUGH
#define ZVAL_UNDEF(z)
#define IS_UNDEF
Definition zend_types.h:600
#define IS_STRING
Definition zend_types.h:606
#define Z_TYPE(zval)
Definition zend_types.h:659