hexdump.py 3.4 KB

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