Skip to content

Commit 4c36740

Browse files
authored
Refinement of the C# lexer (#563)
1 parent 0e8972e commit 4c36740

17 files changed

+197
-116
lines changed

Diff for: lexers/c/csharp.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ func cSharpRules() Rules {
2424
{`^\s*\[.*?\]`, NameAttribute, nil},
2525
{`[^\S\n]+`, Text, nil},
2626
{`\\\n`, Text, nil},
27-
{`//.*?\n`, CommentSingle, nil},
27+
{`///[^\n\r]+`, CommentSpecial, nil},
28+
{`//[^\n\r]+`, CommentSingle, nil},
2829
{`/[*].*?[*]/`, CommentMultiline, nil},
2930
{`\n`, Text, nil},
3031
{`[~!%^&*()+=|\[\]:;,.<>/?-]`, Punctuation, nil},
@@ -34,12 +35,12 @@ func cSharpRules() Rules {
3435
{`"(\\\\|\\"|[^"\n])*["\n]`, LiteralString, nil},
3536
{`'\\.'|'[^\\]'`, LiteralStringChar, nil},
3637
{`0[xX][0-9a-fA-F]+[Ll]?|[0-9_](\.[0-9]*)?([eE][+-]?[0-9]+)?[flFLdD]?`, LiteralNumber, nil},
37-
{`#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma)\b.*?\n`, CommentPreproc, nil},
38+
{`#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma|nullable)\b[^\n\r]+`, CommentPreproc, nil},
3839
{`\b(extern)(\s+)(alias)\b`, ByGroups(Keyword, Text, Keyword), nil},
39-
{`(abstract|as|async|await|base|break|by|case|catch|checked|const|continue|default|delegate|do|else|enum|event|explicit|extern|false|finally|fixed|for|foreach|goto|if|implicit|in|interface|internal|is|let|lock|new|null|on|operator|out|override|params|private|protected|public|readonly|ref|return|sealed|sizeof|stackalloc|static|switch|this|throw|true|try|typeof|unchecked|unsafe|virtual|void|while|get|set|new|partial|yield|add|remove|value|alias|ascending|descending|from|group|into|orderby|select|thenby|where|join|equals)\b`, Keyword, nil},
40+
{`(abstract|as|async|await|base|break|by|case|catch|checked|const|continue|default|delegate|do|else|enum|event|explicit|extern|false|finally|fixed|for|foreach|goto|if|implicit|in|init|internal|is|let|lock|new|null|on|operator|out|override|params|private|protected|public|readonly|ref|return|sealed|sizeof|stackalloc|static|switch|this|throw|true|try|typeof|unchecked|unsafe|virtual|void|while|get|set|new|partial|yield|add|remove|value|alias|ascending|descending|from|group|into|orderby|select|thenby|where|join|equals)\b`, Keyword, nil},
4041
{`(global)(::)`, ByGroups(Keyword, Punctuation), nil},
4142
{`(bool|byte|char|decimal|double|dynamic|float|int|long|object|sbyte|short|string|uint|ulong|ushort|var)\b\??`, KeywordType, nil},
42-
{`(class|struct)(\s+)`, ByGroups(Keyword, Text), Push("class")},
43+
{`(class|struct|record|interface)(\s+)`, ByGroups(Keyword, Text), Push("class")},
4344
{`(namespace|using)(\s+)`, ByGroups(Keyword, Text), Push("namespace")},
4445
{`@?[_a-zA-Z]\w*`, Name, nil},
4546
},

Diff for: lexers/testdata/csharp.actual

-16
This file was deleted.

Diff for: lexers/testdata/csharp.expected

-96
This file was deleted.

Diff for: lexers/testdata/csharp/csharp_8_nullable.actual

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
#nullable enable
4+
5+
public struct Student
6+
{
7+
public string FirstName;
8+
public string? MiddleName;
9+
public string LastName;
10+
}

Diff for: lexers/testdata/csharp/csharp_8_nullable.expected

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
[
2+
{"type":"Keyword","value":"using"},
3+
{"type":"Text","value":" "},
4+
{"type":"NameNamespace","value":"System"},
5+
{"type":"Punctuation","value":";"},
6+
{"type":"Text","value":"\n\n"},
7+
{"type":"CommentPreproc","value":"#nullable enable"},
8+
{"type":"Text","value":"\n\n"},
9+
{"type":"Keyword","value":"public"},
10+
{"type":"Text","value":" "},
11+
{"type":"Keyword","value":"struct"},
12+
{"type":"Text","value":" "},
13+
{"type":"NameClass","value":"Student"},
14+
{"type":"Text","value":"\n"},
15+
{"type":"Punctuation","value":"{"},
16+
{"type":"Text","value":"\n "},
17+
{"type":"Keyword","value":"public"},
18+
{"type":"Text","value":" "},
19+
{"type":"KeywordType","value":"string"},
20+
{"type":"Text","value":" "},
21+
{"type":"Name","value":"FirstName"},
22+
{"type":"Punctuation","value":";"},
23+
{"type":"Text","value":"\n "},
24+
{"type":"Keyword","value":"public"},
25+
{"type":"Text","value":" "},
26+
{"type":"KeywordType","value":"string?"},
27+
{"type":"Text","value":" "},
28+
{"type":"Name","value":"MiddleName"},
29+
{"type":"Punctuation","value":";"},
30+
{"type":"Text","value":"\n "},
31+
{"type":"Keyword","value":"public"},
32+
{"type":"Text","value":" "},
33+
{"type":"KeywordType","value":"string"},
34+
{"type":"Text","value":" "},
35+
{"type":"Name","value":"LastName"},
36+
{"type":"Punctuation","value":";"},
37+
{"type":"Text","value":"\n"},
38+
{"type":"Punctuation","value":"}"}
39+
]

Diff for: lexers/testdata/csharp/csharp_9_records_1.actual

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public record Person(string FirstName, string LastName);

Diff for: lexers/testdata/csharp/csharp_9_records_1.expected

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[
2+
{"type":"Keyword","value":"public"},
3+
{"type":"Text","value":" "},
4+
{"type":"Keyword","value":"record"},
5+
{"type":"Text","value":" "},
6+
{"type":"NameClass","value":"Person"},
7+
{"type":"Punctuation","value":"("},
8+
{"type":"KeywordType","value":"string"},
9+
{"type":"Text","value":" "},
10+
{"type":"Name","value":"FirstName"},
11+
{"type":"Punctuation","value":","},
12+
{"type":"Text","value":" "},
13+
{"type":"KeywordType","value":"string"},
14+
{"type":"Text","value":" "},
15+
{"type":"Name","value":"LastName"},
16+
{"type":"Punctuation","value":");"}
17+
]

Diff for: lexers/testdata/csharp/csharp_9_records_2.actual

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public record Person
2+
{
3+
public string FirstName { get; init; } = default!;
4+
public string LastName { get; init; } = default!;
5+
};

Diff for: lexers/testdata/csharp/csharp_9_records_2.expected

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
[
2+
{"type":"Keyword","value":"public"},
3+
{"type":"Text","value":" "},
4+
{"type":"Keyword","value":"record"},
5+
{"type":"Text","value":" "},
6+
{"type":"NameClass","value":"Person"},
7+
{"type":"Text","value":"\n"},
8+
{"type":"Punctuation","value":"{"},
9+
{"type":"Text","value":"\n "},
10+
{"type":"Keyword","value":"public"},
11+
{"type":"Text","value":" "},
12+
{"type":"KeywordType","value":"string"},
13+
{"type":"Text","value":" "},
14+
{"type":"Name","value":"FirstName"},
15+
{"type":"Text","value":" "},
16+
{"type":"Punctuation","value":"{"},
17+
{"type":"Text","value":" "},
18+
{"type":"Keyword","value":"get"},
19+
{"type":"Punctuation","value":";"},
20+
{"type":"Text","value":" "},
21+
{"type":"Keyword","value":"init"},
22+
{"type":"Punctuation","value":";"},
23+
{"type":"Text","value":" "},
24+
{"type":"Punctuation","value":"}"},
25+
{"type":"Text","value":" "},
26+
{"type":"Punctuation","value":"="},
27+
{"type":"Text","value":" "},
28+
{"type":"Keyword","value":"default"},
29+
{"type":"Punctuation","value":"!;"},
30+
{"type":"Text","value":"\n "},
31+
{"type":"Keyword","value":"public"},
32+
{"type":"Text","value":" "},
33+
{"type":"KeywordType","value":"string"},
34+
{"type":"Text","value":" "},
35+
{"type":"Name","value":"LastName"},
36+
{"type":"Text","value":" "},
37+
{"type":"Punctuation","value":"{"},
38+
{"type":"Text","value":" "},
39+
{"type":"Keyword","value":"get"},
40+
{"type":"Punctuation","value":";"},
41+
{"type":"Text","value":" "},
42+
{"type":"Keyword","value":"init"},
43+
{"type":"Punctuation","value":";"},
44+
{"type":"Text","value":" "},
45+
{"type":"Punctuation","value":"}"},
46+
{"type":"Text","value":" "},
47+
{"type":"Punctuation","value":"="},
48+
{"type":"Text","value":" "},
49+
{"type":"Keyword","value":"default"},
50+
{"type":"Punctuation","value":"!;"},
51+
{"type":"Text","value":"\n"},
52+
{"type":"Punctuation","value":"};"}
53+
]

Diff for: lexers/testdata/csharp/csharp_comment_multi.actual

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
Hello World
3+
*/
4+
5+
/***
6+
* Hello Developer
7+
***/

Diff for: lexers/testdata/csharp/csharp_comment_multi.expected

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"type":"CommentMultiline","value":"/*\n Hello World\n*/"},
3+
{"type":"Text","value":"\n\n"},
4+
{"type":"CommentMultiline","value":"/***\n* Hello Developer\n***/"}
5+
]

