logo

oasis-root

Compiled tree of Oasis Linux based on own branch at <https://hacktivis.me/git/oasis/> git clone https://anongit.hacktivis.me/git/oasis-root.git

moonscript.lua (5387B)


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