-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse support for
where
operator (#4275)
Includes support for the `impls`, `=`, and `==` requirement operators to the right of a `where`, but `and` to allow multiple requirements is still a TODO. --------- Co-authored-by: Josh L <josh11b@users.noreply.github.com> Co-authored-by: Richard Smith <richard@metafoo.co.uk>
- Loading branch information
1 parent
8fa173b
commit c33c9a0
Showing
19 changed files
with
784 additions
and
38 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
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
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,83 @@ | ||
// 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/lex/token_kind.h" | ||
#include "toolchain/parse/context.h" | ||
#include "toolchain/parse/handle.h" | ||
|
||
namespace Carbon::Parse { | ||
|
||
auto HandleRequirementBegin(Context& context) -> void { | ||
context.PopAndDiscardState(); | ||
context.PushState(State::RequirementOperator); | ||
context.PushStateForExpr(PrecedenceGroup::ForRequirements()); | ||
} | ||
|
||
auto HandleRequirementOperator(Context& context) -> void { | ||
auto state = context.PopState(); | ||
|
||
switch (context.PositionKind()) { | ||
case Lex::TokenKind::Impls: { | ||
break; | ||
} | ||
case Lex::TokenKind::Equal: { | ||
break; | ||
} | ||
case Lex::TokenKind::EqualEqual: { | ||
break; | ||
} | ||
default: { | ||
if (!state.has_error) { | ||
CARBON_DIAGNOSTIC( | ||
ExpectedRequirementOperator, Error, | ||
"Requirement should use `impls`, `=`, or `==` operator."); | ||
context.emitter().Emit(*context.position(), | ||
ExpectedRequirementOperator); | ||
} | ||
context.ReturnErrorOnState(); | ||
return; | ||
} | ||
} | ||
state.token = context.Consume(); | ||
context.PushState(state, State::RequirementOperatorFinish); | ||
context.PushStateForExpr(PrecedenceGroup::ForRequirements()); | ||
} | ||
|
||
auto HandleRequirementOperatorFinish(Context& context) -> void { | ||
auto state = context.PopState(); | ||
|
||
switch (auto token_kind = context.tokens().GetKind(state.token)) { | ||
case Lex::TokenKind::Impls: { | ||
context.AddNode(NodeKind::RequirementImpls, state.token, state.has_error); | ||
break; | ||
} | ||
case Lex::TokenKind::Equal: { | ||
context.AddNode(NodeKind::RequirementEqual, state.token, state.has_error); | ||
break; | ||
} | ||
case Lex::TokenKind::EqualEqual: { | ||
context.AddNode(NodeKind::RequirementEqualEqual, state.token, | ||
state.has_error); | ||
break; | ||
} | ||
default: | ||
// RequirementOperatorFinish state is only pushed in | ||
// HandleRequirementOperator on one of the three requirement operator | ||
// tokens. | ||
CARBON_FATAL() << "Unexpected token kind for requirement operator: " | ||
<< token_kind; | ||
return; | ||
} | ||
// TODO: Handle `and` token. | ||
} | ||
|
||
auto HandleWhereFinish(Context& context) -> void { | ||
auto state = context.PopState(); | ||
if (state.has_error) { | ||
context.ReturnErrorOnState(); | ||
} | ||
context.AddNode(NodeKind::WhereExpr, state.token, state.has_error); | ||
} | ||
|
||
} // 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
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.