Parse out hunk meta.

@param Token[] $hunkTokenList

@return array Containing Original Start, Original Count, New Start, New Count & number of tokens consumed.
183816
private function getHunkMeta(array $hunkTokenList): array
    {
        switch (true) {
            case Token::FILE_DELETION_LINE_COUNT === $hunkTokenList[0]->getType():
                $originalStart = 1;
                $originalCount = intval($hunkTokenList[0]->getValue());
                $newStart = intval($hunkTokenList[1]->getValue());
                $newCount = intval($hunkTokenList[2]->getValue());
                $tokensReadCount = 3;
                break;

            case Token::FILE_CREATION_LINE_COUNT === $hunkTokenList[2]->getType():
                $originalStart = intval($hunkTokenList[0]->getValue());
                $originalCount = intval($hunkTokenList[1]->getValue());
                $newStart = 1;
                $newCount = intval($hunkTokenList[2]->getValue());
                $tokensReadCount = 3;
                break;

            default:
                $originalStart = intval($hunkTokenList[0]->getValue());
                $originalCount = intval($hunkTokenList[1]->getValue());
                $newStart = intval($hunkTokenList[2]->getValue());
                $newCount = intval($hunkTokenList[3]->getValue());
                $tokensReadCount = 4;
                break;
        }

        return [
            $originalStart,
            $originalCount,
            $newStart,
            $newCount,
            $tokensReadCount
        ];
    }
825429
public static function parse(Parser $parser, TokensList $list, array $options = [])
    {
        $ret = [];

        $expr = new self();

        /**
         * The state of the parser.
         *
         * Below are the states of the parser.
         *
         *      0 -----------------------[ JOIN ]----------------------> 1
         *
         *      1 -----------------------[ expr ]----------------------> 2
         *
         *      2 ------------------------[ ON ]-----------------------> 3
         *      2 -----------------------[ USING ]---------------------> 4
         *
         *      3 --------------------[ conditions ]-------------------> 0
         *
         *      4 ----------------------[ columns ]--------------------> 0
         *
         * @var int
         */
        $state = 0;

        // By design, the parser will parse first token after the keyword.
        // In this case, the keyword must be analyzed too, in order to determine
        // the type of this join.
        if ($list->idx > 0) {
            --$list->idx;
        }

        for (; $list->idx < $list->count; ++$list->idx) {
            /**
             * Token parsed at this moment.
             *
             * @var Token
             */
            $token = $list->tokens[$list->idx];

            // End of statement.
            if ($token->type === Token::TYPE_DELIMITER) {
                break;
            }

            // Skipping whitespaces and comments.
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
                continue;
            }

            if ($state === 0) {
                if (($token->type === Token::TYPE_KEYWORD)
                    && ! empty(static::$JOINS[$token->keyword])
                ) {
                    $expr->type = static::$JOINS[$token->keyword];
                    $state = 1;
                } else {
                    break;
                }
            } elseif ($state === 1) {
                $expr->expr = Expression::parse($parser, $list, ['field' => 'table']);
                $state = 2;
            } elseif ($state === 2) {
                if ($token->type === Token::TYPE_KEYWORD) {
                    switch ($token->keyword) {
                        case 'ON':
                            $state = 3;
                            break;
                        case 'USING':
                            $state = 4;
                            break;
                        default:
                            if (! empty(static::$JOINS[$token->keyword])
                            ) {
                                $ret[] = $expr;
                                $expr = new self();
                                $expr->type = static::$JOINS[$token->keyword];
                                $state = 1;
                            } else {
                                /* Next clause is starting */
                                break 2;
                            }
                            break;
                    }
                }
            } elseif ($state === 3) {
                $expr->on = Condition::parse($parser, $list);
                $ret[] = $expr;
                $expr = new self();
                $state = 0;
            } elseif ($state === 4) {
                $expr->using = ArrayObj::parse($parser, $list);
                $ret[] = $expr;
                $expr = new self();
                $state = 0;
            }
        }

        if (! empty($expr->type)) {
            $ret[] = $expr;
        }

        --$list->idx;

        return $ret;
    }
