Passed
Push — extract-store ( d338ce...38a23e )
by Konrad
04:01
created

SPARQLParser   F

Complexity

Total Complexity 258

Size/Duplication

Total Lines 817
Duplicated Lines 0 %

Test Coverage

Coverage 76.16%

Importance

Changes 0
Metric Value
eloc 424
dl 0
loc 817
ccs 329
cts 432
cp 0.7616
rs 2
c 0
b 0
f 0
wmc 258

35 Methods

Rating   Name   Duplication   Size   Complexity  
A indexBnodes() 0 13 6
A xIRIrefOrFunction() 0 8 5
A xSolutionModifier() 0 11 6
B xGraphPatternNotTriples() 0 28 11
A xAskQuery() 0 23 6
A xWhereClause() 0 10 4
A xBuiltInCall() 0 12 4
B xConditionalAndExpression() 0 19 8
A xBrackettedExpression() 0 11 5
B xIRI_REF() 0 13 7
B xExpression() 0 19 8
B xFilter() 0 17 8
B xRelationalExpression() 0 26 10
C xOrderCondition() 0 27 12
A xPrologue() 0 13 5
B xArgList() 0 25 8
A xOrderClause() 0 16 5
C xGroupGraphPattern() 0 43 15
A xResultVar() 0 3 1
A xPrimaryExpression() 0 10 4
A __construct() 0 7 1
B xAdditiveExpression() 0 24 10
C xSelectQuery() 0 55 17
B xGraphGraphPattern() 0 21 10
D xDescribeQuery() 0 54 19
A xDatasetClause() 0 10 5
B xConstructQuery() 0 35 10
B xUnaryExpression() 0 21 7
A xConstructTemplate() 0 13 5
B xMultiplicativeExpression() 0 21 8
A xFunctionCall() 0 9 5
A xOptionalGraphPattern() 0 11 4
A xLimitOrOffsetClause() 0 14 6
A xQuery() 0 11 4
B parse() 0 26 9

How to fix   Complexity   

Complex Class

Complex classes like SPARQLParser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use SPARQLParser, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/*
4
 * This file is part of the sweetrdf/InMemoryStoreSqlite package and licensed under
5
 * the terms of the GPL-3 license.
6
 *
7
 * (c) Konrad Abicht <[email protected]>
8
 * (c) Benjamin Nowack
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace sweetrdf\InMemoryStoreSqlite\Parser;
15
16
use function sweetrdf\InMemoryStoreSqlite\calcBase;
17
use sweetrdf\InMemoryStoreSqlite\Log\Logger;
18
use sweetrdf\InMemoryStoreSqlite\NamespaceHelper;
19
20
class SPARQLParser extends TurtleParser
21
{
22 85
    public function __construct(Logger $logger)
23
    {
24 85
        parent::__construct($logger);
25
26 85
        $this->bnode_prefix = 'arc'.substr(md5(uniqid(rand())), 0, 4).'b';
0 ignored issues
show
Bug Best Practice introduced by
The property bnode_prefix does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
27 85
        $this->bnode_id = 0;
28 85
        $this->bnode_pattern_index = ['patterns' => [], 'bnodes' => []];
0 ignored issues
show
Bug Best Practice introduced by
The property bnode_pattern_index does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
29 85
    }
30
31 85
    public function parse(string $q, string $path = ''): void
32
    {
33 85
        $this->base = $path ? calcBase($path) : NamespaceHelper::BASE_NAMESPACE;
34 85
        $this->r = [
35
            'base' => '',
36
            'vars' => [],
37
            'prefixes' => [],
38
        ];
39 85
        $this->unparsed_code = $q;
0 ignored issues
show
Bug Best Practice introduced by
The property unparsed_code does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
40 85
        list($r, $v) = $this->xQuery($q);
41 85
        if ($r) {
42 84
            $this->r['query'] = $r;
43 84
            $this->unparsed_code = trim($v);
44 3
        } elseif (!$this->logger->hasEntries('error') && !$this->unparsed_code) {
45
            $this->logger->error('Query not properly closed');
46
        }
47 85
        $this->r['prefixes'] = $this->prefixes;
48 85
        $this->r['base'] = $this->base;
49
        /* remove trailing comments */
50 85
        while (preg_match('/^\s*(\#[^\xd\xa]*)(.*)$/si', $this->unparsed_code, $m)) {
51
            $this->unparsed_code = $m[2];
52
        }
53 85
        if ($this->unparsed_code && !$this->logger->hasEntries('error')) {
54
            $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($this->unparsed_code, 0, 30));
