1 -- Copyright 2006-2017 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Objective 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
= 'objective_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', 'error', 'if', 'ifdef',
29 'ifndef', 'import', 'include', 'line', 'pragma', 'undef',
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({
39 'asm', 'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else',
40 'extern', 'false', 'for', 'goto', 'if', 'inline', 'register', 'return',
41 'sizeof', 'static', 'switch', 'true', 'typedef', 'void', 'volatile', 'while',
42 'restrict', '_Bool', '_Complex', '_Pragma', '_Imaginary',
44 'oneway', 'in', 'out', 'inout', 'bycopy', 'byref', 'self', 'super',
45 -- Preprocessor directives.
46 '@interface', '@implementation', '@protocol', '@end', '@private',
47 '@protected', '@public', '@class', '@selector', '@encode', '@defs',
48 '@synchronized', '@try', '@throw', '@catch', '@finally',
50 'TRUE', 'FALSE', 'YES', 'NO', 'NULL', 'nil', 'Nil', 'METHOD_NULL'
54 local type = token(l
.TYPE
, word_match
{
55 'apply_t', 'id', 'Class', 'MetaClass', 'Object', 'Protocol', 'retval_t',
56 'SEL', 'STR', 'IMP', 'BOOL', 'TypedStream'
60 local identifier
= token(l
.IDENTIFIER
, l
.word
)
63 local operator
= token(l
.OPERATOR
, S('+-/*%<>!=^&|?~:;.()[]{}'))
70 {'identifier', identifier
},
74 {'operator', operator
},
78 _patterns
= {'%l+', '[{}]', '/%*', '%*/', '//'},
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('//')}