From 256c1b02790dd5a1caf9ac31f360cc21d07f1ba8 Mon Sep 17 00:00:00 2001 From: anon Date: Sat, 19 Aug 2023 20:47:42 +0200 Subject: [PATCH] renamed token structs, hid uxhash into hl.h --- source/hl.h | 45 ++++++++++++++++++++++++++++++++------------- source/main.c | 5 +---- 2 files changed, 33 insertions(+), 17 deletions(-) diff --git a/source/hl.h b/source/hl.h index a8e168b..693ee73 100644 --- a/source/hl.h +++ b/source/hl.h @@ -21,26 +21,28 @@ typedef struct { } hl_group_t; typedef enum { + KEYSYMBOL, KEYWORD, MATCH, REGION -} token_t; +} token_type_t; typedef struct { hl_group_t * hl; - token_t t; + token_type_t t; char* syntax; -} token; // XXX: this will have to be renamed +} token_t; /* Temp solution + * this should be dynamic */ -token * token_table[1000]; +token_t * token_table[1000]; int token_table_top = 0; -token * new_token(const char * const syntax, - const token_t t, +token_t * new_token(const char * const syntax, + const token_type_t t, 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->t = t; mt->syntax = syntax; @@ -50,10 +52,15 @@ token * new_token(const char * const syntax, void new_keyword_tokens(const char * const * words, hl_group_t * const g) { + int i = 0; while (*words) { - new_token(*words, KEYWORD, g); - words = words + 1; + if(new_token(*words, KEYWORD, g)){ + ++i; + } + ++words; } + + return i; } int token_fits(const char* const pattern, @@ -89,18 +96,30 @@ void render_string(const char * const string, int i = 0; for (; i < token_table_top; i++) { f = token_fits(token_table[i]->syntax, s); - if(f){ break; }; + if(f){ break; } } // display_t * display; - HASH_FIND_STR(display_table, mode, display); + HASH_FIND_STR(display_table, + mode, + display); // if (f) { - display->callback(s, f, token_table[i]->hl->attributes); + display->callback(s, + f, + token_table[i]->hl->attributes); s += f; } else { - display->callback(s, 0, NULL); + display->callback(s, + 0, + NULL); ++s; } } } + +void new_display_mode(display_t * mode) { + HASH_ADD_STR(display_table, + key, + mode); +} diff --git a/source/main.c b/source/main.c index f326730..f924641 100644 --- a/source/main.c +++ b/source/main.c @@ -79,10 +79,7 @@ int main(int argc, hl_group_t mygroup = (hl_group_t) { .link = NULL }; - - HASH_ADD_STR(display_table, - key, - cterm); + new_display_mode(cterm); new_keyword_tokens(c_keywords, &mygroup); new_keyword_tokens(preprocessor_keywords, &mygroup);