vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

moonscript.lua

(5316B)


      1 -- Copyright 2016-2025 Alejandro Baez (https://keybase.io/baez). See LICENSE.
      2 -- Moonscript LPeg lexer.
      3 
      4 local lexer = require('lexer')
      5 local token, word_match = lexer.token, lexer.word_match
      6 local P, S = lpeg.P, lpeg.S
      7 
      8 local lex = lexer.new('moonscript', {fold_by_indentation = true})
      9 
     10 -- Whitespace.
     11 lex:add_rule('whitspace', token(lexer.WHITESPACE, lexer.space^1))
     12 
     13 -- Table keys.
     14 lex:add_rule('tbl_key', token('tbl_key', lexer.word * ':' + ':' * lexer.word))
     15 lex:add_style('tbl_key', lexer.styles.regex)
     16 
     17 -- Keywords.
     18 lex:add_rule('keyword', token(lexer.KEYWORD, word_match{
     19 	-- Lua.
     20 	'and', 'break', 'do', 'else', 'elseif', 'false', 'for', 'if', 'in', 'local', 'nil', 'not', 'or',
     21 	'return', 'then', 'true', 'while',
     22 	-- Moonscript.
     23 	'continue', 'class', 'export', 'extends', 'from', 'import', 'super', 'switch', 'unless', 'using',
     24 	'when', 'with'
     25 }))
     26 
     27 -- Error words.
     28 lex:add_rule('error', token(lexer.ERROR, word_match('function end')))
     29 
     30 -- Self reference.
     31 lex:add_rule('self_ref', token('self_ref', '@' * lexer.word^-1 + 'self'))
     32 lex:add_style('self_ref', lexer.styles.label)
     33 
     34 -- Functions.
     35 lex:add_rule('function', token(lexer.FUNCTION, word_match{
     36 	'assert', 'collectgarbage', 'dofile', 'error', 'getmetatable', 'ipairs', 'load', 'loadfile',
     37 	'next', 'pairs', 'pcall', 'print', 'rawequal', 'rawget', 'rawset', 'require', 'select',
     38 	'setmetatable', 'tonumber', 'tostring', 'type', 'xpcall',
     39 	-- Added in 5.2.
     40 	'rawlen'
     41 }))
     42 
     43 -- Constants.
     44 lex:add_rule('constant', token(lexer.CONSTANT, word_match{
     45 	'_G', '_VERSION',
     46 	-- Added in 5.2.
     47 	'_ENV'
     48 }))
     49 
     50 -- Libraries.
     51 lex:add_rule('library', token(lexer.FUNCTION_BUILTIN, word_match{
     52 	-- Coroutine.
     53 	'coroutine', 'coroutine.create', 'coroutine.resume', 'coroutine.running', 'coroutine.status',
     54 	'coroutine.wrap', 'coroutine.yield',
     55 	-- Coroutine added in 5.3.
     56 	'coroutine.isyieldable',
     57 	-- Module.
     58 	'package', 'package.cpath', 'package.loaded', 'package.loadlib', 'package.path',
     59 	'package.preload',
     60 	-- Module added in 5.2.
     61 	'package.config', 'package.searchers', 'package.searchpath',
     62 	-- UTF-8 added in 5.3.
     63 	'utf8', 'utf8.char', 'utf8.charpattern', 'utf8.codepoint', 'utf8.codes', 'utf8.len',
     64 	'utf8.offset',
     65 	-- String.
     66 	'string', 'string.byte', 'string.char', 'string.dump', 'string.find', 'string.format',
     67 	'string.gmatch', 'string.gsub', 'string.len', 'string.lower', 'string.match', 'string.rep',
     68 	'string.reverse', 'string.sub', 'string.upper',
     69 	-- String added in 5.3.
     70 	'string.pack', 'string.packsize', 'string.unpack',
     71 	-- Table.
     72 	'table', 'table.concat', 'table.insert', 'table.remove', 'table.sort',
     73 	-- Table added in 5.2.
     74 	'table.pack', 'table.unpack',
     75 	-- Table added in 5.3.
     76 	'table.move',
     77 	-- Math.
     78 	'math', 'math.abs', 'math.acos', 'math.asin', 'math.atan', 'math.ceil', 'math.cos', 'math.deg',
     79 	'math.exp', 'math.floor', 'math.fmod', 'math.huge', 'math.log', 'math.max', 'math.min',
     80 	'math.modf', 'math.pi', 'math.rad', 'math.random', 'math.randomseed', 'math.sin', 'math.sqrt',
     81 	'math.tan',
     82 	-- Math added in 5.3.
     83 	'math.maxinteger', 'math.mininteger', 'math.tointeger', 'math.type', 'math.ult',
     84 	-- IO.
     85 	'io', 'io.close', 'io.flush', 'io.input', 'io.lines', 'io.open', 'io.output', 'io.popen',
     86 	'io.read', 'io.stderr', 'io.stdin', 'io.stdout', 'io.tmpfile', 'io.type', 'io.write',
     87 	-- OS.
     88 	'os', 'os.clock', 'os.date', 'os.difftime', 'os.execute', 'os.exit', 'os.getenv', 'os.remove',
     89 	'os.rename', 'os.setlocale', 'os.time', 'os.tmpname',
     90 	-- Debug.
     91 	'debug', 'debug.debug', 'debug.gethook', 'debug.getinfo', 'debug.getlocal', 'debug.getmetatable',
     92 	'debug.getregistry', 'debug.getupvalue', 'debug.sethook', 'debug.setlocal', 'debug.setmetatable',
     93 	'debug.setupvalue', 'debug.traceback',
     94 	-- Debug added in 5.2.
     95 	'debug.getuservalue', 'debug.setuservalue', 'debug.upvalueid', 'debug.upvaluejoin',
     96 
     97 	-- MoonScript 0.3.1 standard library.
     98 	-- Printing functions.
     99 	'p',
    100 	-- Table functions.
    101 	'run_with_scope', 'defaultbl', 'extend', 'copy',
    102 	-- Class/object functions.
    103 	'is_object', 'bind_methods', 'mixin', 'mixin_object', 'mixin_table',
    104 	-- Misc functions.
    105 	'fold',
    106 	-- Debug functions.
    107 	'debug.upvalue'
    108 }))
    109 
    110 -- Identifiers.
    111 local identifier = token(lexer.IDENTIFIER, lexer.word)
    112 local proper_ident = token('proper_ident', lexer.upper * lexer.word)
    113 lex:add_rule('identifier', proper_ident + identifier)
    114 lex:add_style('proper_ident', lexer.styles.class)
    115 
    116 -- Strings.
    117 local sq_str = lexer.range("'", false, false)
    118 local dq_str = lexer.range('"', false, false)
    119 local longstring = lpeg.Cmt('[' * lpeg.C(P('=')^0) * '[', function(input, index, eq)
    120 	local _, e = input:find(']' .. eq .. ']', index, true)
    121 	return (e or #input) + 1
    122 end)
    123 lex:add_rule('string', token(lexer.STRING, sq_str + dq_str) + token(lexer.STRING, longstring))
    124 
    125 -- Comments.
    126 local line_comment = lexer.to_eol('--')
    127 local block_comment = '--' * longstring
    128 lex:add_rule('comment', token(lexer.COMMENT, block_comment + line_comment))
    129 
    130 -- Numbers.
    131 lex:add_rule('number', token(lexer.NUMBER, lexer.number))
    132 
    133 -- Function definition.
    134 lex:add_rule('fndef', token('fndef', P('->') + '=>'))
    135 lex:add_style('fndef', lexer.styles.preprocessor)
    136 
    137 -- Operators.
    138 lex:add_rule('operator', token(lexer.OPERATOR, S('+-*!\\/%^#=<>;:,.')))
    139 lex:add_rule('symbol', token('symbol', S('(){}[]')))
    140 lex:add_style('symbol', lexer.styles.embedded)
    141 
    142 lexer.property['scintillua.comment'] = '--'
    143 
    144 return lex