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\Console\Language; |
11
|
|
|
|
12
|
|
|
use Railt\Io\Readable; |
13
|
|
|
use Railt\Lexer\Factory; |
14
|
|
|
use Railt\Lexer\LexerInterface; |
15
|
|
|
use Railt\Lexer\Result\Eoi; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class Language |
19
|
|
|
*/ |
20
|
|
|
abstract class Language implements LanguageInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string[] |
24
|
|
|
*/ |
25
|
|
|
public const DEFAULT_COLORS = [ |
26
|
|
|
'T_COMMENT' => 'fg=cyan', |
27
|
|
|
'T_KEYWORD' => 'fg=magenta', |
28
|
|
|
'T_STRING' => 'fg=green', |
29
|
|
|
'T_DIGIT' => 'fg=green', |
30
|
|
|
'T_VARIABLE' => 'fg=yellow', |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string[] |
35
|
|
|
*/ |
36
|
|
|
public const DEFAULT_TOKENS = [ |
37
|
|
|
'T_WHITESPACE' => '\s+', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var LexerInterface |
42
|
|
|
*/ |
43
|
|
|
private $lexer; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
private $colors; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Language constructor. |
52
|
|
|
* @throws \InvalidArgumentException |
53
|
|
|
* @throws \Railt\Lexer\Exception\BadLexemeException |
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
$this->lexer = Factory::create(\array_merge(self::DEFAULT_TOKENS, $this->tokens()), [Eoi::T_NAME]); |
58
|
|
|
$this->colors = \array_merge(self::DEFAULT_COLORS, $this->colors()); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return array|string[] |
63
|
|
|
*/ |
64
|
|
|
abstract public function tokens(): array; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return array|string[] |
68
|
|
|
*/ |
69
|
|
|
abstract public function colors(): array; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param Readable $file |
73
|
|
|
* @param array $extensions |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function matchExtension(Readable $file, array $extensions): bool |
77
|
|
|
{ |
78
|
|
|
$ext = \mb_strtolower(\pathinfo($file->getPathname(), \PATHINFO_EXTENSION)); |
79
|
|
|
|
80
|
|
|
return \in_array($ext, $extensions, true); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param Readable $file |
85
|
|
|
* @return iterable |
86
|
|
|
*/ |
87
|
|
|
public function lex(Readable $file): iterable |
88
|
|
|
{ |
89
|
|
|
return $this->lexer->lex($file); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $token |
94
|
|
|
* @param string $content |
95
|
|
|
* @return string |
96
|
|
|
*/ |
97
|
|
|
public function highlight(string $token, string $content): string |
98
|
|
|
{ |
99
|
|
|
$color = $this->colors[$token] ?? null; |
100
|
|
|
$content = \str_replace('\\', "\u{0000}", $content); |
101
|
|
|
$content = $color === null ? $content : \sprintf('<%s>%s</>', $color, $content); |
102
|
|
|
|
103
|
|
|
return $content; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $tokens |
108
|
|
|
* @return string |
109
|
|
|
*/ |
110
|
|
|
protected function oneOf(array $tokens): string |
111
|
|
|
{ |
112
|
|
|
return \sprintf('(%s)\b', \implode('|', $tokens)); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|