Skip to content

Commit

Permalink
Refinement of the C# lexer (alecthomas#563)
Browse files Browse the repository at this point in the history
  • Loading branch information
samtrion committed Oct 21, 2021
1 parent 0e8972e commit 4c36740
Show file tree
Hide file tree
Showing 17 changed files with 197 additions and 116 deletions.
9 changes: 5 additions & 4 deletions lexers/c/csharp.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ func cSharpRules() Rules {
{`^\s*\[.*?\]`, NameAttribute, nil},
{`[^\S\n]+`, Text, nil},
{`\\\n`, Text, nil},
{`//.*?\n`, CommentSingle, nil},
{`///[^\n\r]+`, CommentSpecial, nil},
{`//[^\n\r]+`, CommentSingle, nil},
{`/[*].*?[*]/`, CommentMultiline, nil},
{`\n`, Text, nil},
{`[~!%^&*()+=|\[\]:;,.<>/?-]`, Punctuation, nil},
Expand All @@ -34,12 +35,12 @@ func cSharpRules() Rules {
{`"(\\\\|\\"|[^"\n])*["\n]`, LiteralString, nil},
{`'\\.'|'[^\\]'`, LiteralStringChar, nil},
{`0[xX][0-9a-fA-F]+[Ll]?|[0-9_](\.[0-9]*)?([eE][+-]?[0-9]+)?[flFLdD]?`, LiteralNumber, nil},
{`#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma)\b.*?\n`, CommentPreproc, nil},
{`#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma|nullable)\b[^\n\r]+`, CommentPreproc, nil},
{`\b(extern)(\s+)(alias)\b`, ByGroups(Keyword, Text, Keyword), nil},
{`(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},
{`(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},
{`(global)(::)`, ByGroups(Keyword, Punctuation), nil},
{`(bool|byte|char|decimal|double|dynamic|float|int|long|object|sbyte|short|string|uint|ulong|ushort|var)\b\??`, KeywordType, nil},
{`(class|struct)(\s+)`, ByGroups(Keyword, Text), Push("class")},
{`(class|struct|record|interface)(\s+)`, ByGroups(Keyword, Text), Push("class")},
{`(namespace|using)(\s+)`, ByGroups(Keyword, Text), Push("namespace")},
{`@?[_a-zA-Z]\w*`, Name, nil},
},
Expand Down
16 changes: 0 additions & 16 deletions lexers/testdata/csharp.actual

This file was deleted.

96 changes: 0 additions & 96 deletions lexers/testdata/csharp.expected

This file was deleted.

10 changes: 10 additions & 0 deletions lexers/testdata/csharp/csharp_8_nullable.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

#nullable enable

public struct Student
{
public string FirstName;
public string? MiddleName;
public string LastName;
}
39 changes: 39 additions & 0 deletions lexers/testdata/csharp/csharp_8_nullable.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[
{"type":"Keyword","value":"using"},
{"type":"Text","value":" "},
{"type":"NameNamespace","value":"System"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"CommentPreproc","value":"#nullable enable"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"struct"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Student"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"FirstName"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string?"},
{"type":"Text","value":" "},
{"type":"Name","value":"MiddleName"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"LastName"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"}
]
1 change: 1 addition & 0 deletions lexers/testdata/csharp/csharp_9_records_1.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
public record Person(string FirstName, string LastName);
17 changes: 17 additions & 0 deletions lexers/testdata/csharp/csharp_9_records_1.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"record"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Person"},
{"type":"Punctuation","value":"("},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"FirstName"},
{"type":"Punctuation","value":","},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"LastName"},
{"type":"Punctuation","value":");"}
]
5 changes: 5 additions & 0 deletions lexers/testdata/csharp/csharp_9_records_2.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public record Person
{
public string FirstName { get; init; } = default!;
public string LastName { get; init; } = default!;
};
53 changes: 53 additions & 0 deletions lexers/testdata/csharp/csharp_9_records_2.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
[
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"record"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"Person"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"FirstName"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"get"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"init"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"Keyword","value":"default"},
{"type":"Punctuation","value":"!;"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
{"type":"Name","value":"LastName"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"get"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"init"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"="},
{"type":"Text","value":" "},
{"type":"Keyword","value":"default"},
{"type":"Punctuation","value":"!;"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"};"}
]
7 changes: 7 additions & 0 deletions lexers/testdata/csharp/csharp_comment_multi.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
Hello World
*/

/***
* Hello Developer
***/
5 changes: 5 additions & 0 deletions lexers/testdata/csharp/csharp_comment_multi.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"type":"CommentMultiline","value":"/*\n Hello World\n*/"},
{"type":"Text","value":"\n\n"},
{"type":"CommentMultiline","value":"/***\n* Hello Developer\n***/"}
]
2 changes: 2 additions & 0 deletions lexers/testdata/csharp/csharp_comment_single.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// TODO: Maybe later
// TODO: Implement more features
5 changes: 5 additions & 0 deletions lexers/testdata/csharp/csharp_comment_single.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"type":"CommentSingle","value":"// TODO: Maybe later"},
{"type":"Text","value":"\n"},
{"type":"CommentSingle","value":"// TODO: Implement more features"}
]
3 changes: 3 additions & 0 deletions lexers/testdata/csharp/csharp_comment_summary.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/// <summary>
/// Class <c>Point</c> models a point in a two-dimensional plane.
/// </summary>
7 changes: 7 additions & 0 deletions lexers/testdata/csharp/csharp_comment_summary.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{"type":"CommentSpecial","value":"/// \u003csummary\u003e"},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"/// Class \u003cc\u003ePoint\u003c/c\u003e models a point in a two-dimensional plane."},
{"type":"Text","value":"\n"},
{"type":"CommentSpecial","value":"/// \u003c/summary\u003e"}
]
5 changes: 5 additions & 0 deletions lexers/testdata/csharp/csharp_interface.actual
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
public interface IOrder
{
DateTime Purchased { get; }
decimal Cost { get; }
}
33 changes: 33 additions & 0 deletions lexers/testdata/csharp/csharp_interface.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{"type":"Keyword","value":"public"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"interface"},
{"type":"Text","value":" "},
{"type":"NameClass","value":"IOrder"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":"\n "},
{"type":"Name","value":"DateTime"},
{"type":"Text","value":" "},
{"type":"Name","value":"Purchased"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"get"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n "},
{"type":"KeywordType","value":"decimal"},
{"type":"Text","value":" "},
{"type":"Name","value":"Cost"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"{"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"get"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":" "},
{"type":"Punctuation","value":"}"},
{"type":"Text","value":"\n"},
{"type":"Punctuation","value":"}"}
]

0 comments on commit 4c36740

Please sign in to comment.