<commit_before><?php
/*
* This file is a part of graphql-youshido project.
*
* @author <NAME> <<EMAIL>>
* created: 11/23/15 1:22 AM
*/

namespace Youshido\GraphQL\Parser;

use Youshido\GraphQL\Parser\Ast\TypedFragment;
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference;
use Youshido\GraphQL\Parser\Value\InputList;
use Youshido\GraphQL\Parser\Ast\Argument;
use Youshido\GraphQL\Parser\Ast\Field;
use Youshido\GraphQL\Parser\Ast\Fragment;
use Youshido\GraphQL\Parser\Ast\FragmentReference;
use Youshido\GraphQL\Parser\Value\InputObject;
use Youshido\GraphQL\Parser\Value\Literal;
use Youshido\GraphQL\Parser\Ast\Mutation;
use Youshido\GraphQL\Parser\Ast\Query;
use Youshido\GraphQL\Parser\Value\Variable;

class Parser extends Tokenizer
{

    public function parse()
    {
        $data = ['queries' => [], 'mutations' => [], 'fragments' => []];

        while (!$this->end()) {
            $tokenType = $this->peek()->getType();

            switch ($tokenType) {
                case Token::TYPE_LBRACE:
                case Token::TYPE_QUERY:
                    $data = array_merge($data, ['queries' => $this->parseBody()]);
                    break;

                case Token::TYPE_MUTATION:
                    $data = array_merge($data, ['mutations' => $this->parseBody(Token::TYPE_MUTATION)]);
                    break;

                case Token::TYPE_FRAGMENT:
                    $data['fragments'][] = $this->parseFragment();
                    break;

                default:
                    throw new SyntaxErrorException();
            }
        }

        return $data;
    }

    protected function parseBody($token = Token::TYPE_QUERY, $highLevel = true)
    {
        $fields = [];
        $first  = true;

        if ($this->peek()->getType() == $token) {
            $this->lex();
            $this->eat(Token::TYPE_IDENTIFIER);
        }

        $this->lex();

        while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) {
            if ($first) {
                $first = false;
            } else {
                $this->eatMulti([Token::TYPE_COMMA]);
            }

            if ($this->match(Token::TYPE_FRAGMENT_REFERENCE)) {
                $this->lex();

                if ($this->eat(Token::TYPE_ON)) {
                    $fields[] = $this->parseBodyItem(Token::TYPE_TYPED_FRAGMENT, $highLevel);
                } else {
                    $fields[] = $this->parseFragmentReference();
                }
            } else {
                $fields[] = $this->parseBodyItem($token, $highLevel);
            }
        }

        $this->expect(Token::TYPE_RBRACE);

