vis

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

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

reason.lua

(3226B)


      1 -- Copyright 2018-2025 Hugo O. Rivera. See LICENSE.
      2 -- Reason (https://reasonml.github.io/) 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('reason')
      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', 'inherit', 'incr', 'land', 'let', 'load', 'los', 'lsl', 'lsr', 'lxor',
     18 	'method', 'mod', 'module', 'mutable', 'new', 'not', 'of', 'open', 'option', 'or', 'parser',
     19 	'private', 'ref', 'rec', 'raise', 'regexp', 'sig', 'struct', 'stdout', 'stdin', 'stderr',
     20 	'switch', 'then', 'to', 'true', 'try', 'type', 'val', 'virtual', 'when', 'while', 'with'
     21 }))
     22 
     23 -- Types.
     24 lex:add_rule('type', token(lexer.TYPE, word_match('int float bool char string unit')))
     25 
     26 -- Functions.
     27 lex:add_rule('function', token(lexer.FUNCTION, word_match{
     28 	'raise', 'invalid_arg', 'failwith', 'compare', 'min', 'max', 'succ', 'pred', 'mod', 'abs',
     29 	'max_int', 'min_int', 'sqrt', 'exp', 'log', 'log10', 'cos', 'sin', 'tan', 'acos', 'asin', 'atan',
     30 	'atan2', 'cosh', 'sinh', 'tanh', 'ceil', 'floor', 'abs_float', 'mod_float', 'frexp', 'ldexp',
     31 	'modf', 'float', 'float_of_int', 'truncate', 'int_of_float', 'infinity', 'nan', 'max_float',
     32 	'min_float', 'epsilon_float', 'classify_float', 'int_of_char', 'char_of_int', 'ignore',
     33 	'string_of_bool', 'bool_of_string', 'string_of_int', 'int_of_string', 'string_of_float',
     34 	'float_of_string', 'fst', 'snd', 'stdin', 'stdout', 'stderr', 'print_char', 'print_string',
     35 	'print_int', 'print_float', 'print_endline', 'print_newline', 'prerr_char', 'prerr_string',
     36 	'prerr_int', 'prerr_float', 'prerr_endline', 'prerr_newline', 'read_line', 'read_int',
     37 	'read_float', 'open_out', 'open_out_bin', 'open_out_gen', 'flush', 'flush_all', 'output_char',
     38 	'output_string', 'output', 'output_byte', 'output_binary_int', 'output_value', 'seek_out',
     39 	'pos_out', 'out_channel_length', 'close_out', 'close_out_noerr', 'set_binary_mode_out', 'open_in',
     40 	'open_in_bin', 'open_in_gen', 'input_char', 'input_line', 'input', 'really_input', 'input_byte',
     41 	'input_binary_int', 'input_value', 'seek_in', 'pos_in', 'in_channel_length', 'close_in',
     42 	'close_in_noerr', 'set_binary_mode_in', 'incr', 'decr', 'string_of_format', 'format_of_string',
     43 	'exit', 'at_exit'
     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 local line_comment = lexer.to_eol('//')
     56 local block_comment = lexer.range('/*', '*/', false, false, true)
     57 lex:add_rule('comment', token(lexer.COMMENT, line_comment + block_comment))
     58 
     59 -- Numbers.
     60 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
     61 
     62 -- Operators.
     63 lex:add_rule('operator', token(lexer.OPERATOR, S('=<>+-*/.,:;~!#%^&|?[](){}')))
     64 
     65 lexer.property['scintillua.comment'] = '//'
     66 
     67 return lex