-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathToken.php
More file actions
128 lines (102 loc) · 3.12 KB
/
Token.php
File metadata and controls
128 lines (102 loc) · 3.12 KB
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
<?php
declare(strict_types=1);
namespace Doctrine\SqlFormatter;
use function in_array;
final class Token
{
// Constants for token types
public const TOKEN_TYPE_WHITESPACE = 0;
public const TOKEN_TYPE_WORD = 1;
public const TOKEN_TYPE_QUOTE = 2;
public const TOKEN_TYPE_BACKTICK_QUOTE = 3;
public const TOKEN_TYPE_RESERVED = 4;
public const TOKEN_TYPE_RESERVED_TOPLEVEL = 5;
public const TOKEN_TYPE_RESERVED_NEWLINE = 6;
public const TOKEN_TYPE_BOUNDARY = 7;
public const TOKEN_TYPE_COMMENT = 8;
public const TOKEN_TYPE_BLOCK_COMMENT = 9;
public const TOKEN_TYPE_NUMBER = 10;
public const TOKEN_TYPE_ERROR = 11;
public const TOKEN_TYPE_VARIABLE = 12;
// Constants for different components of a token
public const TOKEN_TYPE = 0;
public const TOKEN_VALUE = 1;
/** @var int */
private $type;
/** @var string */
private $value;
public function __construct(int $type, string $value)
{
$this->type = $type;
$this->value = $value;
}
public function value(): string
{
return $this->value;
}
public function type(): int
{
return $this->type;
}
public function isOfType(int ...$types): bool
{
return in_array($this->type, $types, true);
}
public function withValue(string $value): self
{
return new self($this->type(), $value);
}
public function isBlockStart(): ?Condition
{
$condition = new Condition();
if ($this->value === '(') {
$condition->values = [')'];
$condition->addNewline = true;
return $condition;
}
if ($this->value === 'CASE WHEN' || $this->value === 'ELSE') {
$condition->values = ['ELSE', 'END'];
return $condition;
}
if ($this->value === 'CASE') {
$condition->values = ['END'];
return $condition;
}
$joins = [
'LEFT OUTER JOIN',
'RIGHT OUTER JOIN',
'LEFT JOIN',
'RIGHT JOIN',
'OUTER JOIN',
'INNER JOIN',
'CROSS JOIN',
'JOIN',
];
if (in_array($this->value, $joins, true)) {
$condition->values = $joins;
$condition->types = [self::TOKEN_TYPE_RESERVED_TOPLEVEL];
$condition->eof = true;
return $condition;
}
return null;
}
public function isBlockEnd(Condition $condition): bool
{
if ($this->isOfType(...$condition->types)) {
return true;
}
return in_array($this->value, $condition->values, true);
}
public function wantsSpaceBefore(): bool
{
if (in_array($this->value, ['.', ',', ';', ')'], true)) {
return false;
}
return ! $this->isOfType(
self::TOKEN_TYPE_RESERVED_NEWLINE,
self::TOKEN_TYPE_RESERVED_TOPLEVEL,
self::TOKEN_TYPE_COMMENT,
self::TOKEN_TYPE_BLOCK_COMMENT
);
}
}