Skip to content

Commit

Permalink
Introduce the TokenType enum
Browse files Browse the repository at this point in the history
Used to replace the Token::TYPE_* constants.

The TokenType::cases() method replaces the Token::TYPE_ALL constant.

Adds the Token::FLAG_NONE constant with the same value as Token::TYPE_NONE.

Signed-off-by: Maurício Meneghini Fauth <[email protected]>
  • Loading branch information
MauricioFauth committed Sep 9, 2023
1 parent b737500 commit be8d997
Show file tree
Hide file tree
Showing 480 changed files with 64,468 additions and 21,099 deletions.
2 changes: 1 addition & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1355,7 +1355,7 @@
<code>$i</code>
</PossiblyNullOperand>
<PossiblyNullPropertyFetch>
<code><![CDATA[$lexer->list->getNextOfType(Token::TYPE_KEYWORD)->keyword]]></code>
<code><![CDATA[$lexer->list->getNextOfType(TokenType::Keyword)->keyword]]></code>
<code><![CDATA[$statement->into->dest]]></code>
</PossiblyNullPropertyFetch>
<PossiblyNullReference>
Expand Down
23 changes: 12 additions & 11 deletions src/Components/AlterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

use function array_key_exists;
use function in_array;
Expand Down Expand Up @@ -343,17 +344,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$token = $list->tokens[$list->idx];

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping comments.
if ($token->type === Token::TYPE_COMMENT) {
if ($token->type === TokenType::Comment) {
continue;
}

// Skipping whitespaces.
if ($token->type === Token::TYPE_WHITESPACE) {
if ($token->type === TokenType::Whitespace) {
if ($state === 2) {
// When parsing the unknown part, the whitespaces are
// included to not break anything.
Expand All @@ -369,7 +370,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
// body in the unknown tokens list, as they define their own statements.
if ($ret->options->has('AS') || $ret->options->has('DO')) {
for (; $list->idx < $list->count; ++$list->idx) {
if ($list->tokens[$list->idx]->type === Token::TYPE_DELIMITER) {
if ($list->tokens[$list->idx]->type === TokenType::Delimiter) {
break;
}

Expand Down Expand Up @@ -414,7 +415,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$arrayKey = $token->token;
}

if ($token->type === Token::TYPE_OPERATOR) {
if ($token->type === TokenType::Operator) {
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
Expand All @@ -430,7 +431,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$nextToken = $list->getNext();

if ($nextToken !== null && $nextToken->value === '(') {
$list->getNextOfTypeAndValue(Token::TYPE_OPERATOR, ')');
$list->getNextOfTypeAndValue(TokenType::Operator, ')');
} elseif ($nextToken !== null && $nextToken->value === 'DEFAULT') {
// to avoid adding the `DEFAULT` token to the unknown tokens.
++$list->idx;
Expand Down Expand Up @@ -467,12 +468,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$list->idx++; // Ignore the current token
$nextToken = $list->getNext();
if (
($token->type === Token::TYPE_KEYWORD)
($token->type === TokenType::Keyword)
&& (($token->keyword === 'PARTITION BY')
|| ($token->keyword === 'PARTITION' && $nextToken && $nextToken->value !== '('))
) {
$partitionState = 1;
} elseif (($token->type === Token::TYPE_KEYWORD) && ($token->keyword === 'PARTITION')) {
} elseif (($token->type === TokenType::Keyword) && ($token->keyword === 'PARTITION')) {
$partitionState = 2;
}

Expand All @@ -495,15 +496,15 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

if (
$token->type === Token::TYPE_OPERATOR
$token->type === TokenType::Operator
&& $token->value === '('
&& $nextToken
&& $nextToken->keyword === 'PARTITION'
) {
$partitionState = 2;
--$list->idx; // Current idx is on "(". We need a step back for ArrayObj::parse incoming.
} else {
$ret->field .= $token->type === Token::TYPE_WHITESPACE ? ' ' : $token->token;
$ret->field .= $token->type === TokenType::Whitespace ? ' ' : $token->token;
}
} elseif ($partitionState === 2) {
$ret->partitions = ArrayObj::parse(
Expand Down Expand Up @@ -584,7 +585,7 @@ private static function checkIfColumnDefinitionKeyword($tokenValue): bool
*/
private static function checkIfTokenQuotedSymbol($token): bool
{
return $token->type === Token::TYPE_SYMBOL && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
return $token->type === TokenType::Symbol && $token->flags === Token::FLAG_SYMBOL_BACKTICK;
}

public function __toString(): string
Expand Down
7 changes: 4 additions & 3 deletions src/Components/Array2d.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
use PhpMyAdmin\SqlParser\Translator;
use RuntimeException;

Expand Down Expand Up @@ -58,17 +59,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$token = $list->tokens[$list->idx];

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
continue;
}

// No keyword is expected.
if (($token->type === Token::TYPE_KEYWORD) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
if (($token->type === TokenType::Keyword) && ($token->flags & Token::FLAG_KEYWORD_RESERVED)) {
break;
}

Expand Down
10 changes: 5 additions & 5 deletions src/Components/ArrayObj.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use PhpMyAdmin\SqlParser\Component;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

use function implode;
use function strlen;
Expand Down Expand Up @@ -88,23 +88,23 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$token = $list->tokens[$list->idx];

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
$lastRaw .= $token->token;
$lastValue = trim($lastValue) . ' ';
continue;
}

if (($brackets === 0) && (($token->type !== Token::TYPE_OPERATOR) || ($token->value !== '('))) {
if (($brackets === 0) && (($token->type !== TokenType::Operator) || ($token->value !== '('))) {
$parser->error('An opening bracket was expected.', $token);
break;
}

if ($token->type === Token::TYPE_OPERATOR) {
if ($token->type === TokenType::Operator) {
if ($token->value === '(') {
if (++$brackets === 1) { // 1 is the base level.
continue;
Expand Down
29 changes: 15 additions & 14 deletions src/Components/CaseExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

use function count;

Expand Down Expand Up @@ -98,12 +99,12 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$token = $list->tokens[$list->idx];

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
continue;
}

if ($state === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->type === TokenType::Keyword) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
Expand Down Expand Up @@ -132,7 +133,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}
} elseif ($state === 1) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD) {
if ($token->type === TokenType::Keyword) {
switch ($token->keyword) {
case 'WHEN':
++$list->idx; // Skip 'WHEN'
Expand All @@ -154,23 +155,23 @@ public static function parse(Parser $parser, TokensList $list, array $options =
break 2;
}
}
} elseif ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
} elseif ($token->type === TokenType::Keyword && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$state = 0;
$ret->results[] = $newResult;
} elseif ($token->type === Token::TYPE_KEYWORD) {
} elseif ($token->type === TokenType::Keyword) {
$parser->error('Unexpected keyword.', $token);
break;
}
} elseif ($state === 2) {
if ($type === 0) {
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'THEN') {
if ($token->type === TokenType::Keyword && $token->keyword === 'THEN') {
++$list->idx; // Skip 'THEN'
$newResult = Expression::parse($parser, $list);
$ret->results[] = $newResult;
$state = 1;
} elseif ($token->type === Token::TYPE_KEYWORD) {
} elseif ($token->type === TokenType::Keyword) {
$parser->error('Unexpected keyword.', $token);
break;
}
Expand All @@ -187,17 +188,17 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$token = $list->tokens[$list->idx];

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping whitespaces and comments.
if (($token->type === Token::TYPE_WHITESPACE) || ($token->type === Token::TYPE_COMMENT)) {
if (($token->type === TokenType::Whitespace) || ($token->type === TokenType::Comment)) {
continue;
}

// Handle optional AS keyword before alias
if ($token->type === Token::TYPE_KEYWORD && $token->keyword === 'AS') {
if ($token->type === TokenType::Keyword && $token->keyword === 'AS') {
if ($asFound || ! empty($ret->alias)) {
$parser->error('Potential duplicate alias of CASE expression.', $token);
break;
Expand All @@ -209,7 +210,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =

if (
$asFound
&& $token->type === Token::TYPE_KEYWORD
&& $token->type === TokenType::Keyword
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED || $token->flags & Token::FLAG_KEYWORD_FUNCTION)
) {
$parser->error('An alias expected after AS but got ' . $token->value, $token);
Expand All @@ -219,9 +220,9 @@ public static function parse(Parser $parser, TokensList $list, array $options =

if (
$asFound
|| $token->type === Token::TYPE_STRING
|| ($token->type === Token::TYPE_SYMBOL && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
|| $token->type === Token::TYPE_NONE
|| $token->type === TokenType::String
|| ($token->type === TokenType::Symbol && ! $token->flags & Token::FLAG_SYMBOL_VARIABLE)
|| $token->type === TokenType::None
) {
// An alias is expected (the keyword `AS` was previously found).
if (! empty($ret->alias)) {
Expand Down
19 changes: 10 additions & 9 deletions src/Components/Condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;

use function implode;
use function in_array;
Expand Down Expand Up @@ -126,18 +127,18 @@ public static function parse(Parser $parser, TokensList $list, array $options =
$token = $list->tokens[$list->idx];

// End of statement.
if ($token->type === Token::TYPE_DELIMITER) {
if ($token->type === TokenType::Delimiter) {
break;
}

// Skipping whitespaces and comments.
if ($token->type === Token::TYPE_COMMENT) {
if ($token->type === TokenType::Comment) {
continue;
}

// Replacing all whitespaces (new lines, tabs, etc.) with a single
// space character.
if ($token->type === Token::TYPE_WHITESPACE) {
if ($token->type === TokenType::Whitespace) {
$expr->expr .= ' ';
continue;
}
Expand Down Expand Up @@ -166,7 +167,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}

if (
($token->type === Token::TYPE_KEYWORD)
($token->type === TokenType::Keyword)
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED)
&& ! ($token->flags & Token::FLAG_KEYWORD_FUNCTION)
) {
Expand All @@ -179,7 +180,7 @@ public static function parse(Parser $parser, TokensList $list, array $options =
}
}

if ($token->type === Token::TYPE_OPERATOR) {
if ($token->type === TokenType::Operator) {
if ($token->value === '(') {
++$brackets;
} elseif ($token->value === ')') {
Expand All @@ -193,11 +194,11 @@ public static function parse(Parser $parser, TokensList $list, array $options =

$expr->expr .= $token->token;
if (
($token->type !== Token::TYPE_NONE)
&& (($token->type !== Token::TYPE_KEYWORD)
($token->type !== TokenType::None)
&& (($token->type !== TokenType::Keyword)
|| ($token->flags & Token::FLAG_KEYWORD_RESERVED))
&& ($token->type !== Token::TYPE_STRING)
&& ($token->type !== Token::TYPE_SYMBOL)
&& ($token->type !== TokenType::String)
&& ($token->type !== TokenType::Symbol)
) {
continue;
}
Expand Down
Loading

0 comments on commit be8d997

Please sign in to comment.