aboutsummaryrefslogtreecommitdiff
path: root/match.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2022-05-02 17:25:18 -0400
committerBruce Hill <bruce@bruce-hill.com>2022-05-02 17:25:18 -0400
commitaf668004e85e99858df4a025af82c4602e0ab399 (patch)
tree09f9213ef9780ac4fcbd3b96c421ee09eaa5e128 /match.c
parenteb0f18dc7956a1f3ee51c20dedcc02b5304d5ce8 (diff)
Updated Make rules so default is `bp`
Diffstat (limited to 'match.c')
-rw-r--r--match.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/match.c b/match.c
index 8231fc0..474bd85 100644
--- a/match.c
+++ b/match.c
@@ -565,7 +565,22 @@ static match_t *match(match_ctx_t *ctx, const char *str, pat_t *pat)
if (m1->pat->type == BP_CAPTURE && m1->pat->args.capture.name) {
// Temporarily add a rule that the backref name matches the
// exact string of the original match (no replacements)
- pat_t *backref = bp_raw_literal(m1->start, (size_t)(m1->end - m1->start));
+ pat_t *backref;
+ if (m1->children && m1->children[0]->pat->type == BP_CURDENT) {
+ const char *linestart = m1->start;
+ while (linestart > ctx->start && linestart[-1] != '\n') --linestart;
+
+ // Current indentation:
+ char denter = *linestart;
+ size_t dents = 0;
+ if (denter == ' ' || denter == '\t') {
+ while (linestart[dents] == denter && &linestart[dents] < ctx->end)
+ ++dents;
+ }
+ backref = bp_raw_literal(linestart, dents);
+ } else {
+ backref = bp_raw_literal(m1->start, (size_t)(m1->end - m1->start));
+ }
match_ctx_t ctx2 = *ctx;
ctx2.cache = &(cache_t){0};
ctx2.parent_ctx = ctx;
@@ -711,6 +726,9 @@ static match_t *match(match_ctx_t *ctx, const char *str, pat_t *pat)
return new_match(pat, start, &str[dents], NULL);
}
+ case BP_CURDENT: {
+ return new_match(pat, str, str, NULL);
+ }
default: {
errx(EXIT_FAILURE, "Unknown pattern type: %u", pat->type);
return NULL;