fork download
  1. <?php
  2. function getFunction($input, &$results) {
  3. $input = trim($input);
  4.  
  5. $result = array();
  6. $moved = 0;
  7.  
  8. //echo sprintf('Parsing: "%s"' . "\n", $input);
  9.  
  10. if(preg_match('/\A\&([a-z]+)\(/', $input, $matches)) {
  11. $matchlength = strlen($matches[0]);
  12.  
  13. $remaining = substr($input, $matchlength);
  14. $moved += $matchlength;
  15.  
  16. $result['type'] = 'call';
  17. $result['name'] = $matches[1];
  18. $result['args'] = array();
  19.  
  20. //echo sprintf('Function: "%s"' . "\n", $result['name']);
  21.  
  22. $tmpArgs = array();
  23. while(!preg_match('/\A\)/', $remaining)) {
  24. $recMoved = getFunction($remaining, $tmpArgs);
  25.  
  26. $remaining = substr($remaining, $recMoved);
  27. $moved += $recMoved;
  28. }
  29.  
  30. $remaining = substr($remaining, 1);
  31. $moved += 1;
  32.  
  33. foreach($tmpArgs as $arg) {
  34. if($arg['type'] == 'call') {
  35. $result['args'][] = $arg;
  36. } else {
  37. echo sprintf('Parsing content as args "%s"' . "\n", $arg['content']);
  38. foreach(explode(',', $arg['content']) as $j) {
  39. //echo sprintf('Parsing content as args = "%s"' . "\n", $j);
  40. preg_match('/\A\s*(.*?)\s*\Z/', $j, $matches);
  41. //echo sprintf('Parsing content as args ... "%s"' . "\n", $matches[1]);
  42. if($matches[1]) {
  43. $result['args'][] = array('type' => 'content', 'content' => $matches[1]);
  44. }
  45. }
  46. }
  47. }
  48. } else {
  49. preg_match('/\A[^&\)]+/', $input, $matches);
  50. $matchlength = strlen($matches[0]);
  51.  
  52. //echo sprintf('Content: "%s"' . "\n", $matches[0]);
  53.  
  54. $result['content'] = $matches[0];
  55.  
  56. $moved += $matchlength;
  57. }
  58.  
  59. $results[] = $result;
  60.  
  61. return $moved;
  62. }
  63.  
  64. $input = fgets(STDIN);
  65.  
  66. $result = array();
  67. getFunction($input, $result);
  68.  
  69. // you can now recursively parse the structure using your own code
  70.  
  71. var_dump($result);
  72. ?>
Success #stdin #stdout 0.02s 13112KB
stdin
&gradient(#1073b8, &saturate(#1073b8, test, &cool(asd, 123, 321), &deep(&recursion(&is(&fun(, 10, 9), 8, 7), 6)), 40), awesome gradient);
stdout
Parsing content as args "asd, 123, 321"
Parsing content as args ", 10, 9"
Parsing content as args ", 8, 7"
Parsing content as args ", 6"
Parsing content as args "#1073b8, test, "
Parsing content as args ", "
Parsing content as args ", 40"
Parsing content as args "#1073b8, "
Parsing content as args ", awesome gradient"
array(1) {
  [0]=>
  array(3) {
    ["type"]=>
    string(4) "call"
    ["name"]=>
    string(8) "gradient"
    ["args"]=>
    array(3) {
      [0]=>
      array(2) {
        ["type"]=>
        string(7) "content"
        ["content"]=>
        string(7) "#1073b8"
      }
      [1]=>
      array(3) {
        ["type"]=>
        string(4) "call"
        ["name"]=>
        string(8) "saturate"
        ["args"]=>
        array(5) {
          [0]=>
          array(2) {
            ["type"]=>
            string(7) "content"
            ["content"]=>
            string(7) "#1073b8"
          }
          [1]=>
          array(2) {
            ["type"]=>
            string(7) "content"
            ["content"]=>
            string(4) "test"
          }
          [2]=>
          array(3) {
            ["type"]=>
            string(4) "call"
            ["name"]=>
            string(4) "cool"
            ["args"]=>
            array(3) {
              [0]=>
              array(2) {
                ["type"]=>
                string(7) "content"
                ["content"]=>
                string(3) "asd"
              }
              [1]=>
              array(2) {
                ["type"]=>
                string(7) "content"
                ["content"]=>
                string(3) "123"
              }
              [2]=>
              array(2) {
                ["type"]=>
                string(7) "content"
                ["content"]=>
                string(3) "321"
              }
            }
          }
          [3]=>
          array(3) {
            ["type"]=>
            string(4) "call"
            ["name"]=>
            string(4) "deep"
            ["args"]=>
            array(1) {
              [0]=>
              array(3) {
                ["type"]=>
                string(4) "call"
                ["name"]=>
                string(9) "recursion"
                ["args"]=>
                array(2) {
                  [0]=>
                  array(3) {
                    ["type"]=>
                    string(4) "call"
                    ["name"]=>
                    string(2) "is"
                    ["args"]=>
                    array(3) {
                      [0]=>
                      array(3) {
                        ["type"]=>
                        string(4) "call"
                        ["name"]=>
                        string(3) "fun"
                        ["args"]=>
                        array(2) {
                          [0]=>
                          array(2) {
                            ["type"]=>
                            string(7) "content"
                            ["content"]=>
                            string(2) "10"
                          }
                          [1]=>
                          array(2) {
                            ["type"]=>
                            string(7) "content"
                            ["content"]=>
                            string(1) "9"
                          }
                        }
                      }
                      [1]=>
                      array(2) {
                        ["type"]=>
                        string(7) "content"
                        ["content"]=>
                        string(1) "8"
                      }
                      [2]=>
                      array(2) {
                        ["type"]=>
                        string(7) "content"
                        ["content"]=>
                        string(1) "7"
                      }
                    }
                  }
                  [1]=>
                  array(2) {
                    ["type"]=>
                    string(7) "content"
                    ["content"]=>
                    string(1) "6"
                  }
                }
              }
            }
          }
          [4]=>
          array(2) {
            ["type"]=>
            string(7) "content"
            ["content"]=>
            string(2) "40"
          }
        }
      }
      [2]=>
      array(2) {
        ["type"]=>
        string(7) "content"
        ["content"]=>
        string(16) "awesome gradient"
      }
    }
  }
}