renamed token structs, hid uxhash into hl.h

This commit is contained in:
anon 2023-08-19 20:47:42 +02:00
parent b47f01079c
commit 256c1b0279
2 changed files with 33 additions and 17 deletions

View File

@ -21,26 +21,28 @@ typedef struct {
} hl_group_t; } hl_group_t;
typedef enum { typedef enum {
KEYSYMBOL,
KEYWORD, KEYWORD,
MATCH, MATCH,
REGION REGION
} token_t; } token_type_t;
typedef struct { typedef struct {
hl_group_t * hl; hl_group_t * hl;
token_t t; token_type_t t;
char* syntax; char* syntax;
} token; // XXX: this will have to be renamed } token_t;
/* Temp solution /* Temp solution
* this should be dynamic
*/ */
token * token_table[1000]; token_t * token_table[1000];
int token_table_top = 0; int token_table_top = 0;
token * new_token(const char * const syntax, token_t * new_token(const char * const syntax,
const token_t t, const token_type_t t,
const hl_group_t * const g) { const hl_group_t * const g) {
token * mt = (token*)malloc(sizeof(token)); token_t * mt = (token_t*)malloc(sizeof(token_t));
mt->hl = g; mt->hl = g;
mt->t = t; mt->t = t;
mt->syntax = syntax; mt->syntax = syntax;
@ -50,10 +52,15 @@ token * new_token(const char * const syntax,
void new_keyword_tokens(const char * const * words, void new_keyword_tokens(const char * const * words,
hl_group_t * const g) { hl_group_t * const g) {
int i = 0;
while (*words) { while (*words) {
new_token(*words, KEYWORD, g); if(new_token(*words, KEYWORD, g)){
words = words + 1; ++i;
}
++words;
} }
return i;
} }
int token_fits(const char* const pattern, int token_fits(const char* const pattern,
@ -89,18 +96,30 @@ void render_string(const char * const string,
int i = 0; int i = 0;
for (; i < token_table_top; i++) { for (; i < token_table_top; i++) {
f = token_fits(token_table[i]->syntax, s); f = token_fits(token_table[i]->syntax, s);
if(f){ break; }; if(f){ break; }
} }
// //
display_t * display; display_t * display;
HASH_FIND_STR(display_table, mode, display); HASH_FIND_STR(display_table,
mode,
display);
// //
if (f) { if (f) {
display->callback(s, f, token_table[i]->hl->attributes); display->callback(s,
f,
token_table[i]->hl->attributes);
s += f; s += f;
} else { } else {
display->callback(s, 0, NULL); display->callback(s,
0,
NULL);
++s; ++s;
} }
} }
} }
void new_display_mode(display_t * mode) {
HASH_ADD_STR(display_table,
key,
mode);
}

View File

@ -79,10 +79,7 @@ int main(int argc,
hl_group_t mygroup = (hl_group_t) { hl_group_t mygroup = (hl_group_t) {
.link = NULL .link = NULL
}; };
new_display_mode(cterm);
HASH_ADD_STR(display_table,
key,
cterm);
new_keyword_tokens(c_keywords, &mygroup); new_keyword_tokens(c_keywords, &mygroup);
new_keyword_tokens(preprocessor_keywords, &mygroup); new_keyword_tokens(preprocessor_keywords, &mygroup);