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

python.lua (6245B)


  1. -- Copyright 2006-2024 Mitchell. See LICENSE.
  2. -- Python LPeg lexer.
  3. local lexer = lexer
  4. local token, word_match = lexer.token, lexer.word_match
  5. local P, S, B = lpeg.P, lpeg.S, lpeg.B
  6. local lex = lexer.new(..., {fold_by_indentation = true})
  7. -- Classes.
  8. lex:add_rule('classdef', lex:tag(lexer.KEYWORD, 'class') * lex:get_rule('whitespace') *
  9. lex:tag(lexer.CLASS, lexer.word))
  10. -- Keywords.
  11. lex:add_rule('keyword', lex:tag(lexer.KEYWORD, lex:word_match(lexer.KEYWORD)) +
  12. lex:tag(lexer.KEYWORD .. '.soft', lex:word_match(lexer.KEYWORD .. '.soft')))
  13. -- Functions.
  14. local builtin_func = -B('.') *
  15. lex:tag(lexer.FUNCTION_BUILTIN, lex:word_match(lexer.FUNCTION_BUILTIN))
  16. local special_func = lex:tag(lexer.FUNCTION_BUILTIN .. '.special',
  17. lex:word_match(lexer.FUNCTION_BUILTIN .. '.special'))
  18. local func = lex:tag(lexer.FUNCTION, lexer.word)
  19. local method = B('.') * lex:tag(lexer.FUNCTION_METHOD, lexer.word)
  20. lex:add_rule('function', (builtin_func + special_func + method + func) * #(lexer.space^0 * '('))
  21. -- Constants.
  22. local builtin_const = lex:tag(lexer.CONSTANT_BUILTIN, lex:word_match(lexer.CONSTANT_BUILTIN))
  23. local attr = lex:tag(lexer.ATTRIBUTE, B('.') * lex:word_match(lexer.ATTRIBUTE) + '__name__')
  24. lex:add_rule('constant', builtin_const + attr)
  25. -- Strings.
  26. local sq_str = lexer.range("'", true)
  27. local dq_str = lexer.range('"', true)
  28. local tq_str = lexer.range("'''") + lexer.range('"""')
  29. lex:add_rule('string', lex:tag(lexer.STRING, (S('fFrRbBrR') * S('rRfFrRbB') + S('ruRUfFbB'))^-1 *
  30. (tq_str + sq_str + dq_str)))
  31. -- Identifiers.
  32. lex:add_rule('identifier', lex:tag(lexer.IDENTIFIER, lexer.word))
  33. -- Comments.
  34. lex:add_rule('comment', lex:tag(lexer.COMMENT, lexer.to_eol('#', true)))
  35. -- Numbers.
  36. lex:add_rule('number', lex:tag(lexer.NUMBER, lexer.number_('_') * S('jJ')^-1))
  37. -- Decorators.
  38. lex:add_rule('decorator', lex:tag(lexer.ANNOTATION, '@' * lexer.word))
  39. -- Operators.
  40. lex:add_rule('operator', lex:tag(lexer.OPERATOR, S('!@%^&*()[]{}-=+/|:;.,<>~')))
  41. -- Word lists.
  42. lex:set_word_list(lexer.KEYWORD, {
  43. 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif',
  44. 'else', 'except', 'False', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is',
  45. 'lambda', 'None', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'True', 'try', 'while',
  46. 'with', 'yield'
  47. })
  48. lex:set_word_list(lexer.KEYWORD .. '.soft', '_ case match')
  49. lex:set_word_list(lexer.FUNCTION_BUILTIN, {
  50. 'abs', 'aiter', 'all', 'any', 'anext', 'ascii', 'bin', 'bool', 'breakpoint', 'bytearray', 'bytes',
  51. 'callable', 'chr', 'classmethod', 'compile', 'complex', 'delattr', 'dict', 'dir', 'divmod',
  52. 'enumerate', 'eval', 'exec', 'filter', 'float', 'format', 'frozenset', 'getattr', 'globals',
  53. 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int', 'isinstance', 'issubclass', 'iter', 'len',
  54. 'list', 'locals', 'map', 'max', 'memoryview', 'min', 'next', 'object', 'oct', 'open', 'ord',
  55. 'pow', 'print', 'property', 'range', 'repr', 'reversed', 'round', 'set', 'setattr', 'slice',
  56. 'sorted', 'staticmethod', 'str', 'sum', 'super', 'tuple', 'type', 'vars', 'zip', '__import__'
  57. })
  58. lex:set_word_list(lexer.FUNCTION_BUILTIN .. '.special', {
  59. '__new__', '__init__', '__del__', '__repr__', '__str__', '__bytes', '__format__', '__lt__',
  60. '__le__', '__eq__', '__ne__', '__gt__', '__ge__', '__hash__', '__bool__', --
  61. '__getattr__', '__getattribute__', '__setattr__', '__delattr__', '__dir__', --
  62. '__get__', '__set__', '__delete__', '__slots__', --
  63. '__init_subclass__', '__set_name__', --
  64. '__instancecheck__', '__subclasscheck__', --
  65. '__class_getitem__', --
  66. '__call__', --
  67. '__len__', '__length_hint', '__getitem__', '__setitem__', '__delitem__', '__missing__',
  68. '__iter__', '__reversed__', '__contains__', --
  69. '__add__', '__sub__', '__mul__', '__matmul__', '__truediv__', '__floordiv__', '__mod__',
  70. '__divmod__', '__pow__', '__lshift__', '__rshift__', '__and__', '__xor__', '__or__', --
  71. '__radd__', '__rsub__', '__rmul__', '__rmatmul__', '__rtruediv__', '__rfloordiv__', '__rmod__',
  72. '__rdivmod__', '__rpow__', '__rlshift__', '__rrshift__', '__rand__', '__rxor__', '__ror__', --
  73. '__iadd__', '__isub__', '__imul__', '__imatmul__', '__itruediv__', '__ifloordiv__', '__imod__',
  74. '__idivmod__', '__ipow__', '__ilshift__', '__irshift__', '__iand__', '__ixor__', '__ior__', --
  75. '__neg__', '__pos__', '__abs__', '__invert__', '__complex__', '__int__', '__float__', '__index__',
  76. '__round__', '__trunc__', '__floor__', '__ceil__', --
  77. '__enter__', '__exit__', --
  78. '__match_args__', --
  79. '__await__', --
  80. '__aiter__', '__anext__', '__aenter__', '__aexit__' --
  81. })
  82. lex:set_word_list(lexer.CONSTANT_BUILTIN, {
  83. 'BaseException', 'Exception', 'Exception', 'ArithmeticError', 'BufferError', 'LookupError', --
  84. 'AssertionError', 'AttributeError', 'EOFError', 'FloatingPointError', 'GeneratorExit',
  85. 'ImportError', 'ModuleNotFoundError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
  86. 'MemoryError', 'NameError', 'NotImplementedError', 'OSError', 'OverflowError', 'RecursionError',
  87. 'ReferenceError', 'RuntimeError', 'StopIteration', 'StopAsyncIteration', 'SyntaxError',
  88. 'IndentationError', 'TabError', 'SystemError', 'SystemExit', 'TypeError', 'UnboundLocalError',
  89. 'UnicodeError', 'UnicodeEncodeError', 'UnicodeDecodeError', 'UnicodeTranslateError', 'ValueError',
  90. 'ZeroDivisionError', --
  91. 'EnvironmentError', 'IOError', 'WindowsError', --
  92. 'BlockingIOError', 'ChildProcessError', 'ConnectionError', 'BrokenPipeError',
  93. 'ConnectionAbortedError', 'ConnectionRefusedError', 'FileExistsError', 'FileNotFoundError',
  94. 'InterruptedError', 'IsADirectoryError', 'NotADirectoryError', 'PermissionError',
  95. 'ProcessLookupError', 'TimeoutError', --
  96. 'Warning', 'UserWarning', 'DeprecationWarning', 'PendingDeprecationWarning', 'SyntaxWarning',
  97. 'RuntimeWarning', 'FutureWarning', 'ImportWarning', 'UnicodeWarning', 'BytesWarning',
  98. 'ResourceWarning'
  99. })
  100. lex:set_word_list(lexer.ATTRIBUTE, {
  101. '__doc__', '__name__', '__qualname__', '__module__', '__defaults__', '__code__', '__globals__',
  102. '__dict__', '__closure__', '__annotations__', '__kwdefaults__', --
  103. '__file__', '__bases__', --
  104. '__class__', --
  105. '__self__', '__func__' --
  106. })
  107. lexer.property['scintillua.comment'] = '#'
  108. return lex