tree-sitter-c-sharp icon indicating copy to clipboard operation
tree-sitter-c-sharp copied to clipboard

c# 11 raw string literals

Open kasperk81 opened this issue 3 years ago • 6 comments

per docs, these are valid and invalid cases starting with c# 11

// valid
x = $"""The point "{X}, {Y}" is {Math.Sqrt(X * X + Y * Y)} from the origin""";
x = $"""The point ""{X}, {Y}"" is {Math.Sqrt(X * X + Y * Y)} from the origin""";
x = $""""The point """{X}, {Y}""" is {Math.Sqrt(X * X + Y * Y)} from the origin"""";
x = $$"""The point {{{X}}, {{Y}}} is {{Math.Sqrt(X * X + Y * Y)}} from the origin""";

// invalid
x = $"The point "{X}, {Y}" is {Math.Sqrt(X * X + Y * Y)} from the origin";
x = $""The point ""{X}, {Y}"" is {Math.Sqrt(X * X + Y * Y)} from the origin"";
x = $"""The point """{X}, {Y}""" is {Math.Sqrt(X * X + Y * Y)} from the origin""";
x = $$"""The point {{X}, {{Y}}} is {{Math.Sqrt(X * X + Y * Y)}} from the origin""";

also take a look at 'what's new in c# 11' series: https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-11#raw-string-literals

tree-sitter doesn't handle valid cases.

kasperk81 avatar Apr 30 '22 14:04 kasperk81

I'm planning on tackling the C# 11 features next week :)

damieng avatar Apr 30 '22 14:04 damieng

cool. we still have time until november when c# 11 will officially released (as part of .net 7 release). using preview 3 of .net 7 let us play with it.

here is another one "utf-8 string literals" (https://github.com/dotnet/csharplang/blob/main/proposals/utf8-string-literals.md)

following five are equal:

byte[] utf8String;

utf8String = new byte [] { 97, 98, 99 };

utf8String = new byte [] {
    (byte)'a', (byte)'b', (byte)'c' };

// new in c# 11
utf8String = "abc";
utf8String = "abc"u8; // tree-sitter has issue with the new 'u8' suffix
utf8String = "abc"U8; // tree-sitter has issue with the new 'U8' suffix

u8 suffix name is case insensitive like other constant suffix (f/F for float, d/D for double and m/M for decimal)

kasperk81 avatar Apr 30 '22 16:04 kasperk81

I think the u8 feature is yet to be confirmed - neither of those u8 suffixes work in the latest VS/.NET SDK preview bits (VS 2022 Preview 5, .NET 7 SDK 7.0 preview 3)

image

damieng avatar Apr 30 '22 19:04 damieng

i am using preview 4 (second column https://github.com/dotnet/installer#table) with <LangVersion>Preview<LangVersion> in csproj.

kasperk81 avatar Apr 30 '22 22:04 kasperk81

Just updated to preview 4 and still having the same issue. I'm going to leave utf-8 support for now until things are further along.

damieng avatar May 01 '22 11:05 damieng

ok. for reference, i can run it locally with daily build (first column from installer table) and here is an example running on sharplab.

kasperk81 avatar May 04 '22 12:05 kasperk81