vis: implement :set cursorline
[vis.git] / lexers / java.lua
bloba94ae6dc24c7832a63a10fb9b94e545c58285e6d
1 -- Copyright 2006-2015 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- Java LPeg lexer.
3 -- Modified by Brian Schott.
5 local l = require('lexer')
6 local token, word_match = l.token, l.word_match
7 local P, R, S = lpeg.P, lpeg.R, lpeg.S
9 local M = {_NAME = 'java'}
11 -- Whitespace.
12 local ws = token(l.WHITESPACE, l.space^1)
14 -- Comments.
15 local line_comment = '//' * l.nonnewline_esc^0
16 local block_comment = '/*' * (l.any - '*/')^0 * P('*/')^-1
17 local comment = token(l.COMMENT, line_comment + block_comment)
19 -- Strings.
20 local sq_str = l.delimited_range("'", true)
21 local dq_str = l.delimited_range('"', true)
22 local string = token(l.STRING, sq_str + dq_str)
24 -- Numbers.
25 local number = token(l.NUMBER, (l.float + l.integer) * S('LlFfDd')^-1)
27 -- Keywords.
28 local keyword = token(l.KEYWORD, word_match{
29 'abstract', 'assert', 'break', 'case', 'catch', 'class', 'const', 'continue',
30 'default', 'do', 'else', 'enum', 'extends', 'final', 'finally', 'for', 'goto',
31 'if', 'implements', 'import', 'instanceof', 'interface', 'native', 'new',
32 'package', 'private', 'protected', 'public', 'return', 'static', 'strictfp',
33 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient',
34 'try', 'while', 'volatile',
35 -- Literals.
36 'true', 'false', 'null'
39 -- Types.
40 local type = token(l.TYPE, word_match{
41 'boolean', 'byte', 'char', 'double', 'float', 'int', 'long', 'short', 'void',
42 'Boolean', 'Byte', 'Character', 'Double', 'Float', 'Integer', 'Long', 'Short',
43 'String'
46 -- Identifiers.
47 local identifier = token(l.IDENTIFIER, l.word)
49 -- Operators.
50 local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}'))
52 -- Annotations.
53 local annotation = token('annotation', '@' * l.word)
55 -- Functions.
56 local func = token(l.FUNCTION, l.word) * #P('(')
58 -- Classes.
59 local class_sequence = token(l.KEYWORD, P('class')) * ws^1 *
60 token(l.CLASS, l.word)
62 M._rules = {
63 {'whitespace', ws},
64 {'class', class_sequence},
65 {'keyword', keyword},
66 {'type', type},
67 {'function', func},
68 {'identifier', identifier},
69 {'string', string},
70 {'comment', comment},
71 {'number', number},
72 {'annotation', annotation},
73 {'operator', operator},
76 M._tokenstyles = {
77 annotation = l.STYLE_PREPROCESSOR
80 M._foldsymbols = {
81 _patterns = {'[{}]', '/%*', '%*/', '//'},
82 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
83 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
86 return M