Skip to content

Commit 2a8b21b

Browse files
authored
Move keywords to class methods in CSharp lexer (#2139)
1 parent 2f130c9 commit 2a8b21b

File tree

1 file changed

+35
-31
lines changed

1 file changed

+35
-31
lines changed

lib/rouge/lexers/csharp.rb

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,38 @@ class CSharp < RegexLexer
1414

1515
id = /@?[_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}][\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Nl}\p{Nd}\p{Pc}\p{Cf}\p{Mn}\p{Mc}]*/
1616

17-
#Reserved Identifiers
18-
#Contextual Keywords
19-
#LINQ Query Expressions
20-
keywords = %w(
21-
abstract as base break case catch checked const continue
22-
default delegate do else enum event explicit extern false
23-
finally fixed for foreach goto if implicit in interface
24-
internal is lock new null operator out override params private
25-
protected public readonly ref return sealed sizeof stackalloc
26-
static switch this throw true try typeof unchecked unsafe
27-
virtual void volatile while
28-
add alias async await get global partial remove set value where
29-
yield nameof notnull
30-
ascending by descending equals from group in init into join let
31-
on orderby select unmanaged when and not or with
32-
)
33-
34-
keywords_type = %w(
35-
bool byte char decimal double dynamic float int long nint nuint
36-
object sbyte short string uint ulong ushort var
37-
)
38-
39-
cpp_keywords = %w(
40-
if endif else elif define undef line error warning region
41-
endregion pragma nullable
42-
)
17+
# Reserved Identifiers
18+
# Contextual Keywords
19+
# LINQ Query Expressions
20+
def self.keywords
21+
@keywords ||= %w(
22+
abstract add alias and as ascending async await base
23+
break by case catch checked const continue default delegate
24+
descending do else enum equals event explicit extern false
25+
finally fixed for foreach from get global goto group
26+
if implicit in init interface internal into is join
27+
let lock nameof new notnull null on operator orderby
28+
out override params partial private protected public readonly
29+
ref remove return sealed set sizeof stackalloc static
30+
switch this throw true try typeof unchecked unsafe
31+
unmanaged value virtual void volatile when where while
32+
with yield
33+
)
34+
end
35+
36+
def self.keywords_type
37+
@keywords_type ||= %w(
38+
bool byte char decimal double dynamic float int long nint nuint
39+
object sbyte short string uint ulong ushort var
40+
)
41+
end
42+
43+
def self.cpp_keywords
44+
@cpp_keywords ||= %w(
45+
if endif else elif define undef line error warning region
46+
endregion pragma nullable
47+
)
48+
end
4349

4450
state :whitespace do
4551
rule %r/\s+/m, Text
@@ -90,10 +96,9 @@ class CSharp < RegexLexer
9096
)ix, Num
9197
rule %r/\b(?:class|record|struct|interface)\b/, Keyword, :class
9298
rule %r/\b(?:namespace|using)\b/, Keyword, :namespace
93-
rule %r/^#[ \t]*(#{cpp_keywords.join('|')})\b.*?\n/,
94-
Comment::Preproc
95-
rule %r/\b(#{keywords.join('|')})\b/, Keyword
96-
rule %r/\b(#{keywords_type.join('|')})\b/, Keyword::Type
99+
rule %r/^#[ \t]*(#{CSharp.cpp_keywords.join('|')})\b.*?\n/, Comment::Preproc
100+
rule %r/\b(#{CSharp.keywords.join('|')})\b/, Keyword
101+
rule %r/\b(#{CSharp.keywords_type.join('|')})\b/, Keyword::Type
97102
rule %r/#{id}(?=\s*[(])/, Name::Function
98103
rule id, Name
99104
end
@@ -108,7 +113,6 @@ class CSharp < RegexLexer
108113
rule %r/(?=[(])/, Text, :pop!
109114
rule %r/(#{id}|[.])+/, Name::Namespace, :pop!
110115
end
111-
112116
end
113117
end
114118
end

0 commit comments

Comments
 (0)