You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.9 KiB

27 years ago
27 years ago
24 years ago
24 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
10 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
24 years ago
24 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
27 years ago
10 years ago
27 years ago
10 years ago
27 years ago
27 years ago
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Zend Engine |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1998-2016 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@zend.com> |
  16. | Zeev Suraski <zeev@zend.com> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /* $Id$ */
  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. ZEND_API void zend_html_putc(char c)
  28. {
  29. switch (c) {
  30. case '\n':
  31. ZEND_PUTS("<br />");
  32. break;
  33. case '<':
  34. ZEND_PUTS("&lt;");
  35. break;
  36. case '>':
  37. ZEND_PUTS("&gt;");
  38. break;
  39. case '&':
  40. ZEND_PUTS("&amp;");
  41. break;
  42. case ' ':
  43. ZEND_PUTS("&nbsp;");
  44. break;
  45. case '\t':
  46. ZEND_PUTS("&nbsp;&nbsp;&nbsp;&nbsp;");
  47. break;
  48. default:
  49. ZEND_PUTC(c);
  50. break;
  51. }
  52. }
  53. ZEND_API void zend_html_puts(const char *s, size_t len)
  54. {
  55. const unsigned char *ptr = (const unsigned char*)s, *end = ptr + len;
  56. unsigned char *filtered = NULL;
  57. size_t filtered_len;
  58. if (LANG_SCNG(output_filter)) {
  59. LANG_SCNG(output_filter)(&filtered, &filtered_len, ptr, len);
  60. ptr = filtered;
  61. end = filtered + filtered_len;
  62. }
  63. while (ptr<end) {
  64. if (*ptr==' ') {
  65. do {
  66. zend_html_putc(*ptr);
  67. } while ((++ptr < end) && (*ptr==' '));
  68. } else {
  69. zend_html_putc(*ptr++);
  70. }
  71. }
  72. if (LANG_SCNG(output_filter)) {
  73. efree(filtered);
  74. }
  75. }
  76. ZEND_API void zend_highlight(zend_syntax_highlighter_ini *syntax_highlighter_ini)
  77. {
  78. zval token;
  79. int token_type;
  80. char *last_color = syntax_highlighter_ini->highlight_html;
  81. char *next_color;
  82. zend_printf("<code>");
  83. zend_printf("<span style=\"color: %s\">\n", last_color);
  84. /* highlight stuff coming back from zendlex() */
  85. ZVAL_UNDEF(&token);
  86. while ((token_type=lex_scan(&token))) {
  87. switch (token_type) {
  88. case T_INLINE_HTML:
  89. next_color = syntax_highlighter_ini->highlight_html;
  90. break;
  91. case T_COMMENT:
  92. case T_DOC_COMMENT:
  93. next_color = syntax_highlighter_ini->highlight_comment;
  94. break;
  95. case T_OPEN_TAG:
  96. case T_OPEN_TAG_WITH_ECHO:
  97. case T_CLOSE_TAG:
  98. case T_LINE:
  99. case T_FILE:
  100. case T_DIR:
  101. case T_TRAIT_C:
  102. case T_METHOD_C:
  103. case T_FUNC_C:
  104. case T_NS_C:
  105. case T_CLASS_C:
  106. next_color = syntax_highlighter_ini->highlight_default;
  107. break;
  108. case '"':
  109. case T_ENCAPSED_AND_WHITESPACE:
  110. case T_CONSTANT_ENCAPSED_STRING:
  111. next_color = syntax_highlighter_ini->highlight_string;
  112. break;
  113. case T_WHITESPACE:
  114. zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng)); /* no color needed */
  115. ZVAL_UNDEF(&token);
  116. continue;
  117. break;
  118. default:
  119. if (Z_TYPE(token) == IS_UNDEF) {
  120. next_color = syntax_highlighter_ini->highlight_keyword;
  121. } else {
  122. next_color = syntax_highlighter_ini->highlight_default;
  123. }
  124. break;
  125. }
  126. if (last_color != next_color) {
  127. if (last_color != syntax_highlighter_ini->highlight_html) {
  128. zend_printf("</span>");
  129. }
  130. last_color = next_color;
  131. if (last_color != syntax_highlighter_ini->highlight_html) {
  132. zend_printf("<span style=\"color: %s\">", last_color);
  133. }
  134. }
  135. zend_html_puts((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  136. if (Z_TYPE(token) == IS_STRING) {
  137. switch (token_type) {
  138. case T_OPEN_TAG:
  139. case T_OPEN_TAG_WITH_ECHO:
  140. case T_CLOSE_TAG:
  141. case T_WHITESPACE:
  142. case T_COMMENT:
  143. case T_DOC_COMMENT:
  144. break;
  145. default:
  146. zend_string_release(Z_STR(token));
  147. break;
  148. }
  149. }
  150. ZVAL_UNDEF(&token);
  151. }
  152. if (last_color != syntax_highlighter_ini->highlight_html) {
  153. zend_printf("</span>\n");
  154. }
  155. zend_printf("</span>\n");
  156. zend_printf("</code>");
  157. /* Discard parse errors thrown during tokenization */
  158. zend_clear_exception();
  159. }
  160. ZEND_API void zend_strip(void)
  161. {
  162. zval token;
  163. int token_type;
  164. int prev_space = 0;
  165. ZVAL_UNDEF(&token);
  166. while ((token_type=lex_scan(&token))) {
  167. switch (token_type) {
  168. case T_WHITESPACE:
  169. if (!prev_space) {
  170. zend_write(" ", sizeof(" ") - 1);
  171. prev_space = 1;
  172. }
  173. /* lack of break; is intentional */
  174. case T_COMMENT:
  175. case T_DOC_COMMENT:
  176. ZVAL_UNDEF(&token);
  177. continue;
  178. case T_END_HEREDOC:
  179. zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  180. /* read the following character, either newline or ; */
  181. if (lex_scan(&token) != T_WHITESPACE) {
  182. zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  183. }
  184. zend_write("\n", sizeof("\n") - 1);
  185. prev_space = 1;
  186. ZVAL_UNDEF(&token);
  187. continue;
  188. default:
  189. zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  190. break;
  191. }
  192. if (Z_TYPE(token) == IS_STRING) {
  193. switch (token_type) {
  194. case T_OPEN_TAG:
  195. case T_OPEN_TAG_WITH_ECHO:
  196. case T_CLOSE_TAG:
  197. case T_WHITESPACE:
  198. case T_COMMENT:
  199. case T_DOC_COMMENT:
  200. break;
  201. default:
  202. zend_string_release(Z_STR(token));
  203. break;
  204. }
  205. }
  206. prev_space = 0;
  207. ZVAL_UNDEF(&token);
  208. }
  209. /* Discard parse errors thrown during tokenization */
  210. zend_clear_exception();
  211. }
  212. /*
  213. * Local variables:
  214. * tab-width: 4
  215. * c-basic-offset: 4
  216. * indent-tabs-mode: t
  217. * End:
  218. */