ui: make primary cursor blink even if no lua theme has been loaded
[vis.git] / lexers / ansi_c.lua
blob5c8e007699bb70f93607b781e116498ec286c0ae
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 = 'ansi_c'}
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 = P('L')^-1 * l.delimited_range("'", true)
20 local dq_str = P('L')^-1 * l.delimited_range('"', true)
21 local string = token(l.STRING, sq_str + dq_str)
23 -- Numbers.
24 local number = token(l.NUMBER, l.float + l.integer)
26 -- Preprocessor.
27 local preproc_word = word_match{
28 'define', 'elif', 'else', 'endif', 'if', 'ifdef', 'ifndef', 'include', 'line',
29 'pragma', 'undef'
31 local preproc = token(l.PREPROCESSOR,
32 l.starts_line('#') * S('\t ')^0 * preproc_word)
34 -- Keywords.
35 local keyword = token(l.KEYWORD, word_match{
36 'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else',
37 'extern', 'for', 'goto', 'if', 'inline', 'register', 'restrict', 'return',
38 'sizeof', 'static', 'switch', 'typedef', 'volatile', 'while'
41 -- Types.
42 local type = token(l.TYPE, word_match{
43 'char', 'double', 'enum', 'float', 'int', 'long', 'short', 'signed', 'struct',
44 'union', 'unsigned', 'void', '_Bool', '_Complex', '_Imaginary'
47 -- Identifiers.
48 local identifier = token(l.IDENTIFIER, l.word)
50 -- Operators.
51 local operator = token(l.OPERATOR, S('+-/*%<>~!=^&|?~:;,.()[]{}'))
53 M._rules = {
54 {'whitespace', ws},
55 {'keyword', keyword},
56 {'type', type},
57 {'identifier', identifier},
58 {'string', string},
59 {'comment', comment},
60 {'number', number},
61 {'preproc', preproc},
62 {'operator', operator},
65 M._foldsymbols = {
66 _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
67 [l.PREPROCESSOR] = {['if'] = 1, ifdef = 1, ifndef = 1, endif = -1},
68 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
69 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
72 return M