This commit is contained in:
anon 2023-08-24 03:17:00 +02:00
commit e82948f35d

View File

@ -8,21 +8,6 @@
// ------------------- // -------------------
// ### Definitions ### // ### Definitions ###
// ------------------- // -------------------
typedef void (*attribute_callback_t)(const char * const string,
const int length,
void * const attributes);
typedef struct {
char * key;
attribute_callback_t callback;
UT_hash_handle hh;
} display_t;
display_t * display_table = NULL;
typedef struct {
void * attributes;
struct hl_group_t * link;
} hl_group_t;
typedef enum { typedef enum {
KEYSYMBOL, KEYSYMBOL,
@ -32,63 +17,77 @@ typedef enum {
} token_type_t; } token_type_t;
typedef struct { typedef struct {
hl_group_t * hl; char * key;
token_type_t t; attribute_callback_t callback;
char* syntax; UT_hash_handle hh;
} display_t;
typedef struct {
void * attributes;
struct hl_group_t * link;
} hl_group_t;
typedef struct {
hl_group_t * hl;
token_type_t t;
char * syntax;
} token_t; } token_t;
/* Temp solution typedef void (*attribute_callback_t) (const char * const string,
* this should be dynamic const int length,
*/ void * const attributes);
// GLOBALS
token_t * token_table[1000]; token_t * token_table[1000];
int token_table_top = 0; int token_table_top = 0;
display_t * display_table = NULL;
// -------------------------------- // --------------------------------
// ### Constructors/Destructors ### // ### Constructors/Destructors ###
// -------------------------------- // --------------------------------
void new_display_mode(display_t * mode) { void new_display_mode(display_t * mode) {
HASH_ADD_STR(display_table, HASH_ADD_STR(display_table,
key, key,
mode); mode);
} }
int free_token(token_t * token){ int free_token(token_t * token) {
/* XXX: since hl could be shared,
* this might free an already freed object
* when invoked from a loop
*/
free(token->hl); free(token->hl);
free(token->syntax); free(token->syntax);
return 0; return 0;
} }
int append_token(token_t * token){ int append_token(token_t * token) {
token_table[token_table_top++] = token; token_table[token_table_top++] = token;
return 0; return 0;
} }
token_t * new_symbol_token(const char * const word, token_t * new_symbol_token(const char * const word,
hl_group_t * const g) { hl_group_t * const g) {
char * new_word = strdup(word); char * new_word = strdup(word);
token_t * mt = (token_t*)malloc(sizeof(token_t)); token_t * mt = (token_t*)malloc(sizeof(token_t));
mt->hl = g;
mt->t = KEYSYMBOL;
mt->syntax = new_word;
append_token(mt);
return mt;
mt->hl = g;
mt->t = KEYSYMBOL;
mt->syntax = new_word;
append_token(mt);
return mt;
} }
int new_symbol_tokens(const char * const * symbols, int new_symbol_tokens(const char * const * symbols,
hl_group_t * const g) { hl_group_t * const g) {
int i = 0; int i = 0;
while (*symbols) { while (*symbols) {
if(new_symbol_token(*symbols, g)){ if(new_symbol_token(*symbols, g)) {
++i; ++i;
} }
++symbols; ++symbols;
@ -99,40 +98,47 @@ int new_symbol_tokens(const char * const * symbols,
int new_char_tokens(const char * characters, int new_char_tokens(const char * characters,
hl_group_t * const g) { hl_group_t * const g) {
int i = 0; int i = 0;
char buffer[2]; char buffer[2] = "";
buffer[1] = '\00'; buffer[1] = '\00';
for(const char * s = characters; *s != '\00'; s++){
for(const char * s = characters; *s != '\0'; s++) {
buffer[0] = *s; buffer[0] = *s;
if(new_symbol_token(buffer, g)){ if(new_symbol_token(buffer, g)) {
++i; ++i;
} }
} }
return i; return i;
} }
token_t * new_keyword_token(const char * const word, token_t * new_keyword_token(const char * const word,
hl_group_t * const g) { hl_group_t * const g) {
size_t word_length = strlen(word);
char * new_word = (char*)malloc(word_length + 4 + 1);
size_t word_length = strlen(word);
char * new_word = (char*)malloc(word_length + 4 + 1);
memcpy(new_word, "\\<", 2); memcpy(new_word, "\\<", 2);
memcpy(new_word + 2, word, word_length); memcpy(new_word + 2, word, word_length);
strcpy(new_word + 2 + word_length, "\\>"); strcpy(new_word + 2 + word_length, "\\>");
token_t * mt = (token_t*)malloc(sizeof(token_t)); token_t * mt = (token_t*)malloc(sizeof(token_t));
mt->hl = g;
mt->t = KEYWORD; mt->hl = g;
mt->t = KEYWORD;
mt->syntax = new_word; mt->syntax = new_word;
append_token(mt); append_token(mt);
return mt; return mt;
} }
int new_keyword_tokens(const char * const * words, int new_keyword_tokens(const char * const * words,
hl_group_t * const g) { hl_group_t * const g) {
int i = 0; int i = 0;
while (*words) { while (*words) {
if(new_keyword_token(*words, g)){ if(new_keyword_token(*words, g)) {
++i; ++i;
} }
++words; ++words;
@ -144,36 +150,33 @@ int new_keyword_tokens(const char * const * words,
token_t * new_token(const char * const word, token_t * new_token(const char * const word,
const token_type_t t, const token_type_t t,
hl_group_t * const g) { hl_group_t * const g) {
switch(t){ switch (t) {
case KEYSYMBOL: { case KEYSYMBOL: {
return new_symbol_token(word, g); return new_symbol_token(word, g);
}; }
case KEYWORD: { case KEYWORD: {
return new_keyword_token(word, g); return new_keyword_token(word, g);
}; }
case MATCH: { case MATCH: {
} break; } break;
case REGION: { case REGION: {
} break; } break;
} }
// XXX: implement the rest
return NULL; return NULL;
} }
// -------------------- // --------------------
// ### Highlighting ### // ### Highlighting ###
// -------------------- // --------------------
int token_fits(const token_t * const token, int token_fits(const token_t * const token,
const char * const to, const char * const to,
const int string_offset, const int string_offset,
int * match_offset) { int * match_offset) {
const char * const pattern = token->syntax; const char * const pattern = token->syntax;
if (pattern == NULL) { if (! pattern) {
return true; return true;
} }
@ -182,14 +185,17 @@ int token_fits(const token_t * const token,
} }
void render_string(const char * const string, void render_string(const char * const string,
const char * const mode) { const char * const mode) {
for (const char * s = string; *s != '\00';) { for (const char * s = string; *s != '\00';) {
int f; int f;
int token_index = 0; int token_index = 0;
int offset; int offset;
for (; token_index < token_table_top; token_index++) { for (; token_index < token_table_top; token_index++) {
f = token_fits(token_table[token_index], string, s - string, &offset); f = token_fits(token_table[token_index], string, s - string, &offset);
if(f){ break; } if (f) {
break;
}
} }
// //
display_t * display; display_t * display;
@ -198,10 +204,10 @@ void render_string(const char * const string,
display); display);
// //
if (f) { if (f) {
for(int i = 0; i < offset; i++){ for (int i = 0; i < offset; i++) {
display->callback(s + i, display->callback(s + i,
0, 0,
token_table[token_index]->hl->attributes); token_table[token_index]->hl->attributes);
} }
display->callback(s + offset, display->callback(s + offset,
f, f,
@ -216,18 +222,18 @@ void render_string(const char * const string,
} }
} }
// ------------------------- // -------------------------
// ### Library Mangement ### // ### Library Mangement ###
// ------------------------- // -------------------------
int hl_init(void) { int hl_init(void) {
return 0; return 0;
} }
int hl_deinit(void) { int hl_deinit(void) {
for(int i = 0; i < token_table_top; i++){ for (int i = 0; i < token_table_top; i++) {
free_token(token_table[i]); free_token(token_table[i]);
} }
return 0; return 0;
} }