vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
javascript.lua
(3586B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- JavaScript LPeg lexer.
3
4 local lexer = lexer
5 local P, S, B = lpeg.P, lpeg.S, lpeg.B
6
7 local lex = lexer.new(...)
8
9 -- Keywords.
10 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
11
12 -- Types.
13 lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
14
15 -- Functions.
16 local builtin_func = -B('.') *
17 lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
18 local func = lex:tag(lexer.FUNCTION, lexer.word)
19 local method = B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
20 lex:add_rule('function', (builtin_func + method + func) * #(lexer.space^0 * '('))
21
22 -- Constants.
23 lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
24
25 -- Identifiers.
26 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
27
28 -- Comments.
29 local line_comment = lexer.to_eol('//', true)
30 local block_comment = lexer.range('/*', '*/')
31 lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
32
33 -- Strings.
34 local sq_str = lexer.range("'")
35 local dq_str = lexer.range('"')
36 local bq_str = lexer.range('`')
37 local string = lex:tag(lexer.STRING, sq_str + dq_str + bq_str)
38 local regex_str = lexer.after_set('+-*%^!=&|?:;,([{<>', lexer.range('/', true) * S('igm')^0)
39 local regex = lex:tag(lexer.REGEX, regex_str)
40 lex:add_rule('string', string + regex)
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 -- Fold points.
49 lex:add_fold_point(lexer.OPERATOR, '{', '}')
50 lex:add_fold_point(lexer.COMMENT, '/*', '*/')
51
52 -- Word lists.
53 lex:set_word_list(lexer.KEYWORD, {
54 'abstract', 'async', 'await', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class',
55 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export',
56 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'get', 'goto', 'if',
57 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new',
58 'null', 'of', 'package', 'private', 'protected', 'public', 'return', 'set', 'short', 'static',
59 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try',
60 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield'
61 })
62
63 lex:set_word_list(lexer.TYPE, {
64 -- Fundamental objects.
65 'Object', 'Function', 'Boolean', 'Symbol',
66 -- Error Objects.
67 'Error', 'AggregateError', 'EvalError', 'InternalError', 'RangeError', 'ReferenceError',
68 'SyntaxError', 'TypeError', 'URIError',
69 -- Numbers and dates.
70 'Number', 'BigInt', 'Math', 'Date',
71 -- Text Processing.
72 'String', 'RegExp',
73 -- Indexed collections.
74 'Array', 'Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array',
75 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array', 'BigInt64Array', 'BigUint64Array',
76 -- Keyed collections.
77 'Map', 'Set', 'WeakMap', 'WeakSet',
78 -- Structured data.
79 'ArrayBuffer', 'SharedArrayBuffer', 'Atomics', 'DataView', 'JSON',
80 -- Control abstraction objects.
81 'GeneratorFunction', 'AsyncGeneratorFunction', 'Generator', 'AsyncGenerator', 'AsyncFunction',
82 'Promise',
83 -- Reflection.
84 'Reflect', 'Proxy',
85 -- Other.
86 'Intl', 'WebAssembly'
87 })
88
89 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
90 'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'decodeURI', 'decodeURIComponent',
91 'encodeURI', 'encodeURIComponent'
92 })
93
94 lex:set_word_list(lexer.CONSTANT_BUILTIN, 'Infinity NaN undefined globalThis arguments')
95
96 lexer.property['scintillua.comment'] = '//'
97
98 return lex