-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathJSONPathLexer.php
More file actions
369 lines (284 loc) · 11.4 KB
/
JSONPathLexer.php
File metadata and controls
369 lines (284 loc) · 11.4 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* JSONPath implementation for PHP.
*
* @license https://github.com/SoftCreatR/JSONPath/blob/main/LICENSE MIT License
*/
declare(strict_types=1);
namespace Flow\JSONPath;
class JSONPathLexer
{
/*
* Match within bracket groups
* Matches are whitespace insensitive
*/
// e.g.: foo or 40f35757-2563-4790-b0b1-caa904be455f or $
public const string MATCH_INDEX = '(?!-)[\-\w]+ | \\$ | \\*';
// Eg. 0,1,2 or *,1 or 0,1,2,
public const string MATCH_INDEXES = '\s* (?:-?\d+|\*) (?: \s* , \s* (?:-?\d+|\*) )+ \s* ,? \s*';
// Eg. [0:2:1] or [-1]
public const string MATCH_SLICE = '(?:-?\d*:-?\d*(?::-?\d*)?|-\\d+)';
// Eg. ?(@.length - 1)
public const string MATCH_QUERY_RESULT = '\s* \( .+? \) \s*';
// Eg. ?(@.foo = "bar")
public const string MATCH_QUERY_MATCH = '\s* \?\(.+?\) \s*';
// Eg. 'bar'
public const string MATCH_INDEX_IN_SINGLE_QUOTES = '\s* \' (.+?)? \' \s*';
// Eg. "bar"
public const string MATCH_INDEX_IN_DOUBLE_QUOTES = '\s* " (.+?)? " \s*';
private readonly string $expression;
private readonly int $expressionLength;
public function __construct(string $expression)
{
$expression = \trim($expression);
$len = \strlen($expression);
if ($len === 0) {
$this->expression = '';
$this->expressionLength = 0;
return;
}
if ($expression[0] === '$' || $expression[0] === '@') {
$expression = \substr($expression, 1);
}
if ($expression === '') {
$this->expression = '';
$this->expressionLength = 0;
return;
}
if ($expression[0] !== '.' && $expression[0] !== '[') {
$expression = '.' . $expression;
}
$this->expression = $expression;
$this->expressionLength = \strlen($expression);
}
/**
* @return list<JSONPathToken>
* @throws JSONPathException
*/
public function parseExpressionTokens(): array
{
$squareBracketDepth = 0;
$tokenValue = '';
$tokens = [];
$inBracketQuote = null;
$inQuote = null;
for ($i = 0; $i < $this->expressionLength; $i++) {
$char = $this->expression[$i];
if ($squareBracketDepth === 0 && ($char === "'" || $char === '"')) {
$escaped = $this->isEscaped($tokenValue);
$inQuote = $inQuote === $char && !$escaped ? null : ($inQuote ?? $char);
}
if (($squareBracketDepth === 0) && $inQuote === null && $char === '.') {
if ($this->lookAhead($i) === '.') {
$tokens[] = new JSONPathToken(TokenType::Recursive, null);
}
continue;
}
if ($char === '[' && $inBracketQuote === null) {
$squareBracketDepth++;
if ($squareBracketDepth === 1) {
$inBracketQuote = null;
continue;
}
}
if ($char === ']' && $squareBracketDepth > 0 && $inBracketQuote === null) {
$squareBracketDepth--;
if ($squareBracketDepth === 0) {
$tokens[] = $this->createToken($tokenValue);
$tokenValue = '';
continue;
}
}
/*
* Within square brackets
*/
if ($squareBracketDepth > 0) {
if (($char === "'" || $char === '"')) {
$escaped = $this->isEscaped($tokenValue);
if ($inBracketQuote === null && !$escaped) {
$inBracketQuote = $char;
} elseif ($inBracketQuote === $char && !$escaped) {
$inBracketQuote = null;
}
}
$tokenValue .= $char;
continue;
}
/*
* Outside square brackets
*/
$tokenValue .= $char;
if (
$inQuote === null
&& ($this->atEnd($i) || \in_array($this->lookAhead($i), ['.', '['], true))
) {
$tokens[] = $this->createToken($tokenValue);
$tokenValue = '';
}
}
if ($tokenValue !== '') {
$tokens[] = $this->createToken($tokenValue);
}
return $tokens;
}
protected function lookAhead(int $pos, int $forward = 1): ?string
{
return $this->expression[$pos + $forward] ?? null;
}
protected function atEnd(int $pos): bool
{
return $pos === ($this->expressionLength - 1);
}
/**
* @return list<JSONPathToken>
* @throws JSONPathException
*/
public function parseExpression(): array
{
return $this->parseExpressionTokens();
}
/**
* @throws JSONPathException
*/
protected function createToken(string $value): JSONPathToken
{
// The IDE doesn't like, what we do with $value, so let's
// move it to a separate variable, to get rid of any IDE warnings
$tokenValue = \trim($value);
/** @var JSONPathToken|null $ret */
$ret = null;
if (\str_contains($tokenValue, ',')) {
$parts = \array_values(\array_filter(
\array_map('trim', \explode(',', $tokenValue)),
static fn (string $part): bool => $part !== ''
));
if ($parts !== []) {
$union = [];
$hasSlice = false;
$hasQuery = false;
foreach ($parts as $part) {
if (
\preg_match('/^' . static::MATCH_INDEX_IN_SINGLE_QUOTES . '$/xu', $part, $matches)
|| \preg_match('/^' . static::MATCH_INDEX_IN_DOUBLE_QUOTES . '$/xu', $part, $matches)
) {
$union[] = $this->decodeQuotedIndex($matches[1] ?? '', $matches[0][0]);
continue;
}
if (\preg_match('/^-\\d+$/', $part)) {
$union[] = (int)$part;
continue;
}
if (\preg_match('/^' . static::MATCH_SLICE . '$/u', $part)) {
$union[] = [
'type' => 'slice',
'value' => $this->parseSlice($part),
];
$hasSlice = true;
continue;
}
if (\preg_match('/^(' . static::MATCH_INDEX . ')$/xu', $part)) {
$union[] = \preg_match('/^-?\d+$/', $part) ? (int)$part : $part;
continue;
}
if (\preg_match('/^' . static::MATCH_QUERY_MATCH . '$/xu', $part)) {
$union[] = [
'type' => 'query',
'value' => \substr($part, 2, -1),
];
$hasQuery = true;
}
}
if (\count($union) === \count($parts)) {
$quotedPattern = '/^(' . static::MATCH_INDEX_IN_SINGLE_QUOTES . '|'
. static::MATCH_INDEX_IN_DOUBLE_QUOTES . ')$/xu';
$quotedCallback = static function (string $part) use ($quotedPattern): bool {
return \preg_match($quotedPattern, $part) === 1;
};
$quotedParts = \array_filter($parts, $quotedCallback);
$allQuoted = \count($quotedParts) === \count($parts);
$tokenType = ($hasSlice || $hasQuery || !$allQuoted) ? TokenType::Indexes : TokenType::Index;
return new JSONPathToken($tokenType, $union, $allQuoted);
}
}
}
if (\preg_match('/^-\\d+$/', $tokenValue)) {
return new JSONPathToken(TokenType::Index, (int)$tokenValue);
}
if ($tokenValue === '') {
return new JSONPathToken(TokenType::Indexes, []);
}
if (
($tokenValue[0] === "'" || $tokenValue[0] === '"')
&& $tokenValue[\strlen($tokenValue) - 1] === $tokenValue[0]
) {
$tokenValue = $this->decodeQuotedIndex(\substr($tokenValue, 1, -1), $tokenValue[0]);
return new JSONPathToken(TokenType::Index, $tokenValue, true);
}
if (\preg_match('/^(' . static::MATCH_INDEX . ')$/xu', $tokenValue, $matches)) {
if (\preg_match('/^-?\d+$/', $tokenValue)) {
$tokenValue = (int)$tokenValue;
}
$ret = new JSONPathToken(TokenType::Index, $tokenValue);
} elseif (\preg_match('/^' . static::MATCH_SLICE . '$/xu', $tokenValue, $matches)) {
$tokenValue = $this->parseSlice($tokenValue);
$ret = new JSONPathToken(TokenType::Slice, $tokenValue);
} elseif (\preg_match('/^' . static::MATCH_QUERY_RESULT . '$/xu', $tokenValue)) {
$tokenValue = \substr($tokenValue, 1, -1);
$ret = new JSONPathToken(TokenType::QueryResult, $tokenValue);
} elseif ($tokenValue === '?()') {
$ret = new JSONPathToken(TokenType::QueryMatch, '', shorthand: false);
} elseif ($tokenValue === '?') {
$ret = new JSONPathToken(TokenType::QueryMatch, '@', shorthand: true);
} elseif (\preg_match('/^\\?@/', $tokenValue)) {
$expr = \substr($tokenValue, 1);
$expr = $expr === '' ? '@' : $expr;
$ret = new JSONPathToken(TokenType::QueryMatch, $expr, shorthand: true);
} elseif (\preg_match('/^' . static::MATCH_QUERY_MATCH . '$/xu', $tokenValue)) {
$tokenValue = \substr($tokenValue, 2, -1);
$ret = new JSONPathToken(TokenType::QueryMatch, $tokenValue);
}
if ($ret !== null) {
return $ret;
}
throw new JSONPathException("Unable to parse token {$tokenValue} in expression: {$this->expression}");
}
/**
* @return array{start: int|null, end: int|null, step: int|null}
*/
private function parseSlice(string $tokenValue): array
{
$parts = \explode(':', $tokenValue);
return [
'start' => $parts[0] !== '' ? (int)$parts[0] : null,
'end' => isset($parts[1]) && $parts[1] !== '' ? (int)$parts[1] : null,
'step' => isset($parts[2]) && $parts[2] !== '' ? (int)$parts[2] : null,
];
}
private function isEscaped(string $tokenValue): bool
{
$len = \strlen($tokenValue);
if ($len === 0) {
return false;
}
$backslashCount = 0;
for ($i = $len - 1; $i >= 0; $i--) {
if ($tokenValue[$i] === '\\') {
$backslashCount++;
continue;
}
break;
}
return ($backslashCount % 2) === 1;
}
private function decodeQuotedIndex(string $tokenValue, string $quote): string
{
// Unescape backslashes first, then the quote type used
$tokenValue = \str_replace('\\\\', '\\', $tokenValue);
if ($quote === "'") {
$tokenValue = \str_replace("\\'", "'", $tokenValue);
} elseif ($quote === '"') {
$tokenValue = \str_replace('\\"', '"', $tokenValue);
}
return $tokenValue;
}
}