mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-25 12:20:10 +01:00
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>
81 lines
2.8 KiB
C++
81 lines
2.8 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 {
|
|
|
|
static auto ExpectAsOrTypeExpression(Carbon::Parse::Context& context) -> void {
|
|
if (context.PositionIs(Lex::TokenKind::As)) {
|
|
// as <expression> ...
|
|
context.AddLeafNode(NodeKind::DefaultSelfImplAs, context.Consume());
|
|
context.PushState(State::Expr);
|
|
} else {
|
|
// <expression> as <expression>...
|
|
context.PushState(State::ImplBeforeAs);
|
|
context.PushStateForExpr(PrecedenceGroup::ForImplAs());
|
|
}
|
|
}
|
|
|
|
auto HandleImplAfterIntroducer(Carbon::Parse::Context& context) -> void {
|
|
auto state = context.PopState();
|
|
state.state = State::DeclOrDefinitionAsImpl;
|
|
context.PushState(state);
|
|
|
|
if (context.PositionIs(Lex::TokenKind::Forall)) {
|
|
// forall [<implicit parameter list>] ...
|
|
context.PushState(State::ImplAfterForall);
|
|
context.ConsumeAndDiscard();
|
|
if (context.PositionIs(Lex::TokenKind::OpenSquareBracket)) {
|
|
context.PushState(State::PatternListAsImplicit);
|
|
} else {
|
|
CARBON_DIAGNOSTIC(ImplExpectedAfterForall, Error,
|
|
"Expected `[` after `forall` in `impl` declaration.");
|
|
context.emitter().Emit(*context.position(), ImplExpectedAfterForall);
|
|
context.ReturnErrorOnState();
|
|
// If we aren't producing a node from the PatternListAsImplicit state,
|
|
// we still need to create a node to be the child of the `ImplForall`
|
|
// token created in the `ImplAfterForall` state.
|
|
context.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
|
/*has_error=*/true);
|
|
}
|
|
} else {
|
|
// One of:
|
|
// as <expression> ...
|
|
// <expression> as <expression>...
|
|
ExpectAsOrTypeExpression(context);
|
|
}
|
|
}
|
|
|
|
auto HandleImplAfterForall(Carbon::Parse::Context& context) -> void {
|
|
auto state = context.PopState();
|
|
if (state.has_error) {
|
|
context.ReturnErrorOnState();
|
|
}
|
|
context.AddNode(NodeKind::ImplForall, state.token, state.subtree_start,
|
|
state.has_error);
|
|
// One of:
|
|
// as <expression> ...
|
|
// <expression> as <expression>...
|
|
ExpectAsOrTypeExpression(context);
|
|
}
|
|
|
|
auto HandleImplBeforeAs(Carbon::Parse::Context& context) -> void {
|
|
auto state = context.PopState();
|
|
if (auto as = context.ConsumeIf(Lex::TokenKind::As)) {
|
|
context.AddNode(NodeKind::TypeImplAs, *as, state.subtree_start,
|
|
state.has_error);
|
|
context.PushState(State::Expr);
|
|
} else {
|
|
if (!state.has_error) {
|
|
CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
|
|
"Expected `as` in `impl` declaration.");
|
|
context.emitter().Emit(*context.position(), ImplExpectedAs);
|
|
}
|
|
context.ReturnErrorOnState();
|
|
}
|
|
}
|
|
|
|
} // namespace Carbon::Parse
|