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