From 270f1633613e6fac88100fe3f74d774bf43a2f39 Mon Sep 17 00:00:00 2001 From: Scott Vokes Date: Wed, 20 Dec 2023 12:16:44 -0500 Subject: [PATCH] ast_analysis: Add `prune_repeat_node`. Rather than modifying this directly, add a function, which has its own asserts and logging. --- src/libre/ast_analysis.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/libre/ast_analysis.c b/src/libre/ast_analysis.c index 987e0b86f..bc5bc6bed 100644 --- a/src/libre/ast_analysis.c +++ b/src/libre/ast_analysis.c @@ -86,6 +86,15 @@ is_unsatisfiable_or_skipped(const struct ast_expr *n) && n->u.repeat.max == 0); } +static void +prune_repeat_node(struct ast_expr *n) +{ + assert(n->type == AST_EXPR_REPEAT && n->u.repeat.min == 0); + LOG(3, "%s: setting REPEAT node %p's max count to 0\n", + __func__, (void *)n); + n->u.repeat.max = 0; +} + static enum ast_analysis_res analysis_iter(struct ast_expr *n) { @@ -937,7 +946,7 @@ analysis_iter_anchoring(struct anchoring_env *env, struct ast_expr *n) if (child->type == AST_EXPR_REPEAT && (child->flags & AST_FLAG_UNSATISFIABLE) && child->u.repeat.min == 0) { - child->u.repeat.max = 0; + prune_repeat_node(child); } } else if (!after_end_anchor && child->flags & AST_FLAG_ANCHORED_END @@ -1090,7 +1099,7 @@ analysis_iter_anchoring(struct anchoring_env *env, struct ast_expr *n) if (n->u.repeat.min == 0) { LOG(3 - LOG_ANCHORING, "%s: REPEAT: UNSATISFIABLE but can be repeated 0 times, ignoring\n", __func__); - n->u.repeat.max = 0; + prune_repeat_node(n); break; } else if (n->u.repeat.min > 0) { set_flags(n, AST_FLAG_UNSATISFIABLE); @@ -1102,7 +1111,7 @@ analysis_iter_anchoring(struct anchoring_env *env, struct ast_expr *n) if (n->u.repeat.min == 0) { LOG(3 - LOG_ANCHORING, "%s: REPEAT: analysis on child returned %d, setting repeat count to 0\n", __func__, res); - n->u.repeat.max = 0; + prune_repeat_node(n); } else { LOG(3 - LOG_ANCHORING, "%s: REPEAT: analysis on child returned %d\n", __func__, res); @@ -1134,7 +1143,7 @@ analysis_iter_anchoring(struct anchoring_env *env, struct ast_expr *n) if (orig_after_end_anchor && always_consumes_input(n) && n->u.repeat.min == 0) { - n->u.repeat.max = 0; + prune_repeat_node(n); } break; @@ -1489,7 +1498,7 @@ analysis_iter_reverse_anchoring(struct anchoring_env *env, struct ast_expr *n) if (n->u.repeat.min == 0) { LOG(3 - LOG_ANCHORING, "%s: REPEAT: UNSATISFIABLE but can be repeated 0 times, ignoring\n", __func__); - n->u.repeat.max = 0; /* skip */ + prune_repeat_node(n); break; } else if (n->u.repeat.min > 0) { LOG(3 - LOG_ANCHORING, @@ -1511,7 +1520,7 @@ analysis_iter_reverse_anchoring(struct anchoring_env *env, struct ast_expr *n) LOG(3 - LOG_ANCHORING, "%s: REPEAT: repeated group that consumes input before ^, setting max count to 0\n", __func__); - n->u.repeat.max = 0; + prune_repeat_node(n); } break;