MacroAbstract::parse()   B
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 4
nop 3
1
<?php
2
/**
3
 * This file is part of PHP-Yacc package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace PhpYacc\Yacc;
11
12
use PhpYacc\Exception\LogicException;
13
use PhpYacc\Macro;
14
use PhpYacc\Support\Utils;
15
16
/**
17
 * Class MacroAbstract.
18
 */
19
abstract class MacroAbstract implements Macro
20
{
21
    /**
22
     * @param string $string
23
     * @param int    $lineNumber
24
     * @param string $filename
25
     *
26
     * @return array
27
     */
28
    protected function parse(string $string, int $lineNumber, string $filename): array
29
    {
30
        $i = 0;
31
        $length = \mb_strlen($string);
32
        $buffer = '';
33
        $tokens = [];
34
35
        while ($i < $length) {
36
            if (Utils::isSymChar($string[$i])) {
37
                do {
38
                    $buffer .= $string[$i++];
39
                } while ($i < $length && Utils::isSymChar($string[$i]));
40
41
                $type = \ctype_digit($buffer) ? Token::T_NUMBER : Token::T_NAME;
42
                $tokens[] = new Token($type, $buffer, $lineNumber, $filename);
43
                $buffer = '';
44
            } else {
45
                $tokens[] = new Token(Token::T_UNKNOWN, $string[$i++], $lineNumber, $filename);
46
            }
47
        }
48
49
        return $tokens;
50
    }
51
52
    /**
53
     * @param \Iterator $it
54
     *
55
     * @throws LogicException
56
     *
57
     * @return Token
58
     */
59
    protected static function next(\Iterator $it): Token
60
    {
61
        $it->next();
62
63
        if (!$it->valid()) {
64
            throw new LogicException('Unexpected end of action stream: this should never happen');
65
        }
66
67
        return $it->current();
68
    }
69
}
70