1 -- Copyright 2006-2015 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
= 'ansi_c'}
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
= 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
)
24 local number = token(l
.NUMBER
, l
.float
+ l
.integer
)
27 local preproc_word
= word_match
{
28 'define', 'elif', 'else', 'endif', 'if', 'ifdef', 'ifndef', 'include', 'line',
31 local preproc
= token(l
.PREPROCESSOR
,
32 l
.starts_line('#') * S('\t ')^
0 * preproc_word
)
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'
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'
48 local identifier
= token(l
.IDENTIFIER
, l
.word
)
51 local operator
= token(l
.OPERATOR
, S('+-/*%<>~!=^&|?~:;,.()[]{}'))
57 {'identifier', identifier
},
62 {'operator', operator
},
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('//')}