Skip to content

Commit

Permalink
Add more keyword operators
Browse files Browse the repository at this point in the history
  • Loading branch information
dkearns committed Dec 21, 2021
1 parent 4e4e287 commit ea87d35
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 21 deletions.
35 changes: 25 additions & 10 deletions syntax/cs.vim
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ set cpoptions&vim

syn keyword csType bool byte char decimal double float int long object sbyte short string T uint ulong ushort var void dynamic
syn keyword csType nint nuint " contextual
syn keyword csStorage delegate enum interface namespace struct
syn keyword csStorage enum interface namespace struct
syn match csStorage "\<delegate\>"
syn keyword csRepeat break continue do for foreach goto return while
syn keyword csConditional else if switch
syn keyword csConditional else if
syn match csCondtional "\<switch\>"
syn keyword csLabel case default
syn match csOperatorError display +::+
syn match csGlobal display +global::+
Expand Down Expand Up @@ -54,7 +56,7 @@ syn match csStatement "\<fixed\ze\_s*("
syn keyword csStatement lock
syn match csStatement "\<yield\ze\_s\+\%(return\|break\)\>"

syn keyword csUnspecifiedStatement as base in is nameof operator out params ref sizeof stackalloc this using
syn keyword csUnspecifiedStatement as base in is operator out params ref this using
syn keyword csUnsupportedStatement value
syn keyword csUnspecifiedKeyword explicit implicit

Expand All @@ -63,10 +65,22 @@ syn match csContextualStatement "\<\%(get\|set\|init\|add\|remove\)\ze\_s*\%([;{
syn match csContextualStatement "\<where\>\ze[^:]\+:"

" Operators
syn keyword csTypeOf typeof nextgroup=csTypeOfOperand,csTypeOfError skipwhite skipempty
syn region csTypeOfOperand matchgroup=csParens start="(" end=")" contained contains=csType
syn match csTypeOfError "[^([:space:]]" contained
syn keyword csKeywordOperator stackalloc
syn match csKeywordOperator "\<\%(checked\|unchecked\)\ze\_s*("
syn match csKeywordOperator "\<delegate\ze\_s*[({]"
syn match csKeywordOperator "\<switch\ze\_s*{"
syn match csKeywordOperator "\<with\ze\_s*{"

syn keyword csKeywordOperator typeof nextgroup=csTypeOfOperand,csMissingParenError skipwhite skipempty
syn region csTypeOfOperand matchgroup=csParens start="(" end=")" contained contains=csType

syn keyword csKeywordOperator sizeof nextgroup=csSizeOfOperand,csMissingParenError skipwhite skipempty
syn region csSizeOfOperand matchgroup=csParens start="(" end=")" contained contains=csType

syn keyword csKeywordOperator nameof nextgroup=csNameOfOperand,csMissingParenError skipwhite skipempty
syn region csNameOfOperand matchgroup=csParens start="(" end=")" contained contains=csType

syn match csMissingParenError "[^([:space:]]" contained

" Punctuation
syn match csBraces "[{}\[\]]" display
Expand Down Expand Up @@ -193,7 +207,7 @@ syn region csInterVerbString matchgroup=csQuote start=+$@"+ start=+@$"+ end=+"+
syn region csBracketed matchgroup=csParens start=+(+ end=+)+ extend contained transparent contains=@csAll,csBraced,csBracketed
syn region csBraced matchgroup=csParens start=+{+ end=+}+ extend contained transparent contains=@csAll,csBraced,csBracketed

syn cluster csAll contains=csCharacter,csClassType,@csComment,csContextualStatement,csEndColon,csIsType,csLabel,csLogicSymbols,csNewType,csBoolean,csConstant,@csNumber,csOpSymbols,csOperatorError,csParens,csPreCondit,csRegion,csString,csSummary,csType,csUnicodeNumber,csUnicodeSpecifier,csInterpolatedString,csVerbatimString,csInterVerbString,csUserType,csUserIdentifier,csUserInterface,csUserMethod
syn cluster csAll contains=csCharacter,csClassType,@csComment,csContextualStatement,csEndColon,csIsType,csLabel,csLogicSymbols,csNewType,csBoolean,csConstant,@csNumber,csOpSymbols,csKeywordOperator,csOperatorError,csParens,csPreCondit,csRegion,csString,csSummary,csType,csUnicodeNumber,csUnicodeSpecifier,csInterpolatedString,csVerbatimString,csInterVerbString,csUserType,csUserIdentifier,csUserInterface,csUserMethod

" Keyword identifiers
syn match csIdentifier "@\h\w*"
Expand All @@ -216,7 +230,7 @@ hi def link csUnsupportedStatement Statement
hi def link csUnspecifiedKeyword Keyword
hi def link csNew Statement
hi def link csLinq Statement
hi def link csIsAs Keyword
hi def link csIsAs csKeywordOperator
hi def link csAsyncModifier csModifier
hi def link csContextualStatement Statement

