Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 137
Highlight
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 2
6
0.00% covered (danger)
0.00%
0 / 137
 fromPHPToken
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 133
 fromPHPChar
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 4
1<?php declare(strict_types=1);
2
3namespace Aviat\Kilo\Enum;
4
5use Aviat\Kilo\Traits;
6
7/**
8 * @enum
9 */
10class Highlight {
11    use Traits\ConstList;
12
13    public const NORMAL = 0;
14    public const COMMENT = 1;
15    public const ML_COMMENT = 2;
16    public const KEYWORD1 = 3;
17    public const KEYWORD2 = 4;
18    public const STRING = 5;
19    public const NUMBER = 6;
20    public const OPERATOR = 7;
21    public const VARIABLE = 8;
22    public const DELIMITER = 9;
23    public const INVALID = 10;
24    public const MATCH = 11;
25    public const IDENTIFIER = 12;
26    public const CHARACTER = 13;
27
28    /**
29     * Map a PHP syntax token to its associated highlighting type
30     *
31     * @param int $token
32     * @return int
33     */
34    public static function fromPHPToken(int $token): int
35    {
36        return match($token) {
37            // Delimiters
38            T_ARRAY,
39            T_CURLY_OPEN,
40            T_DOLLAR_OPEN_CURLY_BRACES,
41            T_OPEN_TAG,
42            T_OPEN_TAG_WITH_ECHO,
43            T_CLOSE_TAG,
44            T_START_HEREDOC,
45            T_END_HEREDOC => Highlight::DELIMITER,
46
47            // Number literals and magic constants
48            T_CLASS_C,
49            T_DIR,
50            T_DNUMBER,
51            T_LNUMBER,
52            T_FILE,
53            T_FUNC_C,
54            T_LINE,
55            T_METHOD_C,
56            T_NS_C,
57            T_NUM_STRING,
58            T_TRAIT_C => Highlight::NUMBER,
59
60            // String literals
61            T_CONSTANT_ENCAPSED_STRING, T_ENCAPSED_AND_WHITESPACE => Highlight::STRING,
62
63            // Simple variables
64            T_VARIABLE, T_STRING_VARNAME => Highlight::VARIABLE,
65
66            // Operators
67            T_AS,
68            T_AND_EQUAL,
69            T_BOOLEAN_AND,
70            T_BOOLEAN_OR,
71            T_COALESCE,
72            T_COALESCE_EQUAL,
73            T_CONCAT_EQUAL,
74            T_DEC,
75            T_DIV_EQUAL,
76            T_DOUBLE_ARROW,
77            T_DOUBLE_COLON,
78            T_ELLIPSIS,
79            T_INC,
80            T_INSTANCEOF,
81            T_INSTEADOF,
82            T_IS_EQUAL,
83            T_IS_GREATER_OR_EQUAL,
84            T_IS_IDENTICAL,
85            T_IS_NOT_EQUAL,
86            T_IS_NOT_IDENTICAL,
87            T_IS_SMALLER_OR_EQUAL,
88            T_SPACESHIP,
89            T_LOGICAL_AND,
90            T_LOGICAL_OR,
91            T_LOGICAL_XOR,
92            T_MINUS_EQUAL,
93            T_MOD_EQUAL,
94            T_MUL_EQUAL,
95            T_NS_SEPARATOR,
96            T_NULLSAFE_OBJECT_OPERATOR,
97            T_OBJECT_OPERATOR,
98            T_OR_EQUAL,
99            T_PLUS_EQUAL,
100            T_POW,
101            T_POW_EQUAL,
102            T_SL,
103            T_SL_EQUAL,
104            T_SR,
105            T_SR_EQUAL,
106            T_XOR_EQUAL => Highlight::OPERATOR,
107
108            // Keywords1
109            T_ABSTRACT,
110            T_BREAK,
111            T_CASE,
112            T_CATCH,
113            T_CLASS,
114            T_CLONE,
115            T_CONST,
116            T_CONTINUE,
117            T_DECLARE,
118            T_DEFAULT,
119            T_DO,
120            T_ECHO,
121            T_ELSE,
122            T_ELSEIF,
123            T_EMPTY,
124            T_ENDDECLARE,
125            T_ENDFOR,
126            T_ENDFOREACH,
127            T_ENDIF,
128            T_ENDSWITCH,
129            T_ENDWHILE,
130            T_EVAL,
131            T_EXIT,
132            T_EXTENDS,
133            T_FINAL,
134            T_FINALLY,
135            T_FN,
136            T_FOR,
137            T_FOREACH,
138            T_FUNCTION,
139            T_GLOBAL,
140            T_GOTO,
141            T_HALT_COMPILER,
142            T_IF,
143            T_IMPLEMENTS,
144            T_INCLUDE,
145            T_INCLUDE_ONCE,
146            T_INTERFACE,
147            T_ISSET,
148            T_LIST,
149            T_MATCH,
150            T_NAMESPACE,
151            T_NEW,
152            T_PRINT,
153            T_PRIVATE,
154            T_PUBLIC,
155            T_PROTECTED,
156            T_REQUIRE,
157            T_REQUIRE_ONCE,
158            T_RETURN,
159            T_STATIC,
160            T_SWITCH,
161            T_THROW,
162            T_TRAIT,
163            T_TRY,
164            T_UNSET,
165            T_USE,
166            T_VAR,
167            T_WHILE,
168            T_YIELD,
169            T_YIELD_FROM => Highlight::KEYWORD1,
170
171            // Not string literals, but identifiers, keywords, etc.
172            T_STRING => Highlight::IDENTIFIER,
173
174            // Types and casts
175            T_ARRAY_CAST,
176            T_BOOL_CAST,
177            T_CALLABLE,
178            T_DOUBLE_CAST,
179            T_INT_CAST,
180            T_OBJECT_CAST,
181            T_STRING_CAST,
182            T_UNSET_CAST => Highlight::KEYWORD2,
183
184            // Invalid syntax
185            T_BAD_CHARACTER => Highlight::INVALID,
186
187            default => Highlight::NORMAL,
188        };
189    }
190
191    /**
192     * Map a single character PHP delimiter or operator to its associated
193     * highlighting type
194     *
195     * @param string $char
196     * @return int
197     */
198    public static function fromPHPChar(string $char): int
199    {
200        return match ($char) {
201            // Delimiter characters
202            '[', ']', '{', '}', '(', ')', '"', "'" => Highlight::DELIMITER,
203
204            // Single character operators
205            '?', ',', ';', ':', '^', '%', '+', '-',
206            '*', '/', '.', '|', '~', '>', '<', '=', '!' => Highlight::OPERATOR,
207
208            default => Highlight::NORMAL,
209        };
210    }
211}