forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract.cpp
442 lines (405 loc) · 14.1 KB
/
extract.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include <tuple>
#include <typeinfo>
#include <utility>
#include "common/error.h"
#include "common/struct_reflection.h"
#include "toolchain/parse/tree.h"
#include "toolchain/parse/tree_and_subtrees.h"
#include "toolchain/parse/typed_nodes.h"
namespace Carbon::Parse {
namespace {
// Implementation of the process of extracting a typed node structure from the
// parse tree. The extraction process uses the class `Extractable<T>`, defined
// below, to extract individual fields of type `T`.
class NodeExtractor {
public:
struct CheckpointState {
TreeAndSubtrees::SiblingIterator it;
};
NodeExtractor(const TreeAndSubtrees* tree, const Lex::TokenizedBuffer* tokens,
ErrorBuilder* trace, NodeId node_id,
llvm::iterator_range<TreeAndSubtrees::SiblingIterator> children)
: tree_(tree),
tokens_(tokens),
trace_(trace),
node_id_(node_id),
it_(children.begin()),
end_(children.end()) {}
auto at_end() const -> bool { return it_ == end_; }
auto kind() const -> NodeKind { return tree_->tree().node_kind(*it_); }
auto has_token() const -> bool { return node_id_.is_valid(); }
auto token() const -> Lex::TokenIndex {
return tree_->tree().node_token(node_id_);
}
auto token_kind() const -> Lex::TokenKind {
return tokens_->GetKind(token());
}
auto trace() const -> ErrorBuilder* { return trace_; }
// Saves a checkpoint of our current position so we can return later if
// extraction of a child node fails.
auto Checkpoint() const -> CheckpointState { return {.it = it_}; }
auto RestoreCheckpoint(CheckpointState checkpoint) { it_ = checkpoint.it; }
// Determines whether the current position matches the specified node kind. If
// not, produces a suitable trace message.
auto MatchesNodeIdForKind(NodeKind kind) const -> bool;
// Determines whether the current position matches the specified node
// category. If not, produces a suitable trace message.
auto MatchesNodeIdInCategory(NodeCategory category) const -> bool;
// Determines whether the current position matches any of the specified node
// kinds. If not, produces a suitable trace message.
auto MatchesNodeIdOneOf(std::initializer_list<NodeKind> kinds) const -> bool;
// Determines whether the token corresponding to the enclosing node is of the
// specified kind. If not, produces a suitable trace message.
auto MatchesTokenKind(Lex::TokenKind expected_kind) const -> bool;
// Extracts the next node from the tree.
auto ExtractNode() -> NodeId { return *it_++; }
// Extracts a tuple-like type `T` by extracting its components and then
// assembling a `T` value.
template <typename T, typename... U, std::size_t... Index>
auto ExtractTupleLikeType(std::index_sequence<Index...> /*indices*/,
std::tuple<U...>* /*type*/) -> std::optional<T>;
private:
const TreeAndSubtrees* tree_;
const Lex::TokenizedBuffer* tokens_;
ErrorBuilder* trace_;
NodeId node_id_;
TreeAndSubtrees::SiblingIterator it_;
TreeAndSubtrees::SiblingIterator end_;
};
} // namespace
namespace {
// A trait type that should be specialized by types that can be extracted
// from a parse tree. A specialization should provide the following API:
//
// ```cpp
// template<>
// struct Extractable<T> {
// // Extract a value of this type from the sequence of nodes starting at
// // `it`, and increment `it` past this type. Returns `std::nullopt` if
// // the tree is malformed. If `trace != nullptr`, writes what actions
// // were taken to `*trace`.
// static auto Extract(NodeExtractor* extractor) -> std::optional<T>;
// };
// ```
//
// Note that `TreeAndSubtrees::SiblingIterator`s iterate in reverse order
// through the children of a node.
//
// This class is only in this file.
template <typename T>
struct Extractable;
} // namespace
// Extract a `NodeId` as a single child.
template <>
struct Extractable<NodeId> {
static auto Extract(NodeExtractor& extractor) -> std::optional<NodeId> {
if (extractor.at_end()) {
if (auto* trace = extractor.trace()) {
*trace << "NodeId error: no more children\n";
}
return std::nullopt;
}
if (auto* trace = extractor.trace()) {
*trace << "NodeId: " << extractor.kind() << " consumed\n";
}
return extractor.ExtractNode();
}
};
auto NodeExtractor::MatchesNodeIdForKind(NodeKind expected_kind) const -> bool {
if (at_end() || kind() != expected_kind) {
if (trace_) {
if (at_end()) {
*trace_ << "NodeIdForKind error: no more children, expected "
<< expected_kind << "\n";
} else {
*trace_ << "NodeIdForKind error: wrong kind " << kind() << ", expected "
<< expected_kind << "\n";
}
}
return false;
}
if (trace_) {
*trace_ << "NodeIdForKind: " << expected_kind << " consumed\n";
}
return true;
}
// Extract a `FooId`, which is the same as `NodeIdForKind<NodeKind::Foo>`,
// as a single required child.
template <const NodeKind& Kind>
struct Extractable<NodeIdForKind<Kind>> {
static auto Extract(NodeExtractor& extractor)
-> std::optional<NodeIdForKind<Kind>> {
if (extractor.MatchesNodeIdForKind(Kind)) {
return NodeIdForKind<Kind>(extractor.ExtractNode());
} else {
return std::nullopt;
}
}
};
auto NodeExtractor::MatchesNodeIdInCategory(NodeCategory category) const
-> bool {
if (at_end() || !kind().category().HasAnyOf(category)) {
if (trace_) {
*trace_ << "NodeIdInCategory " << category << " error: ";
if (at_end()) {
*trace_ << "no more children\n";
} else {
*trace_ << "kind " << kind() << " doesn't match\n";
}
}
return false;
}
if (trace_) {
*trace_ << "NodeIdInCategory " << category << ": kind " << kind()
<< " consumed\n";
}
return true;
}
// Extract a `NodeIdInCategory<Category>` as a single child.
template <NodeCategory::RawEnumType Category>
struct Extractable<NodeIdInCategory<Category>> {
static auto Extract(NodeExtractor& extractor)
-> std::optional<NodeIdInCategory<Category>> {
if (extractor.MatchesNodeIdInCategory(Category)) {
return NodeIdInCategory<Category>(extractor.ExtractNode());
} else {
return std::nullopt;
}
}
};
auto NodeExtractor::MatchesNodeIdOneOf(
std::initializer_list<NodeKind> kinds) const -> bool {
auto trace_kinds = [&] {
llvm::ListSeparator sep(" or ");
for (auto kind : kinds) {
*trace_ << sep << kind;
}
};
auto node_kind = kind();
if (at_end() ||
std::find(kinds.begin(), kinds.end(), node_kind) == kinds.end()) {
if (trace_) {
if (at_end()) {
*trace_ << "NodeIdOneOf error: no more children, expected ";
trace_kinds();
*trace_ << "\n";
} else {
*trace_ << "NodeIdOneOf error: wrong kind " << node_kind
<< ", expected ";
trace_kinds();
*trace_ << "\n";
}
}
return false;
}
if (trace_) {
*trace_ << "NodeIdOneOf ";
trace_kinds();
*trace_ << ": " << node_kind << " consumed\n";
}
return true;
}
// Extract a `NodeIdOneOf<T...>` as a single required child.
template <typename... T>
struct Extractable<NodeIdOneOf<T...>> {
static auto Extract(NodeExtractor& extractor)
-> std::optional<NodeIdOneOf<T...>> {
if (extractor.MatchesNodeIdOneOf({T::Kind...})) {
return NodeIdOneOf<T...>(extractor.ExtractNode());
} else {
return std::nullopt;
}
}
};
// Extract a `NodeIdNot<T>` as a single required child.
// Note: this is only instantiated once, so no need to create a helper function.
template <typename T>
struct Extractable<NodeIdNot<T>> {
static auto Extract(NodeExtractor& extractor) -> std::optional<NodeIdNot<T>> {
if (extractor.at_end() || extractor.kind() == T::Kind) {
if (auto* trace = extractor.trace()) {
if (extractor.at_end()) {
*trace << "NodeIdNot " << T::Kind << " error: no more children\n";
} else {
*trace << "NodeIdNot error: unexpected " << T::Kind << "\n";
}
}
return std::nullopt;
}
if (auto* trace = extractor.trace()) {
*trace << "NodeIdNot " << T::Kind << ": " << extractor.kind()
<< " consumed\n";
}
return NodeIdNot<T>(extractor.ExtractNode());
}
};
// Extract an `llvm::SmallVector<T>` by extracting `T`s until we can't.
template <typename T>
struct Extractable<llvm::SmallVector<T>> {
static auto Extract(NodeExtractor& extractor)
-> std::optional<llvm::SmallVector<T>> {
if (auto* trace = extractor.trace()) {
*trace << "Vector: begin\n";
}
llvm::SmallVector<T> result;
while (!extractor.at_end()) {
auto checkpoint = extractor.Checkpoint();
auto item = Extractable<T>::Extract(extractor);
if (!item.has_value()) {
extractor.RestoreCheckpoint(checkpoint);
break;
}
result.push_back(*item);
}
std::reverse(result.begin(), result.end());
if (auto* trace = extractor.trace()) {
*trace << "Vector: end\n";
}
return result;
}
};
// Extract an `optional<T>` from a list of child nodes by attempting to extract
// a `T`, and extracting nothing if that fails.
template <typename T>
struct Extractable<std::optional<T>> {
static auto Extract(NodeExtractor& extractor)
-> std::optional<std::optional<T>> {
if (auto* trace = extractor.trace()) {
*trace << "Optional " << typeid(T).name() << ": begin\n";
}
auto checkpoint = extractor.Checkpoint();
std::optional<T> value = Extractable<T>::Extract(extractor);
if (value) {
if (auto* trace = extractor.trace()) {
*trace << "Optional " << typeid(T).name() << ": found\n";
}
return value;
}
if (auto* trace = extractor.trace()) {
*trace << "Optional " << typeid(T).name() << ": missing\n";
}
extractor.RestoreCheckpoint(checkpoint);
return value;
}
};
auto NodeExtractor::MatchesTokenKind(Lex::TokenKind expected_kind) const
-> bool {
if (!node_id_.is_valid()) {
if (trace_) {
*trace_ << "Token " << expected_kind
<< " expected but processing root node\n";
}
return false;
}
if (token_kind() != expected_kind) {
if (trace_) {
*trace_ << "Token " << expected_kind << " expected for "
<< tree_->tree().node_kind(node_id_) << ", found " << token_kind()
<< "\n";
}
return false;
}
return true;
}
// Extract the token corresponding to a node.
template <const Lex::TokenKind& Kind>
struct Extractable<Lex::TokenIndexForKind<Kind>> {
static auto Extract(NodeExtractor& extractor)
-> std::optional<Lex::TokenIndexForKind<Kind>> {
if (extractor.MatchesTokenKind(Kind)) {
return static_cast<Lex::TokenIndexForKind<Kind>>(extractor.token());
} else {
return std::nullopt;
}
}
};
// Extract the token corresponding to a node.
template <>
struct Extractable<Lex::TokenIndex> {
static auto Extract(NodeExtractor& extractor)
-> std::optional<Lex::TokenIndex> {
if (!extractor.has_token()) {
if (auto* trace = extractor.trace()) {
*trace << "Token expected but processing root node\n";
}
return std::nullopt;
}
return extractor.token();
}
};
template <typename T, typename... U, std::size_t... Index>
auto NodeExtractor::ExtractTupleLikeType(
std::index_sequence<Index...> /*indices*/, std::tuple<U...>* /*type*/)
-> std::optional<T> {
std::tuple<std::optional<U>...> fields;
if (trace_) {
*trace_ << "Aggregate " << typeid(T).name() << ": begin\n";
}
// Use a fold over the `=` operator to parse fields from right to left.
[[maybe_unused]] int unused;
bool ok = true;
static_cast<void>(
((ok && (ok = (std::get<Index>(fields) = Extractable<U>::Extract(*this))
.has_value()),
unused) = ... = 0));
if (!ok) {
if (trace_) {
*trace_ << "Aggregate " << typeid(T).name() << ": error\n";
}
return std::nullopt;
}
if (trace_) {
*trace_ << "Aggregate " << typeid(T).name() << ": success\n";
}
return T{std::move(std::get<Index>(fields).value())...};
}
namespace {
// Extract the fields of a simple aggregate type.
template <typename T>
struct Extractable {
static_assert(std::is_aggregate_v<T>, "Unsupported child type");
static auto ExtractImpl(NodeExtractor& extractor) -> std::optional<T> {
// Compute the corresponding tuple type.
using TupleType = decltype(StructReflection::AsTuple(std::declval<T>()));
return extractor.ExtractTupleLikeType<T>(
std::make_index_sequence<std::tuple_size_v<TupleType>>(),
static_cast<TupleType*>(nullptr));
}
static auto Extract(NodeExtractor& extractor) -> std::optional<T> {
static_assert(!HasKindMember<T>, "Missing Id suffix");
return ExtractImpl(extractor);
}
};
} // namespace
template <typename T>
auto TreeAndSubtrees::TryExtractNodeFromChildren(
NodeId node_id,
llvm::iterator_range<TreeAndSubtrees::SiblingIterator> children,
ErrorBuilder* trace) const -> std::optional<T> {
NodeExtractor extractor(this, tokens_, trace, node_id, children);
auto result = Extractable<T>::ExtractImpl(extractor);
if (!extractor.at_end()) {
if (trace) {
*trace << "Error: " << tree_->node_kind(extractor.ExtractNode())
<< " node left unconsumed.";
}
return std::nullopt;
}
return result;
}
// Manually instantiate Tree::TryExtractNodeFromChildren
#define CARBON_PARSE_NODE_KIND(KindName) \
template auto TreeAndSubtrees::TryExtractNodeFromChildren<KindName>( \
NodeId node_id, \
llvm::iterator_range<TreeAndSubtrees::SiblingIterator> children, \
ErrorBuilder * trace) const -> std::optional<KindName>;
// Also instantiate for `File`, even though it isn't a parse node.
CARBON_PARSE_NODE_KIND(File)
#include "toolchain/parse/node_kind.def"
auto TreeAndSubtrees::ExtractFile() const -> File {
return ExtractNodeFromChildren<File>(NodeId::Invalid, roots());
}
} // namespace Carbon::Parse