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

lua.lua (5752B)


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