-
Notifications
You must be signed in to change notification settings - Fork 94
Description
Describe the bug
I am a maintainer for https://github.com/antlr/grammars-v4 and I am in the process of bringing the grammar in grammars-v4 up to date for C#. I am also scraping and validating the grammars over here and in the published ECMA 334 .pdf.
The rule for the foreach_statement is incorrect, starting in version 7.
csharpstandard/standard/statements.md
Lines 1109 to 1112 in 0346abf
| foreach_statement | |
| : 'await'? 'foreach' '(' ref_kind? local_variable_type identifier | |
| 'in' expression ')' embedded_statement | |
| ; |
Unless it really is not part of the spec, the rule should have an alternative for deconstruction_expression.
// Source: §13.9.5.1 General
foreach_statement
: 'await'? 'foreach' '(' ref_kind? local_variable_type identifier
'in' expression ')' embedded_statement
| 'await'? 'foreach' '(' deconstruction_expression
'in' expression ')' embedded_statement
;
Without the fix, the spec EBNF does not parse several input in the Roslyn v7 compiler itself. But, below is a simpler example that does not parse. (There are other places the Roslyn compiler diverges, so I don't know what is intended.)
Example
using System.Collections.Generic;
class Program
{
static void Main()
{
var map = new Dictionary<string, int> { ["a"] = 1 };
foreach (var (key, value) in map) { }
}
}Attached is a zip file containing a full program with the example. It builds and runs with compiler version 7.
Additional context
The current means of generating and testing the Antlr4 grammar is archaic. The "BuildGrammar" tool should generate a C#-target port, not a Java one, and definitely not using the ancient Antlr 4.9.2. There are better tools nowadays for analyzing the EBNF.