eric6/ThirdParty/Pygments/pygments/lexers/diff.py

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

eric ide

mercurial