<?php

function array_property(array $parameters, $property)
{
    return $parameters[$property] ?? 'undefined';
}

function get_parameters(StringReader $reader): array
{
    $response = [];
    
    do
    {
        $response[] = $reader->callback_filter([RaBotFilter::class, 'is_parameter']);
    }
    while ($reader->check_symbol('\\'));
    
    return $response;
}

final class RaBotFilter
{
    public static function is_parameter(string $symbol, int $offset): bool
    {
        return $symbol !== '\\'
            && $symbol !== "\n";
    }
}

final class StringFilter
{
    public static function is_integer(string $symbol, int $offset): bool
    {
        return '0' <= $symbol && $symbol <= '9';
    }
    
    public static function is_double(string $symbol, int $offset): bool
    {
        return self::is_integer($symbol, $offset)
            || $symbol === '.';
    }
    
    public static function is_string(string $symbol, int $offset): bool
    {
        return ('A' <= $symbol && $symbol <= 'Z')
            || ('a' <= $symbol && $symbol <= 'z');
    }
    
    public static function is_skip(string $symbol, int $offset): bool
    {
        return $symbol === ' ';
    }
    
    public static function is_name(string $symbol, int $offset): bool
    {
        $status = self::is_string($symbol, $offset)
            || $symbol === '$'
            || $symbol === '_';
        
        if ($status === false && $offset !== 0)
        {
            $status = self::is_integer($symbol, $offset);
        }
        
        return $status;
    }
    
    public static function is_end(string $symbol, int $offset): bool
    {
        return true;
    }
}

final class StringReader
{
    private $buffer;
    private $length;
    private $offset;
    
    public function move_position(int $offset): void
    {
        $this->offset += $offset;
    }
    
    public function get_symbol(int $offset): string
    {
        return array_property($this->buffer, $this->offset + $offset);
    }
    
    public function is_end(int $offset): bool
    {
        return $this->length <= $this->offset + $offset;
    }
    
    public function check_symbol(string $symbol): bool
    {
        if ($this->get_symbol(0) === $symbol)
        {
            $this->move_position(1);
            return true;
        }
        
        return false;
    }
    
    public function check_string(string $buffer): bool
    {
        $buffer = str_split($buffer);
        $length = sizeof($buffer);
        
        for ($offset = 0; $offset < $length; $offset++)
        {
            if ($this->get_symbol($offset) !== array_property($buffer, $offset))
            {
                return false;
            }
        }
        
        $this->move_position($length);
        return true;
    }
    
    public function callback_filter(array $callback): string
    {
        $buffer = '';
        $offset = 0;
        
        while ($this->is_end(0) === false)
        {
            $symbol = $this->get_symbol(0);
            $result = $callback($symbol, $offset);
            
            if ($result === null)
            {
                continue;
            }
            
            if ($result === false)
            {
                break;
            }
            
            if ($result === true)
            {
                $result = $symbol;
            }
            
            $offset++;
            $buffer .= $result;
            $this->move_position(1);
        }
        
        return $buffer;
    }
    
    public function __construct(string $buffer)
    {
        $this->buffer = str_split($buffer);
        $this->length = sizeof($this->buffer);
        $this->offset = 0;
    }
}

(function ()
{
    $buffer = implode("\n", [
        'привет\пока\0',
        'пока\привет\0'
    ]);
    
    $reader = new StringReader($buffer);
    
    do
    {
        $parameters = get_parameters($reader);
        
        echo 'команда: ' . array_property($parameters, 0) . PHP_EOL;
        echo 'ответ: ' . array_property($parameters, 1) . PHP_EOL;
        echo 'очки: ' . array_property($parameters, 2) . PHP_EOL;
        echo '===== ===== =====' . PHP_EOL;
    }
    while ($reader->check_symbol("\n"));
})();

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: