|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Digia\GraphQL\Language; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @param int $cp |
|
7
|
|
|
* @return string |
|
8
|
|
|
*/ |
|
9
|
|
|
function chrUTF8(int $cp) |
|
10
|
|
|
{ |
|
11
|
|
|
return mb_convert_encoding(pack('N', $cp), 'UTF-8', 'UCS-4BE'); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param string $string |
|
16
|
|
|
* @return int |
|
17
|
|
|
*/ |
|
18
|
|
|
function ordUTF8(string $string) |
|
19
|
|
|
{ |
|
20
|
|
|
[, $ord] = unpack('N', mb_convert_encoding($string, 'UCS-4BE', 'UTF-8')); |
|
21
|
|
|
|
|
22
|
|
|
return $ord; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string $string |
|
27
|
|
|
* @param int $position |
|
28
|
|
|
* @return int |
|
29
|
|
|
*/ |
|
30
|
|
|
function charCodeAt(string $string, int $position): int |
|
31
|
|
|
{ |
|
32
|
|
|
return ordUTF8($string[$position]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param int $code |
|
37
|
|
|
* @return string |
|
38
|
|
|
*/ |
|
39
|
|
|
function printCharCode(int $code): string |
|
40
|
|
|
{ |
|
41
|
|
|
if ($code === null) { |
|
|
|
|
|
|
42
|
|
|
return '<EOF>'; |
|
43
|
|
|
} |
|
44
|
|
|
return $code < 0x007F |
|
45
|
|
|
// Trust JSON for ASCII. |
|
46
|
|
|
? json_encode(chrUTF8($code)) |
|
47
|
|
|
// Otherwise print the escaped form. |
|
48
|
|
|
: '"\\u' . dechex($code) . '"'; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param string $string |
|
53
|
|
|
* @param int $start |
|
54
|
|
|
* @param int|null $end |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
function sliceString(string $string, int $start, int $end = null): string |
|
58
|
|
|
{ |
|
59
|
|
|
$length = $end !== null ? $end - $start : mb_strlen($string) - $start; |
|
60
|
|
|
return mb_substr($string, $start, $length); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param int $code |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
function isLetter(int $code): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return ($code >= 65 && $code <= 90) || ($code >= 97 && $code <= 122); // a-z or A-Z |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param int $code |
|
74
|
|
|
* @return bool |
|
75
|
|
|
*/ |
|
76
|
|
|
function isNumber(int $code): bool |
|
77
|
|
|
{ |
|
78
|
|
|
return $code >= 48 && $code <= 57; // 0-9 |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param int $code |
|
83
|
|
|
* @return bool |
|
84
|
|
|
*/ |
|
85
|
|
|
function isUnderscore(int $code): bool |
|
86
|
|
|
{ |
|
87
|
|
|
return $code === 95; // _ |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param int $code |
|
92
|
|
|
* @return bool |
|
93
|
|
|
*/ |
|
94
|
|
|
function isAlphaNumeric(int $code): bool |
|
95
|
|
|
{ |
|
96
|
|
|
return isLetter($code) || isNumber($code) || isUnderscore($code); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param int $code |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
|
|
function isSourceCharacter(int $code): bool |
|
104
|
|
|
{ |
|
105
|
|
|
return $code < 0x0020 && $code !== 0x0009 && $code !== 0x000a && $code !== 0x000d; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Converts four hexidecimal chars to the integer that the |
|
110
|
|
|
* string represents. For example, uniCharCode('0','0','0','f') |
|
111
|
|
|
* will return 15, and uniCharCode('0','0','f','f') returns 255. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $a |
|
114
|
|
|
* @param string $b |
|
115
|
|
|
* @param string $c |
|
116
|
|
|
* @param string $d |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
function uniCharCode(string $a, string $b, string $c, string $d): string |
|
120
|
|
|
{ |
|
121
|
|
|
return (dechex(ordUTF8($a)) << 12) | (dechex(ordUTF8($b)) << 8) | (dechex(ordUTF8($c)) << 4) | dechex(ordUTF8($d)); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $value |
|
126
|
|
|
* @return bool |
|
127
|
|
|
*/ |
|
128
|
|
|
function isOperation(string $value): bool |
|
129
|
|
|
{ |
|
130
|
|
|
// TODO: Benchmark |
|
131
|
|
|
return \in_array($value, ['query', 'mutation', 'subscription'], true); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param array $shorthand |
|
136
|
|
|
* @return array|null |
|
137
|
|
|
*/ |
|
138
|
|
|
function locationShorthandToArray(array $shorthand): ?array |
|
139
|
|
|
{ |
|
140
|
|
|
return isset($shorthand[0], $shorthand[1]) ? ['line' => $shorthand[0], 'column' => $shorthand[1]] : null; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param array $array |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
|
|
function block(array $array): string |
|
148
|
|
|
{ |
|
149
|
|
|
return !empty($array) ? "{\n" . indent(implode("\n", $array)) . "\n}" : ''; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $start |
|
154
|
|
|
* @param null|string $maybeString |
|
155
|
|
|
* @param null|string $end |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
function wrap(string $start, ?string $maybeString = null, ?string $end = null): string |
|
159
|
|
|
{ |
|
160
|
|
|
return null !== $maybeString ? ($start . $maybeString . ($end ?? '')) : ''; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param null|string $maybeString |
|
165
|
|
|
* @return string |
|
166
|
|
|
*/ |
|
167
|
|
|
function indent(?string $maybeString): string |
|
168
|
|
|
{ |
|
169
|
|
|
return null !== $maybeString ? ' ' . preg_replace("/\n/", "\n ", $maybeString) : ''; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @param string $str |
|
174
|
|
|
* @return string |
|
175
|
|
|
*/ |
|
176
|
|
|
function dedent(string $str): string |
|
177
|
|
|
{ |
|
178
|
|
|
$trimmed = \preg_replace("/^\n*|[ \t]*$/", '', $str); // Remove leading newline and trailing whitespace |
|
179
|
|
|
$matches = []; |
|
180
|
|
|
\preg_match("/^[ \t]*/", $trimmed, $matches); // Figure out indent |
|
181
|
|
|
$indent = $matches[0]; |
|
182
|
|
|
return \str_replace($indent, '', $trimmed); // Remove indent |
|
183
|
|
|
} |
|
184
|
|
|
|