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

Support target-type new expressions. #77

Closed
patrickt opened this issue Sep 24, 2020 · 7 comments
Closed

Support target-type new expressions. #77

patrickt opened this issue Sep 24, 2020 · 7 comments

Comments

@patrickt
Copy link
Contributor

Right now this fails to parse:

        internal static class EnumFormatters
        {
            public static readonly EnumFormatter<AnalysisKind> AnalysisKind = new(value => (int)value, value => (AnalysisKind)value);
        }

failing at the = new( bit. I think this is the relevant piece of syntax.

@patrickt
Copy link
Contributor Author

Note also that we don’t support interpolated strings appearing in declarations like these:

private static readonly string s_defaultDocumentPath = @$"Document\Path\{DefaultDocumentName}”;

@patrickt patrickt changed the title Support constant_declarator syntax Support constant_declarator syntax with new Sep 24, 2020
@patrickt
Copy link
Contributor Author

I think this bug is more general than just constant declarators.

@patrickt patrickt changed the title Support constant_declarator syntax with new Support new expression without type. Sep 24, 2020
@patrickt
Copy link
Contributor Author

Here’s a hack that fixes this:

diff --git i/grammar.js w/grammar.js
index e6432dd..659783d 100644
--- i/grammar.js
+++ w/grammar.js
@@ -891,6 +891,14 @@ module.exports = grammar({
       field('body', choice($.block, $._expression))
     )),
 
+    direct_new_object_creation_expression: $ => seq(
+      'new',
+      '(',
+      commaSep($._expression),
+      optional(','),
+      ')'
+    ),
+
     anonymous_object_creation_expression: $ => seq(
       'new',
       '{',
@@ -1231,6 +1239,7 @@ module.exports = grammar({
     _expression: $ => choice(
       $.anonymous_method_expression,
       $.anonymous_object_creation_expression,
+      $.direct_new_object_creation_expression,
       $.array_creation_expression,
       $.assignment_expression,
       $.await_expression,

@initram
Copy link
Contributor

initram commented Sep 25, 2020

The new without a type is a new C# 9 feature called target-type new expressions. This is the issue for it: dotnet/csharplang#100

So it is simply a new feature, not really a bug in the current syntax.

@initram
Copy link
Contributor

initram commented Sep 25, 2020

Note also that we don’t support interpolated strings appearing in declarations like these:

private static readonly string s_defaultDocumentPath = @$"Document\Path\{DefaultDocumentName}”;

This was a new feature in C# 8, where you can use wither $@"Some string" or @$"Some string": https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-8#enhancement-of-interpolated-verbatim-strings

I can create a PR to fix this.

@patrickt
Copy link
Contributor Author

patrickt commented Sep 25, 2020

@initram Aha! I didn’t even notice that the characters were transposed. Thank you, and thanks for linking to the relevant chapter/verse of the standards.

@patrickt patrickt changed the title Support new expression without type. Support target-type new expressions. Sep 25, 2020
@initram
Copy link
Contributor

initram commented Oct 5, 2020

I think this issue can be closed now.

@damieng damieng closed this as completed Oct 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants