mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Parsing impl...as (#3473)
Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
c4864aa2ff
commit
5f439b842b
@@ -0,0 +1,79 @@
|
||||
// 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::ImplAs, 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();
|
||||
}
|
||||
} 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.AddLeafNode(NodeKind::InvalidParse, *context.position(),
|
||||
/*has_error=*/true);
|
||||
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 (state.has_error) {
|
||||
context.ReturnErrorOnState();
|
||||
return;
|
||||
}
|
||||
if (auto as = context.ConsumeIf(Lex::TokenKind::As)) {
|
||||
context.AddLeafNode(NodeKind::ImplAs, *as);
|
||||
context.PushState(State::Expr);
|
||||
} else {
|
||||
CARBON_DIAGNOSTIC(ImplExpectedAs, Error,
|
||||
"Expected `as` in `impl` declaration.");
|
||||
context.emitter().Emit(*context.position(), ImplExpectedAs);
|
||||
context.ReturnErrorOnState();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Carbon::Parse
|
||||
Reference in New Issue
Block a user