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
<?php
namespace Flow\JSONPath;
class JSONPathLexer
{
/*
* Match within bracket groups
* Matches are whitespace insensitive
*/
const MATCH_INDEX = '-?\w+ | \*'; // Eg. foo
const MATCH_INDEXES = '\s* -?\d+ [-?\d,\s]+'; // Eg. 0,1,2
const MATCH_SLICE = '[-\d:]+ | :'; // Eg. [0:2:1]
const MATCH_QUERY_RESULT = '\s* \( .+? \) \s*'; // Eg. ?(@.length - 1)
const MATCH_QUERY_MATCH = '\s* \?\(.+?\) \s*'; // Eg. ?(@.foo = "bar")
const MATCH_INDEX_IN_SINGLE_QUOTES = '\s* \' (.+?) \' \s*'; // Eg. 'bar'
const MATCH_INDEX_IN_DOUBLE_QUOTES = '\s* " (.+?) " \s*'; // Eg. 'bar'
/**
* The expression being lexed.
*
* @var string
*/
protected $expression = '';
/**
* The length of the expression.
*
* @var int
*/
protected $expressionLength = 0;
public function __construct($expression)
{
$expression = trim($expression);
if (!strlen($expression)) {
return;
}
if ($expression[0] === '$') {
$expression = substr($expression, 1);
}
if ($expression[0] !== '.' && $expression[0] !== '[') {
$expression = '.' . $expression;
}
$this->expression = $expression;
$this->expressionLength = strlen($expression);
}
public function parseExpressionTokens()
{
$dotIndexDepth = 0;
$squareBracketDepth = 0;
$capturing = false;
$tokenValue = '';
$tokens = [];
for ($i = 0; $i < $this->expressionLength; $i++) {
$char = $this->expression[$i];
if ($squareBracketDepth === 0) {
if ($char === '.') {
if ($this->lookAhead($i, 1) === ".") {
$tokens[] = new JSONPathToken(JSONPathToken::T_RECURSIVE, null);
}
continue;
}
}
if ($char === '[') {
$squareBracketDepth += 1;
if ($squareBracketDepth === 1) {
continue;
}
}
if ($char === ']') {
$squareBracketDepth -= 1;
if ($squareBracketDepth === 0) {
continue;
}
}
/*
* Within square brackets
*/
if ($squareBracketDepth > 0) {
$tokenValue .= $char;
if ($this->lookAhead($i, 1) === ']' && $squareBracketDepth === 1) {
$tokens[] = $this->createToken($tokenValue);
$tokenValue = '';
}
}
/*
* Outside square brackets
*/
if ($squareBracketDepth === 0) {
$tokenValue .= $char;
// Double dot ".."
if ($char === "." && $dotIndexDepth > 1) {
$tokens[] = $this->createToken($tokenValue);
$tokenValue = '';
continue;
}
if ($this->lookAhead($i, 1) === '.' || $this->lookAhead($i, 1) === '[' || $this->atEnd($i)) {
$tokens[] = $this->createToken($tokenValue);
$tokenValue = '';
$dotIndexDepth -= 1;
}
}
}
if ($tokenValue !== '') {
$tokens[] = $this->createToken($tokenValue);
$tokenValue = '';
}
return $tokens;
}
protected function lookAhead($pos, $forward = 1)
{
return isset($this->expression[$pos + $forward]) ? $this->expression[$pos + $forward] : null;
}
protected function atEnd($pos)
{
return $pos === $this->expressionLength;
}
public function parseExpression()
{
$tokens = $this->parseExpressionTokens();
return $tokens;
}
/**
* @param $value
* @return string
*/
protected function createToken($value)
{
if (preg_match('/^(' . static::MATCH_INDEX . ')$/x', $value, $matches)) {
if (preg_match('/^-?\d+$/', $value)) {
$value = (int)$value;
}
return new JSONPathToken(JSONPathToken::T_INDEX, $value);
}
if (preg_match('/^' . static::MATCH_INDEXES . '$/x', $value, $matches)) {
$value = explode(',', trim($value, ','));
foreach ($value as $i => $v) {
$value[$i] = (int) trim($v);
}
return new JSONPathToken(JSONPathToken::T_INDEXES, $value);
}
if (preg_match('/^' . static::MATCH_SLICE . '$/x', $value, $matches)) {
$parts = explode(':', $value);
$value = [
'start' => isset($parts[0]) && $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,
];
return new JSONPathToken(JSONPathToken::T_SLICE, $value);
}
if (preg_match('/^' . static::MATCH_QUERY_RESULT . '$/x', $value)) {
$value = substr($value, 1, -1);
return new JSONPathToken(JSONPathToken::T_QUERY_RESULT, $value);
}
if (preg_match('/^' . static::MATCH_QUERY_MATCH . '$/x', $value)) {
$value = substr($value, 2, -1);
return new JSONPathToken(JSONPathToken::T_QUERY_MATCH, $value);
}
if (preg_match('/^' . static::MATCH_INDEX_IN_SINGLE_QUOTES . '$/x', $value, $matches)) {
$value = $matches[1];
$value = trim($value);
return new JSONPathToken(JSONPathToken::T_INDEX, $value);
}
if (preg_match('/^' . static::MATCH_INDEX_IN_DOUBLE_QUOTES . '$/x', $value, $matches)) {
$value = $matches[1];
$value = trim($value);
return new JSONPathToken(JSONPathToken::T_INDEX, $value);
}
throw new JSONPathException("Unable to parse token {$value} in expression: $this->expression");
}
}