Adjust handling of cases where conditions are missing. (#3119)

In #3064, code was changed to look at a future token. This is an issue
because the parser is set up to enforce that tokens aren't used without
being consumed. That's part of #3118; related validation fails. Also,
since it's not necessarily the open paren that was consumed, it could be
a different opening symbol, which the closing symbol handling doesn't
check.

Under this approach, it's tracked whether an open paren was consumed,
and the open paren is associated with the state. That's more aligned
with how the parser expects to be fed information.

In paren condition handling for if and while, I'm also adding some
special casing for `if {` in particular to not assume the `{` is a
struct. I just think that this will come up somewhat often and the
resulting output is better this way (an error either way). I'm not doing
similar with `for` because there's already some `var` handling there,
and I'd need a little more time to think about structure -- whereas
right now I'm just trying to fix the crashes (`if {}`, `if []`, etc).

Fixes #3118
This commit is contained in:
Jon Ross-Perkins
2023-08-18 23:04:30 +00:00
committed by GitHub
parent fe93c0225f
commit 4ae0fa6f86
9 changed files with 184 additions and 18 deletions
+4 -1
View File
@@ -77,15 +77,18 @@ auto ParserContext::AddNode(ParseNodeKind kind, TokenizedBuffer::Token token,
}
auto ParserContext::ConsumeAndAddOpenParen(TokenizedBuffer::Token default_token,
ParseNodeKind start_kind) -> void {
ParseNodeKind start_kind)
-> std::optional<TokenizedBuffer::Token> {
if (auto open_paren = ConsumeIf(TokenKind::OpenParen)) {
AddLeafNode(start_kind, *open_paren, /*has_error=*/false);
return open_paren;
} else {
CARBON_DIAGNOSTIC(ExpectedParenAfter, Error, "Expected `(` after `{0}`.",
TokenKind);
emitter_->Emit(*position_, ExpectedParenAfter,
tokens().GetKind(default_token));
AddLeafNode(start_kind, default_token, /*has_error=*/true);
return std::nullopt;
}
}
+3 -2
View File
@@ -108,9 +108,10 @@ class ParserContext {
// Parses an open paren token, possibly diagnosing if necessary. Creates a
// leaf parse node of the specified start kind. The default_token is used when
// there's no open paren.
// there's no open paren. Returns the open paren token if it was found.
auto ConsumeAndAddOpenParen(TokenizedBuffer::Token default_token,
ParseNodeKind start_kind) -> void;
ParseNodeKind start_kind)
-> std::optional<TokenizedBuffer::Token>;
// Parses a closing symbol corresponding to the opening symbol
// `expected_open`, possibly skipping forward and diagnosing if necessary.
@@ -12,11 +12,23 @@ static auto ParserHandleParenCondition(ParserContext& context,
ParserState finish_state) -> void {
auto state = context.PopState();
context.ConsumeAndAddOpenParen(state.token, start_kind);
std::optional<TokenizedBuffer::Token> open_paren =
context.ConsumeAndAddOpenParen(state.token, start_kind);
if (open_paren) {
state.token = *open_paren;
}
state.state = finish_state;
context.PushState(state);
context.PushState(ParserState::Expression);
if (!open_paren && context.PositionIs(TokenKind::OpenCurlyBrace)) {
// For an open curly, assume the condition was completely omitted.
// Expression parsing would treat the { as a struct, but instead assume it's
// a code block and just emit an invalid parse.
context.AddLeafNode(ParseNodeKind::InvalidParse, *context.position(),
/*has_error=*/true);
} else {
context.PushState(ParserState::Expression);
}
}
auto ParserHandleParenConditionAsIf(ParserContext& context) -> void {
@@ -32,17 +44,15 @@ auto ParserHandleParenConditionAsWhile(ParserContext& context) -> void {
auto ParserHandleParenConditionFinishAsIf(ParserContext& context) -> void {
auto state = context.PopState();
context.ConsumeAndAddCloseSymbol(
*(TokenizedBuffer::TokenIterator(state.token) + 1), state,
ParseNodeKind::IfCondition);
context.ConsumeAndAddCloseSymbol(state.token, state,
ParseNodeKind::IfCondition);
}
auto ParserHandleParenConditionFinishAsWhile(ParserContext& context) -> void {
auto state = context.PopState();
context.ConsumeAndAddCloseSymbol(
*(TokenizedBuffer::TokenIterator(state.token) + 1), state,
ParseNodeKind::WhileCondition);
context.ConsumeAndAddCloseSymbol(state.token, state,
ParseNodeKind::WhileCondition);
}
} // namespace Carbon
+8 -5
View File
@@ -86,8 +86,12 @@ auto ParserHandleStatementContinueFinish(ParserContext& context) -> void {
auto ParserHandleStatementForHeader(ParserContext& context) -> void {
auto state = context.PopState();
context.ConsumeAndAddOpenParen(state.token, ParseNodeKind::ForHeaderStart);
std::optional<TokenizedBuffer::Token> open_paren =
context.ConsumeAndAddOpenParen(state.token,
ParseNodeKind::ForHeaderStart);
if (open_paren) {
state.token = *open_paren;
}
state.state = ParserState::StatementForHeaderIn;
if (context.PositionIs(TokenKind::Var)) {
@@ -118,9 +122,8 @@ auto ParserHandleStatementForHeaderIn(ParserContext& context) -> void {
auto ParserHandleStatementForHeaderFinish(ParserContext& context) -> void {
auto state = context.PopState();
context.ConsumeAndAddCloseSymbol(
*(TokenizedBuffer::TokenIterator(state.token) + 1), state,
ParseNodeKind::ForHeader);
context.ConsumeAndAddCloseSymbol(state.token, state,
ParseNodeKind::ForHeader);
context.PushState(ParserState::CodeBlock);
}
+3 -1
View File
@@ -468,7 +468,9 @@ CARBON_PARSER_STATE_VARIANTS2(ParameterListFinish, Deduced, Regular)
// Handles the processing of a `(condition)` up through the expression.
//
// Always:
// If `OpenCurlyBrace`:
// 1. ParenConditionAs(If|While)Finish
// Else:
// 1. Expression
// 2. ParenConditionAs(If|While)Finish
CARBON_PARSER_STATE_VARIANTS2(ParenCondition, If, While)
+40
View File
@@ -0,0 +1,40 @@
// 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
//
// AUTOUPDATE
fn F() {
// CHECK:STDERR: fail_missing_cond.carbon:[[@LINE+6]]:7: Expected `(` after `for`.
// CHECK:STDERR: for {
// CHECK:STDERR: ^
// CHECK:STDERR: fail_missing_cond.carbon:[[@LINE+3]]:7: Expected `var` declaration.
// CHECK:STDERR: for {
// CHECK:STDERR: ^
for {
}
// CHECK:STDERR: fail_missing_cond.carbon:[[@LINE+6]]:1: Expected braced code block.
// CHECK:STDERR: }
// CHECK:STDERR: ^
// CHECK:STDERR: fail_missing_cond.carbon:[[@LINE+3]]:1: Expected expression.
// CHECK:STDERR: }
// CHECK:STDERR: ^
}
// CHECK:STDOUT: [
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'Name', text: 'F'},
// CHECK:STDOUT: {kind: 'ParameterListStart', text: '('},
// CHECK:STDOUT: {kind: 'ParameterList', text: ')', subtree_size: 2},
// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 5},
// CHECK:STDOUT: {kind: 'ForHeaderStart', text: 'for', has_error: yes},
// CHECK:STDOUT: {kind: 'StructLiteralOrStructTypeLiteralStart', text: '{'},
// CHECK:STDOUT: {kind: 'StructLiteral', text: '}', subtree_size: 2},
// CHECK:STDOUT: {kind: 'ForHeader', text: 'for', has_error: yes, subtree_size: 4},
// CHECK:STDOUT: {kind: 'CodeBlockStart', text: '}', has_error: yes},
// CHECK:STDOUT: {kind: 'InvalidParse', text: '}', has_error: yes},
// CHECK:STDOUT: {kind: 'CodeBlock', text: '}', has_error: yes, subtree_size: 3},
// CHECK:STDOUT: {kind: 'ForStatement', text: 'for', subtree_size: 8},
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 14},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
@@ -0,0 +1,41 @@
// 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
//
// AUTOUPDATE
fn F() {
// CHECK:STDERR: fail_square_brackets.carbon:[[@LINE+12]]:7: Expected `(` after `for`.
// CHECK:STDERR: for [] {
// CHECK:STDERR: ^
// CHECK:STDERR: fail_square_brackets.carbon:[[@LINE+9]]:7: Expected `var` declaration.
// CHECK:STDERR: for [] {
// CHECK:STDERR: ^
// CHECK:STDERR: fail_square_brackets.carbon:[[@LINE+6]]:8: Expected expression.
// CHECK:STDERR: for [] {
// CHECK:STDERR: ^
// CHECK:STDERR: fail_square_brackets.carbon:[[@LINE+3]]:8: Expected `;` in array type.
// CHECK:STDERR: for [] {
// CHECK:STDERR: ^
for [] {
}
}
// CHECK:STDOUT: [
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'Name', text: 'F'},
// CHECK:STDOUT: {kind: 'ParameterListStart', text: '('},
// CHECK:STDOUT: {kind: 'ParameterList', text: ')', subtree_size: 2},
// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 5},
// CHECK:STDOUT: {kind: 'ForHeaderStart', text: 'for', has_error: yes},
// CHECK:STDOUT: {kind: 'ArrayExpressionStart', text: '['},
// CHECK:STDOUT: {kind: 'InvalidParse', text: ']', has_error: yes},
// CHECK:STDOUT: {kind: 'ArrayExpressionSemi', text: ']', has_error: yes, subtree_size: 3},
// CHECK:STDOUT: {kind: 'ArrayExpression', text: ']', has_error: yes, subtree_size: 4},
// CHECK:STDOUT: {kind: 'ForHeader', text: 'for', has_error: yes, subtree_size: 6},
// CHECK:STDOUT: {kind: 'CodeBlockStart', text: '{'},
// CHECK:STDOUT: {kind: 'CodeBlock', text: '}', subtree_size: 2},
// CHECK:STDOUT: {kind: 'ForStatement', text: 'for', subtree_size: 9},
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 15},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
+29
View File
@@ -0,0 +1,29 @@
// 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
//
// AUTOUPDATE
fn F() {
// CHECK:STDERR: fail_missing_cond.carbon:[[@LINE+3]]:6: Expected `(` after `if`.
// CHECK:STDERR: if {
// CHECK:STDERR: ^
if {
}
}
// CHECK:STDOUT: [
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'Name', text: 'F'},
// CHECK:STDOUT: {kind: 'ParameterListStart', text: '('},
// CHECK:STDOUT: {kind: 'ParameterList', text: ')', subtree_size: 2},
// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 5},
// CHECK:STDOUT: {kind: 'IfConditionStart', text: 'if', has_error: yes},
// CHECK:STDOUT: {kind: 'InvalidParse', text: '{', has_error: yes},
// CHECK:STDOUT: {kind: 'IfCondition', text: 'if', has_error: yes, subtree_size: 3},
// CHECK:STDOUT: {kind: 'CodeBlockStart', text: '{'},
// CHECK:STDOUT: {kind: 'CodeBlock', text: '}', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IfStatement', text: 'if', subtree_size: 6},
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 12},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]
@@ -0,0 +1,37 @@
// 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
//
// AUTOUPDATE
fn F() {
// CHECK:STDERR: fail_square_brackets.carbon:[[@LINE+9]]:6: Expected `(` after `if`.
// CHECK:STDERR: if [] {}
// CHECK:STDERR: ^
// CHECK:STDERR: fail_square_brackets.carbon:[[@LINE+6]]:7: Expected expression.
// CHECK:STDERR: if [] {}
// CHECK:STDERR: ^
// CHECK:STDERR: fail_square_brackets.carbon:[[@LINE+3]]:7: Expected `;` in array type.
// CHECK:STDERR: if [] {}
// CHECK:STDERR: ^
if [] {}
}
// CHECK:STDOUT: [
// CHECK:STDOUT: {kind: 'FunctionIntroducer', text: 'fn'},
// CHECK:STDOUT: {kind: 'Name', text: 'F'},
// CHECK:STDOUT: {kind: 'ParameterListStart', text: '('},
// CHECK:STDOUT: {kind: 'ParameterList', text: ')', subtree_size: 2},
// CHECK:STDOUT: {kind: 'FunctionDefinitionStart', text: '{', subtree_size: 5},
// CHECK:STDOUT: {kind: 'IfConditionStart', text: 'if', has_error: yes},
// CHECK:STDOUT: {kind: 'ArrayExpressionStart', text: '['},
// CHECK:STDOUT: {kind: 'InvalidParse', text: ']', has_error: yes},
// CHECK:STDOUT: {kind: 'ArrayExpressionSemi', text: ']', has_error: yes, subtree_size: 3},
// CHECK:STDOUT: {kind: 'ArrayExpression', text: ']', has_error: yes, subtree_size: 4},
// CHECK:STDOUT: {kind: 'IfCondition', text: 'if', has_error: yes, subtree_size: 6},
// CHECK:STDOUT: {kind: 'CodeBlockStart', text: '{'},
// CHECK:STDOUT: {kind: 'CodeBlock', text: '}', subtree_size: 2},
// CHECK:STDOUT: {kind: 'IfStatement', text: 'if', subtree_size: 9},
// CHECK:STDOUT: {kind: 'FunctionDefinition', text: '}', subtree_size: 15},
// CHECK:STDOUT: {kind: 'FileEnd', text: ''},
// CHECK:STDOUT: ]