Skip to content

Commit

Permalink
Update C# lexer (#811)
Browse files Browse the repository at this point in the history
* Fix CommentPreproc regex (and update corresponding test)

* Separate KeywordDeclaration and KeywordConstant from Keyword list (similar to Java lexer).

* Update csharp lexer tests.

* Remove duplicate keyword 'static' from csharp lexer.
  • Loading branch information
wo80 committed Aug 15, 2023
1 parent 9864526 commit 2aad740
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
10 changes: 8 additions & 2 deletions lexers/embedded/csharp.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<rule pattern="0[xX][0-9a-fA-F]+[Ll]?|\d[_\d]*(\.\d*)?([eE][+-]?\d+)?[flFLdD]?">
<token type="LiteralNumber"/>
</rule>
<rule pattern="#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma|nullable)\b[^\n\r]+">
<rule pattern="#[ \t]*(if|endif|else|elif|define|undef|line|error|warning|region|endregion|pragma|nullable)\b">
<token type="CommentPreproc"/>
</rule>
<rule pattern="\b(extern)(\s+)(alias)\b">
Expand All @@ -62,7 +62,7 @@
<token type="Keyword"/>
</bygroups>
</rule>
<rule pattern="(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">
<rule pattern="(as|await|base|break|by|case|catch|checked|continue|default|delegate|do|else|event|finally|fixed|for|foreach|goto|if|in|init|is|let|lock|new|on|out|params|readonly|ref|return|sizeof|stackalloc|switch|this|throw|try|typeof|unchecked|virtual|void|while|get|set|new|yield|add|remove|value|alias|ascending|descending|from|group|into|orderby|select|thenby|where|join|equals)\b">
<token type="Keyword"/>
</rule>
<rule pattern="(global)(::)">
Expand All @@ -71,9 +71,15 @@
<token type="Punctuation"/>
</bygroups>
</rule>
<rule pattern="(abstract|async|const|enum|explicit|extern|implicit|internal|operator|override|partial|private|protected|public|static|sealed|unsafe|volatile)\b">
<token type="KeywordDeclaration"/>
</rule>
<rule pattern="(bool|byte|char|decimal|double|dynamic|float|int|long|object|sbyte|short|string|uint|ulong|ushort|var)\b\??">
<token type="KeywordType"/>
</rule>
<rule pattern="(true|false|null)\b">
<token type="KeywordConstant"/>
</rule>
<rule pattern="(class|struct|record|interface)(\s+)">
<bygroups>
<token type="Keyword"/>
Expand Down
12 changes: 7 additions & 5 deletions lexers/testdata/csharp/csharp_8_nullable.expected
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,33 @@
{"type":"NameNamespace","value":"System"},
{"type":"Punctuation","value":";"},
{"type":"Text","value":"\n\n"},
{"type":"CommentPreproc","value":"#nullable enable"},
{"type":"CommentPreproc","value":"#nullable"},
{"type":"Text","value":" "},
{"type":"Name","value":"enable"},
{"type":"Text","value":"\n\n"},
{"type":"Keyword","value":"public"},
{"type":"KeywordDeclaration","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":"KeywordDeclaration","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":"KeywordDeclaration","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":"KeywordDeclaration","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
Expand Down
2 changes: 1 addition & 1 deletion lexers/testdata/csharp/csharp_9_records_1.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{"type":"Keyword","value":"public"},
{"type":"KeywordDeclaration","value":"public"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"record"},
{"type":"Text","value":" "},
Expand Down
6 changes: 3 additions & 3 deletions lexers/testdata/csharp/csharp_9_records_2.expected
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[
{"type":"Keyword","value":"public"},
{"type":"KeywordDeclaration","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":"KeywordDeclaration","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
Expand All @@ -28,7 +28,7 @@
{"type":"Keyword","value":"default"},
{"type":"Punctuation","value":"!;"},
{"type":"Text","value":"\n "},
{"type":"Keyword","value":"public"},
{"type":"KeywordDeclaration","value":"public"},
{"type":"Text","value":" "},
{"type":"KeywordType","value":"string"},
{"type":"Text","value":" "},
Expand Down
2 changes: 1 addition & 1 deletion lexers/testdata/csharp/csharp_interface.expected
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[
{"type":"Keyword","value":"public"},
{"type":"KeywordDeclaration","value":"public"},
{"type":"Text","value":" "},
{"type":"Keyword","value":"interface"},
{"type":"Text","value":" "},
Expand Down

0 comments on commit 2aad740

Please sign in to comment.