Skip to content

Commit 93a7c2a

Browse files
committed
Fixes #9.
Improved error detection for FieldDefinition and TransactionStatement. When two statements of the same type weren't properly terminated only one was parsed. Fixed a issue where two consecutive SELECT statements where considered a UNION.
1 parent d855fb9 commit 93a7c2a

3 files changed

Lines changed: 36 additions & 3 deletions

File tree

src/Components/FieldDefinition.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,13 @@ public static function parse(Parser $parser, TokensList $list, array $options =
241241
$state = 6;
242242
++$list->idx;
243243
break;
244+
} else {
245+
$parser->error(
246+
__('A comma or a closing bracket was expected.'),
247+
$token
248+
);
249+
$state = 0;
250+
break;
244251
}
245252
}
246253
}

src/Parser.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,6 @@ public function parse()
321321

322322
/**
323323
* Last transaction.
324-
*
325324
* @var TransactionStatement
326325
*/
327326
$lastTransaction = null;
@@ -336,7 +335,7 @@ public function parse()
336335
* Whether a union is parsed or not.
337336
* @var bool $inUnion
338337
*/
339-
$inUnion = true;
338+
$inUnion = false;
340339

341340
/**
342341
* The index of the last token from the last statement.
@@ -436,7 +435,14 @@ public function parse()
436435
$lastTransaction = $statement;
437436
$this->statements[] = $statement;
438437
} elseif ($statement->type === TransactionStatement::TYPE_END) {
439-
$lastTransaction->end = $statement;
438+
if ($lastTransaction === null) {
439+
$this->error(
440+
__('No transaction was previously started.'),
441+
$token
442+
);
443+
} else {
444+
$lastTransaction->end = $statement;
445+
}
440446
$lastTransaction = null;
441447
}
442448
continue;

src/Statement.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,16 @@ public function build()
169169
*/
170170
public function parse(Parser $parser, TokensList $list)
171171
{
172+
/**
173+
* Whether the beginning of this statement was previously parsed.
174+
*
175+
* This is used to delimit statements that don't use the usual
176+
* delimiters.
177+
*
178+
* @var bool
179+
*/
180+
$parsedBeginning = false;
181+
172182
// This may be corrected by the parser.
173183
$this->first = $list->idx;
174184

@@ -236,6 +246,16 @@ public function parse(Parser $parser, TokensList $list)
236246
}
237247

238248
if (!empty(Parser::$STATEMENT_PARSERS[$token->value])) {
249+
if ($parsedBeginning) {
250+
// New statement has started. We let the parser construct a
251+
// new statement and parse that one
252+
$parser->error(
253+
__('A new statement was found, but no delimiter between them.'),
254+
$token
255+
);
256+
break;
257+
}
258+
$parsedBeginning = true;
239259
if (!$parsedOptions) {
240260
++$list->idx; // Skipping keyword.
241261
$this->options = OptionsArray::parse(

0 commit comments

Comments
 (0)