Skip to content

Commit 7a7597c

Browse files
author
jneen
committed
use the keywords api for csharp (and refactor preproc)
1 parent 898a395 commit 7a7597c

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

.rubocop_todo.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ Rouge/NoBuildingAlternationPatternInRegexp:
6060
Exclude:
6161
- 'lib/rouge/lexers/armasm.rb'
6262
- 'lib/rouge/lexers/console.rb'
63-
- 'lib/rouge/lexers/csharp.rb'
6463
- 'lib/rouge/lexers/d.rb'
6564
- 'lib/rouge/lexers/dart.rb'
6665
- 'lib/rouge/lexers/eiffel.rb'

lib/rouge/lexers/csharp.rb

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CSharp < RegexLexer
1818
# Contextual Keywords
1919
# LINQ Query Expressions
2020
def self.keywords
21-
@keywords ||= %w(
21+
@keywords ||= Set.new %w(
2222
abstract add alias and as ascending async await base
2323
break by case catch checked const continue default delegate
2424
descending do else enum equals event explicit extern false
@@ -34,20 +34,22 @@ def self.keywords
3434
end
3535

3636
def self.keywords_type
37-
@keywords_type ||= %w(
37+
@keywords_type ||= Set.new %w(
3838
bool byte char decimal double dynamic float int long nint nuint
3939
object sbyte short string uint ulong ushort var
4040
)
4141
end
4242

4343
def self.cpp_keywords
44-
@cpp_keywords ||= %w(
44+
@cpp_keywords ||= Set.new %w(
4545
if endif else elif define undef line error warning region
4646
endregion pragma nullable
4747
)
4848
end
4949

5050
state :whitespace do
51+
rule %r/^[ \t]*#/, Comment::Preproc, :preproc
52+
5153
rule %r/\s+/m, Text
5254
rule %r(//.*?$), Comment::Single
5355
rule %r(/[*].*?[*]/)m, Comment::Multiline
@@ -96,13 +98,22 @@ def self.cpp_keywords
9698
)ix, Num
9799
rule %r/\b(?:class|record|struct|interface)\b/, Keyword, :class
98100
rule %r/\b(?:namespace|using)\b/, Keyword, :namespace
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
101+
102+
keywords %r/\w+/ do
103+
rule :keywords, Keyword
104+
rule :keywords_type, Keyword::Type
105+
end
106+
102107
rule %r/#{id}(?=\s*[(])/, Name::Function
103108
rule id, Name
104109
end
105110

111+
state :preproc do
112+
rule %r/[^\\\n]+/, Comment::Preproc
113+
rule %r/\\.?/m, Comment::Preproc
114+
rule %r/\n/, Comment::Preproc, :pop!
115+
end
116+
106117
state :class do
107118
mixin :whitespace
108119
rule id, Name::Class, :pop!

spec/visual/samples/csharp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ class Foo {
77
// stuff
88
}
99

10+
#ifdef FOO
11+
#ifdef BAR
12+
#endif
13+
#endif
14+
15+
1016
////////////////////////////////////////////////////////////////////////////////
1117
// //
1218
// MIT X11 license, Copyright (c) 2005-2006 by: //

0 commit comments

Comments
 (0)