Files
carbon-lang/toolchain/parser/parser_handle_statement.cpp
T
Jon Ross-PerkinsandRichard Smith 7157445f97 Set up a 'Parse' namespace. (#3161)
Continuing on #3070.

I moved ParseTree::Node to just Parse::Node, versus Parse::Tree::Node.
Other name changes are just removing "Parse" or "Parser" prefixes.

In EnumBase, I'm directly defining operator<< because the ostream.h
approach just isn't working, not for either of Parse::State nor
Parse::NodeKind. Errors look like:

```toolchain/parser/parser_context.cpp:449:34: error: invalid operands to binary expression ('llvm::raw_ostream' and 'const Carbon::Parse::State')
    output << "\t" << i << ".\t" << entry.state;
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^  ~~~~~~~~~~~
```

The expected template in `Carbon::` is not in the error list; I only see
the:

```
./common/ostream.h:112:6: note: candidate template ignored: requirement 'std::is_base_of_v<std::ostream, llvm::raw_ostream>' was not satisfied [with S = llvm::raw_ostream, T = Carbon::Parse::State]
auto operator<<(S& standard_out, const T& value) -> S& {
     ^
```

I'm still prodding at this, but not seeing an obvious fix.

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
2023-08-28 21:58:38 +00:00

224 lines
6.7 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::Parse {
auto HandleStatement(Context& context) -> void {
context.PopAndDiscardState();
switch (context.PositionKind()) {
case TokenKind::Break: {
context.PushState(State::StatementBreakFinish);
context.AddLeafNode(NodeKind::BreakStatementStart, context.Consume());
break;
}
case TokenKind::Continue: {
context.PushState(State::StatementContinueFinish);
context.AddLeafNode(NodeKind::ContinueStatementStart, context.Consume());
break;
}
case TokenKind::For: {
context.PushState(State::StatementForFinish);
context.PushState(State::StatementForHeader);
++context.position();
break;
}
case TokenKind::If: {
context.PushState(State::StatementIf);
break;
}
case TokenKind::Return: {
context.PushState(State::StatementReturn);
break;
}
case TokenKind::Var: {
context.PushState(State::VarAsSemicolon);
break;
}
case TokenKind::While: {
context.PushState(State::StatementWhile);
break;
}
default: {
context.PushState(State::ExpressionStatementFinish);
context.PushStateForExpression(PrecedenceGroup::ForExpressionStatement());
break;
}
}
}
// Handles the `;` after a keyword statement.
static auto HandleStatementKeywordFinish(Context& context, NodeKind node_kind)
-> void {
auto state = context.PopState();
auto semi = context.ConsumeIf(TokenKind::Semi);
if (!semi) {
CARBON_DIAGNOSTIC(ExpectedStatementSemi, Error,
"`{0}` statements must end with a `;`.", TokenKind);
context.emitter().Emit(*context.position(), ExpectedStatementSemi,
context.tokens().GetKind(state.token));
state.has_error = true;
// Recover to the next semicolon if possible, otherwise indicate the
// keyword for the error.
semi = context.SkipPastLikelyEnd(state.token);
if (!semi) {
semi = state.token;
}
}
context.AddNode(node_kind, *semi, state.subtree_start, state.has_error);
}
auto HandleStatementBreakFinish(Context& context) -> void {
HandleStatementKeywordFinish(context, NodeKind::BreakStatement);
}
auto HandleStatementContinueFinish(Context& context) -> void {
HandleStatementKeywordFinish(context, NodeKind::ContinueStatement);
}
auto HandleStatementForHeader(Context& context) -> void {
auto state = context.PopState();
std::optional<TokenizedBuffer::Token> open_paren =
context.ConsumeAndAddOpenParen(state.token, NodeKind::ForHeaderStart);
if (open_paren) {
state.token = *open_paren;
}
state.state = State::StatementForHeaderIn;
if (context.PositionIs(TokenKind::Var)) {
context.PushState(state);
context.PushState(State::VarAsFor);
} else {
CARBON_DIAGNOSTIC(ExpectedVariableDeclaration, Error,
"Expected `var` declaration.");
context.emitter().Emit(*context.position(), ExpectedVariableDeclaration);
if (auto next_in = context.FindNextOf({TokenKind::In})) {
context.SkipTo(*next_in);
++context.position();
}
state.has_error = true;
context.PushState(state);
}
}
auto HandleStatementForHeaderIn(Context& context) -> void {
auto state = context.PopState();
state.state = State::StatementForHeaderFinish;
context.PushState(state);
context.PushState(State::Expression);
}
auto HandleStatementForHeaderFinish(Context& context) -> void {
auto state = context.PopState();
context.ConsumeAndAddCloseSymbol(state.token, state, NodeKind::ForHeader);
context.PushState(State::CodeBlock);
}
auto HandleStatementForFinish(Context& context) -> void {
auto state = context.PopState();
context.AddNode(NodeKind::ForStatement, state.token, state.subtree_start,
state.has_error);
}
auto HandleStatementIf(Context& context) -> void {
context.PopAndDiscardState();
context.PushState(State::StatementIfConditionFinish);
context.PushState(State::ParenConditionAsIf);
++context.position();
}
auto HandleStatementIfConditionFinish(Context& context) -> void {
auto state = context.PopState();
state.state = State::StatementIfThenBlockFinish;
context.PushState(state);
context.PushState(State::CodeBlock);
}
auto HandleStatementIfThenBlockFinish(Context& context) -> void {
auto state = context.PopState();
if (context.ConsumeAndAddLeafNodeIf(TokenKind::Else,
NodeKind::IfStatementElse)) {
state.state = State::StatementIfElseBlockFinish;
context.PushState(state);
// `else if` is permitted as a special case.
context.PushState(context.PositionIs(TokenKind::If) ? State::StatementIf
: State::CodeBlock);
} else {
context.AddNode(NodeKind::IfStatement, state.token, state.subtree_start,
state.has_error);
}
}
auto HandleStatementIfElseBlockFinish(Context& context) -> void {
auto state = context.PopState();
context.AddNode(NodeKind::IfStatement, state.token, state.subtree_start,
state.has_error);
}
auto HandleStatementReturn(Context& context) -> void {
auto state = context.PopState();
state.state = State::StatementReturnFinish;
context.PushState(state);
context.AddLeafNode(NodeKind::ReturnStatementStart, context.Consume());
if (!context.PositionIs(TokenKind::Semi)) {
context.PushState(State::Expression);
}
}
auto HandleStatementReturnFinish(Context& context) -> void {
HandleStatementKeywordFinish(context, NodeKind::ReturnStatement);
}
auto HandleStatementScopeLoop(Context& context) -> void {
// This maintains the current state until we're at the end of the scope.
auto token_kind = context.PositionKind();
if (token_kind == TokenKind::CloseCurlyBrace) {
auto state = context.PopState();
if (state.has_error) {
context.ReturnErrorOnState();
}
} else {
context.PushState(State::Statement);
}
}
auto HandleStatementWhile(Context& context) -> void {
context.PopAndDiscardState();
context.PushState(State::StatementWhileConditionFinish);
context.PushState(State::ParenConditionAsWhile);
++context.position();
}
auto HandleStatementWhileConditionFinish(Context& context) -> void {
auto state = context.PopState();
state.state = State::StatementWhileBlockFinish;
context.PushState(state);
context.PushState(State::CodeBlock);
}
auto HandleStatementWhileBlockFinish(Context& context) -> void {
auto state = context.PopState();
context.AddNode(NodeKind::WhileStatement, state.token, state.subtree_start,
state.has_error);
}
} // namespace Carbon::Parse