enhanced json lexer for pygments it adds support for eventual data prefixing the actual json body eventual nonjson data prefix followed by actual json body fix data prefix number integer or float is not correctly handled json body enhanced json lexer for pygments it adds support for eventual data prefixing the actual json body eventual non json data prefix followed by actual json body fix data prefix number integer or float is not correctly handled json body
import re

from pygments.lexer import bygroups, using, RegexLexer
from pygments.lexers.data import JsonLexer
from pygments.token import Token

PREFIX_TOKEN = Token.Error
PREFIX_REGEX = r'[^{\["]+'


class EnhancedJsonLexer(RegexLexer):
    
    name = 'JSON'
    flags = re.IGNORECASE | re.DOTALL
    tokens = {
        'root': [
            
            
            (
                fr'({PREFIX_REGEX})' + r'((?:[{\["]|true|false|null).+)',
                bygroups(PREFIX_TOKEN, using(JsonLexer))
            ),
            
            (r'.+', using(JsonLexer)),
        ],
    }