-
Notifications
You must be signed in to change notification settings - Fork 15
/
06-improve-array-parser.php
45 lines (37 loc) · 1.16 KB
/
06-improve-array-parser.php
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
<?php
namespace Graviton\RqlParserExample07;
use Graviton\RqlParser\Lexer;
use Graviton\RqlParser\Parser;
use Graviton\RqlParser\Node;
use Graviton\RqlParser\Token;
use Graviton\RqlParser\NodeParser\Query\ComparisonOperator\Rql\InNodeParser;
use Graviton\RqlParser\TokenStream;
use Graviton\RqlParser\ValueParser;
require __DIR__ . '/../vendor/autoload.php';
class ArrayParser extends ValueParser\ArrayParser
{
/**
* @inheritdoc
*/
public function parse(TokenStream $tokenStream)
{
if ($tokenStream->nextIf(Token::T_TYPE, 'array')) {
$tokenStream->expect(Token::T_COLON);
return [$this->itemParser->parse($tokenStream)];
} else {
return parent::parse($tokenStream);
}
}
}
$scalarParser = new ValueParser\ScalarParser();
$fieldNameParser = new ValueParser\FieldParser();
$arrayParser = new ArrayParser($scalarParser);
$nodeParser = new InNodeParser($fieldNameParser, $arrayParser);
// parse
$lexer = new Lexer();
$parser = new Parser($nodeParser);
$tokenStream = $lexer->tokenize(implode('&', [
'in(a,(1,string,true()))',
'in(b,array:1)',
]));
var_dump($parser->parse($tokenStream));