Test Failed
Push — master ( 3ca5bd...939415 )
by Kirill
02:50
created

Lexer::skip()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * This file is part of Railt 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 Railt\Lexer\Driver;
11
12
use Railt\Io\Readable;
13
use Railt\Lexer\LexerInterface;
14
use Railt\Lexer\TokenInterface;
15
16
/**
17
 * Class BaseLexer
18
 */
19
abstract class Lexer implements LexerInterface
20
{
21
    /**
22
     * @var array|string[]
23
     */
24
    protected $skipped = [];
25
26
    /**
27
     * @var array|string[]
28
     */
29
    protected $tokens = [];
30
31
    /**
32 17
     * @param Readable $input
33
     * @return \Traversable|TokenInterface[]
34 17
     */
35
    public function lex(Readable $input): \Traversable
36 17
    {
37
        foreach ($this->exec($input) as $token) {
38 17
            if (! \in_array($token->getName(), $this->skipped, true)) {
39
                yield $token;
40
            }
41
        }
42
    }
43
44
    /**
45
     * @param string $token
46 17
     * @param string $pcre
47
     * @return LexerInterface
48 17
     */
49 17
    public function add(string $token, string $pcre): LexerInterface
50 17
    {
51
        $this->tokens[$token] = $pcre;
52
53 17
        return $this;
54
    }
55
56
    /**
57
     * @param string $name
58
     * @return LexerInterface
59 2
     */
60
    public function skip(string $name): LexerInterface
61 2
    {
62
        $this->skipped[] = $name;
63
64
        return $this;
65
    }
66
67
    /**
68
     * @param Readable $file
69
     * @return \Traversable|TokenInterface[]
70
     */
71
    abstract protected function exec(Readable $file): \Traversable;
72
}
73