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

javascript.lua (3619B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- JavaScript LPeg lexer.
  3. local lexer = lexer
  4. local P, S, B = lpeg.P, lpeg.S, lpeg.B
  5. local lex = lexer.new(...)
  6. -- Keywords.
  7. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)))
  8. -- Types.
  9. lex:add_rule('type', lex:tag(lexer.TYPE, lex:word_match(lexer.TYPE)))
  10. -- Functions.
  11. local builtin_func = -B('.') *
  12. lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
  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', (builtin_func + method + func) * #(lexer.space^0 * '('))
  16. -- Constants.
  17. lex:add_rule('constant', lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN)))
  18. -- Identifiers.
  19. lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
  20. -- Comments.
  21. local line_comment = lexer.to_eol('//', true)
  22. local block_comment = lexer.range('/*', '*/')
  23. lex:add_rule('comment', lex:tag(lexer.COMMENT, line_comment + block_comment))
  24. -- Strings.
  25. local sq_str = lexer.range("'")
  26. local dq_str = lexer.range('"')
  27. local bq_str = lexer.range('`')
  28. local string = lex:tag(lexer.STRING, sq_str + dq_str + bq_str)
  29. local regex_str = lexer.after_set('+-*%^!=&|?:;,([{<>', lexer.range('/', true) * S('igm')^0)
  30. local regex = lex:tag(lexer.REGEX, regex_str)
  31. lex:add_rule('string', string + regex)
  32. -- Numbers.
  33. lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number))
  34. -- Operators.
  35. lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('+-/*%^!=&|?:;,.()[]{}<>')))
  36. -- Fold points.
  37. lex:add_fold_point(lexer.OPERATOR, '{', '}')
  38. lex:add_fold_point(lexer.COMMENT, '/*', '*/')
  39. -- Word lists.
  40. lex:set_word_list(lexer.KEYWORD, {
  41. 'abstract', 'async', 'await', 'boolean', 'break', 'byte', 'case', 'catch', 'char', 'class',
  42. 'const', 'continue', 'debugger', 'default', 'delete', 'do', 'double', 'else', 'enum', 'export',
  43. 'extends', 'false', 'final', 'finally', 'float', 'for', 'function', 'get', 'goto', 'if',
  44. 'implements', 'import', 'in', 'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new',
  45. 'null', 'of', 'package', 'private', 'protected', 'public', 'return', 'set', 'short', 'static',
  46. 'super', 'switch', 'synchronized', 'this', 'throw', 'throws', 'transient', 'true', 'try',
  47. 'typeof', 'var', 'void', 'volatile', 'while', 'with', 'yield'
  48. })
  49. lex:set_word_list(lexer.TYPE, {
  50. -- Fundamental objects.
  51. 'Object', 'Function', 'Boolean', 'Symbol',
  52. -- Error Objects.
  53. 'Error', 'AggregateError', 'EvalError', 'InternalError', 'RangeError', 'ReferenceError',
  54. 'SyntaxError', 'TypeError', 'URIError',
  55. -- Numbers and dates.
  56. 'Number', 'BigInt', 'Math', 'Date',
  57. -- Text Processing.
  58. 'String', 'RegExp',
  59. -- Indexed collections.
  60. 'Array', 'Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array',
  61. 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array', 'BigInt64Array', 'BigUint64Array',
  62. -- Keyed collections.
  63. 'Map', 'Set', 'WeakMap', 'WeakSet',
  64. -- Structured data.
  65. 'ArrayBuffer', 'SharedArrayBuffer', 'Atomics', 'DataView', 'JSON',
  66. -- Control abstraction objects.
  67. 'GeneratorFunction', 'AsyncGeneratorFunction', 'Generator', 'AsyncGenerator', 'AsyncFunction',
  68. 'Promise',
  69. -- Reflection.
  70. 'Reflect', 'Proxy',
  71. -- Other.
  72. 'Intl', 'WebAssembly'
  73. })
  74. lex:set_word_list(lexer.FUNCTION_BUILTIN, {
  75. 'eval', 'isFinite', 'isNaN', 'parseFloat', 'parseInt', 'decodeURI', 'decodeURIComponent',
  76. 'encodeURI', 'encodeURIComponent'
  77. })
  78. lex:set_word_list(lexer.CONSTANT_BUILTIN, 'Infinity NaN undefined globalThis arguments')
  79. lexer.property['scintillua.comment'] = '//'
  80. return lex