Files
carbon-lang/toolchain/parser/parser_handle_var.cpp
T
Jon Ross-Perkins 8ad08e34e2 Refactor diagnostics out of parser_context.h (#2834)
This is addressing an issue left behind by the context switch, removing a few diagnostics that had been in the header rather than figuring out proper homes. I'm splitting one for semis a little further, sharing one, and then the other two are actually able to be moved into more specific homes as-is (one is only used in one place, clearly an oversight that it wasn't there already).
2023-05-18 12:45:08 -07:00

92 lines
3.0 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/parser/parser_context.h"
namespace Carbon {
// Handles VarAs(Semicolon|For).
static auto ParserHandleVar(ParserContext& context, ParserState finish_state)
-> void {
context.PopAndDiscardState();
// These will start at the `var`.
context.PushState(finish_state);
context.PushState(ParserState::VarAfterPattern);
context.AddLeafNode(ParseNodeKind::VariableIntroducer, context.Consume());
// This will start at the pattern.
context.PushState(ParserState::PatternAsVariable);
}
auto ParserHandleVarAsSemicolon(ParserContext& context) -> void {
ParserHandleVar(context, ParserState::VarFinishAsSemicolon);
}
auto ParserHandleVarAsFor(ParserContext& context) -> void {
ParserHandleVar(context, ParserState::VarFinishAsFor);
}
auto ParserHandleVarAfterPattern(ParserContext& context) -> void {
auto state = context.PopState();
if (state.has_error) {
if (auto after_pattern =
context.FindNextOf({TokenKind::Equal, TokenKind::Semi})) {
context.SkipTo(*after_pattern);
}
}
if (auto equals = context.ConsumeIf(TokenKind::Equal)) {
context.AddLeafNode(ParseNodeKind::VariableInitializer, *equals);
context.PushState(ParserState::Expression);
}
}
auto ParserHandleVarFinishAsSemicolon(ParserContext& context) -> void {
auto state = context.PopState();
auto end_token = state.token;
if (context.PositionIs(TokenKind::Semi)) {
end_token = context.Consume();
} else {
// TODO: Disambiguate between statement and member declaration.
CARBON_DIAGNOSTIC(ExpectedSemiAfterVar, Error,
"Expected `;` to terminate `var` declaration.");
context.emitter().Emit(*context.position(), ExpectedSemiAfterVar);
state.has_error = true;
if (auto semi_token = context.SkipPastLikelyEnd(state.token)) {
end_token = *semi_token;
}
}
context.AddNode(ParseNodeKind::VariableDeclaration, end_token,
state.subtree_start, state.has_error);
}
auto ParserHandleVarFinishAsFor(ParserContext& context) -> void {
auto state = context.PopState();
auto end_token = state.token;
if (context.PositionIs(TokenKind::In)) {
end_token = context.Consume();
} else if (context.PositionIs(TokenKind::Colon)) {
CARBON_DIAGNOSTIC(ExpectedInNotColon, Error,
"`:` should be replaced by `in`.");
context.emitter().Emit(*context.position(), ExpectedInNotColon);
state.has_error = true;
end_token = context.Consume();
} else {
CARBON_DIAGNOSTIC(ExpectedIn, Error,
"Expected `in` after loop `var` declaration.");
context.emitter().Emit(*context.position(), ExpectedIn);
state.has_error = true;
}
context.AddNode(ParseNodeKind::ForIn, end_token, state.subtree_start,
state.has_error);
}
} // namespace Carbon