vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
coffeescript.lua
(1811B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- CoffeeScript LPeg lexer.
3
4 local lexer = require('lexer')
5 local word_match = lexer.word_match
6 local P, S = lpeg.P, lpeg.S
7
8 local lex = lexer.new('coffeescript', {fold_by_indentation = true})
9
10 -- Whitespace.
11 lex:add_rule('whitespace', lex:tag(lexer.WHITESPACE, lexer.space^1))
12
13 -- Keywords.
14 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, word_match{
15 'all', 'and', 'bind', 'break', 'by', 'case', 'catch', 'class', 'const', 'continue', 'default',
16 'delete', 'do', 'each', 'else', 'enum', 'export', 'extends', 'false', 'finally', 'for',
17 'function', 'if', 'import', 'in', 'instanceof', 'is', 'isnt', 'let', 'loop', 'native', 'new',
18 'no', 'not', 'of', 'off', 'on', 'or', 'return', 'super', 'switch', 'then', 'this', 'throw',
19 'true', 'try', 'typeof', 'unless', 'until', 'var', 'void', 'when', 'while', 'with', 'yes'
20 }))
21
22 -- Fields: object properties and methods.
23 lex:add_rule('field',
24 lex:tag(lexer.FUNCTION, '.' * (S('_$') + lexer.alpha) * (S('_$') + lexer.alnum)^0))
25
26 -- Identifiers.
27 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
28
29 -- Strings.
30 local sq_str = lexer.range("'")
31 local dq_str = lexer.range('"')
32 local string = lex:tag(lexer.STRING, sq_str + dq_str)
33 local regex_str = lexer.after_set('+-*%<>!=^&|?~:;,([{', lexer.range('/', true) * S('igm')^0)
34 local regex = lex:tag(lexer.REGEX, regex_str)
35 lex:add_rule('string', string + regex)
36
37 -- Comments.
38 local block_comment = lexer.range('###')
39 local line_comment = lexer.to_eol('#', true)
40 lex:add_rule('comment', lex:tag(lexer.COMMENT, block_comment + line_comment))
41
42 -- Numbers.
43 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))
44
45 -- Operators.
46 lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%<>!=^&|?~:;,.()[]{}')))
47
48 lexer.property['scintillua.comment'] = '#'
49
50 return lex