@matheusbsbs / matheus-bagliano / commits / 79d3696cfb
Fork 0

git-grep: boolean expression on pattern matching.

This extends the behaviour of git-grep when multiple -e options are given. So far, we allowed multiple -e to behave just like regular grep with multiple -e, i.e. the patterns are OR'ed together. With this change, you can also have multiple patterns AND'ed together, or form boolean expressions, like this (the parentheses are quoted from the shell in this example): $ git grep -e _PATTERN --and \( -e atom -e token \) Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed Jun 30, 2006 at 03:04 UTC 79d3696cfb438467749160481c6119e43d4d2851
1 file changed +327 -51
builtin-grep.c
+327 -51
@@ -82,17 +82,47 @@ static int pathspec_matches(const char **paths, const char *name)
82 return 0;
83 }
84
85 +enum grep_pat_token {
86 + GREP_PATTERN,
87 + GREP_AND,
88 + GREP_OPEN_PAREN,
89 + GREP_CLOSE_PAREN,
90 + GREP_NOT,
91 + GREP_OR,
92 +};
93 +
94 struct grep_pat {
95 struct grep_pat *next;
96 const char *origin;
97 int no;
98 + enum grep_pat_token token;
99 const char *pattern;
100 regex_t regexp;
101 };
102
103 +enum grep_expr_node {
104 + GREP_NODE_ATOM,
105 + GREP_NODE_NOT,
106 + GREP_NODE_AND,
107 + GREP_NODE_OR,
108 +};
109 +
110 +struct grep_expr {
111 + enum grep_expr_node node;
112 + union {
113 + struct grep_pat *atom;
114 + struct grep_expr *unary;
115 + struct {
116 + struct grep_expr *left;
117 + struct grep_expr *right;
118 + } binary;
119 + } u;
120 +};
121 +
122 struct grep_opt {
123 struct grep_pat *pattern_list;
124 struct grep_pat **pattern_tail;
125 + struct grep_expr *pattern_expression;
126 regex_t regexp;
127 unsigned linenum:1;
128 unsigned invert:1;
@@ -105,43 +135,224 @@ struct grep_opt {
135 #define GREP_BINARY_NOMATCH 1
136 #define GREP_BINARY_TEXT 2
137 unsigned binary:2;
138 + unsigned extended:1;
139 int regflags;
140 unsigned pre_context;
141 unsigned post_context;
142 };
143
144 static void add_pattern(struct grep_opt *opt, const char *pat,
114 - const char *origin, int no)
145 + const char *origin, int no, enum grep_pat_token t)
146 {
147 struct grep_pat *p = xcalloc(1, sizeof(*p));
148 p->pattern = pat;
149 p->origin = origin;
150 p->no = no;
151 + p->token = t;
152 *opt->pattern_tail = p;
153 opt->pattern_tail = &p->next;
154 p->next = NULL;
155 }
156
157 +static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
158 +{
159 + int err = regcomp(&p->regexp, p->pattern, opt->regflags);
160 + if (err) {
161 + char errbuf[1024];
162 + char where[1024];
163 + if (p->no)
164 + sprintf(where, "In '%s' at %d, ",
165 + p->origin, p->no);
166 + else if (p->origin)
167 + sprintf(where, "%s, ", p->origin);
168 + else
169 + where[0] = 0;
170 + regerror(err, &p->regexp, errbuf, 1024);
171 + regfree(&p->regexp);
172 + die("%s'%s': %s", where, p->pattern, errbuf);
173 + }
174 +}
175 +
176 +#if DEBUG
177 +static inline void indent(int in)
178 +{
179 + int i;
180 + for (i = 0; i < in; i++) putchar(' ');
181 +}
182 +
183 +static void dump_pattern_exp(struct grep_expr *x, int in)
184 +{
185 + switch (x->node) {
186 + case GREP_NODE_ATOM:
187 + indent(in);
188 + puts(x->u.atom->pattern);
189 + break;
190 + case GREP_NODE_NOT:
191 + indent(in);
192 + puts("--not");
193 + dump_pattern_exp(x->u.unary, in+1);
194 + break;
195 + case GREP_NODE_AND:
196 + dump_pattern_exp(x->u.binary.left, in+1);
197 + indent(in);
198 + puts("--and");
199 + dump_pattern_exp(x->u.binary.right, in+1);
200 + break;
201 + case GREP_NODE_OR:
202 + dump_pattern_exp(x->u.binary.left, in+1);
203 + indent(in);
204 + puts("--or");
205 + dump_pattern_exp(x->u.binary.right, in+1);
206 + break;
207 + }
208 +}
209 +
210 +static void looking_at(const char *msg, struct grep_pat **list)
211 +{
212 + struct grep_pat *p = *list;
213 + fprintf(stderr, "%s: looking at ", msg);
214 + if (!p)
215 + fprintf(stderr, "empty\n");
216 + else
217 + fprintf(stderr, "<%s>\n", p->pattern);
218 +}
219 +#else
220 +#define looking_at(a,b) do {} while(0)
221 +#endif
222 +
223 +static struct grep_expr *compile_pattern_expr(struct grep_pat **);
224 +static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
225 +{
226 + struct grep_pat *p;
227 + struct grep_expr *x;
228 +
229 + looking_at("atom", list);
230 +
231 + p = *list;
232 + switch (p->token) {
233 + case GREP_PATTERN: /* atom */
234 + x = xcalloc(1, sizeof (struct grep_expr));
235 + x->node = GREP_NODE_ATOM;
236 + x->u.atom = p;
237 + *list = p->next;
238 + return x;
239 + case GREP_OPEN_PAREN:
240 + *list = p->next;
241 + x = compile_pattern_expr(list);
242 + if (!x)
243 + return NULL;
244 + if (!*list || (*list)->token != GREP_CLOSE_PAREN)
245 + die("unmatched parenthesis");
246 + *list = (*list)->next;
247 + return x;
248 + default:
249 + return NULL;
250 + }
251 +}
252 +
253 +static struct grep_expr *compile_pattern_not(struct grep_pat **list)
254 +{
255 + struct grep_pat *p;
256 + struct grep_expr *x;
257 +
258 + looking_at("not", list);
259 +
260 + p = *list;
261 + switch (p->token) {
262 + case GREP_NOT:
263 + if (!p->next)
264 + die("--not not followed by pattern expression");
265 + *list = p->next;
266 + x = xcalloc(1, sizeof (struct grep_expr));
267 + x->node = GREP_NODE_NOT;
268 + x->u.unary = compile_pattern_not(list);
269 + if (!x->u.unary)
270 + die("--not followed by non pattern expression");
271 + return x;
272 + default:
273 + return compile_pattern_atom(list);
274 + }
275 +}
276 +
277 +static struct grep_expr *compile_pattern_and(struct grep_pat **list)
278 +{
279 + struct grep_pat *p;
280 + struct grep_expr *x, *y, *z;
281 +
282 + looking_at("and", list);
283 +
284 + x = compile_pattern_not(list);
285 + p = *list;
286 + if (p && p->token == GREP_AND) {
287 + if (!p->next)
288 + die("--and not followed by pattern expression");
289 + *list = p->next;
290 + y = compile_pattern_and(list);
291 + if (!y)
292 + die("--and not followed by pattern expression");
293 + z = xcalloc(1, sizeof (struct grep_expr));
294 + z->node = GREP_NODE_AND;
295 + z->u.binary.left = x;
296 + z->u.binary.right = y;
297 + return z;
298 + }
299 + return x;
300 +}
301 +
302 +static struct grep_expr *compile_pattern_or(struct grep_pat **list)
303 +{
304 + struct grep_pat *p;
305 + struct grep_expr *x, *y, *z;
306 +
307 + looking_at("or", list);
308 +
309 + x = compile_pattern_and(list);
310 + p = *list;
311 + if (x && p && p->token != GREP_CLOSE_PAREN) {
312 + y = compile_pattern_or(list);
313 + if (!y)
314 + die("not a pattern expression %s", p->pattern);
315 + z = xcalloc(1, sizeof (struct grep_expr));
316 + z->node = GREP_NODE_OR;
317 + z->u.binary.left = x;
318 + z->u.binary.right = y;
319 + return z;
320 + }
321 + return x;
322 +}
323 +
324 +static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
325 +{
326 + looking_at("expr", list);
327 +
328 + return compile_pattern_or(list);
329 +}
330 +
331 static void compile_patterns(struct grep_opt *opt)
332 {
333 struct grep_pat *p;
334 +
335 + /* First compile regexps */
336 for (p = opt->pattern_list; p; p = p->next) {
129 - int err = regcomp(&p->regexp, p->pattern, opt->regflags);
130 - if (err) {
131 - char errbuf[1024];
132 - char where[1024];
133 - if (p->no)
134 - sprintf(where, "In '%s' at %d, ",
135 - p->origin, p->no);
136 - else if (p->origin)
137 - sprintf(where, "%s, ", p->origin);
138 - else
139 - where[0] = 0;
140 - regerror(err, &p->regexp, errbuf, 1024);
141 - regfree(&p->regexp);
142 - die("%s'%s': %s", where, p->pattern, errbuf);
143 - }
337 + if (p->token == GREP_PATTERN)
338 + compile_regexp(p, opt);
339 + else
340 + opt->extended = 1;
341 }
342 +
343 + if (!opt->extended)
344 + return;
345 +
346 + /* Then bundle them up in an expression.
347 + * A classic recursive descent parser would do.
348 + */
349 + p = opt->pattern_list;
350 + opt->pattern_expression = compile_pattern_expr(&p);
351 +#if DEBUG
352 + dump_pattern_exp(opt->pattern_expression, 0);
353 +#endif
354 + if (p)
355 + die("incomplete pattern expression: %s", p->pattern);
356 }
357
358 static char *end_of_line(char *cp, unsigned long *left)
@@ -196,6 +407,79 @@ static int fixmatch(const char *pattern, char *line, regmatch_t *match)
407 }
408 }
409
410 +static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
411 +{
412 + int hit = 0;
413 + regmatch_t pmatch[10];
414 +
415 + if (!opt->fixed) {
416 + regex_t *exp = &p->regexp;
417 + hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
418 + pmatch, 0);
419 + }
420 + else {
421 + hit = !fixmatch(p->pattern, bol, pmatch);
422 + }
423 +
424 + if (hit && opt->word_regexp) {
425 + /* Match beginning must be either
426 + * beginning of the line, or at word
427 + * boundary (i.e. the last char must
428 + * not be alnum or underscore).
429 + */
430 + if ((pmatch[0].rm_so < 0) ||
431 + (eol - bol) <= pmatch[0].rm_so ||
432 + (pmatch[0].rm_eo < 0) ||
433 + (eol - bol) < pmatch[0].rm_eo)
434 + die("regexp returned nonsense");
435 + if (pmatch[0].rm_so != 0 &&
436 + word_char(bol[pmatch[0].rm_so-1]))
437 + hit = 0;
438 + if (pmatch[0].rm_eo != (eol-bol) &&
439 + word_char(bol[pmatch[0].rm_eo]))
440 + hit = 0;
441 + }
442 + return hit;
443 +}
444 +
445 +static int match_expr_eval(struct grep_opt *opt,
446 + struct grep_expr *x,
447 + char *bol, char *eol)
448 +{
449 + switch (x->node) {
450 + case GREP_NODE_ATOM:
451 + return match_one_pattern(opt, x->u.atom, bol, eol);
452 + break;
453 + case GREP_NODE_NOT:
454 + return !match_expr_eval(opt, x->u.unary, bol, eol);
455 + case GREP_NODE_AND:
456 + return (match_expr_eval(opt, x->u.binary.left, bol, eol) &&
457 + match_expr_eval(opt, x->u.binary.right, bol, eol));
458 + case GREP_NODE_OR:
459 + return (match_expr_eval(opt, x->u.binary.left, bol, eol) ||
460 + match_expr_eval(opt, x->u.binary.right, bol, eol));
461 + }
462 + die("Unexpected node type (internal error) %d\n", x->node);
463 +}
464 +
465 +static int match_expr(struct grep_opt *opt, char *bol, char *eol)
466 +{
467 + struct grep_expr *x = opt->pattern_expression;
468 + return match_expr_eval(opt, x, bol, eol);
469 +}
470 +
471 +static int match_line(struct grep_opt *opt, char *bol, char *eol)
472 +{
473 + struct grep_pat *p;
474 + if (opt->extended)
475 + return match_expr(opt, bol, eol);
476 + for (p = opt->pattern_list; p; p = p->next) {
477 + if (match_one_pattern(opt, p, bol, eol))
478 + return 1;
479 + }
480 + return 0;
481 +}
482 +
483 static int grep_buffer(struct grep_opt *opt, const char *name,
484 char *buf, unsigned long size)
485 {
@@ -231,46 +515,15 @@ static int grep_buffer(struct grep_opt *opt, const char *name,
515 hunk_mark = "--\n";
516
517 while (left) {
234 - regmatch_t pmatch[10];
518 char *eol, ch;
519 int hit = 0;
237 - struct grep_pat *p;
520
521 eol = end_of_line(bol, &left);
522 ch = *eol;
523 *eol = 0;
524
243 - for (p = opt->pattern_list; p; p = p->next) {
244 - if (!opt->fixed) {
245 - regex_t *exp = &p->regexp;
246 - hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
247 - pmatch, 0);
248 - }
249 - else {
250 - hit = !fixmatch(p->pattern, bol, pmatch);
251 - }
525 + hit = match_line(opt, bol, eol);
526
253 - if (hit && opt->word_regexp) {
254 - /* Match beginning must be either
255 - * beginning of the line, or at word
256 - * boundary (i.e. the last char must
257 - * not be alnum or underscore).
258 - */
259 - if ((pmatch[0].rm_so < 0) ||
260 - (eol - bol) <= pmatch[0].rm_so ||
261 - (pmatch[0].rm_eo < 0) ||
262 - (eol - bol) < pmatch[0].rm_eo)
263 - die("regexp returned nonsense");
264 - if (pmatch[0].rm_so != 0 &&
265 - word_char(bol[pmatch[0].rm_so-1]))
266 - hit = 0;
267 - if (pmatch[0].rm_eo != (eol-bol) &&
268 - word_char(bol[pmatch[0].rm_eo]))
269 - hit = 0;
270 - }
271 - if (hit)
272 - break;
273 - }
527 /* "grep -v -e foo -e bla" should list lines
528 * that do not have either, so inversion should
529 * be done outside.
@@ -452,6 +705,8 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
705 char *argptr = randarg;
706 struct grep_pat *p;
707
708 + if (opt->extended)
709 + return -1;
710 len = nr = 0;
711 push_arg("grep");
712 if (opt->fixed)
@@ -813,16 +1068,36 @@ int cmd_grep(int argc, const char **argv, char **envp)
1068 /* ignore empty line like grep does */
1069 if (!buf[0])
1070 continue;
816 - add_pattern(&opt, strdup(buf), argv[1], ++lno);
1071 + add_pattern(&opt, strdup(buf), argv[1], ++lno,
1072 + GREP_PATTERN);
1073 }
1074 fclose(patterns);
1075 argv++;
1076 argc--;
1077 continue;
1078 }
1079 + if (!strcmp("--not", arg)) {
1080 + add_pattern(&opt, arg, "command line", 0, GREP_NOT);
1081 + continue;
1082 + }
1083 + if (!strcmp("--and", arg)) {
1084 + add_pattern(&opt, arg, "command line", 0, GREP_AND);
1085 + continue;
1086 + }
1087 + if (!strcmp("--or", arg))
1088 + continue; /* no-op */
1089 + if (!strcmp("(", arg)) {
1090 + add_pattern(&opt, arg, "command line", 0, GREP_OPEN_PAREN);
1091 + continue;
1092 + }
1093 + if (!strcmp(")", arg)) {
1094 + add_pattern(&opt, arg, "command line", 0, GREP_CLOSE_PAREN);
1095 + continue;
1096 + }
1097 if (!strcmp("-e", arg)) {
1098 if (1 < argc) {
825 - add_pattern(&opt, argv[1], "-e option", 0);
1099 + add_pattern(&opt, argv[1], "-e option", 0,
1100 + GREP_PATTERN);
1101 argv++;
1102 argc--;
1103 continue;
@@ -840,7 +1115,8 @@ int cmd_grep(int argc, const char **argv, char **envp)
1115
1116 /* First unrecognized non-option token */
1117 if (!opt.pattern_list) {
843 - add_pattern(&opt, arg, "command line", 0);
1118 + add_pattern(&opt, arg, "command line", 0,
1119 + GREP_PATTERN);
1120 break;
1121 }
1122 else {