aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bp.c10
-rw-r--r--compiler.c50
-rw-r--r--compiler.h6
-rw-r--r--file_loader.c26
-rw-r--r--file_loader.h6
-rw-r--r--grammar.c38
-rw-r--r--grammar.h6
-rw-r--r--json.c12
-rw-r--r--json.h6
-rw-r--r--printing.c12
-rw-r--r--printing.h6
-rw-r--r--types.h30
-rw-r--r--utils.c70
-rw-r--r--utils.h6
-rw-r--r--vm.c50
-rw-r--r--vm.h6
16 files changed, 171 insertions, 169 deletions
diff --git a/bp.c b/bp.c
index cd9e260..f97970a 100644
--- a/bp.c
+++ b/bp.c
@@ -1,8 +1,8 @@
-/*
- * bp.c - Source code for the bp parser
- *
- * See `man ./bp.1` for more details
- */
+//
+// bp.c - Source code for the bp parser
+//
+// See `man ./bp.1` for more details
+//
#include <fcntl.h>
#include <glob.h>
#include <limits.h>
diff --git a/compiler.c b/compiler.c
index daa6c88..74044a8 100644
--- a/compiler.c
+++ b/compiler.c
@@ -1,6 +1,6 @@
-/*
- * compiler.c - Compile strings into BP virtual machine code.
- */
+//
+// compiler.c - Compile strings into BP virtual machine code.
+//
#include <ctype.h>
#include <stdlib.h>
@@ -17,9 +17,9 @@ __attribute__((nonnull)) static vm_op_t *expand_choices(file_t *f, vm_op_t *firs
static vm_op_t *chain_together(vm_op_t *first, vm_op_t *second);
__attribute__((nonnull(1,4))) static void set_range(vm_op_t *op, ssize_t min, ssize_t max, vm_op_t *pat, vm_op_t *sep);
-/*
- * Helper function to initialize a range object.
- */
+//
+// Helper function to initialize a range object.
+//
static void set_range(vm_op_t *op, ssize_t min, ssize_t max, vm_op_t *pat, vm_op_t *sep)
{
op->type = VM_REPEAT;
@@ -39,10 +39,10 @@ static void set_range(vm_op_t *op, ssize_t min, ssize_t max, vm_op_t *pat, vm_op
}
}
-/*
- * Take an opcode and expand it into a chain of patterns if it's followed by
- * any patterns (e.g. "`x `y"), otherwise return the original input.
- */
+//
+// Take an opcode and expand it into a chain of patterns if it's followed by
+// any patterns (e.g. "`x `y"), otherwise return the original input.
+//
static vm_op_t *expand_chain(file_t *f, vm_op_t *first)
{
vm_op_t *second = bp_simplepattern(f, first->end);
@@ -54,11 +54,11 @@ static vm_op_t *expand_chain(file_t *f, vm_op_t *first)
return chain_together(first, second);
}
-/*
- * Take an opcode and parse any "=>" replacements and then expand it into a
- * chain of choices if it's followed by any "/"-separated patterns (e.g.
- * "`x/`y"), otherwise return the original input.
- */
+//
+// Take an opcode and parse any "=>" replacements and then expand it into a
+// chain of choices if it's followed by any "/"-separated patterns (e.g.
+// "`x/`y"), otherwise return the original input.
+//
static vm_op_t *expand_choices(file_t *f, vm_op_t *first)
{
first = expand_chain(f, first);
@@ -130,9 +130,9 @@ static vm_op_t *chain_together(vm_op_t *first, vm_op_t *second)
return chain;
}
-/*
- * Compile a string of BP code into virtual machine opcodes
- */
+//
+// Compile a string of BP code into virtual machine opcodes
+//
vm_op_t *bp_simplepattern(file_t *f, const char *str)
{
str = after_spaces(str);
@@ -487,9 +487,9 @@ vm_op_t *bp_simplepattern(file_t *f, const char *str)
return op;
}
-/*
- * Similar to bp_simplepattern, except that the pattern begins with an implicit, unclosable quote.
- */
+//
+// Similar to bp_simplepattern, except that the pattern begins with an implicit, unclosable quote.
+//
vm_op_t *bp_stringpattern(file_t *f, const char *str)
{
vm_op_t *ret = NULL;
@@ -553,10 +553,10 @@ vm_op_t *bp_stringpattern(file_t *f, const char *str)
return ret;
}
-/*
- * Given a pattern and a replacement string, compile the two into a replacement
- * VM opcode.
- */
+//
+// Given a pattern and a replacement string, compile the two into a replacement
+// VM opcode.
+//
vm_op_t *bp_replacement(file_t *f, vm_op_t *pat, const char *replacement)
{
vm_op_t *op = new(vm_op_t);
diff --git a/compiler.h b/compiler.h
index bae222f..66af44f 100644
--- a/compiler.h
+++ b/compiler.h
@@ -1,6 +1,6 @@
-/*
- * compiler.h - Header file for BP compiler.
- */
+//
+// compiler.h - Header file for BP compiler.
+//
#ifndef COMPILER__H
#define COMPILER__H
diff --git a/file_loader.c b/file_loader.c
index c5dc222..69b71ca 100644
--- a/file_loader.c
+++ b/file_loader.c
@@ -1,6 +1,6 @@
-/*
- * file_loader.c - Implementation of some file loading functionality.
- */
+//
+// file_loader.c - Implementation of some file loading functionality.
+//
#include <ctype.h>
#include <fcntl.h>
@@ -32,9 +32,9 @@ static void populate_lines(file_t *f)
}
}
-/*
- * Read an entire file into memory.
- */
+//
+// Read an entire file into memory.
+//
file_t *load_file(const char *filename)
{
if (filename == NULL) filename = "-";
@@ -75,9 +75,9 @@ file_t *load_file(const char *filename)
return f;
}
-/*
- * Create a virtual file from a string.
- */
+//
+// Create a virtual file from a string.
+//
file_t *spoof_file(const char *filename, char *text)
{
if (filename == NULL) filename = "";
@@ -89,10 +89,10 @@ file_t *spoof_file(const char *filename, char *text)
return f;
}
-/*
- * Ensure that the file's contents are held in memory, rather than being memory
- * mapped IO.
- */
+//
+// Ensure that the file's contents are held in memory, rather than being memory
+// mapped IO.
+//
void intern_file(file_t *f)
{
if (!f->mmapped) return;
diff --git a/file_loader.h b/file_loader.h
index 28a6281..b4e7938 100644
--- a/file_loader.h
+++ b/file_loader.h
@@ -1,6 +1,6 @@
-/*
- * file_loader.h - Definitions of an API for loading files.
- */
+//
+// file_loader.h - Definitions of an API for loading files.
+//
#ifndef FILE_LOADER__H
#define FILE_LOADER__H
diff --git a/grammar.c b/grammar.c
index 431e054..01168af 100644
--- a/grammar.c
+++ b/grammar.c
@@ -1,6 +1,6 @@
-/*
- * grammar.c - Code for defining grammars (sets of rules)
- */
+//
+// grammar.c - Code for defining grammars (sets of rules)
+//
#include <stdlib.h>
#include <string.h>
@@ -10,9 +10,9 @@
#include "grammar.h"
#include "utils.h"
-/*
- * Return a new list of definitions with one added to the front
- */
+//
+// Return a new list of definitions with one added to the front
+//
def_t *with_def(def_t *defs, file_t *f, const char *name, vm_op_t *op)
{
def_t *def = new(def_t);
@@ -23,10 +23,10 @@ def_t *with_def(def_t *defs, file_t *f, const char *name, vm_op_t *op)
return def;
}
-/*
- * Load the given grammar (semicolon-separated definitions)
- * and return the first rule defined.
- */
+//
+// Load the given grammar (semicolon-separated definitions)
+// and return the first rule defined.
+//
def_t *load_grammar(def_t *defs, file_t *f)
{
const char *src = f->contents;
@@ -52,9 +52,9 @@ def_t *load_grammar(def_t *defs, file_t *f)
return defs;
}
-/*
- * Look up a backreference or grammar definition by name
- */
+//
+// Look up a backreference or grammar definition by name
+//
vm_op_t *lookup(def_t *defs, const char *name)
{
for ( ; defs; defs = defs->next) {
@@ -64,9 +64,9 @@ vm_op_t *lookup(def_t *defs, const char *name)
return NULL;
}
-/*
- * Push a backreference onto the backreference stack
- */
+//
+// Push a backreference onto the backreference stack
+//
static def_t *with_backref(def_t *defs, file_t *f, const char *name, match_t *m)
{
vm_op_t *op = new(vm_op_t);
@@ -78,9 +78,9 @@ static def_t *with_backref(def_t *defs, file_t *f, const char *name, match_t *m)
return with_def(defs, f, name, op);
}
-/*
- * Push all the backreferences contained in a match onto the backreference stack
- */
+//
+// Push all the backreferences contained in a match onto the backreference stack
+//
def_t *with_backrefs(def_t *defs, file_t *f, match_t *m)
{
if (m->op->type != VM_REF) {
diff --git a/grammar.h b/grammar.h
index 276789d..fe7280e 100644
--- a/grammar.h
+++ b/grammar.h
@@ -1,6 +1,6 @@
-/*
- * grammar.h - Header file defining grammars (sets of rule definitions)
- */
+//
+// grammar.h - Header file defining grammars (sets of rule definitions)
+//
#ifndef GRAMMAR__H
#define GRAMMAR__H
diff --git a/json.c b/json.c
index 88a5828..f7e34fe 100644
--- a/json.c
+++ b/json.c
@@ -1,14 +1,14 @@
-/*
- * json.c - Code for printing JSON output of matches.
- */
+//
+// json.c - Code for printing JSON output of matches.
+//
#include <stdio.h>
#include "types.h"
-/*
- * Print a match as JSON
- */
+//
+// Print a match as JSON
+//
static int _json_match(const char *text, match_t *m, int comma, int verbose)
{
if (!verbose) {
diff --git a/json.h b/json.h
index 5542256..8bd972e 100644
--- a/json.h
+++ b/json.h
@@ -1,6 +1,6 @@
-/*
- * json.h - Header file for JSON output.
- */
+//
+// json.h - Header file for JSON output.
+//
#ifndef JSON__H
#define JSON__H
diff --git a/printing.c b/printing.c
index 20289da..f35957f 100644
--- a/printing.c
+++ b/printing.c
@@ -1,6 +1,6 @@
-/*
- * printing.c - Code for printing and visualizing matches.
- */
+//
+// printing.c - Code for printing and visualizing matches.
+//
#include <stdio.h>
#include <stdlib.h>
@@ -184,9 +184,9 @@ static void print_line_number(FILE *out, print_state_t *state, print_options_t o
fprintf(out, "% 5ld|", state->line);
}
-/*
- * Print a match with replacements and highlighting.
- */
+//
+// Print a match with replacements and highlighting.
+//
static void _print_match(FILE *out, file_t *f, match_t *m, print_state_t *state, print_options_t options)
{
static const char *hl = "\033[0;31;1m";
diff --git a/printing.h b/printing.h
index 7059562..37e3d41 100644
--- a/printing.h
+++ b/printing.h
@@ -1,6 +1,6 @@
-/*
- * Header file for printing.c (printing/visualizing matches)
- */
+//
+// Header file for printing.c (printing/visualizing matches)
+//
#ifndef PRINTING__H
#define PRINTING__H
diff --git a/types.h b/types.h
index 0afeb53..52b17e5 100644
--- a/types.h
+++ b/types.h
@@ -1,6 +1,6 @@
-/*
- * types.h - Datatypes used by BP
- */
+//
+// types.h - Datatypes used by BP
+//
#ifndef TYPES__H
#define TYPES__H
@@ -17,9 +17,9 @@ enum BPFlag {
BP_INPLACE = 1 << 5,
};
-/*
- * BP virtual machine opcodes (these must be kept in sync with the names in vm.c)
- */
+//
+// BP virtual machine opcodes (these must be kept in sync with the names in vm.c)
+//
enum VMOpcode {
VM_ANYCHAR = 1,
VM_STRING,
@@ -43,9 +43,9 @@ enum VMOpcode {
struct match_s; // forward declared to resolve circular struct defs
-/*
- * A struct reperesenting a BP virtual machine operation
- */
+//
+// A struct reperesenting a BP virtual machine operation
+//
typedef struct vm_op_s {
enum VMOpcode type;
const char *start, *end;
@@ -78,9 +78,9 @@ typedef struct vm_op_s {
} args;
} vm_op_t;
-/*
- * Pattern matching result object
- */
+//
+// Pattern matching result object
+//
typedef struct match_s {
// Where the match starts and ends (end is after the last character)
const char *start, *end;
@@ -88,9 +88,9 @@ typedef struct match_s {
vm_op_t *op;
} match_t;
-/*
- * Pattern matching rule definition(s)
- */
+//
+// Pattern matching rule definition(s)
+//
typedef struct def_s {
const char *name;
file_t *file;
diff --git a/utils.c b/utils.c
index f13db19..b3feb86 100644
--- a/utils.c
+++ b/utils.c
@@ -1,6 +1,6 @@
-/*
- * utils.c - Some helper code for debugging and error logging.
- */
+//
+// utils.c - Some helper code for debugging and error logging.
+//
#include <ctype.h>
#include <stdlib.h>
@@ -8,10 +8,10 @@
#include "utils.h"
-/*
- * Helper function to skip past all spaces (and comments)
- * Returns a pointer to the first non-space character.
- */
+//
+// Helper function to skip past all spaces (and comments)
+// Returns a pointer to the first non-space character.
+//
const char *after_spaces(const char *str)
{
int block_comment_depth = 0;
@@ -43,10 +43,10 @@ const char *after_spaces(const char *str)
return str;
}
-/*
- * Return the first character after a valid BP name, or NULL if none is
- * found.
- */
+//
+// Return the first character after a valid BP name, or NULL if none is
+// found.
+//
const char *after_name(const char *str)
{
if (*str == '|') return &str[1];
@@ -61,9 +61,9 @@ const char *after_name(const char *str)
return str;
}
-/*
- * Check if a character is found and if so, move past it.
- */
+//
+// Check if a character is found and if so, move past it.
+//
int matchchar(const char **str, char c)
{
const char *next = after_spaces(*str);
@@ -75,9 +75,9 @@ int matchchar(const char **str, char c)
}
}
-/*
- * Check if a string is found and if so, move past it.
- */
+//
+// Check if a string is found and if so, move past it.
+//
int matchstr(const char **str, const char *target)
{
const char *next = after_spaces(*str);
@@ -89,11 +89,11 @@ int matchstr(const char **str, const char *target)
}
}
-/*
- * Process a string escape sequence for a character and return the
- * character that was escaped.
- * Set *end = the first character past the end of the escape sequence.
- */
+//
+// Process a string escape sequence for a character and return the
+// character that was escaped.
+// Set *end = the first character past the end of the escape sequence.
+//
unsigned char unescapechar(const char *escaped, const char **end)
{
size_t len = 1;
@@ -134,10 +134,10 @@ unsigned char unescapechar(const char *escaped, const char **end)
return ret;
}
-/*
- * Write an unescaped version of `src` to `dest` (at most bufsize-1 chars,
- * terminated by a null byte)
- */
+//
+// Write an unescaped version of `src` to `dest` (at most bufsize-1 chars,
+// terminated by a null byte)
+//
size_t unescape_string(char *dest, const char *src, size_t bufsize)
{
size_t len = 0;
@@ -187,9 +187,9 @@ size_t unescape_string(char *dest, const char *src, size_t bufsize)
#undef PUT
}
-/*
- * Fail and exit if a memory value is NULL
- */
+//
+// Fail and exit if a memory value is NULL
+//
void *memcheck(void *p)
{
if (p == NULL) {
@@ -199,9 +199,9 @@ void *memcheck(void *p)
return p;
}
-/*
- * Case-insensitive memory comparison
- */
+//
+// Case-insensitive memory comparison
+//
int memicmp(const void *v1, const void *v2, size_t n)
{
int result = 0;
@@ -211,9 +211,9 @@ int memicmp(const void *v1, const void *v2, size_t n)
return result;
}
-/*
- * Free memory, but also set the pointer to NULL for safety
- */
+//
+// Free memory, but also set the pointer to NULL for safety
+//
void xfree(void *p)
{
if (*(void**)p == NULL) {
diff --git a/utils.h b/utils.h
index 8fcb345..aa2fba1 100644
--- a/utils.h
+++ b/utils.h
@@ -1,6 +1,6 @@
-/*
- * utils.h - Some utility and printing functions.
- */
+//
+// utils.h - Some utility and printing functions.
+//
#ifndef UTILS__H
#define UTILS__H
diff --git a/vm.c b/vm.c
index bcc33bd..91c29ae 100644
--- a/vm.c
+++ b/vm.c
@@ -1,6 +1,6 @@
-/*
- * vm.c - Code for the BP virtual machine that performs the matching.
- */
+//
+// vm.c - Code for the BP virtual machine that performs the matching.
+//
#include <ctype.h>
#include <stdio.h>
@@ -13,7 +13,9 @@
#include "vm.h"
+//
// UTF8-compliant char iteration
+//
static inline const char *next_char(file_t *f, const char *str)
{
char c = *str;
@@ -30,9 +32,9 @@ static inline const char *next_char(file_t *f, const char *str)
return str;
}
-/*
- * Recursively deallocate a match object and set to NULL
- */
+//
+// Recursively deallocate a match object and set to NULL
+//
void destroy_match(match_t **m)
{
if (!*m) return;
@@ -49,10 +51,10 @@ typedef struct recursive_ref_s {
match_t *result;
} recursive_ref_t;
-/*
- * Attempt to match text against a previously captured value.
- * Return the character position after the backref has matched, or NULL if no match has occurred.
- */
+//
+// Attempt to match text against a previously captured value.
+// Return the character position after the backref has matched, or NULL if no match has occurred.
+//
static const char *match_backref(const char *str, vm_op_t *op, match_t *cap, unsigned int flags)
{
check(op->type == VM_BACKREF, "Attempt to match backref against something that's not a backref");
@@ -108,11 +110,11 @@ static const char *match_backref(const char *str, vm_op_t *op, match_t *cap, uns
}
-/*
- * Run virtual machine operation against a string and return
- * a match struct, or NULL if no match is found.
- * The returned value should be free()'d to avoid memory leaking.
- */
+//
+// Run virtual machine operation against a string and return
+// a match struct, or NULL if no match is found.
+// The returned value should be free()'d to avoid memory leaking.
+//
static match_t *_match(def_t *defs, file_t *f, const char *str, vm_op_t *op, unsigned int flags, recursive_ref_t *rec)
{
switch (op->type) {
@@ -469,9 +471,9 @@ static match_t *_match(def_t *defs, file_t *f, const char *str, vm_op_t *op, uns
}
}
-/*
- * Get a specific numbered pattern capture.
- */
+//
+// Get a specific numbered pattern capture.
+//
static match_t *get_capture_by_num(match_t *m, int *n)
{
if (*n == 0) return m;
@@ -484,9 +486,9 @@ static match_t *get_capture_by_num(match_t *m, int *n)
return NULL;
}
-/*
- * Get a capture with a specific name.
- */
+//
+// Get a capture with a specific name.
+//
static match_t *get_capture_by_name(match_t *m, const char *name)
{
if (m->op->type == VM_CAPTURE && m->op->args.capture.name
@@ -499,9 +501,9 @@ static match_t *get_capture_by_name(match_t *m, const char *name)
return NULL;
}
-/*
- * Get a capture by name.
- */
+//
+// Get a capture by name.
+//
match_t *get_capture(match_t *m, const char **r)
{
if (isdigit(**r)) {
diff --git a/vm.h b/vm.h
index 189f9f4..b498b6c 100644
--- a/vm.h
+++ b/vm.h
@@ -1,6 +1,6 @@
-/*
- * vm.h - Header file for BP virtual machine.
- */
+//
+// vm.h - Header file for BP virtual machine.
+//
#ifndef VM__H
#define VM__H