Skip to content

Commit f513fae

Browse files
committed
Rewrite (?:|xyz) and (?:xyz|) to (?:xyz)?.
1 parent c36e173 commit f513fae

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/libre/ast_rewrite.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,28 @@ rewrite_alt(struct ast_expr *n, enum re_flags flags)
317317
return 1;
318318
}
319319

320+
if (n->u.alt.count == 2) {
321+
struct ast_expr *child = NULL;
322+
323+
if (n->u.alt.n[0]->type == AST_EXPR_EMPTY) {
324+
ast_expr_free(n->u.alt.n[0]);
325+
child = n->u.alt.n[1];
326+
} else if (n->u.alt.n[1]->type == AST_EXPR_EMPTY) {
327+
ast_expr_free(n->u.alt.n[1]);
328+
child = n->u.alt.n[0];
329+
}
330+
331+
if (child != NULL) {
332+
/* we repurpose the same node */
333+
n->type = AST_EXPR_REPEAT;
334+
n->u.repeat.e = child;
335+
n->u.repeat.min = 0;
336+
n->u.repeat.max = 1;
337+
338+
return rewrite(n, flags);
339+
}
340+
}
341+
320342
return 1;
321343

322344
empty:

0 commit comments

Comments
 (0)