Skip to content

Commit d3d9364

Browse files
committed
Research and debug on #156
1 parent 7db91f8 commit d3d9364

File tree

4 files changed

+110
-12
lines changed

4 files changed

+110
-12
lines changed

src/Components/Expression.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,18 @@ public function __construct($database = null, $table = null, $column = null, $al
143143
*
144144
* If not empty, breaks after last parentheses occurred.
145145
*
146-
* @param Parser $parser the parser that serves as context
147-
* @param TokensList $list the list of tokens that are being parsed
148-
* @param array $options parameters for parsing
146+
* @param Parser $parser the parser that serves as context
147+
* @param TokensList $list the list of tokens that are being parsed
148+
* @param array $options parameters for parsing
149149
*
150150
* @return Expression|null
151+
* @throws \PhpMyAdmin\SqlParser\Exceptions\ParserException
151152
*/
152153
public static function parse(Parser $parser, TokensList $list, array $options = [])
153154
{
155+
// TODO #156 to be removed
156+
echo __METHOD__ . '@' . __LINE__ . ' parsing token list: ' . json_encode($list) . ' with options: ' . json_encode($options) . PHP_EOL;
157+
154158
$ret = new static();
155159

156160
/**
@@ -197,6 +201,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
197201
$options['field'] = $options['parseField'];
198202
}
199203

204+
// TODO #156 to be removed
205+
echo __METHOD__ . '@' . __LINE__ . ' before inside for ret: ' . json_encode($ret) . PHP_EOL;
206+
200207
for (; $list->idx < $list->count; ++$list->idx) {
201208
/**
202209
* Token parsed at this moment.
@@ -205,6 +212,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
205212
*/
206213
$token = $list->tokens[$list->idx];
207214

215+
// TODO #156 to be removed
216+
echo __METHOD__ . '@' . __LINE__ . ' token: ' . json_encode($token) . ' isExpr: ' . json_encode($isExpr) . PHP_EOL;
217+
208218
// End of statement.
209219
if ($token->type === Token::TYPE_DELIMITER) {
210220
break;
@@ -399,6 +409,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
399409
}
400410
}
401411

412+
// TODO #156 to be removed
413+
echo __METHOD__ . '@' . __LINE__ . ' after inside for ret: ' . json_encode($ret) . PHP_EOL;
414+
402415
if ($alias) {
403416
$parser->error(
404417
'An alias was expected.',
@@ -415,6 +428,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
415428

416429
--$list->idx;
417430

431+
// TODO #156 to be removed
432+
echo __METHOD__ . '@' . __LINE__ . ' ret: ' . json_encode($ret) . PHP_EOL;
433+
418434
return $ret;
419435
}
420436

src/Components/ExpressionArray.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717
class ExpressionArray extends Component
1818
{
1919
/**
20-
* @param Parser $parser the parser that serves as context
21-
* @param TokensList $list the list of tokens that are being parsed
22-
* @param array $options parameters for parsing
20+
* @param Parser $parser the parser that serves as context
21+
* @param TokensList $list the list of tokens that are being parsed
22+
* @param array $options parameters for parsing
2323
*
2424
* @return Expression[]
25+
* @throws \PhpMyAdmin\SqlParser\Exceptions\ParserException
2526
*/
2627
public static function parse(Parser $parser, TokensList $list, array $options = [])
2728
{
@@ -41,6 +42,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
4142
*/
4243
$state = 0;
4344

45+
// TODO #156 to be removed
46+
echo __METHOD__ . '@' . __LINE__ . ' for start' . PHP_EOL;
47+
4448
for (; $list->idx < $list->count; ++$list->idx) {
4549
/**
4650
* Token parsed at this moment.
@@ -49,6 +53,14 @@ public static function parse(Parser $parser, TokensList $list, array $options =
4953
*/
5054
$token = $list->tokens[$list->idx];
5155

56+
// TODO #156 to be removed
57+
echo __METHOD__ . '@' . __LINE__ . ' for token: ' . json_encode($token) . PHP_EOL;
58+
// the evidences:
59+
// PhpMyAdmin\SqlParser\Components\ExpressionArray::parse@57 for token:
60+
// {"token":"*","value":"*","keyword":null,"type":2,"flags":1,"position":55}
61+
// PhpMyAdmin\SqlParser\Components\ExpressionArray::parse@57 for token:
62+
// {"token":"from","value":"FROM","keyword":"FROM","type":1,"flags":3,"position":70}
63+
5264
// End of statement.
5365
if ($token->type === Token::TYPE_DELIMITER) {
5466
break;
@@ -77,6 +89,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
7789
$expr = CaseExpression::parse($parser, $list, $options);
7890
} else {
7991
$expr = Expression::parse($parser, $list, $options);
92+
93+
// TODO #156 to be removed
94+
echo __METHOD__ . '@' . __LINE__ . ' state: ' . $state . ' expr: ' . json_encode($expr) . PHP_EOL;
8095
}
8196

8297
if ($expr === null) {
@@ -93,6 +108,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =
93108
}
94109
}
95110

111+
// TODO #156 to be removed
112+
echo __METHOD__ . '@' . __LINE__ . ' for end' . PHP_EOL;
113+
96114
if ($state === 0) {
97115
$parser->error(
98116
'An expression was expected.',
@@ -102,22 +120,48 @@ public static function parse(Parser $parser, TokensList $list, array $options =
102120

103121
--$list->idx;
104122

123+
// TODO #156 Hotfix
124+
if (is_array($ret)) {
125+
$expr = $ret[count($ret) - 1]->expr;
126+
if (preg_match('/\s*--\s.*$/', $expr, $matches)) {
127+
$found = $matches[0];
128+
$ret[count($ret) - 1]->expr = substr($expr, 0, strlen($expr) - strlen($found));
129+
}
130+
}
131+
132+
// TODO #156 to be removed
133+
foreach ($ret as $item) {
134+
echo __METHOD__ . '@' . __LINE__ . ' ret item: ' . json_encode($item) . PHP_EOL;
135+
// eventually the evidence came up:
136+
// $ret[4] as
137+
// PhpMyAdmin\SqlParser\Components\ExpressionArray::parse@114 ret item:
138+
// {"database":null,"table":null,"column":null,"expr":"* -- count(*)","alias":null,"function":null,"subquery":null}
139+
}
140+
105141
return $ret;
106142
}
107143

108144
/**
109-
* @param ExpressionArray[] $component the component to be built
110-
* @param array $options parameters for building
145+
* @param Expression[] $component the component to be built
146+
* @param array $options parameters for building
111147
*
112148
* @return string
113149
*/
114150
public static function build($component, array $options = [])
115151
{
152+
// TODO #156 to be removed
153+
// var_dump($component);
154+
116155
$ret = [];
117156
foreach ($component as $frag) {
157+
// TODO #156 to be removed
158+
//var_dump($frag);
118159
$ret[] = $frag::build($frag);
119160
}
120161

162+
// TODO #156 to be removed
163+
echo __METHOD__ . ' ret[] as ' . json_encode($ret) . PHP_EOL;
164+
121165
return implode(', ', $ret);
122166
}
123167
}

src/Parser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ public function __construct($list = null, $strict = false)
373373

374374
/**
375375
* Builds the parse trees.
376+
* @throws ParserException
376377
*/
377378
public function parse()
378379
{
@@ -486,6 +487,9 @@ public function parse()
486487
*/
487488
$class = static::$STATEMENT_PARSERS[$token->keyword];
488489

490+
// TODO #156 to be removed
491+
echo __METHOD__ . '@' . __LINE__ . ' class=' . $class . ' keyword=' . $token->keyword . PHP_EOL;
492+
489493
/**
490494
* Processed statement.
491495
*

src/Statement.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public function build()
124124
$clauses = $this->getClauses();
125125

126126
foreach ($clauses as $clause) {
127+
// TODO #156 to be removed
128+
echo json_encode($clause) . PHP_EOL;
129+
127130
/**
128131
* The name of the clause.
129132
*
@@ -147,6 +150,9 @@ public function build()
147150
*/
148151
$class = Parser::$KEYWORD_PARSERS[$name]['class'];
149152

153+
// TODO #156 to be removed
154+
echo json_encode([$name, $class]) . PHP_EOL;
155+
150156
/**
151157
* The name of the field that is used as source for the builder.
152158
* Same field is used to store the result of parsing.
@@ -177,6 +183,9 @@ public function build()
177183
if ($type & 1) {
178184
$query = trim($query) . ' ' . $class::build($this->$field);
179185
}
186+
187+
// TODO #156 To be removed
188+
echo json_encode($query) . PHP_EOL;
180189
}
181190

182191
return $query;
@@ -185,8 +194,9 @@ public function build()
185194
/**
186195
* Parses the statements defined by the tokens list.
187196
*
188-
* @param Parser $parser the instance that requests parsing
189-
* @param TokensList $list the list of tokens to be parsed
197+
* @param Parser $parser the instance that requests parsing
198+
* @param TokensList $list the list of tokens to be parsed
199+
* @throws Exceptions\ParserException
190200
*/
191201
public function parse(Parser $parser, TokensList $list)
192202
{
@@ -210,6 +220,9 @@ public function parse(Parser $parser, TokensList $list)
210220
*/
211221
$parsedOptions = empty(static::$OPTIONS);
212222

223+
// TODO #156 to be removed
224+
echo __METHOD__ . '@' . __LINE__ . ' for start' . PHP_EOL;
225+
213226
for (; $list->idx < $list->count; ++$list->idx) {
214227
/**
215228
* Token parsed at this moment.
@@ -218,8 +231,13 @@ public function parse(Parser $parser, TokensList $list)
218231
*/
219232
$token = $list->tokens[$list->idx];
220233

234+
// TODO #156 to be removed
235+
echo __METHOD__ . '@' . __LINE__ . ' token: ' . json_encode($token) . PHP_EOL;
236+
221237
// End of statement.
222238
if ($token->type === Token::TYPE_DELIMITER) {
239+
// TODO #156 to be removed
240+
echo __METHOD__ . '@' . __LINE__ . ' token is delimiter!' . PHP_EOL;
223241
break;
224242
}
225243

@@ -387,6 +405,18 @@ public function parse(Parser $parser, TokensList $list)
387405

388406
$this->before($parser, $list, $token);
389407

408+
// TODO #156 to be removed
409+
echo __METHOD__ . '@' . __LINE__ . ' class: ' . json_encode($class) . PHP_EOL;
410+
{
411+
//var_dump($list);
412+
foreach ($list->tokens as $item) {
413+
echo __METHOD__ . '@' . __LINE__ . ' list item : ' . json_encode($item) . PHP_EOL;
414+
}
415+
416+
// from above shows the last `* -- count(*)` as ["*"," ","-- count(*)"]
417+
// so it must be `class: "PhpMyAdmin\\SqlParser\\Components\\ExpressionArray"`::parse errored!
418+
}
419+
390420
// Parsing this keyword.
391421
if ($class !== null) {
392422
// We can't parse keyword at the end of statement
@@ -408,6 +438,9 @@ public function parse(Parser $parser, TokensList $list)
408438
}
409439
}
410440

441+
// TODO #156 to be removed
442+
echo __METHOD__ . '@' . __LINE__ . ' for over' . PHP_EOL;
443+
411444
// This may be corrected by the parser.
412445
$this->last = --$list->idx; // Go back to last used token.
413446
}
@@ -461,10 +494,11 @@ public function __toString()
461494
* Ideally this should be called after successfully
462495
* completing the parsing of each statement.
463496
*
464-
* @param Parser $parser the instance that requests parsing
465-
* @param TokensList $list the list of tokens to be parsed
497+
* @param Parser $parser the instance that requests parsing
498+
* @param TokensList $list the list of tokens to be parsed
466499
*
467500
* @return bool
501+
* @throws Exceptions\ParserException
468502
*/
469503
public function validateClauseOrder($parser, $list)
470504
{

0 commit comments

Comments
 (0)