1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace GraphQL\Language; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Represents a range of characters represented by a lexical token |
9
|
|
|
* within a Source. |
10
|
|
|
*/ |
11
|
|
|
class Token |
12
|
|
|
{ |
13
|
|
|
// Each kind of token. |
14
|
|
|
public const SOF = '<SOF>'; |
15
|
|
|
public const EOF = '<EOF>'; |
16
|
|
|
public const BANG = '!'; |
17
|
|
|
public const DOLLAR = '$'; |
18
|
|
|
public const AMP = '&'; |
19
|
|
|
public const PAREN_L = '('; |
20
|
|
|
public const PAREN_R = ')'; |
21
|
|
|
public const SPREAD = '...'; |
22
|
|
|
public const COLON = ':'; |
23
|
|
|
public const EQUALS = '='; |
24
|
|
|
public const AT = '@'; |
25
|
|
|
public const BRACKET_L = '['; |
26
|
|
|
public const BRACKET_R = ']'; |
27
|
|
|
public const BRACE_L = '{'; |
28
|
|
|
public const PIPE = '|'; |
29
|
|
|
public const BRACE_R = '}'; |
30
|
|
|
public const NAME = 'Name'; |
31
|
|
|
public const INT = 'Int'; |
32
|
|
|
public const FLOAT = 'Float'; |
33
|
|
|
public const STRING = 'String'; |
34
|
|
|
public const BLOCK_STRING = 'BlockString'; |
35
|
|
|
public const COMMENT = 'Comment'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The kind of Token (see one of constants above). |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $kind; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* The character offset at which this Node begins. |
46
|
|
|
* |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
public $start; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* The character offset at which this Node ends. |
53
|
|
|
* |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
public $end; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* The 1-indexed line number on which this Token appears. |
60
|
|
|
* |
61
|
|
|
* @var int |
62
|
|
|
*/ |
63
|
|
|
public $line; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* The 1-indexed column number at which this Token begins. |
67
|
|
|
* |
68
|
|
|
* @var int |
69
|
|
|
*/ |
70
|
|
|
public $column; |
71
|
|
|
|
72
|
|
|
/** @var string|null */ |
73
|
|
|
public $value; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tokens exist as nodes in a double-linked-list amongst all tokens |
77
|
|
|
* including ignored tokens. <SOF> is always the first node and <EOF> |
78
|
|
|
* the last. |
79
|
|
|
* |
80
|
|
|
* @var Token |
81
|
|
|
*/ |
82
|
|
|
public $prev; |
83
|
|
|
|
84
|
|
|
/** @var Token */ |
85
|
|
|
public $next; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $kind |
89
|
|
|
* @param int $start |
90
|
|
|
* @param int $end |
91
|
|
|
* @param int $line |
92
|
|
|
* @param int $column |
93
|
|
|
* @param mixed|null $value |
94
|
|
|
*/ |
95
|
988 |
|
public function __construct($kind, $start, $end, $line, $column, ?Token $previous = null, $value = null) |
96
|
|
|
{ |
97
|
988 |
|
$this->kind = $kind; |
98
|
988 |
|
$this->start = $start; |
99
|
988 |
|
$this->end = $end; |
100
|
988 |
|
$this->line = $line; |
101
|
988 |
|
$this->column = $column; |
102
|
988 |
|
$this->prev = $previous; |
103
|
988 |
|
$this->next = null; |
104
|
988 |
|
$this->value = $value; |
105
|
988 |
|
} |
106
|
|
|
|
107
|
23 |
|
public function getDescription() : string |
108
|
|
|
{ |
109
|
23 |
|
return $this->kind . ($this->value === null ? '' : ' "' . $this->value . '"'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return (string|int|null)[] |
114
|
|
|
*/ |
115
|
1 |
|
public function toArray() : array |
116
|
|
|
{ |
117
|
|
|
return [ |
118
|
1 |
|
'kind' => $this->kind, |
119
|
1 |
|
'value' => $this->value, |
120
|
1 |
|
'line' => $this->line, |
121
|
1 |
|
'column' => $this->column, |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|