Parse tree for impl that is better for check stage (#3678)

Use two different nodes for "<type> followed by `as`" and "<type>
omitted before `as`, use `self`", so it is easier to determine which
case. Later the second case will push the type id for `self` onto the
node stack, making the two paths more similar.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
josh11b
2024-02-01 22:23:21 +00:00
committed by GitHub
co-authored by Jon Ross-Perkins
parent efde1497c4
commit 03bf22e55e
11 changed files with 88 additions and 56 deletions
+8 -10
View File
@@ -9,7 +9,7 @@ namespace Carbon::Parse {
static auto ExpectAsOrTypeExpression(Carbon::Parse::Context& context) -> void {
if (context.PositionIs(Lex::TokenKind::As)) {
// as <expression> ...
context.AddLeafNode(NodeKind::ImplAs, context.Consume());
context.AddLeafNode(NodeKind::DefaultSelfImplAs, context.Consume());
context.PushState(State::Expr);
} else {
// <expression> as <expression>...
@@ -55,7 +55,6 @@ auto HandleImplAfterForall(Carbon::Parse::Context& context) -> void {
}
context.AddNode(NodeKind::ImplForall, state.token, state.subtree_start,
state.has_error);
// One of:
// as <expression> ...
// <expression> as <expression>...
@@ -64,17 +63,16 @@ auto HandleImplAfterForall(Carbon::Parse::Context& context) -> void {
auto HandleImplBeforeAs(Carbon::Parse::Context& context) -> void {
auto state = context.PopState();
if (state.has_error) {
context.ReturnErrorOnState();
return;
}
if (auto as = context.ConsumeIf(Lex::TokenKind::As)) {
context.AddLeafNode(NodeKind::ImplAs, *as);
context.AddNode(NodeKind::TypeImplAs, *as, state.subtree_start,
state.has_error);
context.PushState(State::Expr);
} else {
CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
"Expected `as` in `impl` declaration.");
context.emitter().Emit(*context.position(), ImplExpectedAs);
if (!state.has_error) {
CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
"Expected `as` in `impl` declaration.");
context.emitter().Emit(*context.position(), ImplExpectedAs);
}
context.ReturnErrorOnState();
}
}