Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse support for tuple patterns in var and let #3448

Merged
merged 7 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Respond to reviewer comments
  • Loading branch information
geoffromer committed Dec 6, 2023
commit 6fc6c213aac3729b58ca91f6ead072b9c8ae1e68
1 change: 1 addition & 0 deletions toolchain/check/handle_variable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ auto HandleVariableDecl(Context& context, Parse::NodeId parse_node) -> bool {
if (next_kind == Parse::NodeKind::ParamList) {
return context.TODO(parse_node, "tuple pattern in var");
}
// TODO: find a more robust way to determine if there was an initializer.
bool has_init = next_kind != Parse::NodeKind::BindingPattern;
if (has_init) {
init_id = context.node_stack().PopExpr();
Expand Down
10 changes: 0 additions & 10 deletions toolchain/parse/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,6 @@ auto Context::ConsumeIf(Lex::TokenKind kind) -> std::optional<Lex::TokenIndex> {
return Consume();
}

auto Context::ConsumeIfBindingPatternKeyword(Lex::TokenKind keyword_token,
State keyword_state,
int subtree_start) -> void {
if (auto token = ConsumeIf(keyword_token)) {
PushState(Context::StateStackEntry(
keyword_state, PrecedenceGroup::ForTopLevelExpr(),
PrecedenceGroup::ForTopLevelExpr(), *token, subtree_start));
}
}

auto Context::FindNextOf(std::initializer_list<Lex::TokenKind> desired_kinds)
-> std::optional<Lex::TokenIndex> {
auto new_position = position_;
Expand Down
5 changes: 0 additions & 5 deletions toolchain/parse/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ class Context {
// Propagates an error up the state stack, to the parent state.
auto ReturnErrorOnState() -> void { state_stack_.back().has_error = true; }

// For HandleBindingPattern, tries to consume a wrapping keyword.
auto ConsumeIfBindingPatternKeyword(Lex::TokenKind keyword_token,
State keyword_state, int subtree_start)
-> void;

// Emits a diagnostic for a declaration missing a semi.
auto EmitExpectedDeclSemi(Lex::TokenKind expected_kind) -> void;

Expand Down
16 changes: 11 additions & 5 deletions toolchain/parse/handle_binding_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ auto HandleBindingPattern(Context& context) -> void {

// Parameters may have keywords prefixing the pattern. They become the parent
// for the full BindingPattern.
context.ConsumeIfBindingPatternKeyword(Lex::TokenKind::Template,
State::BindingPatternTemplate,
state.subtree_start);
context.ConsumeIfBindingPatternKeyword(
Lex::TokenKind::Addr, State::BindingPatternAddress, state.subtree_start);
if (auto token = context.ConsumeIf(Lex::TokenKind::Template)) {
context.PushState(Context::StateStackEntry(
State::BindingPatternTemplate, PrecedenceGroup::ForTopLevelExpr(),
PrecedenceGroup::ForTopLevelExpr(), *token, state.subtree_start));
}

if (auto token = context.ConsumeIf(Lex::TokenKind::Addr)) {
context.PushState(Context::StateStackEntry(
State::BindingPatternAddress, PrecedenceGroup::ForTopLevelExpr(),
PrecedenceGroup::ForTopLevelExpr(), *token, state.subtree_start));
}

// Handle an invalid pattern introducer for parameters and variables.
auto on_error = [&]() {
Expand Down
6 changes: 1 addition & 5 deletions toolchain/parse/handle_let.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@ auto HandleLet(Context& context) -> void {
context.AddLeafNode(NodeKind::LetIntroducer, context.Consume());

// This will start at the pattern.
if (context.PositionKind() == Lex::TokenKind::OpenParen) {
context.PushState(State::ParamListAsRegular);
} else {
context.PushState(State::BindingPattern);
}
context.PushState(State::Pattern);
}

auto HandleLetAfterPattern(Context& context) -> void {
Expand Down
18 changes: 18 additions & 0 deletions toolchain/parse/handle_pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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 {

auto HandlePattern(Context& context) -> void {
context.PopAndDiscardState();
if (context.PositionKind() == Lex::TokenKind::OpenParen) {
context.PushState(State::ParamListAsRegular);
} else {
context.PushState(State::BindingPattern);
}
}

} // namespace Carbon::Parse
6 changes: 1 addition & 5 deletions toolchain/parse/handle_var.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@ static auto HandleVar(Context& context, State finish_state,
context.AddLeafNode(NodeKind::ReturnedModifier, returned_token);
}

if (context.PositionKind() == Lex::TokenKind::OpenParen) {
context.PushState(State::ParamListAsRegular);
} else {
context.PushState(State::BindingPattern);
}
context.PushState(State::Pattern);
}

auto HandleVarAsDecl(Context& context) -> void {
Expand Down
11 changes: 11 additions & 0 deletions toolchain/parse/state.def
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,17 @@ CARBON_PARSE_STATE_VARIANTS2(ParenExprParamFinish, Unknown, Tuple)
// (state done)
CARBON_PARSE_STATE_VARIANTS2(ParenExprFinish, Normal, Tuple)

// Handles processing of a pattern.
//
// ( ... )
// ^
// 1. ParamListAsRegular
//
// ...
// ^
// 1. BindingPattern
CARBON_PARSE_STATE(Pattern)

// Handles the initial part of a binding pattern, enqueuing type expression
// processing.
//
Expand Down