vis

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

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

nemerle.lua

(2395B)


      1 -- Copyright 2006-2025 Mitchell. See LICENSE.
      2 -- Nemerle 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('nemerle')
      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 	'_', 'abstract', 'and', 'array', 'as', 'base', 'catch', 'class', 'def', 'do', 'else', 'extends',
     16 	'extern', 'finally', 'foreach', 'for', 'fun', 'if', 'implements', 'in', 'interface', 'internal',
     17 	'lock', 'macro', 'match', 'module', 'mutable', 'namespace', 'new', 'out', 'override', 'params',
     18 	'private', 'protected', 'public', 'ref', 'repeat', 'sealed', 'static', 'struct', 'syntax', 'this',
     19 	'throw', 'try', 'type', 'typeof', 'unless', 'until', 'using', 'variant', 'virtual', 'when',
     20 	'where', 'while',
     21 	-- Values.
     22 	'null', 'true', 'false'
     23 }))
     24 
     25 -- Types.
     26 lex:add_rule('type', token(lexer.TYPE, word_match{
     27 	'bool', 'byte', 'char', 'decimal', 'double', 'float', 'int', 'list', 'long', 'object', 'sbyte',
     28 	'short', 'string', 'uint', 'ulong', 'ushort', 'void'
     29 }))
     30 
     31 -- Strings.
     32 local sq_str = P('L')^-1 * lexer.range("'", true)
     33 local dq_str = P('L')^-1 * lexer.range('"', true)
     34 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
     35 
     36 -- Identifiers.
     37 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     38 
     39 -- Comments.
     40 local line_comment = lexer.to_eol('//', true)
     41 local block_comment = lexer.range('/*', '*/')
     42 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     43 
     44 -- Numbers.
     45 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     46 
     47 -- Preprocessor.
     48 lex:add_rule('preproc', token(lexer.PREPROCESSOR, lexer.starts_line('#') * S('\t ')^0 * word_match{
     49 	'define', 'elif', 'else', 'endif', 'endregion', 'error', 'if', 'ifdef', 'ifndef', 'line',
     50 	'pragma', 'region', 'undef', 'using', 'warning'
     51 }))
     52 
     53 -- Operators.
     54 lex:add_rule('operator', token(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}')))
     55 
     56 -- Fold points.
     57 lex:add_fold_point(lexer.PREPROCESSOR, 'region', 'endregion')
     58 lex:add_fold_point(lexer.PREPROCESSOR, 'if', 'endif')
     59 lex:add_fold_point(lexer.PREPROCESSOR, 'ifdef', 'endif')
     60 lex:add_fold_point(lexer.PREPROCESSOR, 'ifndef', 'endif')
     61 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     62 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     63 
     64 lexer.property['scintillua.comment'] = '//'
     65 
     66 return lex