vis

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

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

caml.lua

(3098B)


      1 -- Copyright 2006-2025 Mitchell. See LICENSE.
      2 -- OCaml 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('caml')
      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 	'and', 'as', 'asr', 'begin', 'class', 'closed', 'constraint', 'do', 'done', 'downto', 'else',
     16 	'end', 'exception', 'external', 'failwith', 'false', 'flush', 'for', 'fun', 'function', 'functor',
     17 	'if', 'in', 'include', 'incr', 'inherit', 'land', 'let', 'load', 'los', 'lsl', 'lsr', 'lxor',
     18 	'match', 'method', 'mod', 'module', 'mutable', 'new', 'not', 'of', 'open', 'option', 'or',
     19 	'parser', 'private', 'raise', 'rec', 'ref', 'regexp', 'sig', 'stderr', 'stdin', 'stdout',
     20 	'struct', 'then', 'to', 'true', 'try', 'type', 'val', 'virtual', 'when', 'while', 'with'
     21 }))
     22 
     23 -- Types.
     24 lex:add_rule('type', token(lexer.TYPE, word_match('bool char float int string unit')))
     25 
     26 -- Functions.
     27 lex:add_rule('function', token(lexer.FUNCTION, word_match{
     28 	'abs', 'abs_float', 'acos', 'asin', 'atan', 'atan2', 'at_exit', 'bool_of_string', 'ceil',
     29 	'char_of_int', 'classify_float', 'close_in', 'close_in_noerr', 'close_out', 'close_out_noerr',
     30 	'compare', 'cos', 'cosh', 'decr', 'epsilon_float', 'exit', 'exp', 'failwith', 'float',
     31 	'float_of_int', 'float_of_string', 'floor', 'flush', 'flush_all', 'format_of_string', 'frexp',
     32 	'fst', 'ignore', 'in_channel_length', 'incr', 'infinity', 'input', 'input_binary_int',
     33 	'input_byte', 'input_char', 'input_line', 'input_value', 'int_of_char', 'int_of_float',
     34 	'int_of_string', 'invalid_arg', 'ldexp', 'log', 'log10', 'max', 'max_float', 'max_int', 'min',
     35 	'min_float', 'min_int', 'mod', 'modf', 'mod_float', 'nan', 'open_in', 'open_in_bin',
     36 	'open_in_gen', 'open_out', 'open_out_bin', 'open_out_gen', 'out_channel_length', 'output',
     37 	'output_binary_int', 'output_byte', 'output_char', 'output_string', 'output_value', 'pos_in',
     38 	'pos_out', 'pred', 'prerr_char', 'prerr_endline', 'prerr_float', 'prerr_int', 'prerr_newline',
     39 	'prerr_string', 'print_char', 'print_endline', 'print_float', 'print_int', 'print_newline',
     40 	'print_string', 'raise', 'read_float', 'read_int', 'read_line', 'really_input', 'seek_in',
     41 	'seek_out', 'set_binary_mode_in', 'set_binary_mode_out', 'sin', 'sinh', 'snd', 'sqrt', 'stderr',
     42 	'stdin', 'stdout', 'string_of_bool', 'string_of_float', 'string_of_format', 'string_of_int',
     43 	'succ', 'tan', 'tanh', 'truncate'
     44 }))
     45 
     46 -- Identifiers.
     47 lex:add_rule('identifier', token(lexer.IDENTIFIER, lexer.word))
     48 
     49 -- Strings.
     50 local sq_str = lexer.range("'", true)
     51 local dq_str = lexer.range('"', true)
     52 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str))
     53 
     54 -- Comments.
     55 lex:add_rule('comment', token(lexer.COMMENT, lexer.range('(*', '*)', false, false, true)))
     56 
     57 -- Numbers.
     58 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     59 
     60 -- Operators.
     61 lex:add_rule('operator', token(lexer.OPERATOR, S('=<>+-*/.,:;~!#%^&|?[](){}')))
     62 
     63 lexer.property['scintillua.comment'] = '(*|*)'
     64 
     65 return lex