This repository was archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathToken.hack
189 lines (166 loc) · 4.91 KB
/
Token.hack
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HHAST;
use namespace Facebook\TypeAssert;
use namespace HH\Lib\{C, Str, Vec};
abstract class Token extends Node {
private string $_token_kind;
private NodeList<Trivia> $_leading;
private NodeList<Trivia> $_trailing;
private string $_text;
const string SYNTAX_KIND = 'token';
public function __construct(
string $token_kind,
?NodeList<Trivia> $leading,
?NodeList<Trivia> $trailing,
string $text,
?__Private\SourceRef $ref,
) {
$this->_token_kind = $token_kind;
$this->_leading = $leading ?? new NodeList(vec[]);
$this->_trailing = $trailing ?? new NodeList(vec[]);
$this->_text = $text;
$this->_width = Str\length($text) +
$this->_leading->getWidth() +
$this->_trailing->getWidth();
parent::__construct($ref);
}
public function getTokenKind(): string {
return $this->_token_kind;
}
final public function getText(): string {
return $this->_text;
}
final public function hasLeading(): bool {
return $this->_leading->isEmpty();
}
final public function hasTrailing(): bool {
return $this->_trailing->isEmpty();
}
public function getLeading(): NodeList<Trivia> {
return $this->_leading;
}
final public function getLeadingWhitespace(): ?Trivia {
$leading = $this->getLeading();
$last = null;
foreach ($leading->getChildren() as $child) {
if ($child is WhiteSpace || $child is EndOfLine) {
$last = $child;
}
}
return $last;
}
final public function getTrailingWhitespace(): NodeList<Trivia> {
$trailing = $this->getTrailing();
$result = vec[];
foreach ($trailing->getChildren() as $child) {
if ($child is WhiteSpace) {
$result[] = $child;
} else if ($child is EndOfLine) {
$result[] = $child;
break;
}
}
return NodeList::createMaybeEmptyList($result);
}
public function getTrailing(): NodeList<Trivia> {
return $this->_trailing;
}
<<__Override>>
public function getChildren(): dict<string, NodeList<Trivia>> {
return dict[
'leading' => $this->getLeading(),
'trailing' => $this->getTrailing(),
];
}
<<__Override>>
final public function isToken(): bool {
return true;
}
<<__Override>>
protected function getCodeUncached(): string {
return $this->getLeading()->getCode().
$this->getText().
$this->getTrailing()->getCode();
}
public abstract function withLeading(?NodeList<Trivia> $leading): this;
public abstract function withTrailing(?NodeList<Trivia> $trailing): this;
<<__Override>>
final public static function fromJSON(
dict<arraykey, mixed> $json,
string $file,
int $offset,
string $source,
string $_type_hint,
): Token {
$leading_list = __Private\fold_map<dict<string, mixed>, Trivia, int>(
/* HH_FIXME[4110] need like types */ $json['leading'],
($j, $p) ==> Trivia::fromJSON($j, $file, $p, $source, 'Node'),
($j, $p) ==> $j['width'] as int + $p,
$offset,
)
|> Vec\filter_nulls($$);
$leading = C\is_empty($leading_list)
? new NodeList(vec[])
: new NodeList(
$leading_list,
shape(
'file' => $file,
'source' => $source,
'offset' => $offset,
'width' => $json['leading_width'] as int,
),
);
$token_position = $offset + $json['leading_width'] as int;
$token_width = TypeAssert\int($json['width']);
$token_text = Str\slice($source, $token_position, $token_width);
$trailing_position = $token_position + $token_width;
$trailing_list = __Private\fold_map<dict<string, mixed>, Trivia, int>(
/* HH_FIXME[4110] need like-types */ $json['trailing'],
($j, $p) ==> Trivia::fromJSON($j, $file, $p, $source, 'Node'),
($j, $p) ==> $j['width'] as int + $p,
$trailing_position,
)
|> Vec\filter_nulls($$);
$trailing = C\is_empty($trailing_list)
? new NodeList(vec[])
: new NodeList(
$trailing_list,
shape(
'file' => $file,
'source' => $source,
'offset' => $trailing_position,
'width' => $json['trailing_width'] as int,
),
);
$width = $json['leading_width'] as int +
$json['width'] as int +
$json['trailing_width'] as int;
return __Private\token_from_data(
shape(
'file' => $file,
'source' => $source,
'offset' => $offset,
'width' => $width,
),
$json['kind'] as string,
$leading,
$trailing,
$token_text,
);
}
<<__Override>>
final public function getFirstToken(): Token {
return $this;
}
<<__Override>>
final public function getLastToken(): Token {
return $this;
}
}