ui: fall back to xterm-256color if term initialization fails
[vis.git] / lexers / xtend.lua
blob500dc336c3d8f63319a60d16f1a210ac834e3b63
1 -- Copyright (c) 2014-2015 Piotr Orzechowski [drzewo.org]. See LICENSE.
2 -- Xtend 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 = 'xtend'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local line_comment = '//' * l.nonnewline_esc^0
15 local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
16 local comment = token(l.COMMENT, line_comment + block_comment)
18 -- Strings.
19 local sq_str = l.delimited_range("'", true)
20 local dq_str = l.delimited_range('"', true)
21 local string = token(l.STRING, sq_str + dq_str)
23 -- Templates.
24 local templ_str = "'''" * (l.any - P("'''"))^0 * P("'''")^-1
25 local template = token('template', templ_str, true)
27 -- Numbers.
28 local small_suff = S('lL')
29 local med_suff = P(S('bB') * S('iI'))
30 local large_suff = S('dD') + S('fF') + P(S('bB') * S('dD'))
31 local exp = S('eE') * l.digit^1
33 local dec_inf = ('_' * l.digit^1)^0
34 local hex_inf = ('_' * l.xdigit^1)^0
35 local float_pref = l.digit^1 * '.' * l.digit^1
36 local float_suff = exp^-1 * med_suff^-1 * large_suff^-1
38 local dec = l.digit * dec_inf * (small_suff^-1 + float_suff)
39 local hex = l.hex_num * hex_inf * P('#' * (small_suff + med_suff))^-1
40 local float = float_pref * dec_inf * float_suff
42 local number = token(l.NUMBER, float + hex + dec)
44 -- Keywords.
45 local keyword = token(l.KEYWORD, word_match{
46 -- General.
47 'abstract', 'annotation', 'as', 'case', 'catch', 'class', 'create', 'def',
48 'default', 'dispatch', 'do', 'else', 'enum', 'extends', 'extension', 'final',
49 'finally', 'for', 'if', 'implements', 'import', 'interface', 'instanceof',
50 'it', 'new', 'override', 'package', 'private', 'protected', 'public',
51 'return', 'self', 'static', 'super', 'switch', 'synchronized', 'this',
52 'throw', 'throws', 'try', 'typeof', 'val', 'var', 'while',
53 -- Templates.
54 -- 'AFTER', 'BEFORE', 'ENDFOR', 'ENDIF', 'FOR', 'IF', 'SEPARATOR',
55 -- Literals.
56 'true', 'false', 'null'
59 -- Types.
60 local type = token(l.TYPE, word_match{
61 'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void',
62 'Boolean', 'Byte', 'Character', 'Double', 'Float', 'Integer', 'Long', 'Short',
63 'String'
66 -- Identifiers.
67 local identifier = token(l.IDENTIFIER, l.word)
69 -- Operators.
70 local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}#'))
72 -- Annotations.
73 local annotation = token('annotation', '@' * l.word)
75 -- Functions.
76 local func = token(l.FUNCTION, l.word) * #P('(')
78 -- Classes.
79 local class = token(l.KEYWORD, P('class')) * ws^1 * token(l.CLASS, l.word)
81 -- Rules.
82 M._rules = {
83 {'whitespace', ws},
84 {'class', class},
85 {'keyword', keyword},
86 {'type', type},
87 {'function', func},
88 {'identifier', identifier},
89 {'template', template},
90 {'string', string},
91 {'comment', comment},
92 {'number', number},
93 {'annotation', annotation},
94 {'operator', operator},
95 {'error', token(l.ERROR, l.any)},
98 -- Token styles.
99 M._tokenstyles = {
100 annotation = l.STYLE_PREPROCESSOR,
101 template = l.STYLE_EMBEDDED
104 -- Folding.
105 M._foldsymbols = {
106 _patterns = {'[{}]', '/%*', '%*/', '//', 'import'},
107 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
108 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')},
109 [l.KEYWORD] = {['import'] = l.fold_line_comments('import')}
112 return M