zend_indent.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /* This indenter doesn't really work, it's here for no particular reason. */
  21. #include "zend.h"
  22. #include <zend_language_parser.h>
  23. #include "zend_compile.h"
  24. #include "zend_indent.h"
  25. #define zendtext LANG_SCNG(yy_text)
  26. #define zendleng LANG_SCNG(yy_leng)
  27. static void handle_whitespace(int *emit_whitespace)
  28. {
  29. unsigned char c;
  30. int i;
  31. for (c=0; c<128; c++) {
  32. if (emit_whitespace[c]>0) {
  33. for (i=0; i<emit_whitespace[c]; i++) {
  34. zend_write((char *) &c, 1);
  35. }
  36. }
  37. }
  38. memset(emit_whitespace, 0, sizeof(int)*256);
  39. }
  40. ZEND_API void zend_indent()
  41. {
  42. zval token;
  43. int token_type;
  44. int in_string=0;
  45. int nest_level=0;
  46. int emit_whitespace[256];
  47. int i;
  48. TSRMLS_FETCH();
  49. memset(emit_whitespace, 0, sizeof(int)*256);
  50. /* highlight stuff coming back from zendlex() */
  51. token.type = 0;
  52. while ((token_type=lex_scan(&token TSRMLS_CC))) {
  53. switch (token_type) {
  54. case T_INLINE_HTML:
  55. zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  56. break;
  57. case T_WHITESPACE: {
  58. token.type = 0;
  59. /* eat whitespace, emit newlines */
  60. for (i=0; i<LANG_SCNG(yy_leng); i++) {
  61. emit_whitespace[(unsigned char) LANG_SCNG(yy_text)[i]]++;
  62. }
  63. continue;
  64. }
  65. break;
  66. case '"':
  67. in_string = !in_string;
  68. /* break missing intentionally */
  69. default:
  70. if (token.type==0) {
  71. /* keyword */
  72. switch (token_type) {
  73. case ',':
  74. ZEND_PUTS(", ");
  75. goto dflt_printout;
  76. break;
  77. case '{':
  78. nest_level++;
  79. if (emit_whitespace['\n']>0) {
  80. ZEND_PUTS(" {\n");
  81. memset(emit_whitespace, 0, sizeof(int)*256);
  82. } else {
  83. ZEND_PUTS("{");
  84. }
  85. break;
  86. case '}':
  87. nest_level--;
  88. if (emit_whitespace['\n']==0) {
  89. ZEND_PUTS("\n");
  90. }
  91. for (i=0; i<nest_level; i++) {
  92. ZEND_PUTS(" ");
  93. }
  94. goto dflt_printout;
  95. break;
  96. dflt_printout:
  97. default:
  98. if (emit_whitespace['\n']>0) {
  99. for (i=0; i<emit_whitespace['\n']; i++) {
  100. ZEND_PUTS("\n");
  101. }
  102. memset(emit_whitespace, 0, sizeof(int)*256);
  103. for (i=0; i<nest_level; i++) {
  104. ZEND_PUTS(" ");
  105. }
  106. } else {
  107. handle_whitespace(emit_whitespace);
  108. }
  109. zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  110. break;
  111. }
  112. } else {
  113. handle_whitespace(emit_whitespace);
  114. if (in_string) {
  115. zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  116. /* a part of a string */
  117. } else {
  118. zend_write((char*)LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
  119. }
  120. }
  121. break;
  122. }
  123. if (token.type == IS_STRING) {
  124. switch (token_type) {
  125. case T_OPEN_TAG:
  126. case T_CLOSE_TAG:
  127. case T_WHITESPACE:
  128. break;
  129. default:
  130. str_efree(token.value.str.val);
  131. break;
  132. }
  133. }
  134. token.type = 0;
  135. }
  136. }
  137. /*
  138. * Local variables:
  139. * tab-width: 4
  140. * c-basic-offset: 4
  141. * indent-tabs-mode: t
  142. * End:
  143. */