From af668004e85e99858df4a025af82c4602e0ab399 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 2 May 2022 17:25:18 -0400 Subject: [PATCH] Updated Make rules so default is `bp` --- Makefile | 8 ++++---- match.c | 20 +++++++++++++++++++- pattern.c | 3 +++ pattern.h | 17 +++++++++-------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index fa0131b..2afb00d 100644 --- a/Makefile +++ b/Makefile @@ -25,14 +25,14 @@ ALL_FLAGS=$(CFLAGS) $(OSFLAGS) -DBP_NAME="\"$(NAME)\"" $(EXTRA) $(CWARN) $(G) $( CFILES=pattern.c utils.c match.c files.c printmatch.c json.c utf8.c OBJFILES=$(CFILES:.c=.o) +$(NAME): $(OBJFILES) bp.c + $(CC) $(ALL_FLAGS) -o $@ $(OBJFILES) bp.c + all: $(NAME) bp.1 lua %.o: %.c %.h utf8.h $(CC) -c $(ALL_FLAGS) -o $@ $< -$(NAME): $(OBJFILES) bp.c - $(CC) $(ALL_FLAGS) -o $@ $(OBJFILES) bp.c - bp.1: bp.1.md pandoc --lua-filter=.pandoc/bold-code.lua -s $< -t man -o $@ @@ -43,7 +43,7 @@ clean: rm -f $(NAME) $(OBJFILES) lua: - cd Lua && make + @cd Lua && make test: $(NAME) ./$(NAME) Comment -r '[@0]' >/dev/null 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; diff --git a/pattern.c b/pattern.c index d0e364f..83ff0a1 100644 --- a/pattern.c +++ b/pattern.c @@ -320,6 +320,9 @@ static pat_t *_bp_simplepattern(const char *str, const char *end, bool inside_st if (*str == 'N') { // \N (nodent) all = either_pat(all, new_pat(BP_NODENT, itemstart, ++str, 1, -1)); continue; + } else if (*str == 'C') { // \C (current indent) + all = either_pat(all, new_pat(BP_CURDENT, itemstart, ++str, 1, -1)); + continue; } else if (*str == 'i') { // \i (identifier char) all = either_pat(all, new_pat(BP_ID_CONTINUE, itemstart, ++str, 1, -1)); continue; diff --git a/pattern.h b/pattern.h index 8dc40d2..89d57b8 100644 --- a/pattern.h +++ b/pattern.h @@ -29,14 +29,15 @@ enum pattype_e { BP_REPLACE = 17, BP_REF = 18, BP_NODENT = 19, - BP_START_OF_FILE = 20, - BP_START_OF_LINE = 21, - BP_END_OF_FILE = 22, - BP_END_OF_LINE = 23, - BP_WORD_BOUNDARY = 24, - BP_DEFINITIONS = 25, - BP_TAGGED = 26, - BP_LEFTRECURSION = 27, + BP_CURDENT = 20, + BP_START_OF_FILE = 21, + BP_START_OF_LINE = 22, + BP_END_OF_FILE = 23, + BP_END_OF_LINE = 24, + BP_WORD_BOUNDARY = 25, + BP_DEFINITIONS = 26, + BP_TAGGED = 27, + BP_LEFTRECURSION = 28, }; //