Parses a string into a series of tokens in Reverse Polish Notation order.

@param string $string

@return \Xylemical\Expressions\Token[]

@throws \Xylemical\Expressions\ParserException

@see https://en.wikipedia.org/wiki/Shunting-yard_algorithm
2249
public function parse($string) {
        $output = new \SplQueue();
        $operator = new \SplStack();

        // Convert the string to tokens for the lexer.
        $tokens = $this->lexer->tokenize($string);

        // while there are tokens to be read:
        while (count($tokens)) {
            // read a token.
            $token = array_shift($tokens);

            // if the token is a number, then push it to the output queue.
            if ($token->isValue()) {
                $output->push($token);
                continue;
            }

            // if the token is a function, then push it onto the operator stack.
            if ($token->isFunction()) {
                $operator->push($token);
                continue;
            }

            // if the token is a function argument separator:
            if ($token->getValue() === ',') {
                while (($op = $this->getTop($operator)) && $op->getValue() !== '(') {
                    $output->push($operator->pop());
                }

                if ($operator->isEmpty()) {
                    throw new ParserException('Mismatched parentheses or misplaced comma.');
                }

                continue;
            }

            // if the token is an operator, then:
            if ($token->isOperator()) {
                // while (
                //  (there is an operator at the top of the operator stack with greater precedence) or
                //  (the operator at the top of the operator stack has equal precedence and the operator is left associative)) and
                //  (the operator at the top of the stack is not a left bracket):
                //     pop operators from the operator stack, onto the output queue.
                while (($op = $this->getTop($operator)) && $op->isOperator() && $token->hasHigherPriority($op)) {
                    $output->push($operator->pop());
                }

                // push the read operator onto the operator stack.
                $operator->push($token);
                continue;
            }

            // if the token is a left bracket (i.e. "("), then:
            if ($token->getValue() === '(') {
                // push it onto the operator stack.
                $operator->push($token);
                continue;
            }

            // if the token is a right bracket (i.e. ")"), then:
            if ($token->getValue() === ')') {
                // while the operator at the top of the operator stack is not a left bracket:
                while (($op = $this->getTop($operator)) && $op->getValue() !== '(') {
                    // pop operators from the operator stack onto the output queue.
                    $output->push($operator->pop());
                }

                // /* if the stack runs out without finding a left bracket, then there are mismatched parentheses. */
                if ($operator->isEmpty()) {
                    throw new ParserException('Mismatched parentheses');
                }

                // pop the left bracket from the stack.
                $operator->pop();

                // If the token at the top of the stack is a function token, pop it onto the output queue.
                if (($op = $this->getTop($operator)) && $op->isFunction()) {
                    $output->push($operator->pop());
                }
            }
        }

        // if there are no more tokens to read:
        // while there are still operator tokens on the stack:
        while (!$operator->isEmpty()) {
            // /* if the operator token on the top of the stack is a bracket, then there are mismatched parentheses. */
            if (is_null($operator->top()->getOperator())) {
                throw new ParserException('Mismatched parentheses or misplaced comma.');
            }

            // pop the operator onto the output queue.
            $output->push($operator->pop());
        }

        // exit.
        return iterator_to_array($output);
    }
242209
protected function parseStringToParts($string) {
    $parts = explode('-', $string);
        
    if (count($parts) != 5) {
      throw new Exception('The UUID string supplied could not be parsed.');
    }

    foreach ($parts as $id => $part) {
      switch ($id) {
        case 0:
          $this->time_low = $part;
          break;
        case 1:
          $this->time_mid = $part;
          break;
        case 2:
          $this->time_hi_version = $part;
          break;
        case 3:
          $this->clock_seq_hi_variant = substr($part, 0, 2);
          $this->clock_seq_low = substr($part, 2);
          break;
        case 4:
          $this->node = $part;
          break;
      }
    }
  }
874733
public function getTokens($string, &$needle = 0)
    {
        $exprTokenizer = new ExpressionTokenizer();
        $ret = array();
        $depth = 0;
        $needle = 0;
        $len = strlen($string);
        while ($needle < $len) {
            $before = $needle;
            $substr = substr($string, $needle);
            if ($depth === 0) {
                // match either '$(' or '@(' and mark that as an EXPR_START token.
                if (preg_match('/^([$@])\(/', $substr, $m)) {
                    $needle += strlen($m[0]);
                    $ret[] = new Token(Token::EXPR_START, $m[0]);

                    // record expression depth, to make sure the usage of parentheses inside the expression doesn't
                    // break tokenization (e.g. '$( ("foo") )'
                    $depth++;
                } else {
                    // store the current token in a temp var for appending, in case it's a DATA token
                    $token = end($ret);

                    // handle escaping of the $( syntax, '$$(' becomes '$('
                    if (preg_match('/^\$\$\(/', $substr, $m)) {
                        $value = substr($m[0], 1);
                        $needle += strlen($m[0]);
                    } else {
                        $value = $string{$needle};
                        $needle += strlen($value);
                    }

                    // if the current token is DATA, and the previous token is DATA, append the value to the previous
                    // and ignore the current.
                    if ($token && $token->match(Token::DATA)) {
                        $token->value .= $value;
                        unset($token);
                    } else {
                        $ret[] = new Token(Token::DATA, $value);
                    }
                }
            } else {
                $ret = array_merge($ret, $exprTokenizer->getTokens($string, $needle));
                $depth = 0;
            }
            if ($before === $needle) {
                // safety net.
                throw new \UnexpectedValueException(
                    "Unexpected input near token {$string{$needle}}, unsupported character"
                );
            }
        }
        return $ret;
    }