974302
public static function parse(Parser $parser, TokensList $list, array $options = [])
    {
        $ret = [];

        $expr = new self();

        /**
         * The state of the parser.
         *
         * Below are the states of the parser.
         *
         *      0 -----------------------[ ( ]------------------------> 1
         *
         *      1 ----------------[ IN / OUT / INOUT ]----------------> 1
         *      1 ----------------------[ name ]----------------------> 2
         *
         *      2 -------------------[ data type ]--------------------> 3
         *
         *      3 ------------------------[ , ]-----------------------> 1
         *      3 ------------------------[ ) ]-----------------------> (END)
         *
         * @var int
         */
        $state = 0;

        for (; $list->idx < $list->count; ++$list->idx) {
            /**
             * Token parsed at this moment.
             *
             * @var Token
             */
            $token = $list->tokens[$list->idx];

            // End of statement.
            if ($token->type === Token::TYPE_DELIMITER) {
                break;
            }

            // Skipping whitespaces and comments.
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
                continue;
            }

            if ($state === 0) {
                if (($token->type === Token::TYPE_OPERATOR) && ($token->value === '(')) {
                    $state = 1;
                }
                continue;
            } elseif ($state === 1) {
                if (($token->value === 'IN') || ($token->value === 'OUT') || ($token->value === 'INOUT')) {
                    $expr->inOut = $token->value;
                    ++$list->idx;
                } elseif ($token->value === ')') {
                    ++$list->idx;
                    break;
                } else {
                    $expr->name = $token->value;
                    $state = 2;
                }
            } elseif ($state === 2) {
                $expr->type = DataType::parse($parser, $list);
                $state = 3;
            } elseif ($state === 3) {
                $ret[] = $expr;
                $expr = new self();
                if ($token->value === ',') {
                    $state = 1;
                } elseif ($token->value === ')') {
                    ++$list->idx;
                    break;
                }
            }
        }

        // Last iteration was not saved.
        if (isset($expr->name) && ($expr->name !== '')) {
            $ret[] = $expr;
        }

        --$list->idx;

        return $ret;
    }
1100120
protected function parseTokens($tokens, MessageCatalogue $catalog)
    {
        $tokenIterator = new \ArrayIterator($tokens);

        for ($key = 0; $key < $tokenIterator->count(); ++$key) {
            foreach ($this->sequences as $sequence) {
                $message = '';
                $domain = 'messages';
                $tokenIterator->seek($key);

                foreach ($sequence as $sequenceKey => $item) {
                    $this->seekToNextRelevantToken($tokenIterator);

                    if ($this->normalizeToken($tokenIterator->current()) === $item) {
                        $tokenIterator->next();
                        continue;
                    } elseif (self::MESSAGE_TOKEN === $item) {
                        $message = $this->getValue($tokenIterator);

                        if (\count($sequence) === ($sequenceKey + 1)) {
                            break;
                        }
                    } elseif (self::METHOD_ARGUMENTS_TOKEN === $item) {
                        $this->skipMethodArgument($tokenIterator);
                    } elseif (self::DOMAIN_TOKEN === $item) {
                        $domainToken = $this->getValue($tokenIterator);
                        if ('' !== $domainToken) {
                            $domain = $domainToken;
                        }

                        break;
                    } else {
                        break;
                    }
                }

                if ($message) {
                    $catalog->set($message, $this->prefix.$message, $domain);
                    break;
                }
            }
        }
    }
234433
public function parse(array $diffLineList): Changeset
    {
        $tokenList = $this->tokenizer->tokenize($diffLineList);

        $fileList = [];
        $startIndex = 0;
        $tokenCount = count($tokenList);
        for ($i = 0; $i < $tokenCount; $i++) {
            // File begin
            if (Token::ORIGINAL_FILENAME === $tokenList[$i]->getType()) {
                $startIndex = $i;
            }

            // File end, hydrate object
            if ($this->fileEnd($tokenList, $i + 1, Token::ORIGINAL_FILENAME)) {
                $fileList[] = $this->parseFile(
                    array_slice($tokenList, $startIndex, ($i - $startIndex) + 1)
                );
            }
        }

        return new Changeset($fileList);
    }
