vis: implement :set cursorline
[vis.git] / lexers / cpp.lua
blob3d64c93ca0d2378962900c54f034a62c65fdc3d7
1 -- Copyright 2006-2015 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 = 'cpp'}
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', 'error', 'if', 'ifdef', 'ifndef', 'import',
29 'include', 'line', 'pragma', 'undef', 'using', 'warning'
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 'asm', 'auto', 'break', 'case', 'catch', 'class', 'const', 'const_cast',
37 'continue', 'default', 'delete', 'do', 'dynamic_cast', 'else', 'explicit',
38 'export', 'extern', 'false', 'for', 'friend', 'goto', 'if', 'inline',
39 'mutable', 'namespace', 'new', 'operator', 'private', 'protected', 'public',
40 'register', 'reinterpret_cast', 'return', 'sizeof', 'static', 'static_cast',
41 'switch', 'template', 'this', 'throw', 'true', 'try', 'typedef', 'typeid',
42 'typename', 'using', 'virtual', 'volatile', 'while',
43 -- Operators
44 'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not', 'not_eq', 'or', 'or_eq',
45 'xor', 'xor_eq',
46 -- C++11
47 'alignas', 'alignof', 'constexpr', 'decltype', 'final', 'noexcept',
48 'override', 'static_assert', 'thread_local'
51 -- Types.
52 local type = token(l.TYPE, word_match{
53 'bool', 'char', 'double', 'enum', 'float', 'int', 'long', 'short', 'signed',
54 'struct', 'union', 'unsigned', 'void', 'wchar_t',
55 -- C++11
56 'char16_t', 'char32_t', 'nullptr'
59 -- Identifiers.
60 local identifier = token(l.IDENTIFIER, l.word)
62 -- Operators.
63 local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}'))
65 M._rules = {
66 {'whitespace', ws},
67 {'keyword', keyword},
68 {'type', type},
69 {'identifier', identifier},
70 {'string', string},
71 {'comment', comment},
72 {'number', number},
73 {'preproc', preproc},
74 {'operator', operator},
77 M._foldsymbols = {
78 _patterns = {'%l+', '[{}]', '/%*', '%*/', '//'},
79 [l.PREPROCESSOR] = {
80 region = 1, endregion = -1,
81 ['if'] = 1, ifdef = 1, ifndef = 1, endif = -1
83 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
84 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
87 return M