vis: implement :set cursorline
[vis.git] / lexers / scala.lua
blobd455996e551f8f29f2832984cc583f8de5a50aaf
1 -- Copyright 2006-2013 JMS. See LICENSE.
2 -- Scala 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 = 'scala'}
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 symbol = "'" * l.word
20 local dq_str = l.delimited_range('"', true)
21 local tq_str = '"""' * (l.any - '"""')^0 * P('"""')^-1
22 local string = token(l.STRING, tq_str + symbol + 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', 'case', 'catch', 'class', 'def', 'do', 'else', 'extends', 'false',
30 'final', 'finally', 'for', 'forSome', 'if', 'implicit', 'import', 'lazy',
31 'match', 'new', 'null', 'object', 'override', 'package', 'private',
32 'protected', 'return', 'sealed', 'super', 'this', 'throw', 'trait', 'try',
33 'true', 'type', 'val', 'var', 'while', 'with', 'yield'
36 -- Types.
37 local type = token(l.TYPE, word_match{
38 'Array', 'Boolean', 'Buffer', 'Byte', 'Char', 'Collection', 'Double', 'Float',
39 'Int', 'Iterator', 'LinkedList', 'List', 'Long', 'Map', 'None', 'Option',
40 'Set', 'Short', 'SortedMap', 'SortedSet', 'String', 'TreeMap', 'TreeSet'
43 -- Identifiers.
44 local identifier = token(l.IDENTIFIER, l.word)
46 -- Operators.
47 local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;.()[]{}'))
49 -- Functions.
50 local func = token(l.FUNCTION, l.word) * #P('(')
52 -- Classes.
53 local class_sequence = token(l.KEYWORD, P('class')) * ws^1 *
54 token(l.CLASS, l.word)
56 M._rules = {
57 {'whitespace', ws},
58 {'class', class_sequence},
59 {'keyword', keyword},
60 {'type', type},
61 {'function', func},
62 {'identifier', identifier},
63 {'string', string},
64 {'comment', comment},
65 {'number', number},
66 {'operator', operator},
69 M._foldsymbols = {
70 _patterns = {'[{}]', '/%*', '%*/', '//'},
71 [l.OPERATOR] = {['{'] = 1, ['}'] = -1},
72 [l.COMMENT] = {['/*'] = 1, ['*/'] = -1, ['//'] = l.fold_line_comments('//')}
75 return M