File indexing completed on 2024-04-28 15:52:52

0001 <?php
0002 //compares the tokens returned by token_get_all with the lexer
0003 
0004 if (!isset($_SERVER['argv'][1])) {
0005     die("Usage: {$_SERVER['argv'][0]} [FILE | --code CODE]\n");
0006 }
0007 
0008 if ( $_SERVER['argv'][1] == '--code' ) {
0009     echo "processing code:\n\n";
0010     processString($_SERVER['argv'][2]);
0011 } else {
0012     unset($_SERVER['argv'][0]);
0013     foreach ($_SERVER['argv'] as $file) {
0014         echo "processing file $file:\n\n";
0015         processString(file_get_contents($file));
0016     }
0017 }
0018 
0019 function processString($c) {
0020     $tokens = array();
0021     $start = microtime(true);
0022     $c = str_replace("\r", "", $c);
0023     foreach (token_get_all($c) as $i) {
0024         if (is_string($i)) {
0025             $name = $i;
0026             $chars = $i;
0027         } else if (is_int($i[0])) {
0028             $name = token_name($i[0]);
0029             $chars = $i[1];
0030         } else {
0031             $name = $i[0];
0032             $chars = $i[1];
0033         }
0034         if (!$name) $name = $i[1];
0035         $tokens[] = str_replace(array("\r", "\n"), array('\r', '\n'), $chars) . ' ' . $name;
0036     }
0037     $phpTime = microtime(true) - $start;
0038 
0039     var_dump($tokens);
0040 
0041     $out = array();
0042     $start = microtime(true);
0043     exec("php-parser --print-tokens --code ".escapeshellarg($c), $out, $ret);
0044     $parserTime = microtime(true) - $start;
0045     if ($ret != 0) {
0046         echo "php-parser failed\n";
0047         exit(255);
0048     }
0049 
0050     unset($out[0]); //remove "Parsing file ..."
0051     array_pop($out); //remove "successfully parsed"
0052     array_pop($out); //remove "end of file"
0053     $diff = array_diff($tokens, $out);
0054     if (!$diff || (count($tokens) != count($out))) {
0055         echo "code correctly tokenized ($parserTime / $phpTime)...\n";
0056     } else {
0057         echo "******* parser output:\n";
0058         out($out);
0059         echo "******* expected:\n";
0060         out($tokens);
0061         echo "******* differences in code:\n";
0062         out($diff);
0063         exit(255);
0064     }
0065 }
0066 
0067 function out($a) {
0068     foreach ($a as $i) {
0069         echo "'$i'\n";
0070     }
0071 }
0072 
0073