900947
private function parse($string)
    {
        $params = explode(PHP_EOL, trim($string));
        $result = [];

        foreach ($params as $param) {
            $parts = explode(':', $param);
            $result[$parts[0]] = trim($parts[1]);
        }

        return $result;
    }
353896
private function _parseHADocField($string) {
    $separater = '=';
    $pos = strpos($string, $separater);

    if ($pos !== false) {
      $key = substr($string, 0, $pos);
      $value = substr($string, $pos + 1);
      return array($key, $value);
    } else {
      throw new Exception('The are no key and value in the field.');
    }
  }
1321089
public function parse($string) {

		$response = new Response();

		// normalize line endings
		$filtered = preg_replace("/(\n|\r){2,}/", PHP_EOL, $string);

		$lines = explode(PHP_EOL, $filtered);

		foreach($lines as $line) {
			$matches = array();
			if(preg_match('/^(\s*)\[(.*)\]$/', $line, $matches)) {
				// entering new section
				$depth = strlen($matches[1]);
				// merge all lower sections
				$this->completeSection($response, $depth);
				$this->stack[$depth] = new Section($matches[2]);
			} else if(preg_match('/^(\s*)(.*)=(.*)$/', $line, $matches)) {
				$depth = strlen($matches[1]);
				if(isset($this->stack[$depth])) {
					$section = $this->stack[$depth];
					$section->__set($matches[2], self::cast($matches[3]));
				}
			}
		}
		// merge remaining children into parents
		$this->completeSection($response, 0);
		
		return $response;
	}
680317
protected static function tokenize($string)
    {
        self::init();

        $tokens = array();

        // Used for debugging if there is an error while tokenizing the string
        $original_length = strlen($string);

        // Used to make sure the string keeps shrinking on each iteration
        $old_string_len = strlen($string) + 1;

        $token = null;

        $current_length = strlen($string);

        // Keep processing the string until it is empty
        while ($current_length) {
            // If the string stopped shrinking, there was a problem
            if ($old_string_len <= $current_length) {
                $tokens[] = array(
                    self::TOKEN_VALUE=>$string,
                    self::TOKEN_TYPE=>self::TOKEN_TYPE_ERROR
                );

                return $tokens;
            }
            $old_string_len =  $current_length;

            // Determine if we can use caching
            if ($current_length >= self::$max_cachekey_size) {
                $cacheKey = substr($string,0,self::$max_cachekey_size);
            } else {
                $cacheKey = false;
            }

            // See if the token is already cached
            if ($cacheKey && isset(self::$token_cache[$cacheKey])) {
                // Retrieve from cache
                $token = self::$token_cache[$cacheKey];
                $token_length = strlen($token[self::TOKEN_VALUE]);
                self::$cache_hits++;
            } else {
                // Get the next token and the token type
                $token = self::getNextToken($string, $token);
                $token_length = strlen($token[self::TOKEN_VALUE]);
                self::$cache_misses++;

                // If the token is shorter than the max length, store it in cache
                if ($cacheKey && $token_length < self::$max_cachekey_size) {
                    self::$token_cache[$cacheKey] = $token;
                }
            }

            $tokens[] = $token;

            // Advance the string
            $string = substr($string, $token_length);

            $current_length -= $token_length;
        }

        return $tokens;
    }
320206
public function parse(string $string)
    {
        try {
            if (defined('\Symfony\Component\Yaml\Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE')) {
                return $this->parser->parse(
                    $string,
                    Yaml::PARSE_EXCEPTION_ON_INVALID_TYPE | Yaml::PARSE_OBJECT_FOR_MAP
                );
            } else {
                $data = $this->parser->parse($string, true, false, false);

                return $this->fixHashMaps($data);
            }
        } catch (\Throwable $e) {
            throw new ParseException("Failed to parse as YAML", 0, $e);
        }
    }
1209742
protected function parseResponse($string)
    {
        $exp = explode(Driver::SEP, $string);

        if ($exp[0] != 0) {
            throw new HSException(isset($exp[2]) ? $exp[2] : '', $exp[0]);
        }

        array_shift($exp); // skip error code

        $numCols = intval(array_shift($exp));
        $exp = array_map([$this->socket, 'decode'], $exp);

        return array_chunk($exp, $numCols);
    }
