Skip to content

Commit 7f341fc

Browse files
C#: Improved pattern matching (#2411)
1 parent 2a2e79e commit 7f341fc

5 files changed

+679
-4
lines changed

components/prism-csharp.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
typeDeclaration: 'class enum interface struct',
4848
// contextual keywords
4949
// ("var" and "dynamic" are missing because they are used like types)
50-
contextual: 'add alias ascending async await by descending from get global group into join let nameof notnull on orderby partial remove select set unmanaged value when where where',
50+
contextual: 'add alias and ascending async await by descending from get global group into join let nameof not notnull on or orderby partial remove select set unmanaged value when where where',
5151
// all other keywords
5252
other: 'abstract as base break case catch checked const continue default delegate do else event explicit extern finally fixed for foreach goto if implicit in internal is lock namespace new null operator out override params private protected public readonly ref return sealed sizeof stackalloc static switch this throw try typeof unchecked unsafe using virtual volatile while yield'
5353
};
@@ -68,6 +68,7 @@
6868
var genericName = replace(/<<0>>(?:\s*<<1>>)?/.source, [name, generic]);
6969
var identifier = replace(/(?!<<0>>)<<1>>(?:\s*\.\s*<<1>>)*/.source, [nonTypeKeywords, genericName]);
7070
var array = /\[\s*(?:,\s*)*\]/.source;
71+
var typeExpressionWithoutTuple = replace(/<<0>>(?:\s*(?:\?\s*)?<<1>>)*(?:\s*\?)?/.source, [identifier, array]);
7172
var tupleElement = replace(/[^,()<>[\];=+\-*/%&|^]|<<0>>|<<1>>|<<2>>/.source, [generic, nestedRound, array])
7273
var tuple = replace(/\(<<0>>+(?:,<<0>>+)+\)/.source, [tupleElement]);
7374
var typeExpression = replace(/(?:<<0>>|<<1>>)(?:\s*(?:\?\s*)?<<2>>)*(?:\s*\?)?/.source, [tuple, identifier, array]);
@@ -150,14 +151,14 @@
150151
// Casts and checks via as and is.
151152
// as Foo<A>, is Bar<B>
152153
// (things like if(a is Foo b) is covered by variable declaration)
153-
pattern: re(/(\b(?:is|as)\s+)<<0>>/.source, [typeExpression]),
154+
pattern: re(/(\b(?:is(?:\s+not)?|as)\s+)<<0>>/.source, [typeExpressionWithoutTuple]),
154155
lookbehind: true,
155156
inside: typeInside
156157
},
157158
{
158159
// Variable, field and parameter declaration
159160
// (Foo bar, Bar baz, Foo[,,] bay, Foo<Bar, FooBar<Bar>> bax)
160-
pattern: re(/\b<<0>>(?=\s+(?!<<1>>)<<2>>(?:\s*[=,;:{)\]]|\s+in))/.source, [typeExpression, nonContextualKeywords, name]),
161+
pattern: re(/\b<<0>>(?=\s+(?!<<1>>)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
161162
inside: typeInside
162163
}
163164
],

components/prism-csharp.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/languages/csharp/class-name-variables-parameters_feature.test

+86
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ void Foo(Foo a, Bar<Foo> b, Bar[, ] c, Foo<(int, int)> d);
77
Bar<Foo> Abc => 0;
88
Bar<Foo>.FooBar<IFoo> Abc;
99

10+
if (foo is Bar)
11+
if (foo is not Bar)
12+
if (foo is null)
13+
if (foo is not null)
14+
if (t is (int, string))
15+
if ((e1, e2) is (0, int i) or (int i, 0))
16+
var baz = foo as Bar;
17+
1018
----------------------------------------------------
1119

1220
[
@@ -113,6 +121,84 @@ Bar<Foo>.FooBar<IFoo> Abc;
113121
["punctuation", ">"]
114122
]],
115123
" Abc",
124+
["punctuation", ";"],
125+
126+
["keyword", "if"],
127+
["punctuation", "("],
128+
"foo ",
129+
["keyword", "is"],
130+
["class-name", [
131+
"Bar"
132+
]],
133+
["punctuation", ")"],
134+
["keyword", "if"],
135+
["punctuation", "("],
136+
"foo ",
137+
["keyword", "is"],
138+
["keyword", "not"],
139+
["class-name", [
140+
"Bar"
141+
]],
142+
["punctuation", ")"],
143+
["keyword", "if"],
144+
["punctuation", "("],
145+
"foo ",
146+
["keyword", "is"],
147+
["keyword", "null"],
148+
["punctuation", ")"],
149+
["keyword", "if"],
150+
["punctuation", "("],
151+
"foo ",
152+
["keyword", "is"],
153+
["keyword", "not"],
154+
["keyword", "null"],
155+
["punctuation", ")"],
156+
["keyword", "if"],
157+
["punctuation", "("],
158+
"t ",
159+
["keyword", "is"],
160+
["punctuation", "("],
161+
["keyword", "int"],
162+
["punctuation", ","],
163+
["keyword", "string"],
164+
["punctuation", ")"],
165+
["punctuation", ")"],
166+
["keyword", "if"],
167+
["punctuation", "("],
168+
["punctuation", "("],
169+
"e1",
170+
["punctuation", ","],
171+
" e2",
172+
["punctuation", ")"],
173+
["keyword", "is"],
174+
["punctuation", "("],
175+
["number", "0"],
176+
["punctuation", ","],
177+
["class-name", [
178+
["keyword", "int"]
179+
]],
180+
" i",
181+
["punctuation", ")"],
182+
["keyword", "or"],
183+
["punctuation", "("],
184+
["class-name", [
185+
["keyword", "int"]
186+
]],
187+
" i",
188+
["punctuation", ","],
189+
["number", "0"],
190+
["punctuation", ")"],
191+
["punctuation", ")"],
192+
["class-name", [
193+
["keyword", "var"]
194+
]],
195+
" baz ",
196+
["operator", "="],
197+
" foo ",
198+
["keyword", "as"],
199+
["class-name", [
200+
"Bar"
201+
]],
116202
["punctuation", ";"]
117203
]
118204

tests/languages/csharp/keyword_feature.test

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
abstract
22
add
33
alias
4+
and
45
as
56
ascending
67
async
@@ -54,11 +55,13 @@ long
5455
nameof
5556
namespace;
5657
new;
58+
not
5759
notnull
5860
null
5961
object
6062
on
6163
operator
64+
or
6265
orderby
6366
out
6467
override
@@ -109,6 +112,7 @@ yield
109112
["keyword", "abstract"],
110113
["keyword", "add"],
111114
["keyword", "alias"],
115+
["keyword", "and"],
112116
["keyword", "as"],
113117
["keyword", "ascending"],
114118
["keyword", "async"],
@@ -162,11 +166,13 @@ yield
162166
["keyword", "nameof"],
163167
["keyword", "namespace"], ["punctuation", ";"],
164168
["keyword", "new"], ["punctuation", ";"],
169+
["keyword", "not"],
165170
["keyword", "notnull"],
166171
["keyword", "null"],
167172
["keyword", "object"],
168173
["keyword", "on"],
169174
["keyword", "operator"],
175+
["keyword", "or"],
170176
["keyword", "orderby"],
171177
["keyword", "out"],
172178
["keyword", "override"],

0 commit comments

Comments
 (0)