Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deconstruction-declaration parsing for local declaration, for and foreach #12104

Merged
merged 13 commits into from
Jun 24, 2016
66 changes: 34 additions & 32 deletions docs/features/deconstruction.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,61 +121,63 @@ Note that tuples (`System.ValueTuple`) don't need to invoke Deconstruct.
declaration_statement
: local_variable_declaration ';'
| local_constant_declaration ';'
| local_variable_combo_declaration ';' // new
;

local_variable_combo_declaration
: local_variable_combo_declaration_lhs '=' expression
local_variable_declaration
: local_variable_type local_variable_declarators
| deconstruction_declaration // new
;

local_variable_combo_declaration_lhs
: 'var' '(' identifier_list ')'
| '(' local_variable_list ')'
;
deconstruction_declaration // new
: deconstruction_variables '=' expression
;

identifier_list
: identifier ',' identifier
| identifier_list ',' identifier
;
deconstuction_variables
: '(' deconstuction_variables_nested (',' deconstuction_variables_nested)* ')'
| 'var' deconstruction_identifiers
;

local_variable_list
: local_variable_type identifier ',' local_variable_type identifier
| local_variable_list ',' local_variable_type identifier
;
deconstuction_variables_nested // new
: deconstuction_variables
| type identifier
;

deconstruction_identifiers
: '(' deconstruction_identifiers_nested (',' deconstruction_identifiers_nested)* ')'
;

deconstruction_identifiers_nested // new
: deconstruction_identifiers
| identifier
;

foreach_statement
: 'foreach' '(' local_variable_type identifier 'in' expression ')' embedded_statement
| 'foreach' '(' local_variable_combo_declaration_lhs 'in' expression ')' embedded_statement // new
| 'foreach' '(' deconstruction_variables 'in' expression ')' embedded_statement // new
;

for_statement
: 'for' '(' for_initializer? ';' for_condition? ';' for_iterator? ')' embedded_statement
;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has for_statement changed or is the same as without deconstruction variables?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the grammar, I kept for_statement as before. The difference appears below, in for_initializer.


for_initializer
: local_variable_declaration
| local_variable_combo_declaration // new
| deconstruction_declaration // new
| statement_expression_list
;

let_clause
: 'let' identifier '=' expression
| 'let' '(' identifier_list ')' '=' expression // new
| 'let' deconstruction_identifiers '=' expression // new
;

from_clause // not sure
from_clause
: 'from' type? identifier 'in' expression
;

join_clause // not sure
: 'join' type? identifier 'in' expression 'on' expression 'equals' expression
;

join_into_clause // not sure
: 'join' type? identifier 'in' expression 'on' expression 'equals' expression 'into' identifier
;

constant_declarator // not sure
: identifier '=' constant_expression
| 'from' deconstuction_variables 'in' expression // new
| 'from' deconstruction_identifiers 'in' expression // new
;
```

Treat deconstruction of a tuple into new variables as a new kind of node (not AssignmentExpression).
It would pick up the behavior of each contexts where new variables can be declared (TODO: need to list). For instance, in LINQ, new variables go into a transparent identifiers.
It is seen as deconstructing into separate variables (we don't introduce transparent identifiers in contexts where they didn't exist previously).

Expand Down
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4896,4 +4896,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_CannotDeconstructDynamic" xml:space="preserve">
<value>Cannot deconstruct dynamic objects.</value>
</data>
<data name="ERR_DeconstructTooFewElements" xml:space="preserve">
<value>Deconstruction must contain at least two variables.</value>
</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1398,5 +1398,6 @@ internal enum ErrorCode
ERR_DeconstructRequiresExpression = 8210,
ERR_DeconstructWrongCardinality = 8211,
ERR_CannotDeconstructDynamic = 8212,
ERR_DeconstructTooFewElements = 8213,
}
}
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.WhenClause(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.WhenClauseSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.IncompleteMember(Microsoft.CodeAnalysis.CSharp.Syntax.TypeSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.ReturnStatement(Microsoft.CodeAnalysis.CSharp.Syntax.ExpressionSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.ReturnStatementSyntax")]
[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("ApiDesign", "RS0027:Public API with optional parameter(s) should have the most parameters amongst its public overloads.", Justification = "<Pending>", Scope = "member", Target = "~M:Microsoft.CodeAnalysis.CSharp.SyntaxFactory.DeconstructionVariablesVar(Microsoft.CodeAnalysis.CSharp.Syntax.TupleExpressionSyntax)~Microsoft.CodeAnalysis.CSharp.Syntax.DeconstructionVariablesVarSyntax")]

Loading