Expand All @@ -227,9 +241,10 @@ hi def link csBlockComment csComment

hi def link csKeywordOperator Keyword
hi def link csAsyncOperator csKeywordOperator
hi def link csTypeOf csKeywordOperator
hi def link csTypeOfOperand Typedef
hi def link csTypeOfError Error
hi def link csSizeOfOperand Typedef
hi def link csNameOfOperand Typedef
hi def link csMissingParenError Error
hi def link csOpSymbols Operator
hi def link csLogicSymbols Operator
hi def link csOperatorError Error
Expand Down
98 changes: 87 additions & 11 deletions test/operators.vader
Original file line number Diff line number Diff line change
Expand Up @@ -77,32 +77,82 @@ Given cs (typeof operator):
typeof(Dictionary<string,List<Image>>)

Execute:
AssertEqual 'csTypeOf', SyntaxAt(1, 1)
AssertEqual 'csParens', SyntaxAt(1, 7)
AssertEqual 'csTypeOfOperand', SyntaxAt(1, 8)
AssertEqual 'csParens', SyntaxAt(1, 38)
AssertEqual 'csKeywordOperator', SyntaxOf('typeof')
AssertEqual 'csParens', SyntaxOf('(')
AssertEqual 'csTypeOfOperand', SyntaxOf('(\zs')
AssertEqual 'csParens', SyntaxOf(')')

Given cs (typeof operator with space before open paren):
typeof (Dictionary<string,List<Image>>)

Execute:
AssertEqual 'csTypeOf', SyntaxAt(1, 1)
AssertEqual 'csParens', SyntaxAt(1, 8)
AssertEqual 'csTypeOfOperand', SyntaxAt(1, 9)
AssertEqual 'csParens', SyntaxAt(1, 39)
AssertEqual 'csKeywordOperator', SyntaxOf('typeof')
AssertEqual 'csParens', SyntaxOf('(')
AssertEqual 'csTypeOfOperand', SyntaxOf('(\zs')
AssertEqual 'csParens', SyntaxOf(')')

Given cs (typeof operator with missing operand parens):
typeof Dictionary<string,List<Image>>

Execute:
AssertEqual 'csTypeOf', SyntaxAt(1, 1)
AssertEqual 'csTypeOfError', SyntaxAt(1, 8)
AssertEqual 'csKeywordOperator', SyntaxOf('typeof')
AssertEqual 'csMissingParenError', SyntaxAt(1, 8)

Given cs (sizeof operator):
sizeof(byte)

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('sizeof')
AssertEqual 'csParens', SyntaxOf('(')
AssertEqual 'csType', SyntaxOf('(\zs')
AssertEqual 'csParens', SyntaxOf(')')

Given cs (sizeof operator with space before open paren):
sizeof (byte)

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('sizeof')
AssertEqual 'csParens', SyntaxOf('(')
AssertEqual 'csType', SyntaxOf('(\zs')
AssertEqual 'csParens', SyntaxOf(')')

Given cs (sizeof operator with missing operand parens):
sizeof byte

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('sizeof')
AssertEqual 'csMissingParenError', SyntaxOf('b\zeyte')

Given cs (nameof operator):
nameof(foobar)

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('nameof')
AssertEqual 'csParens', SyntaxOf('(')
AssertEqual 'csNameOfOperand', SyntaxOf('(\zs')
AssertEqual 'csParens', SyntaxOf(')')

Given cs (nameof operator with space before open paren):
nameof (foobar)

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('nameof')
AssertEqual 'csParens', SyntaxOf('(')
AssertEqual 'csNameOfOperand', SyntaxOf('(\zs')
AssertEqual 'csParens', SyntaxOf(')')

Given cs (nameof operator with missing operand parens):
nameof foobar

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('nameof')
AssertEqual 'csMissingParenError', SyntaxOf('f\zeoobar')

Given cs (await operator):
await foobar();

Execute:
AssertEqual 'csAsyncOperator', SyntaxAt(1, 1)
AssertEqual 'csAsyncOperator', SyntaxOf('await')

Given cs (checked operator):
var foobar = checked(42 + 1);
Expand All @@ -116,3 +166,29 @@ Given cs (unchecked operator):
Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('unchecked')

Given cs (stackalloc operator):
Span<byte> foobar = stackalloc byte[42];

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('stackalloc')

Given cs (switch operator):
var foo = bar switch { _ => 42 };

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('switch')

Given cs (delegate operator):
Func<byte> foobar = delegate (byte a) { return 42; };
Action foobar = delegate { FortyTwo(); };

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('delegate', 1)
AssertEqual 'csKeywordOperator', SyntaxOf('delegate', 2)

Given cs (with operator):
var foo = bar with { Baz = 42 };

Execute:
AssertEqual 'csKeywordOperator', SyntaxOf('with')

0 comments on commit ea87d35

Please sign in to comment.