282075
public static function parseStringForSearch($string)
    {
        $words = explode(' ', $string);
        $conditions = [];

        foreach ($words as $word) {
            $word = trim($word);

            if (empty($word)) {
                continue;
            }

            if (is_numeric($word)) {
                $conditions[] = $word;
                continue;
            }

            if (1 === mb_strlen($word)) {
                continue;
            }

            if (mb_strlen($word) > 7) {
                $conditions[] = mb_substr($word, 0, -3, 'UTF-8');
                continue;
            }

            if (mb_strlen($word) > 5) {
                $conditions[] = mb_substr($word, 0, -1, 'UTF-8');
                continue;
            }

            if (in_array(mb_substr($word, -1, null, 'UTF-8'), ['ě', 'š', 'č', 'ř', 'ž', 'ý', 'á', 'í', 'é', 'ú', 'ů'], true)) {
                $conditions[] = mb_substr($word, 0, -1, 'UTF-8');
                continue;
            }

            $conditions[] = $word;
        }

        return $conditions;
    }
1098695
public static function parse($string)
    {
        if (static::isJSON($string)) {
            $result = static::jsonDecode($string);
        } elseif (static::isXML($string)) {
            $result = static::xmlDecode($string);
        } else {
            throw new \InvalidArgumentException(sprintf('Unable to parse: %s', (string) $string));
        }

        return (array) $result;
    }
1117436
public function parseString($string, $values)
    {
        $positions = [];
        $pos = 0;
        $offset = 0;

        while (($pos = strpos($string, "?", $offset)) !== false) {
            $positions[] = $pos;
            $offset = $pos + 1;
        }

        $count = count($positions);

        if ($count == 1) {
            $values = array($values);
        }

        for ($i = 0; $i < $count; $i++) {
            $value = $values[$i];
            if ($value instanceof Query) {
                $value = $this->parseValueQuery($value);
            } elseif (is_array($value)) {
                foreach ($value as $key => $subvalue) {
                    if (trim($subvalue) != '') {
                        $value[$key] = is_numeric($subvalue) ? $subvalue : $this->getQuery()->getManager()->getAdapter()->quote($subvalue);
                    } else {
                        unset($value[$key]);
                    }
                }
                $value = "(" . implode(", ", $value) . ")";
            } elseif (is_numeric($value)) {
            } else {
                $value = $this->getQuery()->getManager()->getAdapter()->quote($values[$i]);
            }
            $string = substr_replace($string, $value, strpos($string, '?'), 1);
        }

        return $string;
    }
963900
public function tokenize($string)
    {
        $line     = 1;
        $position = 1;
        $tokens   = array();

        while (strlen($string)) {
            foreach ($this->expressions as $rule) {
                if (!preg_match($rule['match'], $string, $match)) {
                    continue;
                }

                // Remove matched string from input
                $string = substr($string, strlen($match[0]));

                // Update position in file
                $line += substr_count($match[0], "\n");
                if (($pos = strrpos($match[0], "\n")) !== false) {
                    $position  = strrpos($match[0], "\n") + 1;
                } else {
                    $position += strlen($match[0]);
                }

                // Skip irrelevant rules
                if (in_array($rule['type'], $this->ignoreTokens)) {
                    continue 2;
                }

                // Add all other rules including their match to the token array
                $match = $this->removeNumericKeys($match);
                $tokens[] = new Token($rule['type'], $line, $position, $match);
                continue 2;
            }

            throw new \RuntimeException(
                "Could not parse string: '" . substr($string, 0, 20) . "' in line $line at position $position."
            );
        }

        $tokens[] = new Token(self::T_EOF, $line, $position, null);
        return $tokens;
    }
169980
public function parseString($string)
    {
        $tokens = $this->tokenize($string);

        $tokens = array_filter(
            $tokens,
            function ($token) {
                return !($token['type'] === TransformationProcessor::T_WHITESPACE ||
                          $token['type'] === TransformationProcessor::T_COMMENT);
            }
        );

        $ast = array();
        $section = null;
        while ($token = array_shift($tokens)) {
            if ($token['type'] === TransformationProcessor::T_SECTION) {
                $section = $token['data']['section'];
                continue;
            }

            if ($section === null) {
                throw new RuntimeException('Expected section.');
            }

            $ast[$section][] = $token;
        }

        return $ast;
    }
301194
public static function toArray($string = '')
    {
        if ($string === '') {
            return [];
        }

        if (self::isJson($string)) {
            return json_decode($string, true);
        }

        if (self::isXml($string)) {
            return self::xmlToArray($string);
        }

        $unserialize = self::unserialize($string);
        if ($unserialize === false) {
            return [];
        }

        if (\is_array($unserialize)) {
            return $unserialize;
        }

        return [];

    }
957584
public static function parseString($string)
    {
        $array = preg_split('/[,;\r\n]+/', trim($string, ",;\r\n"));
        if (strpos($string, ':')) {
            $value = [];
            foreach ($array as $val) {
                list($k, $v) = explode(':', $val);
                $value[$k] = $v;
            }
        } else {
            $value = $array;
        }
        
        return $value;
    }
408887
public function parseStyle($string) {
        $groups = [];

        if (preg_match_all("#([a-z]+)=([a-z,]+)#", $string, $matches, PREG_SET_ORDER) !== false) {
            $groups = $matches;
        }

        return $groups;
    }