2011-11-21 01:52:43 +00:00
|
|
|
-- Copyright 2006-2011 Mitchell mitchell<att>caladbolg.net. See LICENSE.
|
|
|
|
-- Moonscript lexer by leaf corcoran <http://leafo.net>
|
|
|
|
|
|
|
|
local l = lexer
|
|
|
|
local token, word_match = l.token, l.word_match
|
2013-02-02 19:09:43 +00:00
|
|
|
local P, S, R = lpeg.P, lpeg.S, lpeg.R
|
2011-11-21 01:52:43 +00:00
|
|
|
|
2013-02-02 19:09:43 +00:00
|
|
|
local M = { _NAME = 'moonscript' }
|
2011-11-21 01:52:43 +00:00
|
|
|
|
|
|
|
-- Whitespace.
|
|
|
|
local ws = token(l.WHITESPACE, l.space^1)
|
|
|
|
|
|
|
|
local longstring = #('[[' + ('[' * P('=')^0 * '['))
|
|
|
|
local longstring = longstring * P(function(input, index)
|
|
|
|
local level = input:match('^%[(=*)%[', index)
|
|
|
|
if level then
|
|
|
|
local _, stop = input:find(']'..level..']', index, true)
|
|
|
|
return stop and stop + 1 or #input + 1
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
-- Comments.
|
|
|
|
local line_comment = '--' * l.nonnewline^0
|
|
|
|
local block_comment = '--' * longstring
|
|
|
|
local comment = token(l.COMMENT, block_comment + line_comment)
|
|
|
|
|
|
|
|
-- Strings.
|
|
|
|
local sq_str = l.delimited_range("'", '\\', true)
|
|
|
|
local dq_str = l.delimited_range('"', '\\', true)
|
2011-11-21 04:29:55 +00:00
|
|
|
local string = token(l.STRING, sq_str + dq_str + longstring)
|
2011-11-21 01:52:43 +00:00
|
|
|
|
|
|
|
-- Numbers.
|
|
|
|
local number = token(l.NUMBER, l.float + l.integer)
|
|
|
|
|
|
|
|
-- Keywords.
|
|
|
|
local keyword = token(l.KEYWORD, word_match {
|
|
|
|
'return', 'break', 'for', 'while',
|
|
|
|
'if', 'else', 'elseif', 'then', 'export',
|
|
|
|
'import', 'from', 'with', 'in', 'and',
|
|
|
|
'or', 'not', 'class', 'extends', 'super', 'do',
|
2011-11-21 04:29:55 +00:00
|
|
|
'using', 'switch', 'when',
|
2011-11-21 01:52:43 +00:00
|
|
|
})
|
|
|
|
|
2013-02-02 19:09:43 +00:00
|
|
|
local special = token("special", word_match { "true", "false", "nil" })
|
2011-11-21 04:29:55 +00:00
|
|
|
|
2011-11-21 01:52:43 +00:00
|
|
|
-- Functions.
|
2011-11-21 07:37:11 +00:00
|
|
|
local builtin = token(l.FUNCTION, word_match({
|
|
|
|
"_G","_VERSION","assert","collectgarbage","dofile","error","getfenv","getmetatable","ipairs","load",
|
|
|
|
"loadfile","loadstring","module","next","pairs","pcall","print","rawequal","rawget","rawset","require",
|
|
|
|
"select","setfenv","setmetatable","tonumber","tostring","type","unpack","xpcall",
|
|
|
|
|
|
|
|
"coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield",
|
|
|
|
|
|
|
|
"debug.debug","debug.getfenv","debug.gethook","debug.getinfo","debug.getlocal","debug.getmetatable",
|
|
|
|
"debug.getregistry","debug.getupvalue","debug.setfenv","debug.sethook","debug.setlocal","debug.setmetatable",
|
|
|
|
"debug.setupvalue","debug.traceback",
|
|
|
|
|
|
|
|
"io.close","io.flush","io.input","io.lines","io.open","io.output","io.popen","io.read","io.stderr","io.stdin",
|
|
|
|
"io.stdout","io.tmpfile","io.type","io.write",
|
|
|
|
|
|
|
|
"math.abs","math.acos","math.asin","math.atan","math.atan2","math.ceil","math.cos","math.cosh","math.deg",
|
|
|
|
"math.exp","math.floor","math.fmod","math.frexp","math.huge","math.ldexp","math.log","math.log10","math.max",
|
|
|
|
"math.min","math.modf","math.pi","math.pow","math.rad","math.random","math.randomseed","math.sin","math.sinh",
|
|
|
|
"math.sqrt","math.tan","math.tanh",
|
|
|
|
|
|
|
|
"os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale",
|
|
|
|
"os.time","os.tmpname",
|
|
|
|
|
|
|
|
"package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload",
|
|
|
|
"package.seeall",
|
|
|
|
|
|
|
|
"string.byte","string.char","string.dump","string.find","string.format","string.gmatch","string.gsub",
|
|
|
|
"string.len","string.lower","string.match","string.rep","string.reverse","string.sub","string.upper",
|
|
|
|
|
|
|
|
"table.concat","table.insert","table.maxn","table.remove","table.sort"
|
|
|
|
}, "%."))
|
2011-11-21 01:52:43 +00:00
|
|
|
|
|
|
|
-- Identifiers.
|
|
|
|
local identifier = token(l.IDENTIFIER, l.word)
|
|
|
|
|
2011-11-21 04:29:55 +00:00
|
|
|
local fndef = token("fndef", P"->" + P"=>")
|
|
|
|
local err = token(l.ERROR, word_match { "function", "end" })
|
|
|
|
|
2011-11-21 01:52:43 +00:00
|
|
|
-- Operators.
|
2011-11-21 04:29:55 +00:00
|
|
|
local symbol = token("symbol", S("(){}[]"))
|
|
|
|
local operator = token(l.OPERATOR, '~=' + S('+-*!\\/%^#=<>;:,.'))
|
2011-11-21 01:52:43 +00:00
|
|
|
|
|
|
|
-- self ref
|
2011-11-21 04:29:55 +00:00
|
|
|
local self_var = token("self_ref", "@" * l.word + "self")
|
2011-11-21 01:52:43 +00:00
|
|
|
|
2011-11-21 04:29:55 +00:00
|
|
|
local proper_ident = token("proper_ident", R("AZ") * l.word)
|
2011-11-21 01:52:43 +00:00
|
|
|
|
2011-11-21 04:51:19 +00:00
|
|
|
local tbl_key = token("tbl_key", l.word * ":" + ":" * l.word )
|
|
|
|
|
2013-02-02 19:09:43 +00:00
|
|
|
M._rules = {
|
2011-11-21 01:52:43 +00:00
|
|
|
{ 'whitespace', ws },
|
2011-11-21 04:29:55 +00:00
|
|
|
{ 'error', err },
|
2011-11-21 01:52:43 +00:00
|
|
|
{ 'self', self_var },
|
2011-11-21 04:29:55 +00:00
|
|
|
{ 'special', special },
|
2011-11-21 01:52:43 +00:00
|
|
|
{ 'keyword', keyword },
|
2011-11-21 04:29:55 +00:00
|
|
|
{ 'builtin', builtin },
|
2011-11-21 04:51:19 +00:00
|
|
|
{ 'identifier', proper_ident + tbl_key + identifier },
|
2011-11-21 01:52:43 +00:00
|
|
|
{ 'comment', comment },
|
|
|
|
{ 'number', number },
|
|
|
|
{ 'string', string },
|
2011-11-21 04:29:55 +00:00
|
|
|
{ 'fndef', fndef },
|
|
|
|
{ 'symbol', symbol },
|
2011-11-21 01:52:43 +00:00
|
|
|
{ 'operator', operator },
|
|
|
|
{ 'any_char', l.any_char },
|
|
|
|
}
|
|
|
|
|
2011-11-21 04:29:55 +00:00
|
|
|
local style_special = { fore = l.colors.light_blue }
|
|
|
|
local style_fndef = { fore = l.colors.green }
|
2011-11-21 01:52:43 +00:00
|
|
|
|
2013-02-02 19:09:43 +00:00
|
|
|
M._tokenstyles = {
|
2011-11-21 04:29:55 +00:00
|
|
|
{ 'self_ref', style_special },
|
|
|
|
{ 'proper_ident', l.style_class },
|
|
|
|
{ 'fndef', style_fndef },
|
|
|
|
{ 'symbol', style_fndef },
|
|
|
|
{ 'special', style_special },
|
2011-11-21 04:51:19 +00:00
|
|
|
{ 'tbl_key', { fore = l.colors.red } },
|
2011-11-21 01:52:43 +00:00
|
|
|
}
|
2013-02-02 19:09:43 +00:00
|
|
|
|
|
|
|
return M
|