vis: implement :set cursorline
[vis.git] / lexers / coffeescript.lua
blobb1c649eec67afb2c9d1f5577c17f0f64c15173d3
1 -- Copyright 2006-2015 Mitchell mitchell.att.foicica.com. See LICENSE.
2 -- CoffeeScript LPeg lexer.
4 local l = require('lexer')
5 local token, word_match = l.token, l.word_match
6 local P, S = lpeg.P, lpeg.S
8 local M = {_NAME = 'coffeescript'}
10 -- Whitespace.
11 local ws = token(l.WHITESPACE, l.space^1)
13 -- Comments.
14 local block_comment = '###' * (l.any - '###')^0 * P('###')^-1
15 local line_comment = '#' * l.nonnewline_esc^0
16 local comment = token(l.COMMENT, block_comment + line_comment)
18 -- Strings.
19 local sq_str = l.delimited_range("'")
20 local dq_str = l.delimited_range('"')
21 local regex_str = #P('/') * l.last_char_includes('+-*%<>!=^&|?~:;,([{') *
22 l.delimited_range('/', true) * S('igm')^0
23 local string = token(l.STRING, sq_str + dq_str) + token(l.REGEX, regex_str)
25 -- Numbers.
26 local number = token(l.NUMBER, l.float + l.integer)
28 -- Keywords.
29 local keyword = token(l.KEYWORD, word_match{
30 'all', 'and', 'bind', 'break', 'by', 'case', 'catch', 'class', 'const',
31 'continue', 'default', 'delete', 'do', 'each', 'else', 'enum', 'export',
32 'extends', 'false', 'for', 'finally', 'function', 'if', 'import', 'in',
33 'instanceof', 'is', 'isnt', 'let', 'loop', 'native', 'new', 'no', 'not', 'of',
34 'off', 'on', 'or', 'return', 'super', 'switch', 'then', 'this', 'throw',
35 'true', 'try', 'typeof', 'unless', 'until', 'var', 'void', 'with', 'when',
36 'while', 'yes'
39 -- Fields: object properties and methods.
40 local field = token(l.FUNCTION, '.' * (S('_$') + l.alpha) *
41 (S('_$') + l.alnum)^0)
43 -- Identifiers.
44 local identifier = token(l.IDENTIFIER, l.word)
46 -- Operators.
47 local operator = token(l.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}'))
49 M._rules = {
50 {'whitespace', ws},
51 {'keyword', keyword},
52 {'field', field},
53 {'identifier', identifier},
54 {'comment', comment},
55 {'number', number},
56 {'string', string},
57 {'operator', operator},
60 M._FOLDBYINDENTATION = true
62 return M