forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandle_impl.cpp
77 lines (69 loc) · 2.62 KB
/
handle_impl.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
// 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"
#include "toolchain/parse/handle.h"
namespace Carbon::Parse {
static auto ExpectAsOrTypeExpression(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(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.AddLeafNode(NodeKind::Forall, context.Consume());
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.AddInvalidParse(*context.position());
}
} else {
// One of:
// as <expression> ...
// <expression> as <expression>...
ExpectAsOrTypeExpression(context);
}
}
auto HandleImplAfterForall(Context& context) -> void {
auto state = context.PopState();
if (state.has_error) {
context.ReturnErrorOnState();
}
// One of:
// as <expression> ...
// <expression> as <expression>...
ExpectAsOrTypeExpression(context);
}
auto HandleImplBeforeAs(Context& context) -> void {
auto state = context.PopState();
if (auto as = context.ConsumeIf(Lex::TokenKind::As)) {
context.AddNode(NodeKind::TypeImplAs, *as, 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