916305
public static function parse(Parser $parser, TokensList $list, array $options = [])
    {
        $ret = new self();

        /**
         * The state of the parser.
         *
         * Below are the states of the parser.
         *
         *      0 ---------------- [ tbl_name ] -----------------> 1
         *      1 ---------------- [ lock_type ] ----------------> 2
         *      2 -------------------- [ , ] --------------------> break
         *
         * @var int
         */
        $state = 0;

        $prevToken = null;

        for (; $list->idx < $list->count; ++$list->idx) {
            /**
             * Token parsed at this moment.
             *
             * @var Token
             */
            $token = $list->tokens[$list->idx];

            // End of statement.
            if ($token->type === Token::TYPE_DELIMITER
                || ($token->type === Token::TYPE_OPERATOR
                && $token->value === ',')
            ) {
                break;
            }

            if ($state === 0) {
                $ret->table = Expression::parse($parser, $list, ['parseField' => 'table']);
                $state = 1;
            } elseif ($state === 1) {
                // parse lock type
                $ret->type = self::parseLockType($parser, $list);
                $state = 2;
            }
            $prevToken = $token;
        }

        // 2 is the only valid end state
        if ($state !== 2) {
            $parser->error('Unexpected end of LOCK expression.', $prevToken);
        }

        --$list->idx;

        return $ret;
    }
266593
private function grabAttribsBeforePropertyToken(Tokens $tokens, $index)
    {
        static $tokenAttribsMap = [
            T_VAR       => null, // destroy T_VAR token!
            T_PRIVATE   => 'visibility',
            T_PROTECTED => 'visibility',
            T_PUBLIC    => 'visibility',
            T_STATIC    => 'static',
        ];

        $result = $this->grabAttribsBeforeToken(
            $tokens,
            $index,
            $tokenAttribsMap,
            [
                'visibility' => new Token([T_PUBLIC, 'public']),
                'static'     => null,
            ]
        );
        if ($result['visibility'] && 'public' === $result['visibility']->getContent()) {
            // If visibility is public and static is set, we remove visibility.
            if ($result['static']) {
                $result['visibility'] = null;
            }
        }
        return $result;
    }
856090
public function parse(string $list)
    {
        if (!$list) {
            return $list;
        }

        $list = collect(explode(',', $list));

        return $list->map(function ($item) {
            $parts = explode(':', $item);
            $model = app()->make(config('mentions.pools.'.$parts[0].'.model'));
            return $model::findOrFail($parts[1]);
        });
    }
1352183
private function parseRule()
    {
        $newToken = $this->tokens[$this->index];
        $set = false;
        if ($this->index > 0) {
            $prevToken = &$this->tokens[$this->index-1];
            $prevTokenType = &$prevToken[0];
            $prevTokenText = &$prevToken[1];
            $concat = false;

            $map = array(
                'DOT' => 'CLASS_SELECTOR',
                'HASH' => 'ID_SELECTOR',
                'COLON' => 'PSEUDO_SELECTOR',
                'DOUBLE_COLON' => 'PSEUDO_SELECTOR'
            );
            if (isset($map[$prevTokenType])) {
                // mark the prev token for deletion and concat into one.
                $newToken[0] = $map[$prevTokenType];
                $prevTokenType = self::$deleteToken;
                $newToken[1] = $prevTokenText . $newToken[1];
                $set = true;
            }
        }
        if (!$set) {
            // must be an element
            $newToken[0] = 'ELEMENT_SELECTOR';
        }
        $this->tokens[$this->index] = $newToken;
    }
