vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

python.lua

(6186B)


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