1""" 
    2    pygments.lexers.hexdump 
    3    ~~~~~~~~~~~~~~~~~~~~~~~ 
    4 
    5    Lexers for hexadecimal dumps. 
    6 
    7    :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS. 
    8    :license: BSD, see LICENSE for details. 
    9""" 
    10 
    11from pygments.lexer import RegexLexer, bygroups, include 
    12from pygments.token import Name, Number, String, Punctuation, Whitespace 
    13 
    14__all__ = ['HexdumpLexer'] 
    15 
    16 
    17class HexdumpLexer(RegexLexer): 
    18    """ 
    19    For typical hex dump output formats by the UNIX and GNU/Linux tools ``hexdump``, 
    20    ``hd``, ``hexcat``, ``od`` and ``xxd``, and the DOS tool ``DEBUG``. For example: 
    21 
    22    .. sourcecode:: hexdump 
    23 
    24        00000000  7f 45 4c 46 02 01 01 00  00 00 00 00 00 00 00 00  |.ELF............| 
    25        00000010  02 00 3e 00 01 00 00 00  c5 48 40 00 00 00 00 00  |..>......H@.....| 
    26 
    27    The specific supported formats are the outputs of: 
    28 
    29    * ``hexdump FILE`` 
    30    * ``hexdump -C FILE`` -- the `canonical` format used in the example. 
    31    * ``hd FILE`` -- same as ``hexdump -C FILE``. 
    32    * ``hexcat FILE`` 
    33    * ``od -t x1z FILE`` 
    34    * ``xxd FILE`` 
    35    * ``DEBUG.EXE FILE.COM`` and entering ``d`` to the prompt. 
    36    """ 
    37    name = 'Hexdump' 
    38    aliases = ['hexdump'] 
    39    url = 'https://en.wikipedia.org/wiki/Hex_dump' 
    40    version_added = '2.1' 
    41 
    42    hd = r'[0-9A-Ha-h]' 
    43 
    44    tokens = { 
    45        'root': [ 
    46            (r'\n', Whitespace), 
    47            include('offset'), 
    48            (r'('+hd+r'{2})(\-)('+hd+r'{2})', 
    49             bygroups(Number.Hex, Punctuation, Number.Hex)), 
    50            (hd+r'{2}', Number.Hex), 
    51            (r'(\s{2,3})(\>)(.{16})(\<)$', 
    52             bygroups(Whitespace, Punctuation, String, Punctuation), 'bracket-strings'), 
    53            (r'(\s{2,3})(\|)(.{16})(\|)$', 
    54             bygroups(Whitespace, Punctuation, String, Punctuation), 'piped-strings'), 
    55            (r'(\s{2,3})(\>)(.{1,15})(\<)$', 
    56             bygroups(Whitespace, Punctuation, String, Punctuation)), 
    57            (r'(\s{2,3})(\|)(.{1,15})(\|)$', 
    58             bygroups(Whitespace, Punctuation, String, Punctuation)), 
    59            (r'(\s{2,3})(.{1,15})$', bygroups(Whitespace, String)), 
    60            (r'(\s{2,3})(.{16}|.{20})$', bygroups(Whitespace, String), 'nonpiped-strings'), 
    61            (r'\s', Whitespace), 
    62            (r'^\*', Punctuation), 
    63        ], 
    64        'offset': [ 
    65            (r'^('+hd+'+)(:)', bygroups(Name.Label, Punctuation), 'offset-mode'), 
    66            (r'^'+hd+'+', Name.Label), 
    67        ], 
    68        'offset-mode': [ 
    69            (r'\s', Whitespace, '#pop'), 
    70            (hd+'+', Name.Label), 
    71            (r':', Punctuation) 
    72        ], 
    73        'piped-strings': [ 
    74            (r'\n', Whitespace), 
    75            include('offset'), 
    76            (hd+r'{2}', Number.Hex), 
    77            (r'(\s{2,3})(\|)(.{1,16})(\|)$', 
    78             bygroups(Whitespace, Punctuation, String, Punctuation)), 
    79            (r'\s', Whitespace), 
    80            (r'^\*', Punctuation), 
    81        ], 
    82        'bracket-strings': [ 
    83            (r'\n', Whitespace), 
    84            include('offset'), 
    85            (hd+r'{2}', Number.Hex), 
    86            (r'(\s{2,3})(\>)(.{1,16})(\<)$', 
    87             bygroups(Whitespace, Punctuation, String, Punctuation)), 
    88            (r'\s', Whitespace), 
    89            (r'^\*', Punctuation), 
    90        ], 
    91        'nonpiped-strings': [ 
    92            (r'\n', Whitespace), 
    93            include('offset'), 
    94            (r'('+hd+r'{2})(\-)('+hd+r'{2})', 
    95             bygroups(Number.Hex, Punctuation, Number.Hex)), 
    96            (hd+r'{2}', Number.Hex), 
    97            (r'(\s{19,})(.{1,20}?)$', bygroups(Whitespace, String)), 
    98            (r'(\s{2,3})(.{1,20})$', bygroups(Whitespace, String)), 
    99            (r'\s', Whitespace), 
    100            (r'^\*', Punctuation), 
    101        ], 
    102    }