ui: fall back to xterm-256color if term initialization fails
[vis.git] / lexers / caml.lua
blobb7013062000dfdc15f2ffd93e8fcf0b5bed242e2
1 -- Copyright 2006-2015 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- OCaml LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, R, S = lpeg.P, lpeg.R, lpeg.S
8 local M = {_NAME = 'caml'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local comment = token(l.COMMENT, l.nested_pair('(*', '*)'))
16 -- Strings.
17 local sq_str = l.delimited_range("'", true)
18 local dq_str = l.delimited_range('"', true)
19 local string = token(l.STRING, sq_str + dq_str)
21 -- Numbers.
22 local number = token(l.NUMBER, l.float + l.integer)
24 -- Keywords.
25 local keyword = token(l.KEYWORD, word_match{
26 'and', 'as', 'asr', 'begin', 'class', 'closed', 'constraint', 'do', 'done',
27 'downto', 'else', 'end', 'exception', 'external', 'failwith', 'false',
28 'flush', 'for', 'fun', 'function', 'functor', 'if', 'in', 'include',
29 'inherit', 'incr', 'land', 'let', 'load', 'los', 'lsl', 'lsr', 'lxor',
30 'match', 'method', 'mod', 'module', 'mutable', 'new', 'not', 'of', 'open',
31 'option', 'or', 'parser', 'private', 'ref', 'rec', 'raise', 'regexp', 'sig',
32 'struct', 'stdout', 'stdin', 'stderr', 'then', 'to', 'true', 'try', 'type',
33 'val', 'virtual', 'when', 'while', 'with'
36 -- Types.
37 local type = token(l.TYPE, word_match{
38 'int', 'float', 'bool', 'char', 'string', 'unit'
41 -- Functions.
42 local func = token(l.FUNCTION, word_match{
43 'raise', 'invalid_arg', 'failwith', 'compare', 'min', 'max', 'succ', 'pred',
44 'mod', 'abs', 'max_int', 'min_int', 'sqrt', 'exp', 'log', 'log10', 'cos',
45 'sin', 'tan', 'acos', 'asin', 'atan', 'atan2', 'cosh', 'sinh', 'tanh', 'ceil',
46 'floor', 'abs_float', 'mod_float', 'frexp', 'ldexp', 'modf', 'float',
47 'float_of_int', 'truncate', 'int_of_float', 'infinity', 'nan', 'max_float',
48 'min_float', 'epsilon_float', 'classify_float', 'int_of_char', 'char_of_int',
49 'ignore', 'string_of_bool', 'bool_of_string', 'string_of_int',
50 'int_of_string', 'string_of_float', 'float_of_string', 'fst', 'snd', 'stdin',
51 'stdout', 'stderr', 'print_char', 'print_string', 'print_int', 'print_float',
52 'print_endline', 'print_newline', 'prerr_char', 'prerr_string', 'prerr_int',
53 'prerr_float', 'prerr_endline', 'prerr_newline', 'read_line', 'read_int',
54 'read_float', 'open_out', 'open_out_bin', 'open_out_gen', 'flush',
55 'flush_all', 'output_char', 'output_string', 'output', 'output_byte',
56 'output_binary_int', 'output_value', 'seek_out', 'pos_out',
57 'out_channel_length', 'close_out', 'close_out_noerr', 'set_binary_mode_out',
58 'open_in', 'open_in_bin', 'open_in_gen', 'input_char', 'input_line', 'input',
59 'really_input', 'input_byte', 'input_binary_int', 'input_value', 'seek_in',
60 'pos_in', 'in_channel_length', 'close_in', 'close_in_noerr',
61 'set_binary_mode_in', 'incr', 'decr', 'string_of_format', 'format_of_string',
62 'exit', 'at_exit'
65 -- Identifiers.
66 local identifier = token(l.IDENTIFIER, l.word)
68 -- Operators.
69 local operator = token(l.OPERATOR, S('=<>+-*/.,:;~!#%^&|?[](){}'))
71 M._rules = {
72 {'whitespace', ws},
73 {'keyword', keyword},
74 {'type', type},
75 {'function', func},
76 {'identifier', identifier},
77 {'string', string},
78 {'comment', comment},
79 {'number', number},
80 {'operator', operator},
83 return M