55
            $msg = trim($rest) ? 'Could not properly handle "'.$rest.'"' : 'Syntax error, probably an incomplete pattern';
56
            $this->logger->error($msg);
57
        }
58 85
    }
59
60
    /* 1 */
61
62
    protected function xQuery($v)
63
    {
64
        list($r, $v) = $this->xPrologue($v);
65
        foreach (['Select', 'Construct', 'Describe', 'Ask'] as $type) {
66
            $m = 'x'.$type.'Query';
67
            if ((list($r, $v) = $this->$m($v)) && $r) {
68
                return [$r, $v];
69
            }
70
        }
71
72
        return [0, $v];
73
    }
74
75
    /* 2 */
76
77 85
    protected function xPrologue($v)
78
    {
79 85
        $r = 0;
80 85
        if ((list($sub_r, $v) = $this->xBaseDecl($v)) && $sub_r) {
81
            $this->base = $sub_r;
82
            $r = 1;
83
        }
84 85
        while ((list($sub_r, $v) = $this->xPrefixDecl($v)) && $sub_r) {
85 12
            $this->prefixes[$sub_r['prefix']] = $sub_r['uri'];
86 12
            $r = 1;
87
        }
88
89 85
        return [$r, $v];
90
    }
91
92
    /* 5.. */
93
94 85
    protected function xSelectQuery($v)
95
    {
96 85
        if ($sub_r = $this->x('SELECT\s+', $v)) {
97 79
            $r = [
98
                'type' => 'select',
99
                'result_vars' => [],
100
                'dataset' => [],
101
            ];
102 79
            $all_vars = 0;
103 79
            $sub_v = $sub_r[1];
104
            /* distinct, reduced */
105 79
            if ($sub_r = $this->x('(DISTINCT|REDUCED)\s+', $sub_v)) {
106 1
                $r[strtolower($sub_r[1])] = 1;
107 1
                $sub_v = $sub_r[2];
108
            }
109
            /* result vars */
110 79
            if ($sub_r = $this->x('\*\s+', $sub_v)) {
111 51
                $all_vars = 1;
112 51
                $sub_v = $sub_r[1];
113
            } else {
114 37
                while ((list($sub_r, $sub_v) = $this->xResultVar($sub_v)) && $sub_r) {
115 36
                    $r['result_vars'][] = $sub_r;
116
                }
117
            }
118 79
            if (!$all_vars && !\count($r['result_vars'])) {
119 1
                $this->logger->error('No result bindings specified.');
120
            }
121
            /* dataset */
122 79
            while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) {
123 34
                $r['dataset'][] = $sub_r;
124
            }
125
            /* where */
126 79
            if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) {
127 77
                $r['pattern'] = $sub_r;
128
            } else {
129 2
                return [0, $v];
130
            }
131
            /* solution modifier */
132 77
            if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) {
133 12
                $r = array_merge($r, $sub_r);
134
            }
135
            /* all vars */
136 77
            if ($all_vars) {
137 50
                foreach ($this->r['vars'] as $var) {
138 50
                    $r['result_vars'][] = ['var' => $var, 'aggregate' => 0, 'alias' => ''];
139
                }
140 50
                if (!$r['result_vars']) {
141
                    $r['result_vars'][] = '*';
142
                }
143
            }
144
145 77
            return [$r, $sub_v];
146
        }
147
148 74
        return [0, $v];
149
    }
150
151
    protected function xResultVar($v)
152
    {
153
        return $this->xVar($v);
154
    }
155
156
    /* 6.. */
157
158 74
    protected function xConstructQuery($v)
159
    {
160 74
        if ($sub_r = $this->x('CONSTRUCT\s*', $v)) {
161 3
            $r = [
162
                'type' => 'construct',
163
                'dataset' => [],
164
            ];
165 3
            $sub_v = $sub_r[1];
166
            /* construct template */
167 3
            if ((list($sub_r, $sub_v) = $this->xConstructTemplate($sub_v)) && \is_array($sub_r)) {
168 3
                $r['construct_triples'] = $sub_r;
169
            } else {
170
                $this->logger->error('Construct Template not found');
171
172
                return [0, $v];
173
            }
174
            /* dataset */
175 3
            while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) {
176
                $r['dataset'][] = $sub_r;
177
            }
178
            /* where */
179 3
            if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) {
180 3
                $r['pattern'] = $sub_r;
181
            } else {
182
                return [0, $v];
183
            }
184
            /* solution modifier */