Diff for: lexers/testdata/csharp/csharp_comment_single.actual

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// TODO: Maybe later
2+
// TODO: Implement more features
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[
2+
{"type":"CommentSingle","value":"// TODO: Maybe later"},
3+
{"type":"Text","value":"\n"},
4+
{"type":"CommentSingle","value":"// TODO: Implement more features"}
5+
]

Diff for: lexers/testdata/csharp/csharp_comment_summary.actual

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/// <summary>
2+
/// Class <c>Point</c> models a point in a two-dimensional plane.
3+
/// </summary>
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[
2+
{"type":"CommentSpecial","value":"/// \u003csummary\u003e"},
3+
{"type":"Text","value":"\n"},
4+
{"type":"CommentSpecial","value":"/// Class \u003cc\u003ePoint\u003c/c\u003e models a point in a two-dimensional plane."},
5+
{"type":"Text","value":"\n"},
6+
{"type":"CommentSpecial","value":"/// \u003c/summary\u003e"}
7+
]

Diff for: lexers/testdata/csharp/csharp_interface.actual

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public interface IOrder
2+
{
3+
DateTime Purchased { get; }
4+
decimal Cost { get; }
5+
}

Diff for: lexers/testdata/csharp/csharp_interface.expected

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[
2+
{"type":"Keyword","value":"public"},
3+
{"type":"Text","value":" "},
4+
{"type":"Keyword","value":"interface"},
5+
{"type":"Text","value":" "},
6+
{"type":"NameClass","value":"IOrder"},
7+
{"type":"Text","value":"\n"},
8+
{"type":"Punctuation","value":"{"},
9+
{"type":"Text","value":"\n "},
10+
{"type":"Name","value":"DateTime"},
11+
{"type":"Text","value":" "},
12+
{"type":"Name","value":"Purchased"},
13+
{"type":"Text","value":" "},
14+
{"type":"Punctuation","value":"{"},
15+
{"type":"Text","value":" "},
16+
{"type":"Keyword","value":"get"},
17+
{"type":"Punctuation","value":";"},
18+
{"type":"Text","value":" "},
19+
{"type":"Punctuation","value":"}"},
20+
{"type":"Text","value":"\n "},
21+
{"type":"KeywordType","value":"decimal"},
22+
{"type":"Text","value":" "},
23+
{"type":"Name","value":"Cost"},
24+
{"type":"Text","value":" "},
25+
{"type":"Punctuation","value":"{"},
26+
{"type":"Text","value":" "},
27+
{"type":"Keyword","value":"get"},
28+
{"type":"Punctuation","value":";"},
29+
{"type":"Text","value":" "},
30+
{"type":"Punctuation","value":"}"},
31+
{"type":"Text","value":"\n"},
32+
{"type":"Punctuation","value":"}"}
33+
]

0 commit comments

Comments
 (0)