Compare commits

...

3 Commits

Author SHA1 Message Date
bddb3885b6 HOOK_ALL is no longer a macro 2023-08-24 21:09:45 +02:00
c25b28304f badass memory bug fix 2023-08-24 21:09:45 +02:00
570f15f635 fixed initialization bugs 2023-08-24 21:09:21 +02:00
3 changed files with 77 additions and 66 deletions

View File

@ -76,21 +76,12 @@ int append_token(token_t * token) {
token_t * new_symbol_token(const char * const c,
hl_group_t * const g) {
char * new_word;
if (is_magic(*c)) {
new_word = (char *)malloc(sizeof(char)*3);
new_word[0] = '\\';
new_word[1] = *c;
new_word[2] = '\00';
} else {
new_word = strdup(c);
}
token_t * mt = (token_t*)malloc(sizeof(token_t));
mt->hl = g;
mt->t = KEYSYMBOL;
mt->syntax = regex_compile(new_word);
mt->syntax = regex_compile(c);
append_token(mt);
@ -113,14 +104,15 @@ int new_symbol_tokens(const char * const * symbols,
int new_char_tokens(const char * characters,
hl_group_t * const g) {
int i = 0;
char buffer[2] = "";
int i = 0;
buffer[1] = '\00';
char buffer[3];
buffer[0] = '\\';
buffer[2] = '\0';
for(const char * s = characters; *s != '\0'; s++) {
buffer[0] = *s;
if(new_symbol_token(buffer, g)) {
buffer[1] = *s;
if(new_symbol_token(is_magic(*s) ? buffer : buffer + 1, g)) {
++i;
}
}
@ -130,7 +122,7 @@ int new_char_tokens(const char * characters,
token_t * new_keyword_token(const char * const word,
hl_group_t * const g) {
char * new_word = strdup(word);
//char * new_word = strdup(word);
//size_t word_length = strlen(word);
//char * new_word = (char*)malloc(word_length + 4 + 1);
@ -142,7 +134,8 @@ token_t * new_keyword_token(const char * const word,
mt->hl = g;
mt->t = KEYWORD;
mt->syntax = regex_compile(new_word);
//mt->syntax = regex_compile(new_word);
mt->syntax = regex_compile(word);
append_token(mt);
@ -198,8 +191,8 @@ int token_fits(const token_t * const token,
void render_string(const char * const string,
const char * const mode) {
for (const char * s = string; *s != '\00';) {
int f;
int token_index = 0;
int f = 0;
size_t token_index = 0;
int offset;
for (; token_index < token_table.element_count; token_index++) {
@ -254,7 +247,7 @@ int hl_init(void) {
}
int hl_deinit(void) {
for (int i = 0; i < token_table.element_count; i++) {
for (size_t i = 0; i < token_table.element_count; i++) {
free_token(vector_get(&token_table, i));
}

View File

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

View File

@ -35,35 +35,29 @@ int terminal_hl_init(void){
hl_init();
new_display_mode(cterm);
//
terminal_hl_t terminal_keyword_hl = (terminal_hl_t) {
.attribute = TERMINAL_STYLE_BOLD,
.foreground_color = TERMINAL_COLOR_FG_GREEN,
.background_color = NULL
};
keyword_hl = &(hl_group_t) {
.link = NULL,
.attributes = (void*)&terminal_keyword_hl
};
terminal_hl_t * terminal_keyword_hl = (terminal_hl_t *)malloc(sizeof(terminal_hl_t));
terminal_keyword_hl->attribute = TERMINAL_STYLE_BOLD;
terminal_keyword_hl->foreground_color = TERMINAL_COLOR_FG_GREEN;;
terminal_keyword_hl->background_color = NULL;
keyword_hl = (hl_group_t *)malloc(sizeof(hl_group_t));
keyword_hl->link = NULL;
keyword_hl->attributes = (void*)terminal_keyword_hl;
//
terminal_hl_t terminal_preprocessor_hl = (terminal_hl_t) {
.attribute = TERMINAL_STYLE_BOLD,
.foreground_color = TERMINAL_COLOR_FG_BLUE,
.background_color = NULL
};
preprocessor_hl = &(hl_group_t) {
.link = NULL,
.attributes = (void*)&terminal_preprocessor_hl
};
terminal_hl_t * terminal_preprocessor_hl = (terminal_hl_t *)malloc(sizeof(terminal_hl_t));
terminal_preprocessor_hl->attribute = TERMINAL_STYLE_BOLD,
terminal_preprocessor_hl->foreground_color = TERMINAL_COLOR_FG_BLUE;
terminal_preprocessor_hl->background_color = NULL;
preprocessor_hl = (hl_group_t *)malloc(sizeof(hl_group_t));
preprocessor_hl->link = NULL;
preprocessor_hl->attributes = (void*)terminal_keyword_hl;
//
terminal_hl_t terminal_symbol_hl = (terminal_hl_t) {
.attribute = TERMINAL_STYLE_BOLD,
.foreground_color = TERMINAL_COLOR_FG_YELLOW,
.background_color = NULL
};
symbol_hl = &(hl_group_t) {
.link = NULL,
.attributes = (void*)&terminal_symbol_hl
};
terminal_hl_t * terminal_symbol_hl = (terminal_hl_t *)malloc(sizeof(terminal_hl_t));
terminal_symbol_hl->attribute = TERMINAL_STYLE_BOLD;
terminal_symbol_hl->foreground_color = TERMINAL_COLOR_FG_YELLOW;
terminal_symbol_hl->background_color = NULL;
symbol_hl = (hl_group_t *)malloc(sizeof(hl_group_t));
symbol_hl->link = NULL;
symbol_hl->attributes = (void*)terminal_symbol_hl;
return 0;
}