Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 1 | #include "cache.h" |
Brandon Williams | b2141fc | 2017-06-14 11:07:36 -0700 | [diff] [blame] | 2 | #include "config.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 3 | #include "userdiff.h" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 4 | #include "attr.h" |
| 5 | |
| 6 | static struct userdiff_driver *drivers; |
| 7 | static int ndrivers; |
| 8 | static int drivers_alloc; |
| 9 | |
Boyd Stephen Smith Jr | ae3b970 | 2009-01-20 22:59:54 -0600 | [diff] [blame] | 10 | #define PATTERNS(name, pattern, word_regex) \ |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 11 | { name, NULL, -1, { pattern, REG_EXTENDED }, \ |
| 12 | word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" } |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 13 | #define IPATTERN(name, pattern, word_regex) \ |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 14 | { name, NULL, -1, { pattern, REG_EXTENDED | REG_ICASE }, \ |
| 15 | word_regex "|[^[:space:]]|[\xc0-\xff][\x80-\xbf]+" } |
Junio C Hamano | b6029b3 | 2021-08-10 15:12:01 -0700 | [diff] [blame] | 16 | |
| 17 | /* |
| 18 | * Built-in drivers for various languages, sorted by their names |
| 19 | * (except that the "default" is left at the end). |
| 20 | * |
| 21 | * When writing or updating patterns, assume that the contents these |
| 22 | * patterns are applied to are syntactically correct. The patterns |
| 23 | * can be simple without implementing all syntactical corner cases, as |
| 24 | * long as they are sufficiently permissive. |
| 25 | */ |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 26 | static struct userdiff_driver builtin_drivers[] = { |
Adrian Johnson | e90d065 | 2012-09-16 13:24:15 +0930 | [diff] [blame] | 27 | IPATTERN("ada", |
Adrian Johnson | 39a87a2 | 2014-02-03 22:03:16 +1030 | [diff] [blame] | 28 | "!^(.*[ \t])?(is[ \t]+new|renames|is[ \t]+separate)([ \t].*)?$\n" |
Adrian Johnson | e90d065 | 2012-09-16 13:24:15 +0930 | [diff] [blame] | 29 | "!^[ \t]*with[ \t].*$\n" |
| 30 | "^[ \t]*((procedure|function)[ \t]+.*)$\n" |
| 31 | "^[ \t]*((package|protected|task)[ \t]+.*)$", |
| 32 | /* -- */ |
| 33 | "[a-zA-Z][a-zA-Z0-9_]*" |
Adrian Johnson | 39a87a2 | 2014-02-03 22:03:16 +1030 | [diff] [blame] | 34 | "|[-+]?[0-9][0-9#_.aAbBcCdDeEfF]*([eE][+-]?[0-9_]+)?" |
Adrian Johnson | e90d065 | 2012-09-16 13:24:15 +0930 | [diff] [blame] | 35 | "|=>|\\.\\.|\\*\\*|:=|/=|>=|<=|<<|>>|<>"), |
Victor Engmark | 2ff6c34 | 2020-10-22 12:45:08 +1300 | [diff] [blame] | 36 | PATTERNS("bash", |
| 37 | /* Optional leading indentation */ |
| 38 | "^[ \t]*" |
| 39 | /* Start of captured text */ |
| 40 | "(" |
| 41 | "(" |
| 42 | /* POSIX identifier with mandatory parentheses */ |
| 43 | "[a-zA-Z_][a-zA-Z0-9_]*[ \t]*\\([ \t]*\\))" |
| 44 | "|" |
| 45 | /* Bashism identifier with optional parentheses */ |
| 46 | "(function[ \t]+[a-zA-Z_][a-zA-Z0-9_]*(([ \t]*\\([ \t]*\\))|([ \t]+))" |
| 47 | ")" |
| 48 | /* Optional whitespace */ |
| 49 | "[ \t]*" |
| 50 | /* Compound command starting with `{`, `(`, `((` or `[[` */ |
| 51 | "(\\{|\\(\\(?|\\[\\[)" |
| 52 | /* End of captured text */ |
| 53 | ")", |
| 54 | /* -- */ |
| 55 | /* Characters not in the default $IFS value */ |
| 56 | "[^ \t]+"), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 57 | PATTERNS("bibtex", |
| 58 | "(@[a-zA-Z]{1,}[ \t]*\\{{0,1}[ \t]*[^ \t\"@',\\#}{~%]*).*$", |
| 59 | /* -- */ |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 60 | "[={}\"]|[^={}\" \t]+"), |
| 61 | PATTERNS("cpp", |
| 62 | /* Jump targets or access declarations */ |
| 63 | "!^[ \t]*[A-Za-z_][A-Za-z_0-9]*:[[:space:]]*($|/[/*])\n" |
| 64 | /* functions/methods, variables, and compounds at top level */ |
| 65 | "^((::[[:space:]]*)?[A-Za-z_].*)$", |
| 66 | /* -- */ |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 67 | /* identifiers and keywords */ |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 68 | "[a-zA-Z_][a-zA-Z0-9_]*" |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 69 | /* decimal and octal integers as well as floatingpoint numbers */ |
Johannes Sixt | 386076e | 2021-10-24 11:56:43 +0200 | [diff] [blame] | 70 | "|[0-9][0-9.]*([Ee][-+]?[0-9]+)?[fFlLuU]*" |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 71 | /* hexadecimal and binary integers */ |
Johannes Sixt | 386076e | 2021-10-24 11:56:43 +0200 | [diff] [blame] | 72 | "|0[xXbB][0-9a-fA-F]+[lLuU]*" |
Johannes Sixt | 350b87c | 2021-10-08 19:09:55 +0000 | [diff] [blame] | 73 | /* floatingpoint numbers that begin with a decimal point */ |
Johannes Sixt | 386076e | 2021-10-24 11:56:43 +0200 | [diff] [blame] | 74 | "|\\.[0-9][0-9]*([Ee][-+]?[0-9]+)?[fFlL]?" |
Johannes Sixt | c4fdba3 | 2021-10-10 17:03:04 +0000 | [diff] [blame] | 75 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->\\*?|\\.\\*|<=>"), |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 76 | PATTERNS("csharp", |
| 77 | /* Keywords */ |
| 78 | "!^[ \t]*(do|while|for|if|else|instanceof|new|return|switch|case|throw|catch|using)\n" |
| 79 | /* Methods and constructors */ |
| 80 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe|async)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[<>@._[:alnum:]]+[ \t]*\\(.*\\))[ \t]*$\n" |
| 81 | /* Properties */ |
| 82 | "^[ \t]*(((static|public|internal|private|protected|new|virtual|sealed|override|unsafe)[ \t]+)*[][<>@.~_[:alnum:]]+[ \t]+[@._[:alnum:]]+)[ \t]*$\n" |
| 83 | /* Type definitions */ |
Julian Verdurmen | c4e3178 | 2021-03-02 00:58:09 +0000 | [diff] [blame] | 84 | "^[ \t]*(((static|public|internal|private|protected|new|unsafe|sealed|abstract|partial)[ \t]+)*(class|enum|interface|struct|record)[ \t]+.*)$\n" |
Ævar Arnfjörð Bjarmason | ddd164d | 2021-04-08 17:04:16 +0200 | [diff] [blame] | 85 | /* Namespace */ |
| 86 | "^[ \t]*(namespace[ \t]+.*)$", |
| 87 | /* -- */ |
| 88 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 89 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" |
| 90 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), |
| 91 | IPATTERN("css", |
| 92 | "![:;][[:space:]]*$\n" |
| 93 | "^[:[@.#]?[_a-z0-9].*$", |
| 94 | /* -- */ |
| 95 | /* |
| 96 | * This regex comes from W3C CSS specs. Should theoretically also |
| 97 | * allow ISO 10646 characters U+00A0 and higher, |
| 98 | * but they are not handled in this regex. |
| 99 | */ |
| 100 | "-?[_a-zA-Z][-_a-zA-Z0-9]*" /* identifiers */ |
| 101 | "|-?[0-9]+|\\#[0-9a-fA-F]+" /* numbers */ |
| 102 | ), |
Stephen Boyd | 3c81760 | 2019-08-19 14:22:43 -0700 | [diff] [blame] | 103 | PATTERNS("dts", |
| 104 | "!;\n" |
Stephen Boyd | 8da56a4 | 2019-10-20 11:52:30 -0700 | [diff] [blame] | 105 | "!=\n" |
Stephen Boyd | 3c81760 | 2019-08-19 14:22:43 -0700 | [diff] [blame] | 106 | /* lines beginning with a word optionally preceded by '&' or the root */ |
Stephen Boyd | 8da56a4 | 2019-10-20 11:52:30 -0700 | [diff] [blame] | 107 | "^[ \t]*((/[ \t]*\\{|&?[a-zA-Z_]).*)", |
Stephen Boyd | 3c81760 | 2019-08-19 14:22:43 -0700 | [diff] [blame] | 108 | /* -- */ |
| 109 | /* Property names and math operators */ |
| 110 | "[a-zA-Z0-9,._+?#-]+" |
| 111 | "|[-+*/%&^|!~]|>>|<<|&&|\\|\\|"), |
Łukasz Niemier | a807200 | 2019-11-08 22:38:24 +0100 | [diff] [blame] | 112 | PATTERNS("elixir", |
| 113 | "^[ \t]*((def(macro|module|impl|protocol|p)?|test)[ \t].*)$", |
Ed Maste | d1b1384 | 2019-12-13 17:55:35 +0000 | [diff] [blame] | 114 | /* -- */ |
Łukasz Niemier | a807200 | 2019-11-08 22:38:24 +0100 | [diff] [blame] | 115 | /* Atoms, names, and module attributes */ |
Ed Maste | d1b1384 | 2019-12-13 17:55:35 +0000 | [diff] [blame] | 116 | "[@:]?[a-zA-Z0-9@_?!]+" |
Łukasz Niemier | a807200 | 2019-11-08 22:38:24 +0100 | [diff] [blame] | 117 | /* Numbers with specific base */ |
| 118 | "|[-+]?0[xob][0-9a-fA-F]+" |
| 119 | /* Numbers */ |
| 120 | "|[-+]?[0-9][0-9_.]*([eE][-+]?[0-9_]+)?" |
| 121 | /* Operators and atoms that represent them */ |
| 122 | "|:?(\\+\\+|--|\\.\\.|~~~|<>|\\^\\^\\^|<?\\|>|<<<?|>?>>|<<?~|~>?>|<~>|<=|>=|===?|!==?|=~|&&&?|\\|\\|\\|?|=>|<-|\\\\\\\\|->)" |
| 123 | /* Not real operators, but should be grouped */ |
| 124 | "|:?%[A-Za-z0-9_.]\\{\\}?"), |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 125 | IPATTERN("fortran", |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 126 | /* Don't match comment lines */ |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 127 | "!^([C*]|[ \t]*!)\n" |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 128 | /* Don't match 'module procedure' lines */ |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 129 | "!^[ \t]*MODULE[ \t]+PROCEDURE[ \t]\n" |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 130 | /* Program, module, block data */ |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 131 | "^[ \t]*((END[ \t]+)?(PROGRAM|MODULE|BLOCK[ \t]+DATA" |
Philippe Blain | b79e692 | 2020-08-12 22:30:28 +0000 | [diff] [blame] | 132 | /* Subroutines and functions */ |
Philippe Blain | 75c3b6b | 2020-08-12 22:30:29 +0000 | [diff] [blame] | 133 | "|([^!'\" \t]+[ \t]+)*(SUBROUTINE|FUNCTION))[ \t]+[A-Z].*)$", |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 134 | /* -- */ |
| 135 | "[a-zA-Z][a-zA-Z0-9_]*" |
| 136 | "|\\.([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])\\." |
| 137 | /* numbers and format statements like 2E14.4, or ES12.6, 9X. |
| 138 | * Don't worry about format statements without leading digits since |
| 139 | * they would have been matched above as a variable anyway. */ |
| 140 | "|[-+]?[0-9.]+([AaIiDdEeFfLlTtXx][Ss]?[-+]?[0-9.]*)?(_[a-zA-Z0-9][a-zA-Z0-9_]*)?" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 141 | "|//|\\*\\*|::|[/<>=]="), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 142 | IPATTERN("fountain", |
| 143 | "^((\\.[^.]|(int|ext|est|int\\.?/ext|i/e)[. ]).*)$", |
| 144 | /* -- */ |
Zoë Blade | 69f9c87 | 2015-07-21 14:22:46 +0100 | [diff] [blame] | 145 | "[^ \t-]+"), |
Alban Gruin | 1dbf0c0 | 2018-03-01 12:19:07 +0100 | [diff] [blame] | 146 | PATTERNS("golang", |
| 147 | /* Functions */ |
| 148 | "^[ \t]*(func[ \t]*.*(\\{[ \t]*)?)\n" |
| 149 | /* Structs and interfaces */ |
| 150 | "^[ \t]*(type[ \t].*(struct|interface)[ \t]*(\\{[ \t]*)?)", |
| 151 | /* -- */ |
| 152 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 153 | "|[-+0-9.eE]+i?|0[xX]?[0-9a-fA-F]+i?" |
| 154 | "|[-+*/<>%&^|=!:]=|--|\\+\\+|<<=?|>>=?|&\\^=?|&&|\\|\\||<-|\\.{3}"), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 155 | PATTERNS("html", |
| 156 | "^[ \t]*(<[Hh][1-6]([ \t].*)?>.*)$", |
| 157 | /* -- */ |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 158 | "[^<>= \t]+"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 159 | PATTERNS("java", |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 160 | "!^[ \t]*(catch|do|for|if|instanceof|new|return|switch|throw|while)\n" |
Tassilo Horn | a8cbc89 | 2021-08-11 19:51:04 +0200 | [diff] [blame] | 161 | /* Class, enum, and interface declarations */ |
| 162 | "^[ \t]*(([a-z]+[ \t]+)*(class|enum|interface)[ \t]+[A-Za-z][A-Za-z0-9_$]*[ \t]+.*)$\n" |
| 163 | /* Method definitions; note that constructor signatures are not */ |
| 164 | /* matched because they are indistinguishable from method calls. */ |
| 165 | "^[ \t]*(([A-Za-z_<>&][][?&<>.,A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$", |
Paolo Bonzini | 959e2e6 | 2009-06-17 16:26:06 +0200 | [diff] [blame] | 166 | /* -- */ |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 167 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 168 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" |
| 169 | "|[-+*/<>%&^|=!]=" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 170 | "|--|\\+\\+|<<=?|>>>?=?|&&|\\|\\|"), |
Ash Holland | 09dad92 | 2020-05-02 14:15:43 +0100 | [diff] [blame] | 171 | PATTERNS("markdown", |
| 172 | "^ {0,3}#{1,6}[ \t].*", |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 173 | /* -- */ |
Ash Holland | 09dad92 | 2020-05-02 14:15:43 +0100 | [diff] [blame] | 174 | "[^<>= \t]+"), |
Gustaf Hendeby | 53b10a1 | 2011-11-15 21:15:03 +0100 | [diff] [blame] | 175 | PATTERNS("matlab", |
Boxuan Li | 2731a78 | 2019-05-30 00:15:39 +0800 | [diff] [blame] | 176 | /* |
| 177 | * Octave pattern is mostly the same as matlab, except that '%%%' and |
Boxuan Li | 91bf382 | 2019-05-18 11:46:23 +0800 | [diff] [blame] | 178 | * '##' can also be used to begin code sections, in addition to '%%' |
Boxuan Li | 2731a78 | 2019-05-30 00:15:39 +0800 | [diff] [blame] | 179 | * that is understood by both. |
| 180 | */ |
Boxuan Li | 91bf382 | 2019-05-18 11:46:23 +0800 | [diff] [blame] | 181 | "^[[:space:]]*((classdef|function)[[:space:]].*)$|^(%%%?|##)[[:space:]].*$", |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 182 | /* -- */ |
Gustaf Hendeby | 53b10a1 | 2011-11-15 21:15:03 +0100 | [diff] [blame] | 183 | "[a-zA-Z_][a-zA-Z0-9_]*|[-+0-9.e]+|[=~<>]=|\\.[*/\\^']|\\|\\||&&"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 184 | PATTERNS("objc", |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 185 | /* Negate C statements that can look like functions */ |
| 186 | "!^[ \t]*(do|for|if|else|return|switch|while)\n" |
| 187 | /* Objective-C methods */ |
| 188 | "^[ \t]*([-+][ \t]*\\([ \t]*[A-Za-z_][A-Za-z_0-9* \t]*\\)[ \t]*[A-Za-z_].*)$\n" |
| 189 | /* C functions */ |
Paolo Bonzini | 959e2e6 | 2009-06-17 16:26:06 +0200 | [diff] [blame] | 190 | "^[ \t]*(([A-Za-z_][A-Za-z_0-9]*[ \t]+)+[A-Za-z_][A-Za-z_0-9]*[ \t]*\\([^;]*)$\n" |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 191 | /* Objective-C class/protocol definitions */ |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 192 | "^(@(implementation|interface|protocol)[ \t].*)$", |
| 193 | /* -- */ |
| 194 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 195 | "|[-+0-9.e]+[fFlL]?|0[xXbB]?[0-9a-fA-F]+[lL]?" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 196 | "|[-+*/<>%&^|=!]=|--|\\+\\+|<<=?|>>=?|&&|\\|\\||::|->"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 197 | PATTERNS("pascal", |
Ævar Arnfjörð Bjarmason | 82512e0 | 2021-04-08 17:04:18 +0200 | [diff] [blame] | 198 | "^(((class[ \t]+)?(procedure|function)|constructor|destructor|interface" |
| 199 | "|implementation|initialization|finalization)[ \t]*.*)$\n" |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 200 | "^(.*=[ \t]*(class|record).*)$", |
| 201 | /* -- */ |
| 202 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 203 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 204 | "|<>|<=|>=|:=|\\.\\."), |
Jonathan Nieder | 71a5d4b | 2010-12-26 03:07:31 -0600 | [diff] [blame] | 205 | PATTERNS("perl", |
Jonathan Nieder | ea2ca44 | 2011-05-21 14:38:26 -0500 | [diff] [blame] | 206 | "^package .*\n" |
| 207 | "^sub [[:alnum:]_':]+[ \t]*" |
| 208 | "(\\([^)]*\\)[ \t]*)?" /* prototype */ |
| 209 | /* |
| 210 | * Attributes. A regex can't count nested parentheses, |
| 211 | * so just slurp up whatever we see, taking care not |
| 212 | * to accept lines like "sub foo; # defined elsewhere". |
| 213 | * |
| 214 | * An attribute could contain a semicolon, but at that |
| 215 | * point it seems reasonable enough to give up. |
| 216 | */ |
| 217 | "(:[^;#]*)?" |
| 218 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ |
| 219 | "(#.*)?$\n" /* comment */ |
Jonathan Nieder | f143d9c | 2011-05-22 12:29:32 -0500 | [diff] [blame] | 220 | "^(BEGIN|END|INIT|CHECK|UNITCHECK|AUTOLOAD|DESTROY)[ \t]*" |
Jonathan Nieder | ea2ca44 | 2011-05-21 14:38:26 -0500 | [diff] [blame] | 221 | "(\\{[ \t]*)?" /* brace can come here or on the next line */ |
| 222 | "(#.*)?$\n" |
Jonathan Nieder | 12f0967 | 2011-05-21 14:35:51 -0500 | [diff] [blame] | 223 | "^=head[0-9] .*", /* POD */ |
Jonathan Nieder | 71a5d4b | 2010-12-26 03:07:31 -0600 | [diff] [blame] | 224 | /* -- */ |
| 225 | "[[:alpha:]_'][[:alnum:]_']*" |
| 226 | "|0[xb]?[0-9a-fA-F_]*" |
| 227 | /* taking care not to interpret 3..5 as (3.)(.5) */ |
| 228 | "|[0-9a-fA-F_]+(\\.[0-9a-fA-F_]+)?([eE][-+]?[0-9_]+)?" |
| 229 | "|=>|-[rwxoRWXOezsfdlpSugkbctTBMAC>]|~~|::" |
| 230 | "|&&=|\\|\\|=|//=|\\*\\*=" |
| 231 | "|&&|\\|\\||//|\\+\\+|--|\\*\\*|\\.\\.\\.?" |
| 232 | "|[-+*/%.^&<>=!|]=" |
| 233 | "|=~|!~" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 234 | "|<<|<>|<=>|>>"), |
Björn Steinbrink | 6d2f208 | 2010-05-23 20:05:40 +0200 | [diff] [blame] | 235 | PATTERNS("php", |
Javier Spagnoletti | aff9282 | 2020-10-07 03:38:18 +0000 | [diff] [blame] | 236 | "^[\t ]*(((public|protected|private|static|abstract|final)[\t ]+)*function.*)$\n" |
USAMI Kenta | 2c7f3aa | 2021-08-31 06:01:25 +0000 | [diff] [blame] | 237 | "^[\t ]*((((final|abstract)[\t ]+)?class|enum|interface|trait).*)$", |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 238 | /* -- */ |
| 239 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 240 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 241 | "|[-+*/<>%&^|=!.]=|--|\\+\\+|<<=?|>>=?|===|&&|\\|\\||::|->"), |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 242 | PATTERNS("python", |
| 243 | "^[ \t]*((class|(async[ \t]+)?def)[ \t].*)$", |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 244 | /* -- */ |
| 245 | "[a-zA-Z_][a-zA-Z0-9_]*" |
| 246 | "|[-+0-9.e]+[jJlL]?|0[xX]?[0-9a-fA-F]+[lL]?" |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 247 | "|[-+*/<>%&^|=!]=|//=?|<<=?|>>=?|\\*\\*=?"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 248 | /* -- */ |
Ævar Arnfjörð Bjarmason | 6d1c9c5 | 2021-04-08 17:04:17 +0200 | [diff] [blame] | 249 | PATTERNS("ruby", |
| 250 | "^[ \t]*((class|module|def)[ \t].*)$", |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 251 | /* -- */ |
| 252 | "(@|@@|\\$)?[a-zA-Z_][a-zA-Z0-9_]*" |
| 253 | "|[-+0-9.e]+|0[xXbB]?[0-9a-fA-F]+|\\?(\\\\C-)?(\\\\M-)?." |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 254 | "|//=?|[-+*/<>%&^|=!]=|<<=?|>>=?|===|\\.{1,3}|::|[!=]~"), |
Marc-André Lureau | d74e786 | 2019-05-17 01:58:15 +0200 | [diff] [blame] | 255 | PATTERNS("rust", |
Konrad Borowski | a04c7e0 | 2020-10-07 13:26:11 +0000 | [diff] [blame] | 256 | "^[\t ]*((pub(\\([^\\)]+\\))?[\t ]+)?((async|const|unsafe|extern([\t ]+\"[^\"]+\"))[\t ]+)?(struct|enum|union|mod|trait|fn|impl|macro_rules!)[< \t]+[^;]*)$", |
Marc-André Lureau | d74e786 | 2019-05-17 01:58:15 +0200 | [diff] [blame] | 257 | /* -- */ |
| 258 | "[a-zA-Z_][a-zA-Z0-9_]*" |
Johannes Sixt | 33be7b3 | 2019-05-30 18:44:35 +0200 | [diff] [blame] | 259 | "|[0-9][0-9_a-fA-Fiosuxz]*(\\.([0-9]*[eE][+-]?)?[0-9_fF]*)?" |
Marc-André Lureau | d74e786 | 2019-05-17 01:58:15 +0200 | [diff] [blame] | 260 | "|[-+*\\/<>%&^|=!:]=|<<=?|>>=?|&&|\\|\\||->|=>|\\.{2}=|\\.{3}|::"), |
Atharva Raykar | a437390 | 2021-04-08 14:44:43 +0530 | [diff] [blame] | 261 | PATTERNS("scheme", |
| 262 | "^[\t ]*(\\(((define|def(struct|syntax|class|method|rules|record|proto|alias)?)[-*/ \t]|(library|module|struct|class)[*+ \t]).*)$", |
| 263 | /* |
| 264 | * R7RS valid identifiers include any sequence enclosed |
| 265 | * within vertical lines having no backslashes |
| 266 | */ |
| 267 | "\\|([^\\\\]*)\\|" |
| 268 | /* All other words should be delimited by spaces or parentheses */ |
| 269 | "|([^][)(}{[ \t])+"), |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 270 | PATTERNS("tex", "^(\\\\((sub)*section|chapter|part)\\*{0,1}\\{.*)$", |
Jonathan Nieder | 664d44e | 2011-01-11 15:48:50 -0600 | [diff] [blame] | 271 | "\\\\[a-zA-Z@]+|\\\\.|[a-zA-Z0-9\x80-\xff]+"), |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 272 | { "default", NULL, -1, { NULL, 0 } }, |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 273 | }; |
Thomas Rast | 80c49c3 | 2009-01-17 17:29:48 +0100 | [diff] [blame] | 274 | #undef PATTERNS |
Brandon Casey | 909a549 | 2010-09-10 11:18:14 -0500 | [diff] [blame] | 275 | #undef IPATTERN |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 276 | |
| 277 | static struct userdiff_driver driver_true = { |
| 278 | "diff=true", |
| 279 | NULL, |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 280 | 0, |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 281 | { NULL, 0 } |
| 282 | }; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 283 | |
| 284 | static struct userdiff_driver driver_false = { |
| 285 | "!diff", |
| 286 | NULL, |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 287 | 1, |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 288 | { NULL, 0 } |
| 289 | }; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 290 | |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 291 | struct find_by_namelen_data { |
| 292 | const char *name; |
| 293 | size_t len; |
| 294 | struct userdiff_driver *driver; |
| 295 | }; |
| 296 | |
| 297 | static int userdiff_find_by_namelen_cb(struct userdiff_driver *driver, |
| 298 | enum userdiff_driver_type type, void *priv) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 299 | { |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 300 | struct find_by_namelen_data *cb_data = priv; |
| 301 | |
| 302 | if (!strncmp(driver->name, cb_data->name, cb_data->len) && |
| 303 | !driver->name[cb_data->len]) { |
| 304 | cb_data->driver = driver; |
| 305 | return 1; /* tell the caller to stop iterating */ |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 306 | } |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static struct userdiff_driver *userdiff_find_by_namelen(const char *name, size_t len) |
| 311 | { |
| 312 | struct find_by_namelen_data udcbdata = { |
| 313 | .name = name, |
| 314 | .len = len, |
| 315 | }; |
| 316 | for_each_userdiff_driver(userdiff_find_by_namelen_cb, &udcbdata); |
| 317 | return udcbdata.driver; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 320 | static int parse_funcname(struct userdiff_funcname *f, const char *k, |
| 321 | const char *v, int cflags) |
| 322 | { |
| 323 | if (git_config_string(&f->pattern, k, v) < 0) |
| 324 | return -1; |
| 325 | f->cflags = cflags; |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 326 | return 0; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 327 | } |
| 328 | |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 329 | static int parse_tristate(int *b, const char *k, const char *v) |
| 330 | { |
| 331 | if (v && !strcasecmp(v, "auto")) |
| 332 | *b = -1; |
| 333 | else |
| 334 | *b = git_config_bool(k, v); |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 335 | return 0; |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 336 | } |
| 337 | |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 338 | static int parse_bool(int *b, const char *k, const char *v) |
| 339 | { |
| 340 | *b = git_config_bool(k, v); |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 341 | return 0; |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Jeff King | c7534ef | 2008-10-26 00:45:55 -0400 | [diff] [blame] | 344 | int userdiff_config(const char *k, const char *v) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 345 | { |
| 346 | struct userdiff_driver *drv; |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 347 | const char *name, *type; |
Jeff King | f5914f4 | 2020-04-10 15:44:28 -0400 | [diff] [blame] | 348 | size_t namelen; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 349 | |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 350 | if (parse_config_key(k, "diff", &name, &namelen, &type) || !name) |
| 351 | return 0; |
| 352 | |
| 353 | drv = userdiff_find_by_namelen(name, namelen); |
| 354 | if (!drv) { |
| 355 | ALLOC_GROW(drivers, ndrivers+1, drivers_alloc); |
| 356 | drv = &drivers[ndrivers++]; |
| 357 | memset(drv, 0, sizeof(*drv)); |
| 358 | drv->name = xmemdupz(name, namelen); |
| 359 | drv->binary = -1; |
| 360 | } |
| 361 | |
| 362 | if (!strcmp(type, "funcname")) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 363 | return parse_funcname(&drv->funcname, k, v, 0); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 364 | if (!strcmp(type, "xfuncname")) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 365 | return parse_funcname(&drv->funcname, k, v, REG_EXTENDED); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 366 | if (!strcmp(type, "binary")) |
Jeff King | 122aa6f | 2008-10-05 17:43:36 -0400 | [diff] [blame] | 367 | return parse_tristate(&drv->binary, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 368 | if (!strcmp(type, "command")) |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 369 | return git_config_string(&drv->external, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 370 | if (!strcmp(type, "textconv")) |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 371 | return git_config_string(&drv->textconv, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 372 | if (!strcmp(type, "cachetextconv")) |
Jeff King | d9bae1a | 2010-04-01 20:12:15 -0400 | [diff] [blame] | 373 | return parse_bool(&drv->textconv_want_cache, k, v); |
Jeff King | 0a5987f | 2013-01-23 01:25:07 -0500 | [diff] [blame] | 374 | if (!strcmp(type, "wordregex")) |
Jeff King | 6680a08 | 2012-02-07 13:23:02 -0500 | [diff] [blame] | 375 | return git_config_string(&drv->word_regex, k, v); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |
| 379 | |
Nguyễn Thái Ngọc Duy | 3b33576 | 2018-12-09 11:25:21 +0100 | [diff] [blame] | 380 | struct userdiff_driver *userdiff_find_by_name(const char *name) |
| 381 | { |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 382 | int len = strlen(name); |
| 383 | return userdiff_find_by_namelen(name, len); |
| 384 | } |
| 385 | |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 386 | struct userdiff_driver *userdiff_find_by_path(struct index_state *istate, |
| 387 | const char *path) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 388 | { |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 389 | static struct attr_check *check; |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 390 | |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 391 | if (!check) |
| 392 | check = attr_check_initl("diff", NULL); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 393 | if (!path) |
| 394 | return NULL; |
Junio C Hamano | 11877b9 | 2018-10-19 13:34:02 +0900 | [diff] [blame] | 395 | git_check_attr(istate, path, check); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 396 | |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 397 | if (ATTR_TRUE(check->items[0].value)) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 398 | return &driver_true; |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 399 | if (ATTR_FALSE(check->items[0].value)) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 400 | return &driver_false; |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 401 | if (ATTR_UNSET(check->items[0].value)) |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 402 | return NULL; |
Junio C Hamano | 2aef63d | 2017-01-27 18:01:57 -0800 | [diff] [blame] | 403 | return userdiff_find_by_name(check->items[0].value); |
Jeff King | be58e70 | 2008-10-05 17:43:21 -0400 | [diff] [blame] | 404 | } |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 405 | |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 406 | struct userdiff_driver *userdiff_get_textconv(struct repository *r, |
| 407 | struct userdiff_driver *driver) |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 408 | { |
| 409 | if (!driver->textconv) |
| 410 | return NULL; |
| 411 | |
| 412 | if (driver->textconv_want_cache && !driver->textconv_cache) { |
| 413 | struct notes_cache *c = xmalloc(sizeof(*c)); |
| 414 | struct strbuf name = STRBUF_INIT; |
| 415 | |
| 416 | strbuf_addf(&name, "textconv/%s", driver->name); |
Nguyễn Thái Ngọc Duy | bd7ad45 | 2018-11-10 06:49:06 +0100 | [diff] [blame] | 417 | notes_cache_init(r, c, name.buf, driver->textconv); |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 418 | driver->textconv_cache = c; |
Rene Scharfe | 460c7eb | 2017-08-30 20:20:15 +0200 | [diff] [blame] | 419 | strbuf_release(&name); |
Jeff King | 3813e69 | 2011-05-23 16:30:14 -0400 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return driver; |
| 423 | } |
Ævar Arnfjörð Bjarmason | f12fa9e | 2021-04-08 17:04:19 +0200 | [diff] [blame] | 424 | |
| 425 | static int for_each_userdiff_driver_list(each_userdiff_driver_fn fn, |
| 426 | enum userdiff_driver_type type, void *cb_data, |
| 427 | struct userdiff_driver *drv, |
| 428 | int drv_size) |
| 429 | { |
| 430 | int i; |
| 431 | int ret; |
| 432 | for (i = 0; i < drv_size; i++) { |
| 433 | struct userdiff_driver *item = drv + i; |
| 434 | if ((ret = fn(item, type, cb_data))) |
| 435 | return ret; |
| 436 | } |
| 437 | return 0; |
| 438 | } |
| 439 | |
| 440 | int for_each_userdiff_driver(each_userdiff_driver_fn fn, void *cb_data) |
| 441 | { |
| 442 | int ret; |
| 443 | |
| 444 | ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_CUSTOM, |
| 445 | cb_data, drivers, ndrivers); |
| 446 | if (ret) |
| 447 | return ret; |
| 448 | |
| 449 | ret = for_each_userdiff_driver_list(fn, USERDIFF_DRIVER_TYPE_BUILTIN, |
| 450 | cb_data, builtin_drivers, |
| 451 | ARRAY_SIZE(builtin_drivers)); |
| 452 | if (ret) |
| 453 | return ret; |
| 454 | |
| 455 | return 0; |
| 456 | } |