forked from carbon-language/carbon-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust handling of cases where conditions are missing. (carbon-langua…
…ge#3119) In carbon-language#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 carbon-language#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 carbon-language#3118
- Loading branch information
Showing
9 changed files
with
184 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: ] |