vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
matlab.lua
(3924B)
1 -- Copyright 2006-2025 Martin Morawetz. See LICENSE.
2 -- Matlab LPeg lexer.
3 -- Based off of lexer code by Mitchell.
4
5 local lexer = lexer
6 local P, B, S = lpeg.P, lpeg.B, lpeg.S
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 -- Functions.
14 -- Note: cannot tag normal functions because indexing variables uses the same syntax.
15 local builtin_func = lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
16 lex:add_rule('function', builtin_func * #(lexer.space^0 * S('(')))
17
18 -- Variable.
19 lex:add_rule('variable', lex:tag(lexer.VARIABLE_BUILTIN, lex:word_match(lexer.VARIABLE_BUILTIN)))
20
21 -- Constants.
22 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
23
24 -- Identifiers.
25 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
26
27 -- Strings.
28 local sq_str = lexer.range("'", true)
29 local dq_str = lexer.range('"')
30 local bq_str = lexer.range('`')
31 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str + bq_str))
32
33 -- Comments.
34 local line_comment = lexer.to_eol(S('%#'))
35 local block_comment = lexer.range('%{', '%}')
36 lex:add_rule('comment', lex:tag(lexer.COMMENT, block_comment + line_comment))
37
38 -- Numbers.
39 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))
40
41 -- Operators.
42 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('!%^&*()[]{}-=+/\\|:;.,?<>~`ยด')))
43
44 -- Fold points.
45 lex:add_fold_point(lexer.KEYWORD, 'if', 'end')
46 lex:add_fold_point(lexer.KEYWORD, 'for', 'end')
47 lex:add_fold_point(lexer.KEYWORD, 'while', 'end')
48 lex:add_fold_point(lexer.KEYWORD, 'switch', 'end')
49 lex:add_fold_point(lexer.OPERATOR, '(', ')')
50 lex:add_fold_point(lexer.OPERATOR, '[', ']')
51 lex:add_fold_point(lexer.COMMENT, '%{', '%}')
52
53 -- Word lists
54 lex:set_word_list(lexer.KEYWORD, {
55 'break', 'case', 'catch', 'continue', 'do', 'else', 'elseif', 'end', 'end_try_catch',
56 'end_unwind_protect', 'endfor', 'endif', 'endswitch', 'endwhile', 'for', 'function',
57 'endfunction', 'global', 'if', 'otherwise', 'persistent', 'replot', 'return', 'static', 'switch',
58 'try', 'until', 'unwind_protect', 'unwind_protect_cleanup', 'varargin', 'varargout', 'while'
59 })
60
61 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
62 'abs', 'any', 'argvatan2', 'axes', 'axis', 'ceil', 'cla', 'clear', 'clf', 'columns', 'cos',
63 'delete', 'diff', 'disp', 'doc', 'double', 'drawnow', 'exp', 'figure', 'find', 'fix', 'floor',
64 'fprintf', 'gca', 'gcf', 'get', 'grid', 'help', 'hist', 'hold', 'isempty', 'isnull', 'length',
65 'load', 'log', 'log10', 'loglog', 'max', 'mean', 'median', 'min', 'mod', 'ndims', 'numel',
66 'num2str', 'ones', 'pause', 'plot', 'printf', 'quit', 'rand', 'randn', 'rectangle', 'rem',
67 'repmat', 'reshape', 'round', 'rows', 'save', 'semilogx', 'semilogy', 'set', 'sign', 'sin',
68 'size', 'sizeof', 'size_equal', 'sort', 'sprintf', 'squeeze', 'sqrt', 'std', 'strcmp', 'subplot',
69 'sum', 'tan', 'tic', 'title', 'toc', 'uicontrol', 'who', 'xlabel', 'ylabel', 'zeros'
70 })
71
72 lex:set_word_list(lexer.VARIABLE_BUILTIN, {
73 'ans', 'automatic_replot', 'default_return_value', 'do_fortran_indexing',
74 'define_all_return_values', 'empty_list_elements_ok', 'eps', 'gnuplot_binary',
75 'ignore_function_time_stamp', 'implicit_str_to_num_ok', 'ok_to_lose_imaginary_part',
76 'output_max_field_width', 'output_precision', 'page_screen_output', 'prefer_column_vectors',
77 'prefer_zero_one_indexing', 'print_answer_id_name', 'print_empty_dimensions',
78 'resize_on_range_error', 'return_last_computed_value', 'save_precision', 'silent_functions',
79 'split_long_rows', 'suppress_verbose_help_message', 'treat_neg_dim_as_zero',
80 'warn_assign_as_truth_value', 'warn_comma_in_global_decl', 'warn_divide_by_zero',
81 'warn_function_name_clash', 'whitespace_in_literal_matrix'
82 })
83
84 lex:set_word_list(lexer.CONSTANT_BUILTIN, {
85 'false', 'Inf', 'inf', 'NaN', 'nan', 'pi', 'realmax', 'realmin', 'true'
86 })
87
88 lexer.property['scintillua.comment'] = '%'
89
90 return lex