bp/types.h

112 lines
2.2 KiB
C
Raw Normal View History

//
// types.h - Datatypes used by BP
//
2020-09-11 01:28:06 -07:00
#ifndef TYPES__H
#define TYPES__H
2020-09-07 23:22:43 -07:00
2020-09-11 01:28:06 -07:00
#include <sys/types.h>
2020-09-07 23:22:43 -07:00
2020-09-16 19:35:43 -07:00
#include "file_loader.h"
2020-12-30 19:42:47 -08:00
enum BPFlag {
BP_VERBOSE = 1 << 0,
BP_IGNORECASE = 1 << 1,
BP_EXPLAIN = 1 << 2,
BP_JSON = 1 << 3,
BP_LISTFILES = 1 << 4,
BP_INPLACE = 1 << 5,
2020-09-14 12:16:15 -07:00
};
//
// BP virtual machine opcodes (these must be kept in sync with the names in vm.c)
//
2020-09-07 23:34:41 -07:00
enum VMOpcode {
VM_ANYCHAR = 1,
VM_STRING,
VM_RANGE,
VM_NOT,
VM_UPTO_AND,
2020-09-07 23:34:41 -07:00
VM_REPEAT,
VM_BEFORE,
VM_AFTER,
VM_CAPTURE,
VM_HIDE,
2020-09-07 23:34:41 -07:00
VM_OTHERWISE,
VM_CHAIN,
2020-09-13 20:33:11 -07:00
VM_EQUAL,
2020-09-28 17:56:02 -07:00
VM_NOT_EQUAL,
2020-09-07 23:34:41 -07:00
VM_REPLACE,
VM_REF,
2020-09-12 18:20:13 -07:00
VM_BACKREF,
2020-09-14 01:21:49 -07:00
VM_NODENT,
2020-09-07 23:34:41 -07:00
};
struct match_s; // forward declared to resolve circular struct defs
//
// A struct reperesenting a BP virtual machine operation
//
2020-09-07 23:34:41 -07:00
typedef struct vm_op_s {
2021-01-12 19:27:57 -08:00
enum VMOpcode type;
2020-09-07 23:34:41 -07:00
const char *start, *end;
// Length of the match, if constant, otherwise -1
ssize_t len;
union {
const char *s;
struct {
2020-09-28 21:30:43 -07:00
unsigned char low, high;
2020-09-07 23:34:41 -07:00
} range;
struct {
ssize_t min, max;
struct vm_op_s *sep, *repeat_pat;
} repetitions;
// TODO: use a linked list instead of a binary tree
2020-09-07 23:34:41 -07:00
struct {
struct vm_op_s *first, *second;
} multiple;
struct {
struct vm_op_s *pat;
const char *text;
size_t len;
2020-09-07 23:34:41 -07:00
} replace;
struct {
struct vm_op_s *capture_pat;
char *name;
} capture;
struct match_s *backref;
2020-09-07 23:34:41 -07:00
struct vm_op_s *pat;
} args;
} vm_op_t;
//
// Pattern matching result object
//
typedef struct match_s {
// Where the match starts and ends (end is after the last character)
const char *start, *end;
struct match_s *child, *nextsibling;
vm_op_t *op;
} match_t;
2020-09-07 23:34:41 -07:00
//
// Pattern matching rule definition(s)
//
typedef struct def_s {
2020-09-07 23:22:43 -07:00
const char *name;
2020-09-16 19:35:43 -07:00
file_t *file;
2020-09-07 23:22:43 -07:00
vm_op_t *op;
struct def_s *next;
2020-09-07 23:22:43 -07:00
} def_t;
//
// Structure used for tracking allocated ops, which must be freed when the file
// is freed.
//
typedef struct allocated_op_s {
struct allocated_op_s *next;
vm_op_t op;
} allocated_op_t;
2020-09-11 01:28:06 -07:00
#endif
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1