-
Notifications
You must be signed in to change notification settings - Fork 15
/
08-string-quoting.php
219 lines (192 loc) · 7.07 KB
/
08-string-quoting.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
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
<?php
namespace Graviton\RqlParserExample05;
use Graviton\RqlParser\Exception\SyntaxErrorException;
use Graviton\RqlParser\Lexer;
use Graviton\RqlParser\Node;
use Graviton\RqlParser\Parser;
use Graviton\RqlParser\Token;
use Graviton\RqlParser\TokenStream;
use Graviton\RqlParser\SubParserInterface;
use Graviton\RqlParser\NodeParserChain;
use Graviton\RqlParser\SubLexerChain;
use Graviton\RqlParser\SubLexer;
use Graviton\RqlParser\SubLexerInterface;
use Graviton\RqlParser\NodeParser;
use Graviton\RqlParser\TypeCaster;
use Graviton\RqlParser\ValueParser;
use Graviton\RqlParser\NodeParser\Query\LogicalOperator;
use Graviton\RqlParser\NodeParser\Query\ComparisonOperator;
require __DIR__ . '/../vendor/autoload.php';
const T_IDENTIFIER = 9999;
Token::$typeNameMap[T_IDENTIFIER] = 'T_IDENTIFIER';
/**
* lex T_IDENTIFIER token
*/
class IdentifierSubLexer implements SubLexerInterface
{
/**
* @inheritdoc
*/
public function getTokenAt($code, $cursor)
{
if (!preg_match('/[a-z][a-z0-9]*(?:\.[a-z][a-z0-9]*)*/Ai', $code, $matches, 0, $cursor)) {
return null;
}
return new Token(
T_IDENTIFIER,
$matches[0],
$cursor,
$cursor + strlen($matches[0])
);
}
}
/**
* quoted string
*/
class StringSubLexer implements SubLexerInterface
{
/**
* @inheritdoc
*/
public function getTokenAt($code, $cursor)
{
if (!preg_match('/"(?:\\\\"|.)*?"/A', $code, $matches, 0, $cursor)) {
return null;
}
return new Token(
Token::T_STRING,
strtr(substr($matches[0], 1, -1), ['\\"' => '"']),
$cursor,
$cursor + strlen($matches[0])
);
}
}
/**
* now we don't need to support empty(). just use "" (empty string)
*/
class ConstantSubLexer implements SubLexerInterface
{
/**
* @inheritdoc
*/
public function getTokenAt($code, $cursor)
{
static $types = [
'true' => Token::T_TRUE,
'false' => Token::T_FALSE,
'null' => Token::T_NULL,
];
if (!preg_match('/(?:true|false|null)/A', $code, $matches, 0, $cursor)) {
return null;
}
return new Token(
$types[$matches[0]],
$matches[0],
$cursor,
$cursor + strlen($matches[0])
);
}
}
/**
* we will use T_IDENTIFIER as a field name
*/
class IdentifierParser implements SubParserInterface
{
/**
* @inheritdoc
*/
public function parse(TokenStream $tokenStream)
{
return $tokenStream->expect(T_IDENTIFIER)->getValue();
}
}
/**
* get rid of type casters
*/
class ScalarParser implements SubParserInterface
{
/**
* @inheritDoc
*/
public function parse(TokenStream $tokenStream)
{
$token = $tokenStream->getCurrent();
if ($tokenStream->nextIf(Token::T_FALSE)) {
return false;
} elseif ($tokenStream->nextIf(Token::T_TRUE)) {
return true;
} elseif ($tokenStream->nextIf(Token::T_NULL)) {
return null;
} elseif ($tokenStream->nextIf(Token::T_DATE)) {
return new \DateTime($token->getValue());
} elseif ($tokenStream->nextIf(Token::T_STRING)) {
return $token->getValue();
} elseif ($tokenStream->nextIf(Token::T_INTEGER)) {
return (int)$token->getValue();
} elseif ($tokenStream->nextIf(Token::T_FLOAT)) {
return (float)$token->getValue();
}
throw new SyntaxErrorException(
sprintf(
'Invalid scalar token "%s" (%s)',
$token->getValue(),
$token->getName()
)
);
}
}
$subLexer = (new SubLexerChain())
->addSubLexer(new SubLexer\PunctuationSubLexer())
->addSubLexer(new SubLexer\FiqlOperatorSubLexer())
->addSubLexer(new SubLexer\RqlOperatorSubLexer())
->addSubLexer(new ConstantSubLexer())
->addSubLexer(new StringSubLexer())
->addSubLexer(new IdentifierSubLexer())
->addSubLexer(new SubLexer\DatetimeSubLexer())
->addSubLexer(new SubLexer\NumberSubLexer())
->addSubLexer(new SubLexer\SortSubLexer());
$identifierParser = new IdentifierParser();
$scalarParser = new ScalarParser();
$arrayParser = new ValueParser\ArrayParser($scalarParser);
$integerParser = new ValueParser\IntegerParser();
$queryNodeParser = new NodeParser\QueryNodeParser();
$queryNodeParser
->addNodeParser(new NodeParser\Query\GroupNodeParser($queryNodeParser))
->addNodeParser(new LogicalOperator\AndNodeParser($queryNodeParser))
->addNodeParser(new LogicalOperator\OrNodeParser($queryNodeParser))
->addNodeParser(new LogicalOperator\NotNodeParser($queryNodeParser))
->addNodeParser(new ComparisonOperator\Rql\InNodeParser($identifierParser, $arrayParser))
->addNodeParser(new ComparisonOperator\Rql\OutNodeParser($identifierParser, $arrayParser))
->addNodeParser(new ComparisonOperator\Rql\EqNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Rql\NeNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Rql\LtNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Rql\GtNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Rql\LeNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Rql\GeNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Rql\LikeNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Fiql\InNodeParser($identifierParser, $arrayParser))
->addNodeParser(new ComparisonOperator\Fiql\OutNodeParser($identifierParser, $arrayParser))
->addNodeParser(new ComparisonOperator\Fiql\EqNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Fiql\NeNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Fiql\LtNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Fiql\GtNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Fiql\LeNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Fiql\GeNodeParser($identifierParser, $scalarParser))
->addNodeParser(new ComparisonOperator\Fiql\LikeNodeParser($identifierParser, $scalarParser));
$nodeParser = (new NodeParserChain())
->addNodeParser($queryNodeParser)
->addNodeParser(new NodeParser\SelectNodeParser($identifierParser))
->addNodeParser(new NodeParser\SortNodeParser($identifierParser))
->addNodeParser(new NodeParser\LimitNodeParser($integerParser));
// parse
$lexer = new Lexer($subLexer);
$parser = new Parser($nodeParser);
$tokenStream = $lexer->tokenize(implode('&', [
'select(a.b,c.d)',
'ne(a.b,"quoted string !@#$%^&*()_+[]{} ;:\'\"\| ,<.>/? ~ + escaped \" double quote")',
'(not(c.d="d")|e=false)',
'out(f,(1,"2",null,2016-06-29T23:30:33Z,true))',
'sort(-a.b,+c.d)',
'limit(1,2)',
]));
var_dump($parser->parse($tokenStream));