vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

objective_c.lua

(2491B)


      1 -- Copyright 2006-2025 Mitchell. See LICENSE.
      2 -- Objective C LPeg lexer.
      3 
      4 local lexer = require('lexer')
      5 local token, word_match = lexer.token, lexer.word_match
      6 local P, S = lpeg.P, lpeg.S
      7 
      8 local lex = lexer.new('objective_c')
      9 
     10 -- Whitespace.
     11 lex:add_rule('whitespace', token(lexer.WHITESPACE, lexer.space^1))
     12 
     13 -- Keywords.
     14 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     15 	-- From C.
     16 	'asm', 'auto', 'break', 'case', 'const', 'continue', 'default', 'do', 'else', 'extern', 'false',
     17 	'for', 'goto', 'if', 'inline', 'register', 'return', 'sizeof', 'static', 'switch', 'true',
     18 	'typedef', 'void', 'volatile', 'while', 'restrict', '_Bool', '_Complex', '_Pragma', '_Imaginary',
     19 	-- Objective C.
     20 	'oneway', 'in', 'out', 'inout', 'bycopy', 'byref', 'self', 'super',
     21 	-- Preprocessor directives.
     22 	'@interface', '@implementation', '@protocol', '@end', '@private', '@protected', '@public',
     23 	'@class', '@selector', '@encode', '@defs', '@synchronized', '@try', '@throw', '@catch',
     24 	'@finally',
     25 	-- Constants.
     26 	'TRUE', 'FALSE', 'YES', 'NO', 'NULL', 'nil', 'Nil', 'METHOD_NULL'
     27 }))
     28 
     29 -- Types.
     30 lex:add_rule('type', token(lexer.TYPE, word_match(
     31 	'apply_t id Class MetaClass Object Protocol retval_t SEL STR IMP BOOL TypedStream')))
     32 
     33 -- Strings.
     34 local sq_str = P('L')^-1 * lexer.range("'", true)
     35 local dq_str = P('L')^-1 * lexer.range('"', true)
     36 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
     37 
     38 -- Identifiers.
     39 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     40 
     41 -- Comments.
     42 local line_comment = lexer.to_eol('//', true)
     43 local block_comment = lexer.range('/*', '*/')
     44 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     45 
     46 -- Numbers.
     47 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     48 
     49 -- Preprocessor.
     50 lex:add_rule('preprocessor',
     51 	#lexer.starts_line('#') * token(lexer.PREPROCESSOR, '#' * S('\t ')^0 * word_match{
     52 		'define', 'elif', 'else', 'endif', 'error', 'if', 'ifdef', 'ifndef', 'import', 'include',
     53 		'line', 'pragma', 'undef', 'warning'
     54 	}))
     55 
     56 -- Operators.
     57 lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}')))
     58 
     59 -- Fold symbols.
     60 lex:add_fold_point(lexer.PREPROCESSOR, 'region', 'endregion')
     61 lex:add_fold_point(lexer.PREPROCESSOR, 'if', 'endif')
     62 lex:add_fold_point(lexer.PREPROCESSOR, 'ifdef', 'endif')
     63 lex:add_fold_point(lexer.PREPROCESSOR, 'ifndef', 'endif')
     64 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     65 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     66 
     67 lexer.property['scintillua.comment'] = '//'
     68 
     69 return lex