default_context = [
    ('comment', '#'),
    ('dict', '{'),
    ('string raw multi single', "[buBU]?[rR]'''"),
    ('string raw single', "[buBU]?[rR]'"),
    ('string raw multi double', '[buBU]?[rR]"""'),
    ('string raw double', '[buBU]?[rR]"'),
    ('string multi single', "[buBU]?'''"),
    ('string single', "[buBU]?'"),
    ('string multi double', '[buBU]?"""'),
    ('string double', '[buBU]?"'),
    ('string raw format multi single', "(?:[fF][rR]|[rR][fF])'''"),
    ('string raw format single', "(?:[fF][rR]|[rR][fF])'"),
    ('string raw format multi double', '(?:[fF][rR]|[rR][fF])"""'),
    ('string raw format double', '(?:[fF][rR]|[rR][fF])"'),
    ('string format multi single', "[fF]'''"),
    ('string format single', "[fF]'"),
    ('string format multi double', '[fF]"""'),
    ('string format double', '[fF]"'),
]

default_key = ('default', 'dict', 'format')

syntax = {
    'formats': {
        'builtin': '{syntax_builtin}',
        'comment': '{syntax_comment}',
        'keyword': '{syntax_keyword}',
        'number': '{syntax_number}',
        'decorator': '{syntax_decorator}',
        'string raw single': '{syntax_string}',
        'string raw multi single': '{syntax_string}',
        'string raw double': '{syntax_string}',
        'string raw multi double': '{syntax_string}',
        'string single': '{syntax_string}',
        'string multi single': '{syntax_string}',
        'string double': '{syntax_string}',
        'string multi double': '{syntax_string}',
        'string raw format single': '{syntax_string}',
        'string raw format multi single': '{syntax_string}',
        'string raw format double': '{syntax_string}',
        'string raw format multi double': '{syntax_string}',
        'string format single': '{syntax_string}',
        'string format multi single': '{syntax_string}',
        'string format double': '{syntax_string}',
        'string format multi double': '{syntax_string}',
        'special': '{syntax_special}',
    },

    'contexts': [
        ('default', default_context, True),
        ('dict', [(None, '}')] + default_context, True),
        ('format', default_context + [
            (None,
             r'(?:![rs])?(?::([^}]?[&lt;&gt;=^])?[ +-]?#?0?[0-9]*(?:\.[0-9]+)?[bcdeEfFgGnosxX%]?)?\s*\}',
             'special')
        ], True),
        ('comment', [(None, '\n')]),
        ('string raw multi single', [(None, "'''")], True),
        ('string raw single', [(None, "'")]),
        ('string raw multi double', [(None, '"""')], True),
        ('string raw double', [(None, '"')]),
        ('string multi single', [(None, "'''")], True),
        ('string single', [(None, "'")]),
        ('string multi double', [(None, '"""')], True),
        ('string double', [(None, '"')]),
        ('string raw format multi single', [(None, "'''"), ('#', "{{"), ('format', "{", 'special')], True),
        ('string raw format single', [(None, "'"), ('#', "{{"), ('format', "{", 'special')]),
        ('string raw format multi double', [(None, '"""'), ('#', "{{"), ('format', "{", 'special')], True),
        ('string raw format double', [(None, '"'), ('#', "{{"), ('format', "{", 'special')]),
        ('string format multi single', [(None, "'''"), ('#', "{{"), ('format', "{", 'special')], True),
        ('string format single', [(None, "'"), ('#', "{{"), ('format', "{", 'special')]),
        ('string format multi double', [(None, '"""'), ('#', "{{"), ('format', "{", 'special')], True),
        ('string format double', [(None, '"'), ('#', "{{"), ('format', "{", 'special')]),
    ],

    'tokens': {
        default_key: [
            ('hexnumber', '(0x)([0-9a-fA-F])+?'),
            ('number', r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?[jJ]?|0x[0-9a-f]+'),
            ('keyword', [
                'and',
                'as',
                'assert',
                'break',
                'class',
                'continue',
                'def',
                'del',
                'elif',
                'else',
                'except',
                'finally',
                'for',
                'from',
                'global',
                'if',
                'import',
                'in',
                'is',
                'lambda',
                'nonlocal',
                'not',
                'or',
                'pass',
                'raise',
                'return',
                'try',
                'while',
                'with',
                'yield'
            ], '(^|[\x08\\W])', '(?:[\x08\\W]|$)'),
            ('builtin', [
                'ArithmeticError',
                'AssertionError',
                'AttributeError',
                'BaseException',
                'BufferError',
                'BytesWarning',
                'DeprecationWarning',
                'EOFError',
                'Ellipsis',
                'EnvironmentError',
                'Exception',
                'False',
                'FloatingPointError',
                'FutureWarning',
                'GeneratorExit',
                'IOError',
                'ImportError',
                'ImportWarning',
                'IndentationError',
                'IndexError',
                'KeyError',
                'KeyboardInterrupt',
                'LookupError',
                'MemoryError',
                'NameError',
                'None',
                'NotImplemented',
                'NotImplementedError',
                'OSError',
                'OverflowError',
                'PendingDeprecationWarning',
                'ReferenceError',
                'ResourceWarning',
                'RuntimeError',
                'RuntimeWarning',
                'StopIteration',
                'SyntaxError',
                'SyntaxWarning',
                'SystemError',
                'SystemExit',
                'TabError',
                'True',
                'TypeError',
                'UnboundLocalError',
                'UnicodeDecodeError',
                'UnicodeEncodeError',
                'UnicodeError',
                'UnicodeTranslateError',
                'UnicodeWarning',
                'UserWarning',
                'ValueError',
                'Warning',
                'ZeroDivisionError',
                '__import__',
                '__name__',
                '__file__',
                'abs',
                'all',
                'any',
                'ascii',
                'bin',
                'bool',
                'bytearray',
                'bytes',
                'callable',
                'chr',
                'classmethod',
                'compile',
                'complex',
                'copyright',
                'credits',
                'delattr',
                'dict',
                'dir',
                'divmod',
                'enumerate',
                'eval',
                'exec',
                'exit',
                'filter',
                'float',
                'format',
                'frozenset',
                'getattr',
                'globals',
                'hasattr',
                'hash',
                'help',
                'hex',
                'id',
                'input',
                'int',
                'isinstance',
                'issubclass',
                'iter',
                'len',
                'license',
                'list',
                'locals',
                'map',
                'max',
                'memoryview',
                'min',
                'next',
                'object',
                'oct',
                'open',
                'ord',
                'pow',
                'print',
                'property',
                'quit',
                'range',
                'repr',
                'reversed',
                'round',
                'self',
                'set',
                'setattr',
                'slice',
                'sorted',
                'staticmethod',
                'str',
                'sum',
                'super',
                'tuple',
                'type',
                'vars',
                'zip'
            ], '([^\\.\\w]|^)', '(?:[\x08\\W]|$)'),
            ('decorator', r'^\s*@[A-Za-z_][A-Za-z_0-9]*'),
            ('ident', '[A-Za-z_][A-Za-z_0-9]*')
        ],
        ('string multi single', 'string single', 'string multi double', 'string double',
         'string format multi single', 'string format single', 'string format multi double', 'string format double'): [
            ('special', r'\\(["\'abfnrtv]|[0-7]{1,3}|'
                        r'x[0-9A-Fa-f]{2}|u[0-9A-Fa-f]{4}|U[0-9A-Fa-f]{8}|N\{[a-zA-Z0-9\- ]+\}|\s*$)')
        ]
    }
}

