mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 21:20:11 +01:00
These are intended to allow the structure of a parse tree node to be
described more precisely in code, to support these use cases:
- Automated checking that the parse tree conforms to the expected
structure. (Added to `Tree::Verify`.)
- Easier reading and understanding of the structure of the parse tree by
toolchain developers. (See `parse/typed_nodes.h`.)
- Easier navigation of the parse tree, for example for tooling uses and
for use when forming diagnostics.
On this last point, an object representing the file may be inspecting
using `Tree::ExtractFile`, as in:
```
auto file = tree->ExtractFile();
for (AnyDeclId decl_id : file.decls) {
// `decl_id` is convertible to a `NodeId`.
if (std::optional<FunctionDecl> fn_decl =
tree->ExtractAs<FunctionDecl>(decl_id)) {
// fn_decl->params is a `TuplePatternId` (which extends `NodeId`)
// that is guaranteed to reference a `TuplePattern`.
std::optional<TuplePattern> params = tree->Extract(fn_decl->params);
// `params` has a value unless there was an error in that node.
} else if (auto class_def = tree->ExtractAs<ClassDefinition>(decl_id)) {
// ...
}
}
```
The `Extract...` functions collect the child nodes into the typed parse
node's fields (internally using a `Tree::SiblingIterator`) for easy
access. However, this is not as fast as directly observing the tree
structure using the postorder strategy being used by the check stage.
These functions rely on using struct reflection on the typed parse node
definitions from `parse/typed_nodes.h` to get the expected structure of
child nodes and then populate them.
Note that validating these in `Tree::Verify` adds significant cost to
it, and is currently included in the parsing stage. Without this change,
a 10 mloc test case of lex & parse takes 4.129 s ± 0.041 s. With this
change, it takes 5.768 s ± 0.036 s.
This builds upon and completes #3393.
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
---------
Co-authored-by: Richard Smith <richard@metafoo.co.uk>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
60 lines
1.7 KiB
C++
60 lines
1.7 KiB
C++
// 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 "toolchain/parse/context.h"
|
|
|
|
namespace Carbon::Parse {
|
|
|
|
auto HandleLet(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
// These will start at the `let`.
|
|
context.PushState(state, State::LetFinish);
|
|
context.PushState(state, State::LetAfterPattern);
|
|
|
|
// This will start at the pattern.
|
|
context.PushState(State::Pattern);
|
|
}
|
|
|
|
auto HandleLetAfterPattern(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
if (state.has_error) {
|
|
if (auto after_pattern =
|
|
context.FindNextOf({Lex::TokenKind::Equal, Lex::TokenKind::Semi})) {
|
|
context.SkipTo(*after_pattern);
|
|
}
|
|
}
|
|
|
|
if (auto equals = context.ConsumeIf(Lex::TokenKind::Equal)) {
|
|
context.AddLeafNode(NodeKind::LetInitializer, *equals);
|
|
context.PushState(State::Expr);
|
|
} else {
|
|
if (!state.has_error) {
|
|
CARBON_DIAGNOSTIC(
|
|
ExpectedInitializerAfterLet, Error,
|
|
"Expected `=`; `let` declaration must have an initializer.");
|
|
context.emitter().Emit(*context.position(), ExpectedInitializerAfterLet);
|
|
}
|
|
context.ReturnErrorOnState();
|
|
}
|
|
}
|
|
|
|
auto HandleLetFinish(Context& context) -> void {
|
|
auto state = context.PopState();
|
|
|
|
auto end_token = state.token;
|
|
if (context.PositionIs(Lex::TokenKind::Semi)) {
|
|
end_token = context.Consume();
|
|
} else {
|
|
context.EmitExpectedDeclSemi(Lex::TokenKind::Let);
|
|
state.has_error = true;
|
|
end_token = context.SkipPastLikelyEnd(state.token);
|
|
}
|
|
context.AddNode(NodeKind::LetDecl, end_token, state.subtree_start,
|
|
state.has_error);
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|