vis
a vi-like editor based on Plan 9's structural regular expressions
git clone https://9o.is/git/vis.git
lua.lua
(5691B)
1 -- Copyright 2006-2025 Mitchell. See LICENSE.
2 -- Lua LPeg lexer.
3 -- Original written by Peter Odding, 2007/04/04.
4
5 local lexer = lexer
6 local B, P, S = lpeg.B, lpeg.P, lpeg.S
7
8 local lex = lexer.new(...)
9
10 -- Keywords.
11 lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
12
13 -- Functions.
14 local non_field = -B('.') + B('_G.') + B('..')
15 local builtin_func = lex:word_match(lexer.FUNCTION_BUILTIN)
16 local lib_func = lex:word_match(lexer.FUNCTION_BUILTIN .. '.library')
17 local func = lex:tag(lexer.FUNCTION, lexer.word)
18 local method = B(':') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
19 lex:add_rule('function',
20 method + ((non_field * lex:tag(lexer.FUNCTION_BUILTIN, builtin_func + lib_func)) + func) *
21 #(lexer.space^0 * S('({\'"')))
22
23 -- Constants.
24 local builtin_const = lex:word_match(lexer.CONSTANT_BUILTIN)
25 local lib_const = lex:word_match(lexer.CONSTANT_BUILTIN .. '.library')
26 lex:add_rule('constant', non_field * lex:tag(lexer.CONSTANT_BUILTIN, builtin_const + lib_const))
27
28 -- Identifiers.
29 lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
30
31 -- Strings.
32 local sq_str = lexer.range("'")
33 local dq_str = lexer.range('"')
34 local longstring = lpeg.Cmt('[' * lpeg.C(P('=')^0) * '[', function(input, index, eq)
35 local _, e = input:find(']' .. eq .. ']', index, true)
36 return (e or #input) + 1
37 end)
38 lex:add_rule('string', lex:tag(lexer.STRING, sq_str + dq_str) +
39 lex:tag(lexer.STRING .. '.longstring', longstring))
40
41 -- Comments.
42 local line_comment = lexer.to_eol('--')
43 local block_comment = '--' * longstring
44 lex:add_rule('comment', lex:tag(lexer.COMMENT, block_comment + line_comment))
45
46 -- Numbers.
47 local lua_integer = P('-')^-1 * (lexer.hex_num + lexer.dec_num)
48 lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.float + lua_integer))
49
50 -- Labels.
51 lex:add_rule('label', lex:tag(lexer.LABEL, '::' * lexer.word * '::'))
52
53 -- Attributes.
54 lex:add_rule('attribute', lex:tag(lexer.ATTRIBUTE, '<' * lexer.space^0 *
55 lexer.word_match('const close') * lexer.space^0 * '>'))
56
57 -- Operators.
58 lex:add_rule('operator', lex:tag(lexer.OPERATOR, '..' + S('+-*/%^#=<>&|~;:,.{}[]()')))
59
60 -- Fold points.
61 local function fold_longcomment(text, pos, line, s, symbol)
62 if symbol == '[' then
63 if line:find('^%[=*%[', s) then return 1 end
64 elseif symbol == ']' then
65 if line:find('^%]=*%]', s) then return -1 end
66 end
67 return 0
68 end
69 lex:add_fold_point(lexer.KEYWORD, 'if', 'end')
70 lex:add_fold_point(lexer.KEYWORD, 'do', 'end')
71 lex:add_fold_point(lexer.KEYWORD, 'function', 'end')
72 lex:add_fold_point(lexer.KEYWORD, 'repeat', 'until')
73 lex:add_fold_point(lexer.COMMENT, '[', fold_longcomment)
74 lex:add_fold_point(lexer.COMMENT, ']', fold_longcomment)
75 lex:add_fold_point(lexer.FUNCTION .. '.longstring', '[', ']')
76 lex:add_fold_point(lexer.OPERATOR, '(', ')')
77 lex:add_fold_point(lexer.OPERATOR, '[', ']')
78 lex:add_fold_point(lexer.OPERATOR, '{', '}')
79
80 -- Word lists.
81 lex:set_word_list(lexer.KEYWORD, {
82 'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'if', 'in', 'local',
83 'or', 'nil', 'not', 'repeat', 'return', 'then', 'true', 'until', 'while', --
84 'goto' -- 5.2
85 })
86
87 lex:set_word_list(lexer.FUNCTION_BUILTIN, {
88 'assert', 'collectgarbage', 'dofile', 'error', 'getmetatable', 'ipairs', 'load', 'loadfile',
89 'next', 'pairs', 'pcall', 'print', 'rawequal', 'rawget', 'rawset', 'require', 'select',
90 'setmetatable', 'tonumber', 'tostring', 'type', 'xpcall', --
91 'rawlen', -- 5.2
92 'warn' -- 5.4
93 })
94
95 lex:set_word_list(lexer.FUNCTION_BUILTIN .. '.library', {
96 'coroutine.create', 'coroutine.resume', 'coroutine.running', 'coroutine.status', 'coroutine.wrap',
97 'coroutine.yield', --
98 'coroutine.isyieldable', -- 5.3
99 'coroutine.close', -- 5.4
100 'package.loadlib', --
101 'package.searchpath', -- 5.2
102 'utf8.char', 'utf8.codepoint', 'utf8.codes', 'utf8.len', 'utf8.offset', -- 5.3
103 'string.byte', 'string.char', 'string.dump', 'string.find', 'string.format', 'string.gmatch',
104 'string.gsub', 'string.len', 'string.lower', 'string.match', 'string.rep', 'string.reverse',
105 'string.sub', 'string.upper', --
106 'string.pack', 'string.packsize', 'string.unpack', -- 5.3
107 'table.concat', 'table.insert', 'table.remove', 'table.sort', --
108 'table.pack', 'table.unpack', -- 5.2
109 'table.move', -- 5.3
110 'math.abs', 'math.acos', 'math.asin', 'math.atan', 'math.ceil', 'math.cos', 'math.deg',
111 'math.exp', 'math.floor', 'math.fmod', 'math.log', 'math.max', 'math.min', 'math.modf',
112 'math.rad', 'math.random', 'math.randomseed', 'math.sin', 'math.sqrt', 'math.tan', --
113 'math.tointeger', 'math.type', 'math.ult', -- 5.3
114 'io.close', 'io.flush', 'io.input', 'io.lines', 'io.open', 'io.output', 'io.popen', 'io.read',
115 'io.tmpfile', 'io.type', 'io.write', --
116 'os.clock', 'os.date', 'os.difftime', 'os.execute', 'os.exit', 'os.getenv', 'os.remove',
117 'os.rename', 'os.setlocale', 'os.time', 'os.tmpname', --
118 'debug', 'debug.debug', 'debug.gethook', 'debug.getinfo', 'debug.getlocal', 'debug.getmetatable',
119 'debug.getregistry', 'debug.getupvalue', 'debug.sethook', 'debug.setlocal', 'debug.setmetatable',
120 'debug.setupvalue', 'debug.traceback', --
121 'debug.getuservalue', 'debug.setuservalue', 'debug.upvalueid', 'debug.upvaluejoin' -- 5.2
122 })
123
124 lex:set_word_list(lexer.CONSTANT_BUILTIN, {
125 '_G', '_VERSION', --
126 '_ENV' -- 5.2
127 })
128
129 lex:set_word_list(lexer.CONSTANT_BUILTIN .. '.library', {
130 'coroutine', --
131 'package', 'package.cpath', 'package.loaded', 'package.path', 'package.preload', --
132 'package.config', 'package.searchers', -- 5.2
133 'utf8', 'utf8.charpattern', -- 5.3
134 'string', --
135 'table', --
136 'math', 'math.huge', 'math.pi', --
137 'math.maxinteger', 'math.mininteger', -- 5.3
138 'io', 'io.stderr', 'io.stdin', 'io.stdout', --
139 'os'
140 })
141
142 lexer.property['scintillua.comment'] = '--'
143
144 return lex