ui: make primary cursor blink even if no lua theme has been loaded
[vis.git] / lexers / csharp.lua
blob6212d371cd16fee3d64723701174a1cd3b10ee24
1 -- Copyright 2006-2016 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- C# LPeg lexer.
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'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
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)
18 -- Strings.
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)
24 -- Numbers.
25 local number = token(l.NUMBER, (l.float + l.integer) * S('lLdDfFMm')^-1)
27 -- Preprocessor.
28 local preproc_word = word_match{
29 'define', 'elif', 'else', 'endif', 'error', 'if', 'line', 'undef', 'warning',
30 'region', 'endregion'
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))
36 -- Keywords.
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'
50 -- Types.
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'
56 -- Identifiers.
57 local identifier = token(l.IDENTIFIER, l.word)
59 -- Operators.
60 local operator = token(l.OPERATOR, S('~!.,:;+-*/<>=\\^|&%?()[]{}'))
62 M._rules = {
63 {'whitespace', ws},
64 {'keyword', keyword},
65 {'type', type},
66 {'identifier', identifier},
67 {'string', string},
68 {'comment', comment},
69 {'number', number},
70 {'preproc', preproc},
71 {'operator', operator},
74 M._foldsymbols = {
75 _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
76 [l.PREPROCESSOR] = {
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('//')}
84 return M