185 3
            if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) {
186
                $r = array_merge($r, $sub_r);
187
            }
188
189 3
            return [$r, $sub_v];
190
        }
191
192 74
        return [0, $v];
193
    }
194
195
    /* 7.. */
196
197 74
    protected function xDescribeQuery($v)
198
    {
199 74
        if ($sub_r = $this->x('DESCRIBE\s+', $v)) {
200 3
            $r = [
201
                'type' => 'describe',
202
                'result_vars' => [],
203
                'result_uris' => [],
204
                'dataset' => [],
205
            ];
206 3
            $sub_v = $sub_r[1];
207 3
            $all_vars = 0;
208
            /* result vars/uris */
209 3
            if ($sub_r = $this->x('\*\s+', $sub_v)) {
210 1
                $all_vars = 1;
211 1
                $sub_v = $sub_r[1];
212
            } else {
213
                do {
214 2
                    $proceed = 0;
215 2
                    if ((list($sub_r, $sub_v) = $this->xResultVar($sub_v)) && $sub_r) {
216 1
                        $r['result_vars'][] = $sub_r;
217 1
                        $proceed = 1;
218
                    }
219 2
                    if ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) {
220 1
                        $r['result_uris'][] = $sub_r;
221 1
                        $proceed = 1;
222
                    }
223 2
                } while ($proceed);
224
            }
225 3
            if (!$all_vars && !\count($r['result_vars']) && !\count($r['result_uris'])) {
226
                $this->logger->error('No result bindings specified.');
227
            }
228
            /* dataset */
229 3
            while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) {
230
                $r['dataset'][] = $sub_r;
231
            }
232
            /* where */
233 3
            if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) {
234 2
                $r['pattern'] = $sub_r;
235
            }
236
            /* solution modifier */
237 3
            if ((list($sub_r, $sub_v) = $this->xSolutionModifier($sub_v)) && $sub_r) {
238
                $r = array_merge($r, $sub_r);
239
            }
240
            /* all vars */
241 3
            if ($all_vars) {
242 1
                foreach ($this->r['vars'] as $var) {
243 1
                    $r['result_vars'][] = ['var' => $var, 'aggregate' => 0, 'alias' => ''];
244
                }
245
            }
246
247 3
            return [$r, $sub_v];
248
        }
249
250 74
        return [0, $v];
251
    }
252
253
    /* 8.. */
254
255 74
    protected function xAskQuery($v)
256
    {
257 74
        if ($sub_r = $this->x('ASK\s+', $v)) {
258 2
            $r = [
259
                'type' => 'ask',
260
                'dataset' => [],
261
            ];
262 2
            $sub_v = $sub_r[1];
263
            /* dataset */
264 2
            while ((list($sub_r, $sub_v) = $this->xDatasetClause($sub_v)) && $sub_r) {
265 1
                $r['dataset'][] = $sub_r;
266
            }
267
            /* where */
268 2
            if ((list($sub_r, $sub_v) = $this->xWhereClause($sub_v)) && $sub_r) {
269 2
                $r['pattern'] = $sub_r;
270
271 2
                return [$r, $sub_v];
272
            } else {
273
                $this->logger->error('Missing or invalid WHERE clause.');
274
            }
275
        }
276
277 74
        return [0, $v];
278
    }
279
280
    /* 9, 10, 11, 12 */
281
282 84
    protected function xDatasetClause($v)
283
    {
284 84
        if ($r = $this->x('FROM(\s+NAMED)?\s+', $v)) {
285 35
            $named = $r[1] ? 1 : 0;
286 35
            if ((list($r, $sub_v) = $this->xIRIref($r[2])) && $r) {
287 35
                return [['graph' => $r, 'named' => $named], $sub_v];
288
            }
289
        }
290
291 84
        return [0, $v];
292
    }
293
294
    /* 13 */
295
296 84
    protected function xWhereClause($v)
297
    {
298 84
        if ($r = $this->x('(WHERE)?', $v)) {
299 84
            $v = $r[2];
300
        }
301 84
        if ((list($r, $v) = $this->xGroupGraphPattern($v)) && $r) {
302 82
            return [$r, $v];
303
        }
304
305 73
        return [0, $v];
306
    }
307
308
    /* 14, 15 */
309
310
    protected function xSolutionModifier($v)
