vis

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

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

rust.lua

(3380B)


      1 -- Copyright 2015-2025 Alejandro Baez (https://keybase.io/baez). See LICENSE.
      2 -- Rust LPeg lexer.
      3 
      4 local lexer = lexer
      5 local P, S = lpeg.P, lpeg.S
      6 local C, Cmt = lpeg.C, lpeg.Cmt
      7 
      8 local lex = lexer.new(...)
      9 
     10 -- Keywords.
     11 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
     12 
     13 -- Library types.
     14 lex:add_rule('library', lex:tag(lexer.TYPE, lexer.upper * (lexer.lower + lexer.dec_num)^1))
     15 
     16 -- Types.
     17 lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
     18 
     19 -- Lifetime annotation.
     20 lex:add_rule('lifetime', lex:tag(lexer.OPERATOR, S('<&') * P("'")))
     21 
     22 -- Strings.
     23 local sq_str = P('b')^-1 * lexer.range("'", true)
     24 local dq_str = P('b')^-1 * lexer.range('"')
     25 local raw_str = Cmt(P('b')^-1 * P('r') * C(P('#')^0) * '"', function(input, index, hashes)
     26 	local _, e = input:find('"' .. hashes, index, true)
     27 	return (e or #input) + 1
     28 end)
     29 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + raw_str))
     30 
     31 -- Functions.
     32 local builtin_macros = lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN) * '!')
     33 local macros = lex:tag(lexer.FUNCTION, lexer.word * '!')
     34 local func = lex:tag(lexer.FUNCTION, lexer.word)
     35 lex:add_rule('function', (builtin_macros + macros + func) * #(lexer.space^0 * '('))
     36 
     37 -- Identifiers.
     38 local identifier = P('r#')^-1 * lexer.word
     39 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, identifier))
     40 
     41 -- Comments.
     42 local line_comment = lexer.to_eol('//', true)
     43 local block_comment = lexer.range('/*', '*/', false, false, true)
     44 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
     45 
     46 -- Numbers.
     47 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number_('_')))
     48 
     49 -- Attributes.
     50 lex:add_rule('preprocessor', lex:tag(lexer.PREPROCESSOR, '#' * lexer.range('[', ']', true)))
     51 
     52 -- Operators.
     53 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%<>!=`^~@&|?#~:;,.()[]{}')))
     54 
     55 -- Fold points.
     56 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
     57 lex:add_fold_point(lexer.OPERATOR, '(', ')')
     58 lex:add_fold_point(lexer.OPERATOR, '{', '}')
     59 
     60 -- https://doc.rust-lang.org/std/#keywords
     61 lex:set_word_list(lexer.KEYWORD, {
     62 	'SelfTy', 'as', 'async', 'await', 'break', 'const', 'continue', 'crate', 'dyn', 'else', 'enum',
     63 	'extern', 'false', 'fn', 'for', 'if', 'impl', 'in', 'let', 'loop', 'match', 'mod', 'move', 'mut',
     64 	'pub', 'ref', 'return', 'self', 'static', 'struct', 'super', 'trait', 'true', 'type', 'union',
     65 	'unsafe', 'use', 'where', 'while'
     66 })
     67 
     68 -- https://doc.rust-lang.org/std/#primitives
     69 lex:set_word_list(lexer.TYPE, {
     70 	'never', 'array', 'bool', 'char', 'f32', 'f64', 'fn', 'i8', 'i16', 'i32', 'i64', 'i128', 'isize',
     71 	'pointer', 'reference', 'slice', 'str', 'tuple', 'u8', 'u16', 'u32', 'u64', 'u128', 'unit',
     72 	'usize'
     73 })
     74 
     75 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
     76 	'assert', 'assert_eq', 'assert_ne', 'cfg', 'column', 'compile_error', 'concat', 'dbg',
     77 	'debug_assert', 'debug_assert_eq', 'debug_assert_ne', 'env', 'eprint', 'eprintln', 'file',
     78 	'format', 'format_args', 'include', 'include_bytes', 'include_str', 'line', 'matches',
     79 	'module_path', 'option_env', 'panic', 'print', 'println', 'stringify', 'thread_local', 'todo',
     80 	'unimplemented', 'unreachable', 'vec', 'write', 'writeln',
     81 	-- Experimental
     82 	'concat_bytes', 'concat_idents', 'const_format_args', 'format_args_nl', 'log_syntax',
     83 	'trace_macros',
     84 	-- Deprecated
     85 	'try'
     86 })
     87 
     88 lexer.property['scintillua.comment'] = '//'
     89 
     90 return lex