934248
public function parseToken($content, $position, $state, TokenList $tokenList) {
        switch ($state) {
            case Lexer::STATE_MATCHED:
                $this->handleMatchedState($content, $position, $tokenList);
                break;
            default:
                parent::parseToken($content, $position, $state, $tokenList);
                break;
        }
    }
358677
public function toArray($tokenList) {
        $expr = array();
        foreach ($tokenList as $token) {
            if ($token instanceof \PHPSQLParser\utils\ExpressionToken) {
                $expr[] = $token->toArray();
            } else {
                $expr[] = $token;
            }
        }
        return $expr;
    }
979724
public function render(TokenList $tokenList, $skin = 'default') {
        if (!array_key_exists($skin, $this->ruleRenderers)) {
            throw new ParserException('Skin ' . $skin . ' not found');
        }

        if (!array_key_exists($skin, $this->tokenRenderers)) {
            $this->tokenRenderers[$skin] = [];
            foreach ($this->ruleRenderers[$skin] as $ruleRenderer) {
                $ruleRenderer->register($this);
            }
        }

        \Event::fire('vikon.parser.before.render', [$tokenList]);

        $output = '';

        foreach ($tokenList->getTokens() as $token) {
            \Event::fire('vikon.parser.token.render.' . $token->getName(), [$token, $tokenList]);

            if (array_key_exists($token->getName(), $this->tokenRenderers[$skin])) {
                $output .= $this->tokenRenderers[$skin][$token->getName()]($token, $tokenList);
            } else {
                $output .= (string)$token;
            }
        }

        return $output;
    }
500731
public static function parse(Parser $parser, TokensList $list, array $options = [])
    {
        $ret = [];

        /**
         * The state of the parser.
         *
         * Below are the states of the parser.
         *
         *      0 ----------------------[ array ]---------------------> 1
         *
         *      1 ------------------------[ , ]------------------------> 0
         *      1 -----------------------[ else ]----------------------> (END)
         *
         * @var int
         */
        $state = 0;

        for (; $list->idx < $list->count; ++$list->idx) {
            /**
             * Token parsed at this moment.
             *
             * @var Token
             */
            $token = $list->tokens[$list->idx];

            // End of statement.
            if ($token->type === Token::TYPE_DELIMITER) {
                break;
            }

            // Skipping whitespaces and comments.
            if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
                continue;
            }

            if (($token->type === Token::TYPE_KEYWORD)
                && ($token->flags & Token::FLAG_KEYWORD_RESERVED)
                && ((~$token->flags & Token::FLAG_KEYWORD_FUNCTION))
                && ($token->value !== 'DUAL')
                && ($token->value !== 'NULL')
                && ($token->value !== 'CASE')
            ) {
                // No keyword is expected.
                break;
            }

            if ($state === 0) {
                if ($token->type === Token::TYPE_KEYWORD
                    && $token->value === 'CASE'
                ) {
                    $expr = CaseExpression::parse($parser, $list, $options);
                } else {
                    $expr = Expression::parse($parser, $list, $options);
                }

                if ($expr === null) {
                    break;
                }
                $ret[] = $expr;
                $state = 1;
            } elseif ($state === 1) {
                if ($token->value === ',') {
                    $state = 0;
                } else {
                    break;
                }
            }
        }

        if ($state === 0) {
            $parser->error(
                'An expression was expected.',
                $list->tokens[$list->idx]
            );
        }

        --$list->idx;

        return $ret;
    }