311
    {
312
        $r = [];
313
        if ((list($sub_r, $sub_v) = $this->xOrderClause($v)) && $sub_r) {
314
            $r['order_infos'] = $sub_r;
315
        }
316
        while ((list($sub_r, $sub_v) = $this->xLimitOrOffsetClause($sub_v)) && $sub_r) {
317
            $r = array_merge($r, $sub_r);
318
        }
319
320
        return ($v == $sub_v) ? [0, $v] : [$r, $sub_v];
321
    }
322
323
    /* 18, 19 */
324
325 84
    protected function xLimitOrOffsetClause($v)
326
    {
327 84
        if ($sub_r = $this->x('(LIMIT|OFFSET)', $v)) {
328 3
            $key = strtolower($sub_r[1]);
329 3
            $sub_v = $sub_r[2];
330 3
            if ((list($sub_r, $sub_v) = $this->xINTEGER($sub_v)) && (false !== $sub_r)) {
331 3
                return [[$key => $sub_r], $sub_v];
332
            }
333
            if ((list($sub_r, $sub_v) = $this->xPlaceholder($sub_v)) && (false !== $sub_r)) {
334
                return [[$key => $sub_r], $sub_v];
335
            }
336
        }
337
338 84
        return [0, $v];
339
    }
340
341
    /* 16 */
342
343 84
    protected function xOrderClause($v)
344
    {
345 84
        if ($sub_r = $this->x('ORDER BY\s+', $v)) {
346 5
            $sub_v = $sub_r[1];
347 5
            $r = [];
348 5
            while ((list($sub_r, $sub_v) = $this->xOrderCondition($sub_v)) && $sub_r) {
349 4
                $r[] = $sub_r;
350
            }
351 5
            if (\count($r)) {
352 4
                return [$r, $sub_v];
353
            } else {
354 1
                $this->logger->error('No order conditions specified.');
355
            }
356
        }
357
358 84
        return [0, $v];
359
    }
360
361
    /* 17, 27 */
362
363 5
    protected function xOrderCondition($v)
364
    {
365 5
        if ($sub_r = $this->x('(ASC|DESC)', $v)) {
366 4
            $dir = strtolower($sub_r[1]);
367 4
            $sub_v = $sub_r[2];
368 4
            if ((list($sub_r, $sub_v) = $this->xBrackettedExpression($sub_v)) && $sub_r) {
369 4
                $sub_r['direction'] = $dir;
370
371 4
                return [$sub_r, $sub_v];
372
            }
373 5
        } elseif ((list($sub_r, $sub_v) = $this->xVar($v)) && $sub_r) {
374
            $sub_r['direction'] = 'asc';
375
376
            return [$sub_r, $sub_v];
377 5
        } elseif ((list($sub_r, $sub_v) = $this->xBrackettedExpression($v)) && $sub_r) {
378
            return [$sub_r, $sub_v];
379 5
        } elseif ((list($sub_r, $sub_v) = $this->xBuiltInCall($v)) && $sub_r) {
380
            $sub_r['direction'] = 'asc';
381
382
            return [$sub_r, $sub_v];
383 5
        } elseif ((list($sub_r, $sub_v) = $this->xFunctionCall($v)) && $sub_r) {
384
            $sub_r['direction'] = 'asc';
385
386
            return [$sub_r, $sub_v];
387
        }
388
389 5
        return [0, $v];
390
    }
391
392
    /* 20 */
393
394 84
    protected function xGroupGraphPattern($v)
395
    {
396 84
        $pattern_id = substr(md5(uniqid(rand())), 0, 4);
397 84
        if ($sub_r = $this->x('\{', $v)) {
398 83
            $r = ['type' => 'group', 'patterns' => []];
399 83
            $sub_v = $sub_r[1];
400 83
            if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_v)) && $sub_r) {
401 82
                $this->indexBnodes($sub_r, $pattern_id);
402 82
                $r['patterns'][] = ['type' => 'triples', 'patterns' => $sub_r];
403
            }
404
            do {
405 83
                $proceed = 0;
406 83
                if ((list($sub_r, $sub_v) = $this->xGraphPatternNotTriples($sub_v)) && $sub_r) {
407 3
                    $r['patterns'][] = $sub_r;
408 3
                    $pattern_id = substr(md5(uniqid(rand())), 0, 4);
409 3
                    $proceed = 1;
410 83
                } elseif ((list($sub_r, $sub_v) = $this->xFilter($sub_v)) && $sub_r) {
411 23
                    $r['patterns'][] = ['type' => 'filter', 'constraint' => $sub_r];
412 23
                    $proceed = 1;
413
                }
414 83
                if ($sub_r = $this->x('\.', $sub_v)) {
415
                    $sub_v = $sub_r[1];
416
                }
417 83
                if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_v)) && $sub_r) {
418
                    $this->indexBnodes($sub_r, $pattern_id);
419
                    $r['patterns'][] = ['type' => 'triples', 'patterns' => $sub_r];
420
                    $proceed = 1;
421
                }
