vis

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

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

hare.lua

(2762B)


      1 -- Copyright 2021-2025 Mitchell. See LICENSE.
      2 -- Hare LPeg lexer
      3 
      4 local lexer = lexer
      5 local P, R, S = lpeg.P, lpeg.R, lpeg.S
      6 
      7 local lex = lexer.new(...)
      8 
      9 -- Keywords.
     10 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
     11 
     12 -- Types.
     13 lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
     14 
     15 -- Functions.
     16 local builtin_func = lex:tag(lexer.FUNCTION_BUILTIN,
     17 	lex:word_match(lexer.FUNCTION_BUILTIN) + 'size' * #(lexer.space^0 * '('))
     18 local func = lex:tag(lexer.FUNCTION, lex:tag(lexer.FUNCTION, lexer.word * ('::' * lexer.word)^0 *
     19 	#(lexer.space^0 * '(')))
     20 lex:add_rule('function', builtin_func + func)
     21 
     22 -- Constants.
     23 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
     24 
     25 -- Identifiers.
     26 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
     27 
     28 -- Strings.
     29 local sq_str = lexer.range("'", true)
     30 local dq_str = lexer.range('"')
     31 local raw_str = lexer.range('`', false, false)
     32 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + raw_str))
     33 
     34 -- Comments.
     35 lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.to_eol('//')))
     36 
     37 -- Numbers.
     38 local bin_num = '0b' * R('01')^1 * -lexer.xdigit
     39 local oct_num = '0o' * R('07')^1 * -lexer.xdigit
     40 local hex_num = '0x' * lexer.xdigit^1
     41 local int_suffix = lexer.word_match('i u z i8 i16 i32 i64 u8 u16 u32 u64')
     42 local float_suffix = lexer.word_match('f32 f64')
     43 local suffix = int_suffix + float_suffix
     44 local integer = S('+-')^-1 *
     45 	((hex_num + oct_num + bin_num) * int_suffix^-1 + lexer.dec_num * suffix^-1)
     46 local float = lexer.float * float_suffix^-1
     47 lex:add_rule('number', lex:tag(lexer.NUMBER, integer + float))
     48 
     49 -- Error assertions
     50 lex:add_rule('error_assert', lex:tag(lexer.ERROR .. '.assert', lpeg.B(')') * P('!')))
     51 
     52 -- Operators.
     53 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%^!=&|?~:;,.()[]{}<>')))
     54 
     55 -- Attributes.
     56 lex:add_rule('attribute', lex:tag(lexer.ANNOTATION, '@' * lexer.word))
     57 
     58 -- Fold points.
     59 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     60 
     61 -- Word lists.
     62 lex:set_word_list(lexer.KEYWORD, {
     63 	'as', 'break', 'case', 'const', 'continue', 'def', 'defer', 'else', 'export', 'fn', 'for', 'if',
     64 	'is', 'let', 'match', 'nullable', 'return', 'static', 'switch', 'type', 'use', 'yield', '_'
     65 })
     66 
     67 lex:set_word_list(lexer.TYPE, {
     68 	'bool', 'enum', 'f32', 'f64', 'i16', 'i32', 'i64', 'i8', 'int', 'opaque', 'never', 'rune', 'size',
     69 	'str', 'struct', 'u16', 'u32', 'u64', 'u8', 'uint', 'uintptr', 'union', 'valist'
     70 })
     71 
     72 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
     73 	'abort', 'align', 'alloc', 'append', 'assert', 'delete', 'free', 'insert', 'len', 'offset',
     74 	'vaarg', 'vaend', 'vastart'
     75 })
     76 
     77 lex:set_word_list(lexer.CONSTANT_BUILTIN, 'done false null true void')
     78 
     79 lexer.property['scintillua.comment'] = '//'
     80 
     81 return lex