Skip to content

Commit

Permalink
C#: Improved pattern matching (#2411)
Browse files Browse the repository at this point in the history
  • Loading branch information
RunDevelopment authored Jun 12, 2020
1 parent 2a2e79e commit 7f341fc
Show file tree
Hide file tree
Showing 5 changed files with 679 additions and 4 deletions.
7 changes: 4 additions & 3 deletions components/prism-csharp.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
typeDeclaration: 'class enum interface struct',
// contextual keywords
// ("var" and "dynamic" are missing because they are used like types)
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',
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',
// all other keywords
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'
};
Expand All @@ -68,6 +68,7 @@
var genericName = replace(/<<0>>(?:\s*<<1>>)?/.source, [name, generic]);
var identifier = replace(/(?!<<0>>)<<1>>(?:\s*\.\s*<<1>>)*/.source, [nonTypeKeywords, genericName]);
var array = /\[\s*(?:,\s*)*\]/.source;
var typeExpressionWithoutTuple = replace(/<<0>>(?:\s*(?:\?\s*)?<<1>>)*(?:\s*\?)?/.source, [identifier, array]);
var tupleElement = replace(/[^,()<>[\];=+\-*/%&|^]|<<0>>|<<1>>|<<2>>/.source, [generic, nestedRound, array])
var tuple = replace(/\(<<0>>+(?:,<<0>>+)+\)/.source, [tupleElement]);
var typeExpression = replace(/(?:<<0>>|<<1>>)(?:\s*(?:\?\s*)?<<2>>)*(?:\s*\?)?/.source, [tuple, identifier, array]);
Expand Down Expand Up @@ -150,14 +151,14 @@
// Casts and checks via as and is.
// as Foo<A>, is Bar<B>
// (things like if(a is Foo b) is covered by variable declaration)
pattern: re(/(\b(?:is|as)\s+)<<0>>/.source, [typeExpression]),
pattern: re(/(\b(?:is(?:\s+not)?|as)\s+)<<0>>/.source, [typeExpressionWithoutTuple]),
lookbehind: true,
inside: typeInside
},
{
// Variable, field and parameter declaration
// (Foo bar, Bar baz, Foo[,,] bay, Foo<Bar, FooBar<Bar>> bax)
pattern: re(/\b<<0>>(?=\s+(?!<<1>>)<<2>>(?:\s*[=,;:{)\]]|\s+in))/.source, [typeExpression, nonContextualKeywords, name]),
pattern: re(/\b<<0>>(?=\s+(?!<<1>>)<<2>>(?:\s*[=,;:{)\]]|\s+(?:in|when)\b))/.source, [typeExpression, nonContextualKeywords, name]),
inside: typeInside
}
],
Expand Down
2 changes: 1 addition & 1 deletion components/prism-csharp.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ void Foo(Foo a, Bar<Foo> b, Bar[, ] c, Foo<(int, int)> d);
Bar<Foo> Abc => 0;
Bar<Foo>.FooBar<IFoo> Abc;

if (foo is Bar)
if (foo is not Bar)
if (foo is null)
if (foo is not null)
if (t is (int, string))
if ((e1, e2) is (0, int i) or (int i, 0))
var baz = foo as Bar;

----------------------------------------------------

[
Expand Down Expand Up @@ -113,6 +121,84 @@ Bar<Foo>.FooBar<IFoo> Abc;
["punctuation", ">"]
]],
" Abc",
["punctuation", ";"],

["keyword", "if"],
["punctuation", "("],
"foo ",
["keyword", "is"],
["class-name", [
"Bar"
]],
["punctuation", ")"],
["keyword", "if"],
["punctuation", "("],
"foo ",
["keyword", "is"],
["keyword", "not"],
["class-name", [
"Bar"
]],
["punctuation", ")"],
["keyword", "if"],
["punctuation", "("],
"foo ",
["keyword", "is"],
["keyword", "null"],
["punctuation", ")"],
["keyword", "if"],
["punctuation", "("],
"foo ",
["keyword", "is"],
["keyword", "not"],
["keyword", "null"],
["punctuation", ")"],
["keyword", "if"],
["punctuation", "("],
"t ",
["keyword", "is"],
["punctuation", "("],
["keyword", "int"],
["punctuation", ","],
["keyword", "string"],
["punctuation", ")"],
["punctuation", ")"],
["keyword", "if"],
["punctuation", "("],
["punctuation", "("],
"e1",
["punctuation", ","],
" e2",
["punctuation", ")"],
["keyword", "is"],
["punctuation", "("],
["number", "0"],
["punctuation", ","],
["class-name", [
["keyword", "int"]
]],
" i",
["punctuation", ")"],
["keyword", "or"],
["punctuation", "("],
["class-name", [
["keyword", "int"]
]],
" i",
["punctuation", ","],
["number", "0"],
["punctuation", ")"],
["punctuation", ")"],
["class-name", [
["keyword", "var"]
]],
" baz ",
["operator", "="],
" foo ",
["keyword", "as"],
["class-name", [
"Bar"
]],
["punctuation", ";"]
]

Expand Down
6 changes: 6 additions & 0 deletions tests/languages/csharp/keyword_feature.test
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
abstract
add
alias
and
as
ascending
async
Expand Down Expand Up @@ -54,11 +55,13 @@ long
nameof
namespace;
new;
not
notnull
null
object
on
operator
or
orderby
out
override
Expand Down Expand Up @@ -109,6 +112,7 @@ yield
["keyword", "abstract"],
["keyword", "add"],
["keyword", "alias"],
["keyword", "and"],
["keyword", "as"],
["keyword", "ascending"],
["keyword", "async"],
Expand Down Expand Up @@ -162,11 +166,13 @@ yield
["keyword", "nameof"],
["keyword", "namespace"], ["punctuation", ";"],
["keyword", "new"], ["punctuation", ";"],
["keyword", "not"],
["keyword", "notnull"],
["keyword", "null"],
["keyword", "object"],
["keyword", "on"],
["keyword", "operator"],
["keyword", "or"],
["keyword", "orderby"],
["keyword", "out"],
["keyword", "override"],
Expand Down
Loading

0 comments on commit 7f341fc

Please sign in to comment.