422 83
                if ((list($sub_r, $sub_v) = $this->xPlaceholder($sub_v)) && $sub_r) {
423
                    $r['patterns'][] = $sub_r;
424
                    $proceed = 1;
425
                }
426 83
            } while ($proceed);
427 83
            if ($sub_r = $this->x('\}', $sub_v)) {
428 82
                $sub_v = $sub_r[1];
429
430 82
                return [$r, $sub_v];
431
            }
432 1
            $rest = preg_replace('/[\x0a|\x0d]/i', ' ', substr($sub_v, 0, 30));
433 1
            $this->logger->error('Incomplete or invalid Group Graph pattern. Could not handle "'.$rest.'"');
434
        }
435
436 84
        return [0, $v];
437
    }
438
439 82
    protected function indexBnodes($triples, $pattern_id)
440
    {
441 82
        $index_id = \count($this->bnode_pattern_index['patterns']);
0 ignored issues
show
Unused Code introduced by
The assignment to $index_id is dead and can be removed.
Loading history...
442 82
        $index_id = $pattern_id;
443 82
        $this->bnode_pattern_index['patterns'][] = $triples;
0 ignored issues
show
Bug Best Practice introduced by
The property bnode_pattern_index does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
444 82
        foreach ($triples as $t) {
445 82
            foreach (['s', 'p', 'o'] as $term) {
446 82
                if ('bnode' == $t[$term.'_type']) {
447 8
                    $val = $t[$term];
448 8
                    if (isset($this->bnode_pattern_index['bnodes'][$val]) && ($this->bnode_pattern_index['bnodes'][$val] != $index_id)) {
449
                        $this->logger->error('Re-used bnode label "'.$val.'" across graph patterns');
450
                    } else {
451 8
                        $this->bnode_pattern_index['bnodes'][$val] = $index_id;
452
                    }
453
                }
454
            }
455
        }
456 82
    }
457
458
    /* 22.., 25.. */
459
460 83
    protected function xGraphPatternNotTriples($v)
461
    {
462 83
        if ((list($sub_r, $sub_v) = $this->xOptionalGraphPattern($v)) && $sub_r) {
463 2
            return [$sub_r, $sub_v];
464
        }
465 83
        if ((list($sub_r, $sub_v) = $this->xGraphGraphPattern($v)) && $sub_r) {
466
            return [$sub_r, $sub_v];
467
        }
468 83
        $r = ['type' => 'union', 'patterns' => []];
469 83
        $sub_v = $v;
470
        do {
471 83
            $proceed = 0;
472 83
            if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) {
473 1
                $r['patterns'][] = $sub_r;
474 1
                if ($sub_r = $this->x('UNION', $sub_v)) {
475 1
                    $sub_v = $sub_r[1];
476 1
                    $proceed = 1;
477
                }
478
            }
479 83
        } while ($proceed);
480 83
        $pc = \count($r['patterns']);
481 83
        if (1 == $pc) {
482
            return [$r['patterns'][0], $sub_v];
483 83
        } elseif ($pc > 1) {
484 1
            return [$r, $sub_v];
485
        }
486
487 83
        return [0, $v];
488
    }
489
490
    /* 23 */
491
492 83
    protected function xOptionalGraphPattern($v)
493
    {
494 83
        if ($sub_r = $this->x('OPTIONAL', $v)) {
495 2
            $sub_v = $sub_r[1];
496 2
            if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) {
497 2
                return [['type' => 'optional', 'patterns' => $sub_r['patterns']], $sub_v];
498
            }
499
            $this->logger->error('Missing or invalid Group Graph Pattern after OPTIONAL');
500
        }
501
502 83
        return [0, $v];
503
    }
504
505
    /* 24.. */
506
507 83
    protected function xGraphGraphPattern($v)
