LightnCandy\Parser::analyze PHP Метод

analyze() защищенный статический Метод

Analyze a token string and return parsed result.
protected static analyze ( string $token, array\arraystring | integer> &$context ) : arrayinteger | array>
$token string preg_match results
$context array\arraystring | integer>
Результат arrayinteger | array>
    protected static function analyze($token, &$context)
    {
        $count = preg_match_all('/(\\s*)([^\\s]+)/', $token, $matchedall);
        // Parse arguments and deal with "..." or [...] or (...) or \'...\' or |...|
        if ($count > 0 && $context['flags']['advar']) {
            $vars = array();
            $prev = '';
            $expect = 0;
            $stack = 0;
            foreach ($matchedall[2] as $index => $t) {
                // continue from previous match when expect something
                if ($expect) {
                    $prev .= "{$matchedall[1][$index]}{$t}";
                    if ($stack > 0 && substr($t, 0, 1) === '(') {
                        $stack++;
                    }
                    // end an argument when end with expected charactor
                    if (substr($t, -1, 1) === $expect) {
                        if ($stack > 0) {
                            preg_match('/(\\)+)$/', $t, $matchedq);
                            $stack -= isset($matchedq[0]) ? strlen($matchedq[0]) : 1;
                            if ($stack > 0) {
                                continue;
                            }
                            if ($stack < 0) {
                                $context['error'][] = "Unexcepted ')' in expression '{$token}' !!";
                                $expect = 0;
                                break;
                            }
                        }
                        $vars[] = $prev;
                        $prev = '';
                        $expect = 0;
                        continue;
                    } else {
                        if ($expect == ']' && strpos($t, $expect) !== false) {
                            $t = $prev;
                            $expect = 0;
                        } else {
                            continue;
                        }
                    }
                }
                // continue to next match when begin with '(' without ending ')'
                if (preg_match('/^\\([^\\)]*$/', $t)) {
                    $prev = $t;
                    $expect = ')';
                    $stack = 1;
                    continue;
                }
                // continue to next match when begin with '"' without ending '"'
                if (preg_match('/^"[^"]*$/', $t)) {
                    $prev = $t;
                    $expect = '"';
                    continue;
                }
                // continue to next match when begin with \' without ending '
                if (preg_match('/^\\\\\'[^\']*$/', $t)) {
                    $prev = $t;
                    $expect = '\'';
                    continue;
                }
                // continue to next match when '="' exists without ending '"'
                if (preg_match('/^[^"]*="[^"]*$/', $t)) {
                    $prev = $t;
                    $expect = '"';
                    continue;
                }
                // continue to next match when '[' exists without ending ']'
                if (preg_match('/^([^"\'].+)?\\[[^\\]]*$/', $t)) {
                    $prev = $t;
                    $expect = ']';
                    continue;
                }
                // continue to next match when =\' exists without ending '
                if (preg_match('/^[^\']*=\\\\\'[^\']*$/', $t)) {
                    $prev = $t;
                    $expect = '\'';
                    continue;
                }
                // continue to next match when =( exists without ending )
                if (preg_match('/.+\\([^\\)]*$/', $t)) {
                    $prev = $t;
                    $expect = ')';
                    $stack = 1;
                    continue;
                }
                // continue to next match when 'as' without ending '|'
                if ($t === 'as' && count($vars) > 0) {
                    $prev = '';
                    $expect = '|';
                    $stack = 1;
                    continue;
                }
                $vars[] = $t;
            }
            if ($expect) {
                $context['error'][] = "Error in '{$token}': expect '{$expect}' but the token ended!!";
            }
            return $vars;
        }
        return $count > 0 ? $matchedall[2] : explode(' ', $token);
    }