|
| 1 | +-- Copyright 2006-2011 Mitchell mitchell<att>caladbolg.net. See LICENSE. |
| 2 | +-- Moonscript lexer by leaf corcoran <http://leafo.net> |
| 3 | + |
| 4 | +local l = lexer |
| 5 | +local token, word_match = l.token, l.word_match |
| 6 | +local P, S, R = l.lpeg.P, l.lpeg.S, l.lpeg.R |
| 7 | + |
| 8 | +module(...) |
| 9 | + |
| 10 | +-- Whitespace. |
| 11 | +local ws = token(l.WHITESPACE, l.space^1) |
| 12 | + |
| 13 | +local longstring = #('[[' + ('[' * P('=')^0 * '[')) |
| 14 | +local longstring = longstring * P(function(input, index) |
| 15 | + local level = input:match('^%[(=*)%[', index) |
| 16 | + if level then |
| 17 | + local _, stop = input:find(']'..level..']', index, true) |
| 18 | + return stop and stop + 1 or #input + 1 |
| 19 | + end |
| 20 | +end) |
| 21 | + |
| 22 | +-- Comments. |
| 23 | +local line_comment = '--' * l.nonnewline^0 |
| 24 | +local block_comment = '--' * longstring |
| 25 | +local comment = token(l.COMMENT, block_comment + line_comment) |
| 26 | + |
| 27 | +-- Strings. |
| 28 | +local sq_str = l.delimited_range("'", '\\', true) |
| 29 | +local dq_str = l.delimited_range('"', '\\', true) |
| 30 | +local regex_str = l.delimited_range('/', '\\', nil, nil, '\n') * S('igm')^0 |
| 31 | +local string = token(l.STRING, sq_str + dq_str) + P(function(input, index) |
| 32 | + if index == 1 then return index end |
| 33 | + local i = index |
| 34 | + while input:sub(i - 1, i - 1):match('[ \t\r\n\f]') do i = i - 1 end |
| 35 | + return input:sub(i - 1, i - 1):match('[+%-*%%^!=&|?:;,()%[%]{}]') and index |
| 36 | +end) * token('regex', regex_str) + token('longstring', longstring) |
| 37 | + |
| 38 | +-- Numbers. |
| 39 | +local number = token(l.NUMBER, l.float + l.integer) |
| 40 | + |
| 41 | +-- Keywords. |
| 42 | +local keyword = token(l.KEYWORD, word_match { |
| 43 | + 'return', 'break', 'for', 'while', |
| 44 | + 'if', 'else', 'elseif', 'then', 'export', |
| 45 | + 'import', 'from', 'with', 'in', 'and', |
| 46 | + 'or', 'not', 'class', 'extends', 'super', 'do', |
| 47 | + 'true', 'false', 'nil', 'using', 'switch', 'when', |
| 48 | +}) |
| 49 | + |
| 50 | +-- Functions. |
| 51 | +local func = token(l.FUNCTION, word_match { |
| 52 | + 'assert', 'collectgarbage', 'dofile', 'error', 'getfenv', 'getmetatable', |
| 53 | + 'ipairs', 'load', 'loadfile', 'loadstring', 'module', 'next', 'pairs', |
| 54 | + 'pcall', 'print', 'rawequal', 'rawget', 'rawset', 'require', 'setfenv', |
| 55 | + 'setmetatable', 'tonumber', 'tostring', 'type', 'unpack', 'xpcall' |
| 56 | +}) |
| 57 | + |
| 58 | +-- Identifiers. |
| 59 | +local identifier = token(l.IDENTIFIER, l.word) |
| 60 | + |
| 61 | +-- Operators. |
| 62 | +local operator = token(l.OPERATOR, '~=' + S('+-*!\\/%^#=<>;:,.{}[]()')) |
| 63 | + |
| 64 | +-- self ref |
| 65 | +local self_var = token('self_ref', "@" * l.word + "self") |
| 66 | + |
| 67 | +local proper_ident = token('proper_ident', R("AZ") * l.word) |
| 68 | + |
| 69 | +_rules = { |
| 70 | + { 'whitespace', ws }, |
| 71 | + { 'self', self_var }, |
| 72 | + { 'keyword', keyword }, |
| 73 | + { 'function', func }, |
| 74 | + { 'identifier', proper_ident + identifier }, |
| 75 | + { 'comment', comment }, |
| 76 | + { 'number', number }, |
| 77 | + { 'string', string }, |
| 78 | + { 'operator', operator }, |
| 79 | + { 'any_char', l.any_char }, |
| 80 | +} |
| 81 | + |
| 82 | +local pink = l.color("ED", "4E", "78") |
| 83 | + |
| 84 | +_tokenstyles = { |
| 85 | + { 'regex', l.style_string..{ back = l.color('44', '44', '44')} }, |
| 86 | + { 'longstring', l.style_string }, |
| 87 | + { 'self_ref', { fore = l.colors.purple } }, |
| 88 | + { 'proper_ident', { fore = pink, bold = true } }, |
| 89 | + { l.OPERATOR, { fore = l.colors.red, bold = true } }, |
| 90 | + { l.FUNCTION, { fore = l.colors.orange } }, |
| 91 | +} |
0 commit comments