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