Files
carbon-lang/toolchain/parse/handle_match_first.cpp
T
Dana Jansens 085e45093e Avoid consuming a token after an invalid match_first opening (#7538)
If the `match_first` is not followed by `{` avoid consuming whatever
comes after it. Recover by leaving whatever comes next alone. It could
even be the `FileEnd` token, and then we would crash when we read off
the end of the token stream looking for `FileEnd`.
2026-07-20 19:09:16 +00:00

38 lines
1.2 KiB
C++

// 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 HandleMatchFirst(Context& context) -> void {
auto state = context.PopState();
// MatchFirstIntroducer node is automatically added for the MatchFirst state.
if (auto open_curly = context.ConsumeAndAddOpenCurlyBrace(
state.token, NodeKind::MatchFirstDefinitionStart)) {
state.token = *open_curly;
} else {
state.has_error = true;
}
context.PushState(state, StateKind::MatchFirstFinish);
if (!state.has_error) {
context.PushState(StateKind::DeclScopeLoopAsRegular);
}
}
auto HandleMatchFirstFinish(Context& context) -> void {
auto state = context.PopState();
if (auto closing_token = context.ConsumeIf(Lex::TokenKind::CloseCurlyBrace)) {
context.AddNode(NodeKind::MatchFirst, *closing_token, state.has_error);
} else {
// Recover from error by just moving on.
context.AddNode(NodeKind::MatchFirst, state.token, state.has_error);
}
}
} // namespace Carbon::Parse