508
    {
509 83
        if ($sub_r = $this->x('GRAPH', $v)) {
510
            $sub_v = $sub_r[1];
511
            $r = ['type' => 'graph', 'var' => '', 'uri' => '', 'patterns' => []];
512
            if ((list($sub_r, $sub_v) = $this->xVar($sub_v)) && $sub_r) {
513
                $r['var'] = $sub_r;
514
            } elseif ((list($sub_r, $sub_v) = $this->xIRIref($sub_v)) && $sub_r) {
515
                $r['uri'] = $sub_r;
516
            }
517
            if ($r['var'] || $r['uri']) {
518
                if ((list($sub_r, $sub_v) = $this->xGroupGraphPattern($sub_v)) && $sub_r) {
519
                    $r['patterns'][] = $sub_r;
520
521
                    return [$r, $sub_v];
522
                }
523
                $this->logger->error('Missing or invalid Graph Pattern');
524
            }
525
        }
526
527 83
        return [0, $v];
528
    }
529
530
    /* 26.., 27.. */
531
532 83
    protected function xFilter($v)
533
    {
534 83
        if ($r = $this->x('FILTER', $v)) {
535 23
            $sub_v = $r[1];
536 23
            if ((list($r, $sub_v) = $this->xBrackettedExpression($sub_v)) && $r) {
537 19
                return [$r, $sub_v];
538
            }
539 4
            if ((list($r, $sub_v) = $this->xBuiltInCall($sub_v)) && $r) {
540 4
                return [$r, $sub_v];
541
            }
542
            if ((list($r, $sub_v) = $this->xFunctionCall($sub_v)) && $r) {
543
                return [$r, $sub_v];
544
            }
545
            $this->logger->error('Incomplete FILTER');
546
        }
547
548 83
        return [0, $v];
549
    }
550
551
    /* 28.. */
552
553 5
    protected function xFunctionCall($v)
554
    {
555 5
        if ((list($r, $sub_v) = $this->xIRIref($v)) && $r) {
556
            if ((list($sub_r, $sub_v) = $this->xArgList($sub_v)) && $sub_r) {
557
                return [['type' => 'function_call', 'uri' => $r, 'args' => $sub_r], $sub_v];
558
            }
559
        }
560
561 5
        return [0, $v];
562
    }
563
564
    /* 29 */
565
566 18
    protected function xArgList($v)
567
    {
568 18
        $r = [];
569 18
        $sub_v = $v;
570 18
        $closed = 0;
571 18
        if ($sub_r = $this->x('\(', $sub_v)) {
572 18
            $sub_v = $sub_r[1];
573
            do {
574 18
                $proceed = 0;
575 18
                if ((list($sub_r, $sub_v) = $this->xExpression($sub_v)) && $sub_r) {
576 18
                    $r[] = $sub_r;
577 18
                    if ($sub_r = $this->x('\,', $sub_v)) {
578 4
                        $sub_v = $sub_r[1];
579 4
                        $proceed = 1;
580
                    }
581
                }
582 18
                if ($sub_r = $this->x('\)', $sub_v)) {
583 18
                    $sub_v = $sub_r[1];
584 18
                    $closed = 1;
585 18
                    $proceed = 0;
586
                }
587 18
            } while ($proceed);
588
        }
589
590 18
        return $closed ? [$r, $sub_v] : [0, $v];
591
    }
592
593
    /* 30, 31 */
594
595 74
    protected function xConstructTemplate($v)
596
    {
597 74
        if ($sub_r = $this->x('\{', $v)) {
598 74
            $r = [];
599 74
            if ((list($sub_r, $sub_v) = $this->xTriplesBlock($sub_r[1])) && \is_array($sub_r)) {
600 74
                $r = $sub_r;
601
            }
602 74
            if ($sub_r = $this->x('\}', $sub_v)) {
603 73
                return [$r, $sub_r[1]];
604
            }
605
        }
606
607 5
        return [0, $v];
608
    }
609
610
    /* 46, 47 */
611
612 27
    protected function xExpression($v)
613
    {
614 27
        if ((list($sub_r, $sub_v) = $this->xConditionalAndExpression($v)) && $sub_r) {
615 27
            $r = ['type' => 'expression', 'sub_type' => 'or', 'patterns' => [$sub_r]];
616
            do {
617 27
                $proceed = 0;
618 27
                if ($sub_r = $this->x('\|\|', $sub_v)) {
619
                    $sub_v = $sub_r[1];
620
                    if ((list($sub_r, $sub_v) = $this->xConditionalAndExpression($sub_v)) && $sub_r) {
621
                        $r['patterns'][] = $sub_r;
622
                        $proceed = 1;
623
                    }
624
                }
625 27
            } while ($proceed);
626
627 27
            return 1 == \count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v];
628
        }
629
630
        return [0, $v];
631
    }
632
633
    /* 48.., 49.. */
634
635 27
    protected function xConditionalAndExpression($v)
