Back to index

php5  5.3.10
zend_indent.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | Zend Engine                                                          |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1998-2012 Zend Technologies Ltd. (http://www.zend.com) |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 2.00 of the Zend license,     |
00008    | that is bundled with this package in the file LICENSE, and is        | 
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.zend.com/license/2_00.txt.                                |
00011    | If you did not receive a copy of the Zend license and are unable to  |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@zend.com so we can mail you a copy immediately.              |
00014    +----------------------------------------------------------------------+
00015    | Authors: Andi Gutmans <andi@zend.com>                                |
00016    |          Zeev Suraski <zeev@zend.com>                                |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: zend_indent.c 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 /* This indenter doesn't really work, it's here for no particular reason. */
00023 
00024 
00025 #include "zend.h"
00026 #include <zend_language_parser.h>
00027 #include "zend_compile.h"
00028 #include "zend_indent.h"
00029 
00030 #define zendtext LANG_SCNG(yy_text)
00031 #define zendleng LANG_SCNG(yy_leng)
00032 
00033 
00034 static void handle_whitespace(int *emit_whitespace)
00035 {
00036        unsigned char c;
00037        int i;
00038 
00039        for (c=0; c<128; c++) {
00040               if (emit_whitespace[c]>0) {
00041                      for (i=0; i<emit_whitespace[c]; i++) {
00042                             zend_write((char *) &c, 1);
00043                      }
00044               }
00045        }
00046        memset(emit_whitespace, 0, sizeof(int)*256);
00047 }
00048 
00049 
00050 ZEND_API void zend_indent()
00051 {
00052        zval token;
00053        int token_type;
00054        int in_string=0;
00055        int nest_level=0;
00056        int emit_whitespace[256];
00057        int i;
00058        TSRMLS_FETCH();
00059 
00060        memset(emit_whitespace, 0, sizeof(int)*256);
00061 
00062        /* highlight stuff coming back from zendlex() */
00063        token.type = 0;
00064        while ((token_type=lex_scan(&token TSRMLS_CC))) {
00065               switch (token_type) {
00066                      case T_INLINE_HTML:
00067                             zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
00068                             break;
00069                      case T_WHITESPACE: {
00070                                    token.type = 0;
00071                                    /* eat whitespace, emit newlines */
00072                                    for (i=0; i<LANG_SCNG(yy_leng); i++) {
00073                                           emit_whitespace[(unsigned char) LANG_SCNG(yy_text)[i]]++;
00074                                    }
00075                                    continue;
00076                             }
00077                             break;
00078                      case '"':
00079                             in_string = !in_string;
00080                             /* break missing intentionally */
00081                      default:
00082                             if (token.type==0) {
00083                                    /* keyword */
00084                                    switch (token_type) {
00085                                           case ',':
00086                                                  ZEND_PUTS(", ");
00087                                                  goto dflt_printout;
00088                                                  break;
00089                                           case '{':
00090                                                  nest_level++;
00091                                                  if (emit_whitespace['\n']>0) {
00092                                                         ZEND_PUTS(" {\n");
00093                                                         memset(emit_whitespace, 0, sizeof(int)*256);
00094                                                  } else {
00095                                                         ZEND_PUTS("{");
00096                                                  }
00097                                                  break;
00098                                           case '}':
00099                                                  nest_level--;
00100                                                  if (emit_whitespace['\n']==0) {
00101                                                         ZEND_PUTS("\n");
00102                                                  }
00103                                                  for (i=0; i<nest_level; i++) {
00104                                                         ZEND_PUTS("    ");
00105                                                  }
00106                                                  goto dflt_printout;
00107                                                  break;
00108 dflt_printout:
00109                                           default:
00110                                                  if (emit_whitespace['\n']>0) {
00111                                                         for (i=0; i<emit_whitespace['\n']; i++) {
00112                                                                ZEND_PUTS("\n");
00113                                                         }
00114                                                         memset(emit_whitespace, 0, sizeof(int)*256);
00115                                                         for (i=0; i<nest_level; i++) {
00116                                                                ZEND_PUTS("    ");
00117                                                         }
00118                                                  } else {
00119                                                         handle_whitespace(emit_whitespace);
00120                                                  }
00121                                                  zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
00122                                                  break;
00123                                    }
00124                             } else {
00125                                    handle_whitespace(emit_whitespace);
00126                                    if (in_string) {
00127                                           zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
00128                                           /* a part of a string */
00129                                    } else {
00130                                           zend_write(LANG_SCNG(yy_text), LANG_SCNG(yy_leng));
00131                                    }
00132                             }
00133                             break;
00134               }
00135               if (token.type == IS_STRING) {
00136                      switch (token_type) {
00137                      case T_OPEN_TAG:
00138                      case T_CLOSE_TAG:
00139                      case T_WHITESPACE:
00140                             break;
00141                      default:
00142                             efree(token.value.str.val);
00143                             break;
00144                      }
00145               }
00146               token.type = 0;
00147        }
00148 }
00149 
00150 /*
00151  * Local variables:
00152  * tab-width: 4
00153  * c-basic-offset: 4
00154  * indent-tabs-mode: t
00155  * End:
00156  */