        return $fields;
    }

    protected function expect($type)
    {
        if ($this->match($type)) {
            return $this->lex();
        }

        throw $this->createUnexpected($this->peek());
    }

    protected function expectMulti($types)
    {
        if ($this->matchMulti($types)) {
            return $this->lex();
        }

        throw $this->createUnexpected($this->peek());
    }

    protected function parseReference()
    {
        $this->expectMulti([Token::TYPE_AMP, Token::TYPE_VARIABLE]);

        if ($this->match(Token::TYPE_NUMBER) || $this->match(Token::TYPE_IDENTIFIER)) {
            return new Variable($this->lex()->getData());
        }

        throw $this->createUnexpected($this->peek());
    }

    protected function parseFragmentReference()
    {
        $name = $this->parseIdentifier();

        return new FragmentReference($name);
    }

    protected function parseIdentifier()
    {
        return $this->expectMulti([
            Token::TYPE_IDENTIFIER,
            Token::TYPE_MUTATION,
            Token::TYPE_QUERY,
            Token::TYPE_FRAGMENT,
        ])->getData();
    }

    protected function parseBodyItem($type = Token::TYPE_QUERY, $highLevel = true)
    {
        $name  = $this->parseIdentifier();
        $alias = null;

        if ($this->eat(Token::TYPE_COLON)) {
            $alias = $name;
            $name  = $this->parseIdentifier();
        }

        $arguments = $this->match(Token::TYPE_LPAREN) ? $this->parseArgumentList() : [];

        if ($this->match(Token::TYPE_LBRACE)) {
            $fields = $this->parseBody($type, false);

            if ($type == Token::TYPE_QUERY) {
                return new Query($name, $alias, $arguments, $fields);
            } elseif ($type == Token::TYPE_TYPED_FRAGMENT) {
                return new TypedFragmentReference($name, $fields);
            } else {
                return new Mutation($name, $alias, $arguments, $fields);
            }
        } else {
            if ($highLevel && $type == Token::TYPE_MUTATION) {
                return new Mutation($name, $alias, $arguments);<commit_msg>parser fix<commit_after><?php
/*
* This file is a part of graphql-youshido project.
*
* @author <NAME> <<EMAIL>>
* created: 11/23/15 1:22 AM
*/

namespace Youshido\GraphQL\Parser;

use Youshido\GraphQL\Parser\Ast\TypedFragment;
use Youshido\GraphQL\Parser\Ast\TypedFragmentReference;
use Youshido\GraphQL\Parser\Value\InputList;
use Youshido\GraphQL\Parser\Ast\Argument;
use Youshido\GraphQL\Parser\Ast\Field;
use Youshido\GraphQL\Parser\Ast\Fragment;
use Youshido\GraphQL\Parser\Ast\FragmentReference;
use Youshido\GraphQL\Parser\Value\InputObject;
use Youshido\GraphQL\Parser\Value\Literal;
use Youshido\GraphQL\Parser\Ast\Mutation;
use Youshido\GraphQL\Parser\Ast\Query;
use Youshido\GraphQL\Parser\Value\Variable;

class Parser extends Tokenizer
{

    public function parse()
    {
        $data = ['queries' => [], 'mutations' => [], 'fragments' => []];

        while (!$this->end()) {
            $tokenType = $this->peek()->getType();

            switch ($tokenType) {
                case Token::TYPE_LBRACE:
                case Token::TYPE_QUERY:
                    $data = array_merge($data, ['queries' => $this->parseBody()]);
                    break;

                case Token::TYPE_MUTATION:
                    $data = array_merge($data, ['mutations' => $this->parseBody(Token::TYPE_MUTATION)]);
                    break;

                case Token::TYPE_FRAGMENT:
                    $data['fragments'][] = $this->parseFragment();
                    break;

                default:
                    throw new SyntaxErrorException();
            }
        }

        return $data;
    }

    protected function parseBody($token = Token::TYPE_QUERY, $highLevel = true)
    {
        $fields = [];
        $first  = true;

        if ($this->peek()->getType() == $token) {
            $this->lex();
            $this->eat(Token::TYPE_IDENTIFIER);
        }

        $this->lex();

        while (!$this->match(Token::TYPE_RBRACE) && !$this->end()) {
            if ($first) {
                $first = false;
            } else {
                $this->eatMulti([Token::TYPE_COMMA]);
            }

            if ($this->match(Token::TYPE_FRAGMENT_REFERENCE)) {
                $this->lex();

                if ($this->eat(Token::TYPE_ON)) {
                    $fields[] = $this->parseBodyItem(Token::TYPE_TYPED_FRAGMENT, $highLevel);
                } else {
                    $fields[] = $this->parseFragmentReference();
                }
            } else {
                $fields[] = $this->parseBodyItem($token, $highLevel);
            }
        }

        $this->expect(Token::TYPE_RBRACE);

        return $fields;
    }

    protected function expect($type)
    {
        if ($this->match($type)) {
            return $this->lex();
        }

        throw $this->createUnexpected($this->peek());
    }

    protected function expectMulti($types)
    {
        if ($this->matchMulti($types)) {
            return $this->lex();
        }

        throw $this->createUnexpected($this->peek());
    }

    protected function parseReference()
    {
        $this->expectMulti([Token::TYPE_AMP, Token::TYPE_VARIABLE]);

        if ($this->match(Token::TYPE_NUMBER) || $this->match(Token::TYPE_IDENTIFIER)) {
            return new Variable($this->lex()->getData());
        }

        throw $this->createUnexpected($this->peek());
    }

    protected function parseFragmentReference()
    {
        $name = $this->parseIdentifier();

        return new FragmentReference($name);
    }

    protected function parseIdentifier()
    {
        return $this->expectMulti([
            Token::TYPE_IDENTIFIER,
            Token::TYPE_MUTATION,
            Token::TYPE_QUERY,
            Token::TYPE_FRAGMENT,
        ])->getData();
    }

    protected function parseBodyItem($type = Token::TYPE_QUERY, $highLevel = true)
    {
        $name  = $this->parseIdentifier();
        $alias = null;

        if ($this->eat(Token::TYPE_COLON)) {
            $alias = $name;
            $name  = $this->parseIdentifier();
        }

        $arguments = $this->match(Token::TYPE_LPAREN) ? $this->parseArgumentList() : [];

        if ($this->match(Token::TYPE_LBRACE)) {
            $fields = $this->parseBody($type == Token::TYPE_TYPED_FRAGMENT ? Token::TYPE_QUERY : $type, false);

            if ($type == Token::TYPE_QUERY) {
                return new Query($name, $alias, $arguments, $fields);
            } elseif ($type == Token::TYPE_TYPED_FRAGMENT) {
                return new TypedFragmentReference($name, $fields);
            } else {
                return new Mutation($name, $alias, $arguments, $fields);
            }
        } else {
            if ($highLevel && $type == Token::TYPE_MUTATION) {
                return new Mutation($name, $alias, $arguments);
{'fraction_non_alphanumeric': 0.14973317986815946, 'fraction_numerical': 0.0018834362247567228, 'mean_word_length': 2.0094458438287153, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 111, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2843680', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2765, 'n_words': 471}