636
    {
637 27
        if ((list($sub_r, $sub_v) = $this->xRelationalExpression($v)) && $sub_r) {
638 27
            $r = ['type' => 'expression', 'sub_type' => 'and', 'patterns' => [$sub_r]];
639
            do {
640 27
                $proceed = 0;
641 27
                if ($sub_r = $this->x('\&\&', $sub_v)) {
642 1
                    $sub_v = $sub_r[1];
643 1
                    if ((list($sub_r, $sub_v) = $this->xRelationalExpression($sub_v)) && $sub_r) {
644 1
                        $r['patterns'][] = $sub_r;
645 1
                        $proceed = 1;
646
                    }
647
                }
648 27
            } while ($proceed);
649
650 27
            return 1 == \count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v];
651
        }
652
653
        return [0, $v];
654
    }
655
656
    /* 50, 51 */
657
658 27
    protected function xRelationalExpression($v)
659
    {
660 27
        if ((list($sub_r, $sub_v) = $this->xAdditiveExpression($v)) && $sub_r) {
661 27
            $r = ['type' => 'expression', 'sub_type' => 'relational', 'patterns' => [$sub_r]];
662
            do {
663 27
                $proceed = 0;
664
                /* don't mistake '<' + uriref with '<'-operator ("longest token" rule) */
665 27
                if ((list($sub_r, $sub_v) = $this->xIRI_REF($sub_v)) && $sub_r) {
666
                    $this->logger->error('Expected operator, found IRIref: "'.$sub_r.'".');
667
                }
668 27
                if ($sub_r = $this->x('(\!\=|\=\=|\=|\<\=|\>\=|\<|\>)', $sub_v)) {
669 9
                    $op = $sub_r[1];
670 9
                    $sub_v = $sub_r[2];
671 9
                    $r['operator'] = $op;
672 9
                    if ((list($sub_r, $sub_v) = $this->xAdditiveExpression($sub_v)) && $sub_r) {
673
                        //$sub_r['operator'] = $op;
674 9
                        $r['patterns'][] = $sub_r;
675 9
                        $proceed = 1;
676
                    }
677
                }
678 27
            } while ($proceed);
679
680 27
            return 1 == \count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v];
681
        }
682
683
        return [0, $v];
684
    }
685
686
    /* 52 */
687
688 27
    protected function xAdditiveExpression($v)
689
    {
690 27
        if ((list($sub_r, $sub_v) = $this->xMultiplicativeExpression($v)) && $sub_r) {
691 27
            $r = ['type' => 'expression', 'sub_type' => 'additive', 'patterns' => [$sub_r]];
692
            do {
693 27
                $proceed = 0;
694 27
                if ($sub_r = $this->x('(\+|\-)', $sub_v)) {
695
                    $op = $sub_r[1];
696
                    $sub_v = $sub_r[2];
697
                    if ((list($sub_r, $sub_v) = $this->xMultiplicativeExpression($sub_v)) && $sub_r) {
698
                        $sub_r['operator'] = $op;
699
                        $r['patterns'][] = $sub_r;
700
                        $proceed = 1;
701
                    } elseif ((list($sub_r, $sub_v) = $this->xNumericLiteral($sub_v)) && $sub_r) {
702
                        $r['patterns'][] = ['type' => 'numeric', 'operator' => $op, 'value' => $sub_r];
703
                        $proceed = 1;
704
                    }
705
                }
706 27
            } while ($proceed);
707
            //return array($r, $sub_v);
708 27
            return 1 == \count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v];
709
        }
710
711
        return [0, $v];
712
    }
713
714
    /* 53 */
715
716 27
    protected function xMultiplicativeExpression($v)
717
    {
718 27
        if ((list($sub_r, $sub_v) = $this->xUnaryExpression($v)) && $sub_r) {
719 27
            $r = ['type' => 'expression', 'sub_type' => 'multiplicative', 'patterns' => [$sub_r]];
720
            do {
721 27
                $proceed = 0;
722 27
                if ($sub_r = $this->x('(\*|\/)', $sub_v)) {
723
                    $op = $sub_r[1];
724
                    $sub_v = $sub_r[2];
725
                    if ((list($sub_r, $sub_v) = $this->xUnaryExpression($sub_v)) && $sub_r) {
726
                        $sub_r['operator'] = $op;
727
                        $r['patterns'][] = $sub_r;
728
                        $proceed = 1;
729
                    }
730
                }
731 27
            } while ($proceed);
732
733 27
            return 1 == \count($r['patterns']) ? [$r['patterns'][0], $sub_v] : [$r, $sub_v];
734
        }
735
736
        return [0, $v];
737
    }
