]> git.xolatile.top Git - public-libhl.git/commitdiff
getting there...
authoranon <anon@anon.anon>
Wed, 20 Sep 2023 20:43:47 +0000 (22:43 +0200)
committeranon <anon@anon.anon>
Wed, 20 Sep 2023 20:43:47 +0000 (22:43 +0200)
include/syntax/c.h
source/hl.c

index d585c80835187c1e32747ac727236ebd472fae1e..dffb5a9617db2ad04b6e65aa224572534d6fc71e 100644 (file)
@@ -12,12 +12,12 @@ const char * preprocessor_keywords[] = {
   NULL
 };
 
-//new_char_tokens("+-&|.()[]{}", operator_hl);
-//new_keyword_tokens(c_keywords, control_hl);
-//new_keyword_tokens(preprocessor_keywords, special_hl);
-//new_region_token("/\\*", "\\*/", comment_hl);
-//new_region_token("//", "\\n", comment_hl);
-//new_region_token("\"", "\"", string_literal_hl);
-//new_region_token("<", ">", string_literal_hl);
-//new_keyword_token("keyword", special_hl);
+new_char_tokens("+-&|.()[]{}", operator_hl);
+new_keyword_tokens(c_keywords, control_hl);
+new_keyword_tokens(preprocessor_keywords, special_hl);
+new_region_token("/\\*", "\\*/", comment_hl);
+new_region_token("//", "\\n", comment_hl);
+new_region_token("\"", "\"", string_literal_hl);
+new_region_token("<", ">", string_literal_hl);
+new_keyword_token("keyword", special_hl);
 new_keyword_token("while", operator_hl);
index 7f745d57c5b9b3ee2f4035cda31014c518143a85..8d933538efc5f7542bcaedf3f5da5673ee55f9c2 100644 (file)
@@ -184,51 +184,82 @@ int token_fits(const token_t * const            token,
                const int                string_offset,
                const bool            is_start_of_line,
                      int     *           match_offset) {
+       return 0;
        UNUSED(match_offset);
-       //return regex_match(pattern, to, string_offset, match_offset);
-       return (int)regex_match(token->syntax, to, is_start_of_line);
+
+       match_t * matches = regex_match(token->syntax, to, is_start_of_line);
+
+       if (matches->position == -1) {
+               return 0;
+       }
+
+       const int r  = matches->width;
+       match_offset = matches->position;
+
+       free(matches);
+
+       return r;
 }
 
 void render_string(const char * const string,
                    const char * const   mode) {
+       display_t * display;
+       HASH_FIND_STR(display_table,
+                     mode,
+                     display);
+
+       typedef struct {
+               const token_t * t;
+               const match_t * m;
+                     int       i;
+       } result_t;
+
+       result_t * const r = (result_t *)malloc(sizeof(result_t) * 1024);       // XXX: dont
+       int rrs = 0;
+       for (int i = 0; i < token_table.element_count; i++) {
+               token_t * t = *(token_t**)vector_get(&token_table,
+                                                    i);
+               match_t * match = regex_match(t->syntax, string, true);
+               if (match->position == -1) {
+                       free(match);
+                       continue;
+               }
+               r[rrs++] = (result_t){
+                       .t = t,
+                       .m = match,
+                       .i = 0,
+               };
+       }
+
        for (const char * s = string; *s != '\00';) {
-               int    f           = 0;
-               size_t token_index = 0;
-               int    offset      = 0;
-
-               for (; token_index < token_table.element_count; token_index++) {
-                       token_t * t = *(token_t**)vector_get(&token_table,
-                                                            token_index);
-                       const bool is_start_of_line = (s == string) || (*s == '\n');
-                       f = token_fits(t, string, (int)(s - string), is_start_of_line, &offset);
-                       if (f) {
-                               break;
+               const result_t sentinel = (result_t){NULL, &(match_t){ -1, -1}, -1};
+               const result_t * max;
+               max = &sentinel;
+               for (int h = 0; h < rrs; h++) {
+                       result_t * const current_result = r + h;
+                       for (int j = 0; current_result->m[j].position != -1; j++) {
+                               if (current_result->m[j].position == (s - string)) {
+                                       if (current_result->m[j].width > max->m->width) {
+                                               current_result->i = j;
+                                               max = current_result;
+                                       }
+                                       break;
+                               }
                        }
                }
-               //
-               display_t * display;
-               HASH_FIND_STR(display_table,
-                             mode,
-                             display);
-               //
-               if (f) {
-                       for (int i = 0; i < offset; i++) {
-                               token_t * t = *(token_t**)vector_get(&token_table,
-                                                                    token_index);
-                               display->callback(s + i,
-                                                 0,
-                                                 t->hl->attributes);
+               if (max != &sentinel) {
+                       const int padding = max->m[max->i].position - (s - string);
+                       if (padding) {
+                               display->callback(s,
+                                                                 padding,
+                                                                 NULL);
                        }
-                       token_t * t = *(token_t**)vector_get(&token_table,
-                                                            token_index);
-                       display->callback(s + offset,
-                                         f,
-                                         t->hl->attributes);
-                       s += f + offset;
+                       display->callback(s + padding,
+                                                         max->m->width,
+                                                         max->t->hl->attributes);
+                       s += padding + max->m->width;
                } else {
-                       display->callback(s,
-                                         0,
-                                         NULL);
+                       display->callback(s, 1, NULL);
                        ++s;
                }
        }
@@ -239,9 +270,9 @@ int hl_init(void) {
 }
 
 int hl_deinit(void) {
-       for (size_t i = 0; i < token_table.element_count; i++) {
-               free_token(*(token_t**)vector_get(&token_table, i));
-       }
+       //for (size_t i = 0; i < token_table.element_count; i++) {
+       //      free_token(*(token_t**)vector_get(&token_table, i));
+       //}
 
        return 0;
 }