forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Jon Ross-Perkins <[email protected]>
- Loading branch information
Showing
19 changed files
with
650 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// 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/check/context.h" | ||
|
||
namespace Carbon::Check { | ||
|
||
auto HandleImplIntroducer(Context& context, Parse::NodeId parse_node) -> bool { | ||
return context.TODO(parse_node, "HandleImplIntroducer"); | ||
} | ||
|
||
auto HandleImplForall(Context& context, Parse::NodeId parse_node) -> bool { | ||
return context.TODO(parse_node, "HandleImplForall"); | ||
} | ||
|
||
auto HandleImplAs(Context& context, Parse::NodeId parse_node) -> bool { | ||
return context.TODO(parse_node, "HandleImplAs"); | ||
} | ||
|
||
auto HandleImplDecl(Context& context, Parse::NodeId parse_node) -> bool { | ||
return context.TODO(parse_node, "HandleImplDecl"); | ||
} | ||
|
||
auto HandleImplDefinitionStart(Context& context, Parse::NodeId parse_node) | ||
-> bool { | ||
return context.TODO(parse_node, "HandleImplDefinitionStart"); | ||
} | ||
|
||
auto HandleImplDefinition(Context& context, Parse::NodeId parse_node) -> bool { | ||
return context.TODO(parse_node, "HandleImplDefinition"); | ||
} | ||
|
||
} // namespace Carbon::Check |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// 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 { | ||
|
||
// Handles processing after params, deciding whether it's a declaration or | ||
// definition. | ||
static auto HandleDeclOrDefinition(Context& context, NodeKind decl_kind, | ||
NodeKind definition_start_kind, | ||
State definition_finish_state) -> void { | ||
auto state = context.PopState(); | ||
|
||
if (state.has_error) { | ||
context.RecoverFromDeclError(state, decl_kind, | ||
/*skip_past_likely_end=*/true); | ||
return; | ||
} | ||
|
||
if (auto semi = context.ConsumeIf(Lex::TokenKind::Semi)) { | ||
context.AddNode(decl_kind, *semi, state.subtree_start, state.has_error); | ||
return; | ||
} | ||
|
||
if (!context.PositionIs(Lex::TokenKind::OpenCurlyBrace)) { | ||
context.EmitExpectedDeclSemiOrDefinition( | ||
context.tokens().GetKind(state.token)); | ||
context.RecoverFromDeclError(state, decl_kind, | ||
/*skip_past_likely_end=*/true); | ||
return; | ||
} | ||
|
||
context.PushState(state, definition_finish_state); | ||
context.PushState(State::DeclScopeLoop); | ||
context.AddNode(definition_start_kind, context.Consume(), state.subtree_start, | ||
state.has_error); | ||
} | ||
|
||
auto HandleDeclOrDefinitionAsClass(Context& context) -> void { | ||
HandleDeclOrDefinition(context, NodeKind::ClassDecl, | ||
NodeKind::ClassDefinitionStart, | ||
State::DeclDefinitionFinishAsClass); | ||
} | ||
|
||
auto HandleDeclOrDefinitionAsImpl(Context& context) -> void { | ||
HandleDeclOrDefinition(context, NodeKind::ImplDecl, | ||
NodeKind::ImplDefinitionStart, | ||
State::DeclDefinitionFinishAsImpl); | ||
} | ||
|
||
auto HandleDeclOrDefinitionAsInterface(Context& context) -> void { | ||
HandleDeclOrDefinition(context, NodeKind::InterfaceDecl, | ||
NodeKind::InterfaceDefinitionStart, | ||
State::DeclDefinitionFinishAsInterface); | ||
} | ||
|
||
auto HandleDeclOrDefinitionAsNamedConstraint(Context& context) -> void { | ||
HandleDeclOrDefinition(context, NodeKind::NamedConstraintDecl, | ||
NodeKind::NamedConstraintDefinitionStart, | ||
State::DeclDefinitionFinishAsNamedConstraint); | ||
} | ||
|
||
// Handles parsing after the declaration scope of a type. | ||
static auto HandleDeclDefinitionFinish(Context& context, | ||
NodeKind definition_kind) -> void { | ||
auto state = context.PopState(); | ||
|
||
context.AddNode(definition_kind, context.Consume(), state.subtree_start, | ||
state.has_error); | ||
} | ||
|
||
auto HandleDeclDefinitionFinishAsClass(Context& context) -> void { | ||
HandleDeclDefinitionFinish(context, NodeKind::ClassDefinition); | ||
} | ||
|
||
auto HandleDeclDefinitionFinishAsImpl(Context& context) -> void { | ||
HandleDeclDefinitionFinish(context, NodeKind::ImplDefinition); | ||
} | ||
|
||
auto HandleDeclDefinitionFinishAsInterface(Context& context) -> void { | ||
HandleDeclDefinitionFinish(context, NodeKind::InterfaceDefinition); | ||
} | ||
|
||
auto HandleDeclDefinitionFinishAsNamedConstraint(Context& context) -> void { | ||
HandleDeclDefinitionFinish(context, NodeKind::NamedConstraintDefinition); | ||
} | ||
|
||
} // namespace Carbon::Parse |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.