738
739
    /* 54 */
740
741 27
    protected function xUnaryExpression($v)
742
    {
743 27
        $sub_v = $v;
744 27
        $op = '';
745 27
        if ($sub_r = $this->x('(\!|\+|\-)', $sub_v)) {
746
            $op = $sub_r[1];
747
            $sub_v = $sub_r[2];
748
        }
749 27
        if ((list($sub_r, $sub_v) = $this->xPrimaryExpression($sub_v)) && $sub_r) {
750 27
            if (!\is_array($sub_r)) {
751
                $sub_r = ['type' => 'unary', 'expression' => $sub_r];
752 27
            } elseif ($sub_op = $sub_r['operator'] ?? '') {
753
                $ops = ['!!' => '', '++' => '+', '--' => '+', '+-' => '-', '-+' => '-'];
754
                $op = isset($ops[$op.$sub_op]) ? $ops[$op.$sub_op] : $op.$sub_op;
755
            }
756 27
            $sub_r['operator'] = $op;
757
758 27
            return [$sub_r, $sub_v];
759
        }
760
761
        return [0, $v];
762
    }
763
764
    /* 55 */
765
766 27
    protected function xPrimaryExpression($v)
767
    {
768 27
        foreach (['BrackettedExpression', 'BuiltInCall', 'IRIrefOrFunction', 'RDFLiteral', 'NumericLiteral', 'BooleanLiteral', 'Var', 'Placeholder'] as $type) {
769 27
            $m = 'x'.$type;
770 27
            if ((list($sub_r, $sub_v) = $this->$m($v)) && $sub_r) {
771 27
                return [$sub_r, $sub_v];
772
            }
773
        }
774
775
        return [0, $v];
776
    }
777
778
    /* 56 */
779
780 28
    protected function xBrackettedExpression($v)
781
    {
782 28
        if ($r = $this->x('\(', $v)) {
783 23
            if ((list($r, $sub_v) = $this->xExpression($r[1])) && $r) {
784 23
                if ($sub_r = $this->x('\)', $sub_v)) {
785 23
                    return [$r, $sub_r[1]];
786
                }
787
            }
788
        }
789
790 28
        return [0, $v];
791
    }
792
793
    /* 57.., 58.. */
794
795 28
    protected function xBuiltInCall($v)
796
    {
797 28
        if ($sub_r = $this->x('(str|lang|langmatches|datatype|bound|sameterm|isiri|isuri|isblank|isliteral|regex)\s*\(', $v)) {
798 18
            $r = ['type' => 'built_in_call', 'call' => strtolower($sub_r[1])];
799 18
            if ((list($sub_r, $sub_v) = $this->xArgList('('.$sub_r[2])) && \is_array($sub_r)) {
800 18
                $r['args'] = $sub_r;
801
802 18
                return [$r, $sub_v];
803
            }
804
        }
805
806 28
        return [0, $v];
807
    }
808
809
    /* 59.. */
810
811 27
    protected function xIRIrefOrFunction($v)
812
    {
813 27
        if ((list($r, $v) = $this->xIRIref($v)) && $r) {
814 1
            if ((list($sub_r, $sub_v) = $this->xArgList($v)) && \is_array($sub_r)) {
815
                return [['type' => 'function', 'uri' => $r, 'args' => $sub_r], $sub_v];
816
            }
817
818 1
            return [['type' => 'uri', 'uri' => $r], $sub_v];
819
        }
820 27
    }
821
822
    /* 70.. @@sync with TurtleParser */
823
824 85
    protected function xIRI_REF($v)
825
    {
826 85
        if (($r = $this->x('\<(\$\{[^\>]*\})\>', $v)) && ($sub_r = $this->xPlaceholder($r[1]))) {
0 ignored issues
show
Unused Code introduced by
The assignment to $sub_r is dead and can be removed.
Loading history...
827
            return [$r[1], $r[2]];
828 85
        } elseif ($r = $this->x('\<([^\<\>\s\"\|\^`]*)\>', $v)) {
829 84
            return [$r[1] ? $r[1] : true, $r[2]];
830
        }
831
        /* allow reserved chars in obvious IRIs */
832 85
        elseif ($r = $this->x('\<(https?\:[^\s][^\<\>]*)\>', $v)) {
833
            return [$r[1] ? $r[1] : true, $r[2]];
834
        }
835
836 85
        return [0, $v];
837
    }
838
}
839