Subversion Repositories fastphp

Rev

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

  1. <?php
  2.  
  3. define('ICON_ATTR_PUBLIC',           1);
  4. define('ICON_ATTR_PRIVATE',          2);
  5. define('ICON_ATTR_PROTECTED',        4);
  6. define('ICON_ATTR_ABSTRACT',         8);
  7. define('ICON_ATTR_STATIC',          16);
  8. define('ICON_ATTR_CONST',           32);
  9. define('ICON_ATTR_VAR',             64);
  10. define('ICON_ATTR_FUNCTION',       128);
  11. define('ICON_ATTR_CONSTRUCTOR',    256);
  12. define('ICON_ATTR_DESTRUCTOR',     512);
  13. define('ICON_ATTR_MAGICMETHOD',   1024);
  14. define('ICON_ATTR_CLASS',         2048);
  15. define('ICON_ATTR_TRAIT',         4096);
  16. define('ICON_ATTR_INTERFACE',     8192);
  17.  
  18. error_reporting(0);
  19.  
  20. while (true) {
  21.         $lines = array();
  22.         while ($f = fgets(STDIN)){
  23.                 if (trim($f) == chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8)) break;
  24.                 $lines[] = $f;
  25.         }
  26.         $code = implode("", $lines);
  27.  
  28.         echo "FAST100!\n";
  29.        
  30.         $x = token_get_all($code);
  31.        
  32.         // file_put_contents('debug.tmp', $code);
  33.        
  34.         if (!$x) {
  35.                 // TODO: when this happens, do not update the output in the editor...
  36.                 echo _outputLeafNode(2048, 1, 'SYNTAX ERROR');
  37.         }
  38.        
  39.         //print_r($x); // debug
  40.         $wait_function = false;
  41.         $wait_class = false;
  42.         $class = array();
  43.         $icon_add_flags = 0;
  44.         $dep = 0;
  45.  
  46.         foreach ($x as $n => $data) {
  47.                 if ($data == '{') $dep++;
  48.                 if ($data == '}') {
  49.                         $dep--;
  50.                         if ((count($class) > 0) && (array_peek($class)[1] == $dep)) {
  51.                                 array_pop($class);
  52.                                 echo _outputDecreaseLevel();
  53.                         }
  54.                 }
  55.  
  56.                 $token = (!is_array($data)) ? null : $data[0];
  57.                 $value = (!is_array($data)) ? null : $data[1];
  58.                 $line = (!is_array($data)) ? null : $data[2];
  59.  
  60.                 if ($wait_function && ($token == T_STRING)) {
  61.                         $desc = "function ";
  62.                         /*
  63.                         foreach ($class as $cdata) {
  64.                                 $desc .= $cdata[0].'->';
  65.                         }
  66.                         */
  67.                         $desc .= "$value()";
  68.                         $icon_add_flags = 0;
  69.                         $wait_function = false;
  70.  
  71.                         if ($value == '__construct') { // TODO: auch eine methode mit dem namen der klasse soll eine konstruktor sein
  72.                                 echo _outputLeafNode(ICON_ATTR_CONSTRUCTOR | $icon_add_flags, $line, $desc);
  73.                         } else if ($value == '__destruct') {
  74.                                 echo _outputLeafNode(ICON_ATTR_DESTRUCTOR  | $icon_add_flags, $line, $desc);
  75.                         } else if (substr($value, 0, 2) == '__') {
  76.                                 echo _outputLeafNode(ICON_ATTR_MAGICMETHOD | $icon_add_flags, $line, $desc);
  77.                         } else {
  78.                                 echo _outputLeafNode(ICON_ATTR_FUNCTION    | $icon_add_flags, $line, $desc);
  79.                         }
  80.                 }
  81.  
  82.                 if ($wait_class && ($token == T_STRING)) {
  83.                         $desc = "Class ";
  84.                         /*
  85.                         foreach ($class as $cdata) {
  86.                                 $desc .= $cdata[0].'->';
  87.                         }
  88.                         */
  89.                         $desc .= "$value\n";
  90.                         $class[] = array($value, $dep);
  91.                         $wait_class = false;
  92.  
  93.                         echo _outputLeafNode(ICON_ATTR_CLASS | $icon_add_flags, $line, $desc);
  94.                         echo _outputIncreaseLevel();
  95.                 }
  96.  
  97.                 if ($token == T_PUBLIC)    $icon_add_flags |= ICON_ATTR_PUBLIC;
  98.                 if ($token == T_PRIVATE)   $icon_add_flags |= ICON_ATTR_PRIVATE;
  99.                 if ($token == T_PROTECTED) $icon_add_flags |= ICON_ATTR_PROTECTED;
  100.                 if ($token == T_STATIC)    $icon_add_flags |= ICON_ATTR_STATIC;
  101.                 if (($data == ';') || ($data == '{') || ($data == '}')) $icon_add_flags = 0;
  102.  
  103.                 if ($token == T_FUNCTION) {
  104.                         $wait_function = true;
  105.                 }
  106.                 if ($token == T_CLASS) {
  107.                         $wait_class = true;
  108.                         $dep = 0;
  109.                 }
  110.  
  111.                 if ($token == T_COMMENT) {
  112.                         if (stripos($value, 'TODO') !== false) {
  113.                                 echo _outputLeafNode(0/*TODO*/, $line, $value);
  114.                         }
  115.                 }
  116.         }
  117.        
  118.         echo _outputExit();
  119.  
  120.         echo chr(1).chr(2).chr(3).chr(4).chr(5).chr(6).chr(7).chr(8);
  121.  
  122.         sleep(1);
  123. }
  124.  
  125. function _outputLeafNode($icon, $line, $description) {
  126.         return 'N'.
  127.              sprintf('%08s', $icon).
  128.              sprintf('%08s', $line).
  129.              sprintf('%04s', strlen(trim($description))).
  130.              trim($description)."\n";
  131. }
  132.  
  133. function _outputIncreaseLevel() {
  134.         return 'I'."\n";
  135. }
  136.  
  137. function _outputDecreaseLevel() {
  138.         return 'D'."\n";
  139. }
  140.  
  141. function _outputExit() {
  142.         return 'X'."\n";
  143. }
  144.  
  145. function array_peek($array) {
  146.         if (!isset($array[count($array)-1])) return null;
  147.         return $array[count($array)-1];
  148. }
  149.  
  150. function strip_comment($x) {
  151.         if (substr($x, 0, 1) == '#') return trim(substr($x, 1));
  152.         if (substr($x, 0, 2) == '//') return trim(substr($x, 2));
  153.         if (substr($x, 0, 2) == '/*') return trim(substr($x, 2, strlen($x)-4));
  154. }
  155.  
  156.  
  157. // TODO: hallo
  158. # TODO: xxx
  159.