diff.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. """
  2. pygments.lexers.diff
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexers for diff/patch formats.
  5. :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import re
  9. from pygments.lexer import RegexLexer, include, bygroups
  10. from pygments.token import Text, Comment, Operator, Keyword, Name, Generic, \
  11. Literal, Whitespace
  12. __all__ = ['DiffLexer', 'DarcsPatchLexer', 'WDiffLexer']
  13. class DiffLexer(RegexLexer):
  14. """
  15. Lexer for unified or context-style diffs or patches.
  16. """
  17. name = 'Diff'
  18. aliases = ['diff', 'udiff']
  19. filenames = ['*.diff', '*.patch']
  20. mimetypes = ['text/x-diff', 'text/x-patch']
  21. tokens = {
  22. 'root': [
  23. (r'( )(.*)(\n)', bygroups(Whitespace, Text, Whitespace)),
  24. (r'(!.*|---)(\n)', bygroups(Generic.Strong, Whitespace)),
  25. (r'((?:< |-).*)(\n)', bygroups(Generic.Deleted, Whitespace)),
  26. (r'((?:> |\+).*)(\n)', bygroups(Generic.Inserted, Whitespace)),
  27. (
  28. r'(@.*|\d(?:,\d+)?(?:a|c|d)\d+(?:,\d+)?)(\n)',
  29. bygroups(Generic.Subheading, Whitespace),
  30. ),
  31. (r'((?:[Ii]ndex|diff).*)(\n)', bygroups(Generic.Heading, Whitespace)),
  32. (r'(=.*)(\n)', bygroups(Generic.Heading, Whitespace)),
  33. (r'(.*)(\n)', bygroups(Text, Whitespace)),
  34. ]
  35. }
  36. def analyse_text(text):
  37. if text[:7] == 'Index: ':
  38. return True
  39. if text[:5] == 'diff ':
  40. return True
  41. if text[:4] == '--- ':
  42. return 0.9
  43. class DarcsPatchLexer(RegexLexer):
  44. """
  45. DarcsPatchLexer is a lexer for the various versions of the darcs patch
  46. format. Examples of this format are derived by commands such as
  47. ``darcs annotate --patch`` and ``darcs send``.
  48. .. versionadded:: 0.10
  49. """
  50. name = 'Darcs Patch'
  51. aliases = ['dpatch']
  52. filenames = ['*.dpatch', '*.darcspatch']
  53. DPATCH_KEYWORDS = ('hunk', 'addfile', 'adddir', 'rmfile', 'rmdir', 'move',
  54. 'replace')
  55. tokens = {
  56. 'root': [
  57. (r'<', Operator),
  58. (r'>', Operator),
  59. (r'\{', Operator),
  60. (r'\}', Operator),
  61. (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)(\])',
  62. bygroups(Operator, Keyword, Name, Whitespace, Name, Operator,
  63. Literal.Date, Whitespace, Operator)),
  64. (r'(\[)((?:TAG )?)(.*)(\n)(.*)(\*\*)(\d+)(\s?)',
  65. bygroups(Operator, Keyword, Name, Whitespace, Name, Operator,
  66. Literal.Date, Whitespace), 'comment'),
  67. (r'New patches:', Generic.Heading),
  68. (r'Context:', Generic.Heading),
  69. (r'Patch bundle hash:', Generic.Heading),
  70. (r'(\s*)(%s)(.*)(\n)' % '|'.join(DPATCH_KEYWORDS),
  71. bygroups(Whitespace, Keyword, Text, Whitespace)),
  72. (r'\+', Generic.Inserted, "insert"),
  73. (r'-', Generic.Deleted, "delete"),
  74. (r'(.*)(\n)', bygroups(Text, Whitespace)),
  75. ],
  76. 'comment': [
  77. (r'[^\]].*\n', Comment),
  78. (r'\]', Operator, "#pop"),
  79. ],
  80. 'specialText': [ # darcs add [_CODE_] special operators for clarity
  81. (r'\n', Whitespace, "#pop"), # line-based
  82. (r'\[_[^_]*_]', Operator),
  83. ],
  84. 'insert': [
  85. include('specialText'),
  86. (r'\[', Generic.Inserted),
  87. (r'[^\n\[]+', Generic.Inserted),
  88. ],
  89. 'delete': [
  90. include('specialText'),
  91. (r'\[', Generic.Deleted),
  92. (r'[^\n\[]+', Generic.Deleted),
  93. ],
  94. }
  95. class WDiffLexer(RegexLexer):
  96. """
  97. A wdiff lexer.
  98. Note that:
  99. * It only works with normal output (without options like ``-l``).
  100. * If the target files contain "[-", "-]", "{+", or "+}",
  101. especially they are unbalanced, the lexer will get confused.
  102. .. versionadded:: 2.2
  103. """
  104. name = 'WDiff'
  105. url = 'https://www.gnu.org/software/wdiff/'
  106. aliases = ['wdiff']
  107. filenames = ['*.wdiff']
  108. mimetypes = []
  109. flags = re.MULTILINE | re.DOTALL
  110. # We can only assume "[-" after "[-" before "-]" is `nested`,
  111. # for instance wdiff to wdiff outputs. We have no way to
  112. # distinct these marker is of wdiff output from original text.
  113. ins_op = r"\{\+"
  114. ins_cl = r"\+\}"
  115. del_op = r"\[\-"
  116. del_cl = r"\-\]"
  117. normal = r'[^{}[\]+-]+' # for performance
  118. tokens = {
  119. 'root': [
  120. (ins_op, Generic.Inserted, 'inserted'),
  121. (del_op, Generic.Deleted, 'deleted'),
  122. (normal, Text),
  123. (r'.', Text),
  124. ],
  125. 'inserted': [
  126. (ins_op, Generic.Inserted, '#push'),
  127. (del_op, Generic.Inserted, '#push'),
  128. (del_cl, Generic.Inserted, '#pop'),
  129. (ins_cl, Generic.Inserted, '#pop'),
  130. (normal, Generic.Inserted),
  131. (r'.', Generic.Inserted),
  132. ],
  133. 'deleted': [
  134. (del_op, Generic.Deleted, '#push'),
  135. (ins_op, Generic.Deleted, '#push'),
  136. (ins_cl, Generic.Deleted, '#pop'),
  137. (del_cl, Generic.Deleted, '#pop'),
  138. (normal, Generic.Deleted),
  139. (r'.', Generic.Deleted),
  140. ],
  141. }