1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 #ifndef INCLUDE_userdiff_h__
8 #define INCLUDE_userdiff_h__
9 
10 #include "regexp.h"
11 
12 /*
13  * This file isolates the built in diff driver function name patterns.
14  * Most of these patterns are taken from Git (with permission from the
15  * original authors for relicensing to libgit2).
16  */
17 
18 typedef struct {
19 	const char *name;
20 	const char *fns;
21 	const char *words;
22 	int flags;
23 } git_diff_driver_definition;
24 
25 #define WORD_DEFAULT "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+"
26 
27 /*
28  * These builtin driver definition macros have same signature as in core
29  * git userdiff.c so that the data can be extracted verbatim
30  */
31 #define PATTERNS(NAME, FN_PATS, WORD_PAT) \
32 	{ NAME, FN_PATS, WORD_PAT WORD_DEFAULT, 0 }
33 #define IPATTERN(NAME, FN_PATS, WORD_PAT) \
34 	{ NAME, FN_PATS, WORD_PAT WORD_DEFAULT, GIT_REGEXP_ICASE }
35 
36 /*
37  * The table of diff driver patterns
38  *
39  * Function name patterns are a list of newline separated patterns that
40  * match a function declaration (i.e. the line you want in the hunk header),
41  * or a negative pattern prefixed with a '!' to reject a pattern (such as
42  * rejecting goto labels in C code).
43  *
44  * Word boundary patterns are just a simple pattern that will be OR'ed with
45  * the default value above (i.e. whitespace or non-ASCII characters).
46  */
47 static git_diff_driver_definition builtin_defs[] = {
48 
49 IPATTERN("ada",
50 	 "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n"
51 	 "!^[ \t]*with[ \t].*$\n"
52 	 "^[ \t]*((procedure|function)[ \t]+.*)$\n"
53 	 "^[ \t]*((package|protected|task)[ \t]+.*)$",
54 	 /* -- */
55 	 "[a-zA-Z][a-zA-Z0-9_]*"
56 	 "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?"
57 	 "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"),
58 
59 IPATTERN("fortran",
60 	 "!^([C*]|[ \t]*!)\n"
61 	 "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n"
62 	 "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA"
63 		"|([^'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$",
64 	 /* -- */
65 	 "[a-zA-Z][a-zA-Z0-9_]*"
66 	 "|\\.([Ee][Qq]|[Nn][Ee]|[Gg][TtEe]|[Ll][TtEe]|[Tt][Rr][Uu][Ee]|[Ff][Aa][Ll][Ss][Ee]|[Aa][Nn][Dd]|[Oo][Rr]|[Nn]?[Ee][Qq][Vv]|[Nn][Oo][Tt])\\."
67 	 /* numbers and format statements like 2E14.4, or ES12.6, 9X.
68 	  * Don't worry about format statements without leading digits since
69 	  * they would have been matched above as a variable anyway. */
70 	 "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?"
71 	 "|//|\\*\\*|::|[/<>=]="),
72 
73 PATTERNS("html", "^[ \t]*(<[Hh][1-6][ \t].*>.*)$",
74 	 "[^<>= \t]+"),
75 
76 PATTERNS("java",
77 	 "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n"
78 	 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$",
79 	 /* -- */
80 	 "[a-zA-Z_][a-zA-Z0-9_]*"
81 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
82 	 "|[-+*/<>%&^|=!]="
83 	 "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"),
84 
85 PATTERNS("matlab",
86 	 "^[[:space:]]*((classdef|function)[[:space:]].*)$|^%%[[:space:]].*$",
87 	 "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"),
88 
89 PATTERNS("objc",
90 	 /* Negate C statements that can look like functions */
91 	 "!^[ \t]*(do|for|if|else|return|switch|while)\n"
92 	 /* Objective-C methods */
93 	 "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n"
94 	 /* C functions */
95 	 "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n"
96 	 /* Objective-C class/protocol definitions */
97 	 "^(@(implementation|interface|protocol)[ \t].*)$",
98 	 /* -- */
99 	 "[a-zA-Z_][a-zA-Z0-9_]*"
100 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
101 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
102 
103 PATTERNS("pascal",
104 	 "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface|"
105 		"implementation|initialization|finalization)[ \t]*.*)$"
106 	 "\n"
107 	 "^(.*=[ \t]*(class|record).*)$",
108 	 /* -- */
109 	 "[a-zA-Z_][a-zA-Z0-9_]*"
110 	 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+"
111 	 "|<>|<=|>=|:=|\\.\\."),
112 
113 PATTERNS("perl",
114 	 "^package .*\n"
115 	 "^sub [[:alnum:]_':]+[ \t]*"
116 		"(\\([^)]*\\)[ \t]*)?" /* prototype */
117 		/*
118 		 * Attributes.  A regex can't count nested parentheses,
119 		 * so just slurp up whatever we see, taking care not
120 		 * to accept lines like "sub foo; # defined elsewhere".
121 		 *
122 		 * An attribute could contain a semicolon, but at that
123 		 * point it seems reasonable enough to give up.
124 		 */
125 		"(:[^;#]*)?"
126 		"(\\{[ \t]*)?" /* brace can come here or on the next line */
127 		"(#.*)?$\n" /* comment */
128 	 "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*"
129 		"(\\{[ \t]*)?" /* brace can come here or on the next line */
130 		"(#.*)?$\n"
131 	 "^=head[0-9] .*",	/* POD */
132 	 /* -- */
133 	 "[[:alpha:]_'][[:alnum:]_']*"
134 	 "|0[xb]?[0-9a-fA-F_]*"
135 	 /* taking care not to interpret 3..5 as (3.)(.5) */
136 	 "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?"
137 	 "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::"
138 	 "|&&=|\\|\\|=|//=|\\*\\*="
139 	 "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?"
140 	 "|[-+*/%.^&<>=!|]="
141 	 "|=~|!~"
142 	 "|<<|<>|<=>|>>"),
143 
144 PATTERNS("python", "^[ \t]*((class|def)[ \t].*)$",
145 	 /* -- */
146 	 "[a-zA-Z_][a-zA-Z0-9_]*"
147 	 "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
148 	 "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"),
149 
150 PATTERNS("ruby", "^[ \t]*((class|module|def)[ \t].*)$",
151 	 /* -- */
152 	 "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*"
153 	 "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?."
154 	 "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"),
155 
156 PATTERNS("bibtex", "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$",
157 	 "[={}\"]|[^={}\" \t]+"),
158 
159 PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$",
160 	 "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"),
161 
162 PATTERNS("cpp",
163 	 /* Jump targets or access declarations */
164 	 "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n"
165 	 /* functions/methods, variables, and compounds at top level */
166 	 "^((::[[:space:]]*)?[A-Za-z_].*)$",
167 	 /* -- */
168 	 "[a-zA-Z_][a-zA-Z0-9_]*"
169 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lLuU]*"
170 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*"),
171 
172 PATTERNS("csharp",
173 	 /* Keywords */
174 	 "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n"
175 	 /* Methods and constructors */
176 	 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n"
177 	 /* Properties */
178 	 "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n"
179 	 /* Type definitions */
180 	 "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct)[ \t]+.*)$\n"
181 	 /* Namespace */
182 	 "^[ \t]*(namespace[ \t]+.*)$",
183 	 /* -- */
184 	 "[a-zA-Z_][a-zA-Z0-9_]*"
185 	 "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?"
186 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
187 
188 PATTERNS("php",
189 	 "^[ \t]*(((public|private|protected|static|final)[ \t]+)*((class|function)[ \t].*))$",
190 	 /* -- */
191 	 "[a-zA-Z_][a-zA-Z0-9_]*"
192 	 "|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
193 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
194 
195 PATTERNS("javascript",
196 	 "([a-zA-Z_$][a-zA-Z0-9_$]*(\\.[a-zA-Z0-9_$]+)*[ \t]*=[ \t]*function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)\n"
197 	 "([a-zA-Z_$][a-zA-Z0-9_$]*[ \t]*:[ \t]*function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)\n"
198 	 "[^a-zA-Z0-9_\\$](function([ \t][a-zA-Z_$][a-zA-Z0-9_$]*)?[^\\{]*)",
199 	 /* -- */
200 	 "[a-zA-Z_][a-zA-Z0-9_]*"
201 	 "|[-+0-9.e]+[fFlL]?|0[xX]?[0-9a-fA-F]+[lL]?"
202 	 "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"),
203 };
204 
205 #undef IPATTERN
206 #undef PATTERNS
207 #undef WORD_DEFAULT
208 
209 #endif
210 
211