fork(19) download
  1. <?php
  2. # originally from http://n...content-available-to-author-only...b.io/2011/10/23/Improving-lexing-performance-in-PHP.html
  3. class Lexer {
  4. protected $regex;
  5. protected $offsetToToken;
  6.  
  7. public function __construct(array $tokenMap) {
  8. $this->regex = '((' . implode(')|(', array_keys($tokenMap)) . '))A';
  9. $this->offsetToToken = array_values($tokenMap);
  10. }
  11.  
  12. public function lex($string) {
  13. $tokens = array();
  14.  
  15. $offset = 0;
  16. while (isset($string[$offset])) {
  17. if (!preg_match($this->regex, $string, $matches, null, $offset)) {
  18. throw new Exception(sprintf('Unexpected character "%s"', $string[$offset]));
  19. }
  20.  
  21. // find the first non-empty element (but skipping $matches[0]) using a quick for loop
  22. for ($i = 1; '' === $matches[$i]; ++$i);
  23. $tokens[] = array($matches[0], $this->offsetToToken[$i - 1]);
  24. $offset += strlen($matches[0]);
  25. }
  26.  
  27. return $tokens;
  28. }
  29.  
  30. // a recursive function to actually build the structure
  31. function generate($arr=array(), $idx=0) {
  32. $output = array();
  33. $current = null;
  34. for($i=$idx;$i<count($arr);$i++) {
  35. list($element, $type) = $arr[$i];
  36. if ($type == T_OPEN)
  37. $output[$current] = $this->generate($arr, $i+1);
  38. elseif ($type == T_CLOSE)
  39. return $output;
  40. elseif ($type == T_FIELD) {
  41. $output[$element] = null;
  42. $current = $element;
  43. }
  44. }
  45. return $output;
  46. }
  47.  
  48. }
  49.  
  50. // here begins the magic
  51.  
  52. // this is our $tokenMap
  53. $tokenMap = array(
  54. '[^,()]+' => T_FIELD,
  55. ',' => T_SEPARATOR,
  56. '\(' => T_OPEN,
  57. '\)' => T_CLOSE
  58. );
  59.  
  60. // this is your string
  61. $string = "id,topic,member(name,email,group(id,name)),message(id,title,body)";
  62.  
  63. $lex = new Lexer($tokenMap);
  64. $structure = $lex->lex($string);
  65. $output = $lex->generate($structure);
  66. print_r($output);
Success #stdin #stdout #stderr 0.02s 52432KB
stdin
Standard input is empty
stdout
Array
(
    [id] => 
    [topic] => 
    [member] => Array
        (
            [name] => 
            [email] => 
            [group] => Array
                (
                    [id] => 
                    [name] => 
                )

            [id] => 
        )

    [name] => 
    [email] => 
    [group] => Array
        (
            [id] => 
            [name] => 
        )

)
stderr
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 54
PHP Notice:  Use of undefined constant T_SEPARATOR - assumed 'T_SEPARATOR' in /home/0mdush/prog.php on line 55
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 56
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 58
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38
PHP Notice:  Use of undefined constant T_FIELD - assumed 'T_FIELD' in /home/0mdush/prog.php on line 40
PHP Notice:  Use of undefined constant T_OPEN - assumed 'T_OPEN' in /home/0mdush/prog.php on line 36
PHP Notice:  Use of undefined constant T_CLOSE - assumed 'T_CLOSE' in /home/0mdush/prog.php on line 38