567300
public function parseRawList(array $rawlist)
    {
        $items = [];
        $path = '';
        foreach ($rawlist as $key => $child) {
            $chunks = preg_split("/\s+/", $child);
            if (isset($chunks[8]) && ($chunks[8] == '.' or $chunks[8] == '..')) {
                continue;
            }
            if (count($chunks) === 1) {
                $len = strlen($chunks[0]);
                if ($len && $chunks[0][$len - 1] == ':') {
                    $path = substr($chunks[0], 0, -1);
                }
                continue;
            }

            // $chunks[8] and up contains filename (multiple elements if filename contains spaces)
            // up to last element or element containing '->' (if type is 'link')
            $target = '';
            $linkArrowElement = array_search('->', $chunks);
            if ($linkArrowElement !== false) {
                $filenameChunks = array_slice($chunks, 8, count($chunks) - $linkArrowElement - 1);
                $filename = implode(' ', $filenameChunks);
                $targetChunks = array_slice($chunks, $linkArrowElement + 1);
                $target = implode(' ', $targetChunks);
            } else {
                $filenameChunks = array_slice($chunks, 8);
                $filename = implode(' ', $filenameChunks);
            }

            $item = [
                'permissions' => $chunks[0],
                'number' => $chunks[1],
                'owner' => $chunks[2],
                'group' => $chunks[3],
                'size' => $chunks[4],
                'month' => $chunks[5],
                'day' => $chunks[6],
                'time' => $chunks[7],
                'name' => $filename,
                'type' => $this->rawToType($chunks[0]),
            ];
            if ($item['type'] == 'link') {
                $item['target'] = $target;
            }
            // if the key is not the path, behavior of ftp_rawlist() PHP function
            if (is_int($key) || false === strpos($key, $item['name'])) {
                array_splice($chunks, 0, 8);
                $key = $item['type'] . '#' . ($path ? $path . '/' : '') . implode(' ', $chunks);
                if ($item['type'] == 'link') {
                    // get the first part of 'link#the-link.ext -> /path/of/the/source.ext'
                    $exp = explode(' ->', $key);
                    $key = rtrim($exp[0]);
                }
                $items[$key] = $item;
            } else {
                // the key is the path, behavior of FtpClient::rawlist() method()
                $items[$key] = $item;
            }
        }

        return $items;
    }
315634
public static function parse(Tokenizer $tokenizer, PdfParser $parser)
    {
        $result = [];

        // Recurse into this function until we reach the end of the array.
        while (($token = $tokenizer->getNextToken()) !== ']') {
            if ($token === false || ($value = $parser->readValue($token)) === false) {
                return false;
            }

            $result[] = $value;
        }

        $v = new self;
        $v->value = $result;

        return $v;
    }
850778
private function grabAttribsBeforeMethodToken(Tokens $tokens, $index)
    {
        static $tokenAttribsMap = [
            T_PRIVATE   => 'visibility',
            T_PROTECTED => 'visibility',
            T_PUBLIC    => null, // destroy T_PUBLIC token. This is literally the only change from the original
            T_ABSTRACT  => 'abstract',
            T_FINAL     => 'final',
            T_STATIC    => 'static',
        ];

        return $this->grabAttribsBeforeToken(
            $tokens,
            $index,
            $tokenAttribsMap,
            [
                'abstract'   => null,
                'final'      => null,
                'visibility' => null,
                'static'     => null,
            ]
        );
    }
394574
protected function parseMethodModifiers(array $tokens)
	{
		$modifiers = MetaInfo::IS_PUBLIC;
	
		foreach($tokens as $tok)
		{
			if(is_array($tok))
			{
				switch($tok[0])
				{
					case T_PUBLIC:
						$modifiers = ($modifiers | MetaInfo::MASK_VISIBILITY) ^ MetaInfo::IS_NOT_PUBLIC;
						break;
					case T_PRIVATE:
						$modifiers = ($modifiers | MetaInfo::MASK_VISIBILITY) ^ MetaInfo::IS_NOT_PRIVATE;
						break;
					case T_PROTECTED:
						$modifiers = ($modifiers | MetaInfo::MASK_VISIBILITY) ^ MetaInfo::IS_NOT_PROTECTED;
						break;
					case T_STATIC:
						$modifiers |= MetaInfo::IS_STATIC;
						break;
					case T_ABSTRACT:
						$modifiers |= MetaInfo::IS_ABSTRACT;
						break;
					case T_FINAL:
						$modifiers |= MetaInfo::IS_FINAL;
						break;
				}
			}
		}
	
		return $modifiers;
	}