Subversion Repositories fastphp

Rev

Rev 33 | Rev 37 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. <?php
  2.  
  3. // TODO: show full signature of each element?
  4.  
  5. error_reporting(0);
  6.  
  7. define('ICON_TYPE_FUNCTION',       'FUN');
  8. define('ICON_TYPE_CONSTRUCTOR',    'CST');
  9. define('ICON_TYPE_DESTRUCTOR',     'DST');
  10. define('ICON_TYPE_MAGICMETHOD',    'MAG');
  11. define('ICON_TYPE_CLASS',          'CLS');
  12. define('ICON_TYPE_TRAIT',          'TRA');
  13. define('ICON_TYPE_INTERFACE',      'INT');
  14. define('ICON_TYPE_VAR',            'VAR');
  15. define('ICON_TYPE_CONST',          'CON');
  16. define('ICON_TYPE_TODO',           'TDO');
  17. define('ICON_TYPE_ERROR',          'ERR');
  18.  
  19. while (true) {
  20.         $lines = array();
  21.         while ($f = fgets(STDIN)){
  22.                 if (trim($f) == chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8)) break;
  23.                 $lines[] = $f;
  24.         }
  25.         $code = implode("", $lines);
  26.  
  27.         echo "FAST100!\n";
  28.        
  29.         $x = token_get_all($code);
  30.        
  31.         if (!$x) {
  32.                 echo _outputLeafNode(ICON_TYPE_ERROR.implode('', $icon_add_flags), 1, 'SYNTAX ERROR');
  33.         }
  34.        
  35.         $wait_function = false;
  36.         $wait_class = false;
  37.         $wait_trait = false;
  38.         $wait_interface = false;
  39.         $wait_abstract_func_list_end = false;
  40.         $levelAry = array();
  41.         $icon_add_flags = array('_', '_', '_');
  42.         $dep = 0;
  43.         $insideFuncAry = array();
  44.  
  45.         foreach ($x as $n => $data) {
  46.                 if ($data == '{') $dep++;
  47.                 if ($data == '}') {
  48.                         $dep--;
  49.                         if ((count($levelAry) > 0) && (array_peek($levelAry) == $dep)) {
  50.                                 array_pop($levelAry);
  51.                                 echo _outputDecreaseLevel();
  52.                         }
  53.                         if ((count($insideFuncAry) > 0) && (array_peek($insideFuncAry) == $dep)) {
  54.                                 array_pop($insideFuncAry);
  55.                         }
  56.                 }
  57.  
  58.                 $token = (!is_array($data)) ? null : $data[0];
  59.                 $value = (!is_array($data)) ? null : $data[1];
  60.                 $line  = (!is_array($data)) ? null : $data[2];
  61.  
  62.                 if ($wait_function && ($token == T_STRING)) {
  63.                         $wait_function = false;
  64.                         if ($icon_add_flags[1] == 'A') {
  65.                                 $desc = "abstract function $value()";
  66.                                 $wait_abstract_func_list_end = true;
  67.                         } else {
  68.                                 $desc = "function $value()";
  69.                                 $insideFuncAry[] = $dep;
  70.                         }
  71.  
  72.                         if ($value == '__construct') { // TODO: auch eine methode mit dem namen der klasse soll eine konstruktor sein
  73.                                 echo _outputLeafNode(ICON_TYPE_CONSTRUCTOR.implode('', $icon_add_flags), $line, $desc);
  74.                         } else if ($value == '__destruct') {
  75.                                 echo _outputLeafNode(ICON_TYPE_DESTRUCTOR.implode('', $icon_add_flags), $line, $desc);
  76.                         } else if (substr($value, 0, 2) == '__') {
  77.                                 echo _outputLeafNode(ICON_TYPE_MAGICMETHOD.implode('', $icon_add_flags), $line, $desc);
  78.                         } else {
  79.                                 echo _outputLeafNode(ICON_TYPE_FUNCTION.implode('', $icon_add_flags), $line, $desc);
  80.                         }
  81.  
  82.                         $icon_add_flags = array('_', '_', '_');
  83.                 }
  84.  
  85.                 if ($wait_class && ($token == T_STRING)) {
  86.                         if ($icon_add_flags[1] == 'A') {
  87.                                 $desc = "abstract class $value\n";
  88.                         } else {
  89.                                 $desc = "class $value\n";
  90.                         }
  91.                         $wait_class = false;
  92.                         $levelAry[] = $dep;
  93.  
  94.                         echo _outputLeafNode(ICON_TYPE_CLASS.implode('', $icon_add_flags), $line, $desc);
  95.                         $icon_add_flags = array('_', '_', '_');
  96.  
  97.                         echo _outputIncreaseLevel();
  98.                 }
  99.  
  100.                 if ($wait_trait && ($token == T_STRING)) {
  101.                         $desc = "Trait $value\n";
  102.                         $wait_trait = false;
  103.                         $levelAry[] = $dep;
  104.  
  105.                         echo _outputLeafNode(ICON_TYPE_TRAIT.implode('', $icon_add_flags), $line, $desc);
  106.                         $icon_add_flags = array('_', '_', '_');
  107.  
  108.                         echo _outputIncreaseLevel();
  109.                 }
  110.  
  111.                 if ($wait_interface && ($token == T_STRING)) {
  112.                         $desc = "Interface $value\n";
  113.                         $wait_interface = false;
  114.                         $levelAry[] = $dep;
  115.  
  116.                         echo _outputLeafNode(ICON_TYPE_INTERFACE.implode('', $icon_add_flags), $line, $desc);
  117.                         $icon_add_flags = array('_', '_', '_');
  118.  
  119.                         echo _outputIncreaseLevel();
  120.                 }
  121.  
  122.                 if ($wait_const && ($token == T_STRING)) {
  123.                         $desc = "const $value\n";
  124.                         $wait_const = false;
  125.  
  126.                         echo _outputLeafNode(ICON_TYPE_CONST.implode('', $icon_add_flags), $line, $desc);
  127.                         $icon_add_flags = array('_', '_', '_');
  128.                 }
  129.  
  130.                 if ((!$wait_abstract_func_list_end) && (count($levelAry) > 0) && (count($insideFuncAry) == 0) && ($token == T_VARIABLE)) {
  131.                         $desc = "$value\n";
  132.  
  133.                         echo _outputLeafNode(ICON_TYPE_VAR.implode('', $icon_add_flags), $line, $desc);
  134.                         $icon_add_flags = array('_', '_', '_');
  135.                 }
  136.  
  137.                 if ($token == T_PRIVATE)   $icon_add_flags[0] = '1';
  138.                 if ($token == T_PROTECTED) $icon_add_flags[0] = '2';
  139.                 if ($token == T_PUBLIC)    $icon_add_flags[0] = '3';
  140.                 if ($token == T_ABSTRACT)  $icon_add_flags[1] = 'A';
  141.                 if ($token == T_FINAL)     $icon_add_flags[1] = 'F';
  142.                 if ($token == T_STATIC)    $icon_add_flags[2] = 'S';
  143.  
  144.                 if (($data == ';') || ($data == '{') || ($data == '}')) {
  145.                         $wait_abstract_func_list_end = false;
  146.                         $icon_add_flags = array('_', '_', '_');
  147.                 }
  148.  
  149.                 if ($token == T_FUNCTION) {
  150.                         $wait_function = true;
  151.                 }
  152.                 if ($token == T_CLASS) {
  153.                         $wait_class = true;
  154.                         $dep = 0;
  155.                 }
  156.                 if ($token == T_INTERFACE) {
  157.                         $wait_interface = true;
  158.                         $dep = 0;
  159.                 }
  160.                 if ($token == T_TRAIT) {
  161.                         $wait_trait = true;
  162.                         $dep = 0;
  163.                 }
  164.  
  165.                 if ($token == T_CONST) {
  166.                         $wait_const = true;
  167.                 }
  168.  
  169.                 if ($token == T_COMMENT) {
  170.                         if ((stripos($value, 'TODO')   !== false) ||
  171.                             (stripos($value, 'BUGBUG') !== false) ||
  172.                             (stripos($value, 'FIXME')  !== false) ||
  173.                             (stripos($value, 'XXX')    !== false)) {
  174.                                 echo _outputLeafNode(ICON_TYPE_TODO.implode('', $icon_add_flags), $line, strip_comment($value));
  175.                         }
  176.                 }
  177.         }
  178.        
  179.         echo _outputExit();
  180.  
  181.         echo chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8);
  182.  
  183.         sleep(1);
  184. }
  185.  
  186. function _iconCodeToIndex($icon) {
  187.         $typ = substr($icon, 0, 3);
  188.  
  189.         if ($icon[3] == '_') $icon[3] = '3'; // public is default visibility
  190.  
  191.         if (($typ == 'MAG') || ($typ == 'CST') || ($typ == 'DST')) {
  192.                 $typ = 'FUN';
  193.         } else if ($typ == 'INT') {
  194.                 $typ = 'CLS';
  195.                 $icon[4] = 'A';
  196.         }
  197.  
  198.              if (($typ == 'CLS')                      && ($icon[4] != 'A')) return  0;
  199.         else if (($typ == 'CLS')                      && ($icon[4] == 'A')) return  1;
  200.         else if (($typ == 'TRA')                                          ) return  2;
  201.         else if (($typ == 'CON') && ($icon[3] == '1')                     ) return  3;
  202.         else if (($typ == 'VAR') && ($icon[3] == '1')                     ) return  4;
  203.         else if (($typ == 'FUN') && ($icon[3] == '1') && ($icon[4] != 'A')) return  5;
  204.         else if (($typ == 'FUN') && ($icon[3] == '1') && ($icon[4] == 'A')) return  6;
  205.         else if (($typ == 'CON') && ($icon[3] == '2')                     ) return  7;
  206.         else if (($typ == 'VAR') && ($icon[3] == '2')                     ) return  8;
  207.         else if (($typ == 'FUN') && ($icon[3] == '2') && ($icon[4] != 'A')) return  9;
  208.         else if (($typ == 'FUN') && ($icon[3] == '2') && ($icon[4] == 'A')) return 10;
  209.         else if (($typ == 'CON') && ($icon[3] == '3')                     ) return 11;
  210.         else if (($typ == 'VAR') && ($icon[3] == '3')                     ) return 12;
  211.         else if (($typ == 'FUN') && ($icon[3] == '3') && ($icon[4] != 'A')) return 13;
  212.         else if (($typ == 'FUN') && ($icon[3] == '3') && ($icon[4] == 'A')) return 14;
  213.         else if (($typ == 'TDO')                                          ) return 15;
  214.         else                                                                return -1;
  215. }
  216.  
  217. function _outputLeafNode($icon, $line, $description) {
  218.         $imageindex = _iconCodeToIndex($icon);
  219.         return 'N'.($imageindex == -1 ? '____' : sprintf('%04s', $imageindex)).
  220.                    sprintf('%08s', $line).
  221.                    sprintf('%04s', strlen(trim($description))).
  222.                    trim($description).
  223.                    "\n";
  224. }
  225.  
  226. function _outputIncreaseLevel() {
  227.         return 'I'."\n";
  228. }
  229.  
  230. function _outputDecreaseLevel() {
  231.         return 'D'."\n";
  232. }
  233.  
  234. function _outputExit() {
  235.         return 'X'."\n";
  236. }
  237.  
  238. function array_peek($array) {
  239.         if (!isset($array[count($array)-1])) return null;
  240.         return $array[count($array)-1];
  241. }
  242.  
  243. function strip_comment($x) {
  244.         if (substr($x, 0, 1) == '#') return trim(substr($x, 1));
  245.         if (substr($x, 0, 2) == '//') return trim(substr($x, 2));
  246.         if (substr($x, 0, 2) == '/*') return trim(substr($x, 2, strlen($x)-4));
  247. }
  248.  
  249.