Adding comments

This commit is contained in:
Bruce Hill 2021-01-12 21:28:44 -08:00
parent b27b71608b
commit 5811ff4554
5 changed files with 55 additions and 4 deletions

View File

@ -581,6 +581,9 @@ vm_op_t *bp_replacement(file_t *f, vm_op_t *pat, const char *replacement)
return op;
}
//
// Compile a string representing a BP pattern into an opcode object.
//
vm_op_t *bp_pattern(file_t *f, const char *str)
{
vm_op_t *op = bp_simplepattern(f, str);

View File

@ -15,6 +15,10 @@
#include "file_loader.h"
#include "utils.h"
//
// In the file object, populate the `lines` array with pointers to the
// beginning of each line.
//
static void populate_lines(file_t *f)
{
// Calculate line numbers:
@ -107,6 +111,10 @@ void intern_file(file_t *f)
populate_lines(f);
}
//
// Free a file and all memory contained inside its members, then set the input
// pointer to NULL.
//
void destroy_file(file_t **f)
{
if ((*f)->filename) {
@ -128,6 +136,9 @@ void destroy_file(file_t **f)
xfree(f);
}
//
// Given a pointer, determine which line number it points to.
//
size_t get_line_number(file_t *f, const char *p)
{
// TODO: binary search
@ -138,18 +149,28 @@ size_t get_line_number(file_t *f, const char *p)
return f->nlines;
}
//
// Given a pointer, determine which character offset within the line it points to.
//
size_t get_char_number(file_t *f, const char *p)
{
size_t linenum = get_line_number(f, p);
return 1 + (size_t)(p - f->lines[linenum-1]);
}
//
// Return a pointer to the line with the specified line number.
//
const char *get_line(file_t *f, size_t line_number)
{
if (line_number == 0 || line_number > f->nlines) return NULL;
return f->lines[line_number - 1];
}
//
// Print the filename/line number, followed by the given message, followed by
// the line itself.
//
void fprint_line(FILE *dest, file_t *f, const char *start, const char *end, const char *fmt, ...)
{
if (start < f->contents) start = f->contents;

7
json.c
View File

@ -7,7 +7,9 @@
#include "types.h"
//
// Print a match as JSON
// Helper function for json_match().
// `comma` is used to track whether a comma will need to be printed before the
// next object or not.
//
static int _json_match(const char *text, match_t *m, int comma, int verbose)
{
@ -41,6 +43,9 @@ static int _json_match(const char *text, match_t *m, int comma, int verbose)
return 1;
}
//
// Print a match object as a JSON object.
//
void json_match(const char *text, match_t *m, int verbose)
{
_json_match(text, m, 0, verbose);

View File

@ -21,6 +21,10 @@ typedef struct {
const char *color;
} print_state_t;
//
// Return the height of a match object (i.e. the number of descendents of the
// structure).
//
static int match_height(match_t *m)
{
int height = 0;
@ -31,6 +35,9 @@ static int match_height(match_t *m)
return 1 + height;
}
//
// Print a visual explanation for the as-yet-unprinted matches provided.
//
static void _visualize_matches(match_node_t *firstmatch, int depth, const char *text, size_t textlen)
{
if (!firstmatch) return;
@ -153,6 +160,10 @@ static void _visualize_matches(match_node_t *firstmatch, int depth, const char *
}
}
//
// Recursively look for references to a rule called "pattern" and print an
// explanation for each one.
//
static void _visualize_patterns(match_t *m)
{
if (m->op->type == VM_REF && streq(m->op->args.s, "pattern")) {
@ -165,15 +176,20 @@ static void _visualize_patterns(match_t *m)
}
}
//
// For a match object, print a visual explanation for each "pattern" matched
// inside it.
//
void visualize_match(match_t *m)
{
printf("\033[?7l");
//match_node_t first = {.m = m};
//_visualize_matches(&first, 0, m->start, (m->end - m->start));
_visualize_patterns(m);
printf("\033[?7h");
}
//
// Print a line number.
//
static void print_line_number(FILE *out, print_state_t *state, print_options_t options)
{
state->printed_line = state->line;
@ -185,7 +201,7 @@ static void print_line_number(FILE *out, print_state_t *state, print_options_t o
}
//
// Print a match with replacements and highlighting.
// Helper function for print_match(), using a struct to keep track of some state.
//
static void _print_match(FILE *out, file_t *f, match_t *m, print_state_t *state, print_options_t options)
{
@ -275,6 +291,9 @@ static void _print_match(FILE *out, file_t *f, match_t *m, print_state_t *state,
}
}
//
// Print a match with replacements and highlighting.
//
void print_match(FILE *out, file_t *f, match_t *m, print_options_t options)
{
print_state_t state = {.line = 1, .color = "\033[0m"};

3
vm.c
View File

@ -522,6 +522,9 @@ match_t *get_capture(match_t *m, const char **r)
return NULL;
}
//
// Wrapper function for _match() to kickstart the recursion info.
//
match_t *match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned int flags)
{
return _match(defs, f, str, op, flags, NULL);