-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbootstrap.php
More file actions
142 lines (129 loc) · 5.27 KB
/
bootstrap.php
File metadata and controls
142 lines (129 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php declare(strict_types=1);
(function () {
$maybeDefined = [
// PHP 8.0
'T_ATTRIBUTE' => true,
'T_MATCH' => true,
'T_NAME_FULLY_QUALIFIED' => true,
'T_NAME_QUALIFIED' => true,
'T_NAME_RELATIVE' => true,
'T_NULLSAFE_OBJECT_OPERATOR' => true,
// PHP 8.1
'T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG' => true,
'T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG' => true,
'T_ENUM' => true,
'T_READONLY' => true,
// PHP 8.4
'T_PRIVATE_SET' => false,
'T_PROTECTED_SET' => false,
'T_PUBLIC_SET' => false,
'T_PROPERTY_C' => false,
// PHP 8.5
'T_VOID_CAST' => false,
'T_PIPE' => false,
// Custom
'T_ATTRIBUTE_COMMENT' => false,
'T_CLOSE_ALT' => false,
'T_NULL' => false,
'T_OPEN_UNENCLOSED' => false,
'T_CLOSE_UNENCLOSED' => false,
];
$defined = [];
$toDefine = [];
foreach ($maybeDefined as $token => $defineIfMissing) {
if (!defined($token)) {
if ($defineIfMissing) {
$toDefine[] = $token;
}
continue;
}
/** @var mixed */
$id = constant($token);
// Fail if tokens defined by other libraries are invalid or do not have
// unique IDs
if (!is_int($id) || !($id < 0 || $id > 255)) {
throw new Error(sprintf('%s is invalid', $token));
}
if (isset($defined[$id])) {
throw new Error(sprintf(
'%s and %s have the same ID',
$token,
$defined[$id],
));
}
$defined[$id] = $token;
}
$getNextId = function () use ($defined, &$id): int {
$id++;
while (isset($defined[$id])) {
$id++;
}
return $id;
};
// Define missing tokens, skipping IDs already in use
$id = 10000;
foreach ($toDefine as $token) {
define($token, $getNextId());
}
// Define PHP 8.4 tokens
defined('T_PRIVATE_SET') || define('T_PRIVATE_SET', $getNextId());
defined('T_PROTECTED_SET') || define('T_PROTECTED_SET', $getNextId());
defined('T_PUBLIC_SET') || define('T_PUBLIC_SET', $getNextId());
defined('T_PROPERTY_C') || define('T_PROPERTY_C', $getNextId());
// Define PHP 8.5 tokens
defined('T_VOID_CAST') || define('T_VOID_CAST', $getNextId());
defined('T_PIPE') || define('T_PIPE', $getNextId());
// Define custom tokens
$id = 20000;
defined('T_ATTRIBUTE_COMMENT') || define('T_ATTRIBUTE_COMMENT', $getNextId());
defined('T_CLOSE_ALT') || define('T_CLOSE_ALT', $getNextId());
defined('T_NULL') || define('T_NULL', $getNextId());
defined('T_OPEN_UNENCLOSED') || define('T_OPEN_UNENCLOSED', $getNextId());
defined('T_CLOSE_UNENCLOSED') || define('T_CLOSE_UNENCLOSED', $getNextId());
// Define single-character tokens because the text of a token cannot be
// relied upon to determine its type, e.g. the following tests may be true
// when `$token->id === T_ENCAPSED_AND_WHITESPACE`, which means they are not
// equivalent to `$token->id === ord('}')`:
//
// ```
// $token->is('}');
// $token->text === '}';
// ```
$isValid = function (string $token, string $char): bool {
if (!defined($token)) {
return false;
}
if (constant($token) === ord($char)) {
return true;
}
throw new Error(sprintf('%s is invalid', $token));
};
$isValid('T_LOGICAL_NOT', '!') || define('T_LOGICAL_NOT', ord('!'));
$isValid('T_DOUBLE_QUOTE', '"') || define('T_DOUBLE_QUOTE', ord('"'));
$isValid('T_DOLLAR', '$') || define('T_DOLLAR', ord('$'));
$isValid('T_MOD', '%') || define('T_MOD', ord('%'));
$isValid('T_AND', '&') || define('T_AND', ord('&'));
$isValid('T_OPEN_PARENTHESIS', '(') || define('T_OPEN_PARENTHESIS', ord('('));
$isValid('T_CLOSE_PARENTHESIS', ')') || define('T_CLOSE_PARENTHESIS', ord(')'));
$isValid('T_MUL', '*') || define('T_MUL', ord('*'));
$isValid('T_PLUS', '+') || define('T_PLUS', ord('+'));
$isValid('T_COMMA', ',') || define('T_COMMA', ord(','));
$isValid('T_MINUS', '-') || define('T_MINUS', ord('-'));
$isValid('T_CONCAT', '.') || define('T_CONCAT', ord('.'));
$isValid('T_DIV', '/') || define('T_DIV', ord('/'));
$isValid('T_COLON', ':') || define('T_COLON', ord(':'));
$isValid('T_SEMICOLON', ';') || define('T_SEMICOLON', ord(';'));
$isValid('T_SMALLER', '<') || define('T_SMALLER', ord('<'));
$isValid('T_EQUAL', '=') || define('T_EQUAL', ord('='));
$isValid('T_GREATER', '>') || define('T_GREATER', ord('>'));
$isValid('T_QUESTION', '?') || define('T_QUESTION', ord('?'));
$isValid('T_AT', '@') || define('T_AT', ord('@'));
$isValid('T_OPEN_BRACKET', '[') || define('T_OPEN_BRACKET', ord('['));
$isValid('T_CLOSE_BRACKET', ']') || define('T_CLOSE_BRACKET', ord(']'));
$isValid('T_XOR', '^') || define('T_XOR', ord('^'));
$isValid('T_BACKTICK', '`') || define('T_BACKTICK', ord('`'));
$isValid('T_OPEN_BRACE', '{') || define('T_OPEN_BRACE', ord('{'));
$isValid('T_OR', '|') || define('T_OR', ord('|'));
$isValid('T_CLOSE_BRACE', '}') || define('T_CLOSE_BRACE', ord('}'));
$isValid('T_NOT', '~') || define('T_NOT', ord('~'));
})();