hexdump.py 3.4 KB

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