1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
4 local l
= require('lexer')
5 local token
, word_match
= l
.token
, l
.word_match
6 local P
, R
, S
= lpeg
.P
, lpeg
.R
, lpeg
.S
8 local M
= {_NAME
= 'csharp'}
11 local ws
= token(l
.WHITESPACE
, l
.space^
1)
14 local line_comment
= '//' * l
.nonnewline_esc^
0
15 local block_comment
= '/*' * (l
.any
- '*/')^
0 * P('*/')^
-1
16 local comment
= token(l
.COMMENT
, line_comment
+ block_comment
)
19 local sq_str
= l
.delimited_range("'", true)
20 local dq_str
= l
.delimited_range('"', true)
21 local ml_str
= P('@')^
-1 * l
.delimited_range('"', false, true)
22 local string = token(l
.STRING
, sq_str
+ dq_str
+ ml_str
)
25 local number = token(l
.NUMBER
, (l
.float
+ l
.integer
) * S('lLdDfFMm')^
-1)
28 local preproc_word
= word_match
{
29 'define', 'elif', 'else', 'endif', 'error', 'if', 'line', 'undef', 'warning',
32 local preproc
= token(l
.PREPROCESSOR
,
33 l
.starts_line('#') * S('\t ')^
0 * preproc_word
*
34 (l
.nonnewline_esc^
1 + l
.space
* l
.nonnewline_esc^
0))
37 local keyword
= token(l
.KEYWORD
, word_match
{
38 'class', 'delegate', 'enum', 'event', 'interface', 'namespace', 'struct',
39 'using', 'abstract', 'const', 'explicit', 'extern', 'fixed', 'implicit',
40 'internal', 'lock', 'out', 'override', 'params', 'partial', 'private',
41 'protected', 'public', 'ref', 'sealed', 'static', 'readonly', 'unsafe',
42 'virtual', 'volatile', 'add', 'as', 'assembly', 'base', 'break', 'case',
43 'catch', 'checked', 'continue', 'default', 'do', 'else', 'finally', 'for',
44 'foreach', 'get', 'goto', 'if', 'in', 'is', 'new', 'remove', 'return', 'set',
45 'sizeof', 'stackalloc', 'super', 'switch', 'this', 'throw', 'try', 'typeof',
46 'unchecked', 'value', 'void', 'while', 'yield',
47 'null', 'true', 'false'
51 local type = token(l
.TYPE
, word_match
{
52 'bool', 'byte', 'char', 'decimal', 'double', 'float', 'int', 'long', 'object',
53 'operator', 'sbyte', 'short', 'string', 'uint', 'ulong', 'ushort'
57 local identifier
= token(l
.IDENTIFIER
, l
.word
)
60 local operator
= token(l
.OPERATOR
, S('~!.,:;+-*/<>=\\^|&%?()[]{}'))
66 {'identifier', identifier
},
71 {'operator', operator
},
75 _patterns
= {'%l+', '[{}]', '/%*', '%*/', '//'},
77 region
= 1, endregion
= -1,
78 ['if'] = 1, ifdef
= 1, ifndef
= 1, endif
= -1
80 [l
.OPERATOR
] = {['{'] = 1, ['}'] = -1},
81 [l
.COMMENT
] = {['/*'] = 1, ['*/'] = -1, ['//'] = l
.fold_line_comments('//')}