<commit_before><?php
/*
Copyright 2016 <NAME> <<EMAIL>>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace tbollmeier\parsian;


class Parser
{
    private $tokenIn;
    private $tokens;
    private $bufSize;

    public function __construct(TokenInput $tokenIn)
    {
        $this->tokenIn = $tokenIn;
        $this->tokens = [];
        $this->bufSize = 1;
    }

    public function openTokenInput()
    {
        $this->tokenIn->open();
    }

    public function closeTokenInput()
    {
        $this->tokenIn->close();
    }

    public function lookup()
    {
        $tokens = $this->lookupMany(1);

        return !empty($tokens) ? $tokens[0] : false;
    }

    public function checkFor(...$expectedTokenTypes)
    {
        $numExpected = count($expectedTokenTypes);
        
        $actualTokens = $this->lookupMany($numExpected);
        $numActual = count($actualTokens);
        
        if ($numActual === $numExpected) {
            for ($i=0; $i<$numActual; $i++) {
                $actualToken = $actualTokens[$i];
                $expectedTokenType = $expectedTokenTypes[$i];
                if ($actualToken->getType() !== $expectedTokenType) {
                    return false;        
                }
            }

            return $actualTokens;

        } else {
            return false;
        }

    }

    public function consumeExpected(...$expectedTokenTypes) 
    {
        $actualTokens = call_user_func_array([$this, "expect"], $expectedTokenTypes);
        if ($actualTokens !== false) {
            $cnt = count($actualTokens);
            for ($i=0; $i<$cnt; $i++) {
                array_shift($this->tokens);
            }
            return $actualTokens;
        } else {
            throw new Exception("Parsing error");
        }
    }

    /** lookup next n tokens
     *
     */
    public function lookupMany($n = 1)<commit_msg>minor fix<commit_after><?php
/*
Copyright 2016 <NAME> <<EMAIL>>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

namespace tbollmeier\parsian;


class Parser
{
    private $tokenIn;
    private $tokens;
    private $bufSize;

    public function __construct(TokenInput $tokenIn)
    {
        $this->tokenIn = $tokenIn;
        $this->tokens = [];
        $this->bufSize = 1;
    }

    public function openTokenInput()
    {
        $this->tokenIn->open();
    }

    public function closeTokenInput()
    {
        $this->tokenIn->close();
    }

    public function lookup()
    {
        $tokens = $this->lookupMany(1);

        return !empty($tokens) ? $tokens[0] : false;
    }

    public function checkFor(...$expectedTokenTypes)
    {
        $numExpected = count($expectedTokenTypes);
        
        $actualTokens = $this->lookupMany($numExpected);
        $numActual = count($actualTokens);
        
        if ($numActual === $numExpected) {
            for ($i=0; $i<$numActual; $i++) {
                $actualToken = $actualTokens[$i];
                $expectedTokenType = $expectedTokenTypes[$i];
                if ($actualToken->getType() !== $expectedTokenType) {
                    return false;        
                }
            }

            return $actualTokens;

        } else {
            return false;
        }

    }

    public function consumeExpected(...$expectedTokenTypes) 
    {
        $actualTokens = call_user_func_array([$this, "expect"], $expectedTokenTypes);
        if ($actualTokens !== false) {
            $cnt = count($actualTokens);
            for ($i=0; $i<$cnt; $i++) {
                array_shift($this->tokens);
            }
            return $actualTokens;
        } else {
            throw new \Exception("Parsing error");
        }
    }

    /** lookup next n tokens
     *
     */
    public function lookupMany($n = 1)
{'fraction_non_alphanumeric': 0.11674055309267468, 'fraction_numerical': 0.005910914080641756, 'mean_word_length': 2.119157340355497, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '863189', 'n_tokens_mistral': 1334, 'n_tokens_neox': 1289, 'n_words': 362}