HOOK_ALL is no longer a macro

This commit is contained in:
anon 2023-08-24 21:08:31 +02:00
parent c25b28304f
commit bddb3885b6

View File

@ -48,6 +48,15 @@ typedef struct {
int to; int to;
} offshoot_t; } offshoot_t;
typedef struct {
bool * do_catch;
bool * is_negative;
int * state;
int * width;
char * whitelist;
regex_t * regex;
} compiler_state;
// ---------------------------------- // ----------------------------------
@ -317,19 +326,24 @@ static bool catch_(const regex_t * const regex,
#define HALT_AND_CATCH_FIRE -1 #define HALT_AND_CATCH_FIRE -1
#define HOOK_ALL(from, str, to) do { \ void HOOK_ALL( int from,
int hook_to = (is_negative) ? -1 : state + to; \ const char * const str,
for (char * s = str; *s != '\0'; s++) { \ int to,
vector_push(&regex->delta_table, \ compiler_state * cs) {
&(delta_t){state + from, *s, hook_to, width} \
); \ int hook_to = (*cs->is_negative) ? HALT_AND_CATCH_FIRE : *cs->state + to;
} \
if (do_catch || is_negative) { \ for (const char * s = str; *s != '\0'; s++) {
vector_push(&regex->catch_table, \ vector_push(&cs->regex->delta_table,
&(offshoot_t){state + from, hook_to} \ &(delta_t){*cs->state + from, *s, hook_to, *cs->width}
); \ );
} \ }
} while (0) if (cs->do_catch || cs->is_negative) {
vector_push(&cs->regex->catch_table,
&(offshoot_t){*cs->state + from, hook_to}
);
}
}
#define EAT(n) do { \ #define EAT(n) do { \
s += n; \ s += n; \
@ -343,10 +357,20 @@ regex_t * regex_compile(const char * const pattern) {
int state = 0; int state = 0;
char whitelist[64];
bool do_catch; bool do_catch;
bool is_negative; bool is_negative;
int width; int width;
char whitelist[64];
compiler_state cs = {
.do_catch = &do_catch,
.is_negative = &is_negative,
.state = &state,
.width = &width,
.whitelist = whitelist,
.regex = regex,
};
for (const char * s = pattern; *s != '\00';) { for (const char * s = pattern; *s != '\00';) {
// Get token // Get token
assert(!is_quantifier(*pattern) && "Pattern starts with quantifier."); assert(!is_quantifier(*pattern) && "Pattern starts with quantifier.");
@ -385,21 +409,21 @@ regex_t * regex_compile(const char * const pattern) {
switch (*s) { switch (*s) {
case '=': case '=':
case '?': { case '?': {
HOOK_ALL(0, whitelist, +1); HOOK_ALL(0, whitelist, +1, &cs);
EAT(1); EAT(1);
} break; } break;
case '*': { case '*': {
HOOK_ALL(0, whitelist, 0); HOOK_ALL(0, whitelist, 0, &cs);
EAT(1); EAT(1);
} break; } break;
case '+': { case '+': {
HOOK_ALL(0, whitelist, +1); HOOK_ALL(0, whitelist, +1, &cs);
state += 1; state += 1;
HOOK_ALL(0, whitelist, 0); HOOK_ALL(0, whitelist, 0, &cs);
EAT(1); EAT(1);
} break; } break;
default: { // Literal default: { // Literal
HOOK_ALL(0, whitelist, +1); HOOK_ALL(0, whitelist, +1, &cs);
state += 1; state += 1;
} break; } break;
} }