Highlight things
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 lines
6.2KB

  1. #include "hl.h"
  2. #include <assert.h>
  3. vector_t token_table = {
  4. .data = NULL,
  5. .element_size = sizeof(token_t *),
  6. .element_count = 0UL
  7. };
  8. display_t * display_table = NULL;
  9. // -------------------------
  10. // ### Library Mangement ###
  11. // -------------------------
  12. hl_group_t * special_hl = NULL;
  13. hl_group_t * control_hl = NULL;
  14. hl_group_t * keyword_hl = NULL;
  15. hl_group_t * block_hl = NULL;
  16. hl_group_t * separator_hl = NULL;
  17. hl_group_t * operator_hl = NULL;
  18. hl_group_t * comment_hl = NULL;
  19. hl_group_t * string_literal_hl = NULL;
  20. // --------------------------------
  21. // ### Constructors/Destructors ###
  22. // --------------------------------
  23. void new_display_mode(display_t * mode) {
  24. HASH_ADD_STR(display_table,
  25. key,
  26. mode);
  27. }
  28. int free_token(token_t * token) {
  29. free(token->hl);
  30. regex_free(token->syntax);
  31. return 0;
  32. }
  33. int append_token(token_t * token) {
  34. vector_push(&token_table, &token);
  35. return 0;
  36. }
  37. token_t * new_symbol_token(const char * const c,
  38. hl_group_t * const g) {
  39. token_t * mt = (token_t*)malloc(sizeof(token_t));
  40. mt->hl = g;
  41. mt->t = KEYSYMBOL;
  42. mt->syntax = regex_compile(c);
  43. append_token(mt);
  44. return mt;
  45. }
  46. int new_symbol_tokens(const char * const * symbols,
  47. hl_group_t * const g) {
  48. int i = 0;
  49. while (*symbols) {
  50. if(new_symbol_token(*symbols, g)) {
  51. ++i;
  52. } else {
  53. assert(!(bool)"Kinda failed to new symbol token thing.");
  54. }
  55. ++symbols;
  56. }
  57. return i;
  58. }
  59. int new_char_tokens(const char * str,
  60. hl_group_t * const g) {
  61. int i = 0;
  62. char buffer[3];
  63. buffer[0] = '\\';
  64. buffer[2] = '\0';
  65. for(const char * s = str; *s != '\0'; s++) {
  66. buffer[1] = *s;
  67. if(new_symbol_token(is_magic(*s) ? buffer : buffer + 1, g)) {
  68. ++i;
  69. } else {
  70. assert(!(bool)"Kinda failed to new char token thing.");
  71. }
  72. }
  73. return i;
  74. }
  75. token_t * new_keyword_token(const char * const word,
  76. hl_group_t * const g) {
  77. //char * new_word = strdup(word);
  78. //size_t word_length = strlen(word);
  79. //char * new_word = (char*)malloc(word_length + 4 + 1);
  80. //memcpy(new_word, "\\<", 2);
  81. //memcpy(new_word + 2, word, word_length);
  82. //strcpy(new_word + 2 + word_length, "\\>");
  83. token_t * mt = (token_t*)malloc(sizeof(token_t));
  84. mt->hl = g;
  85. mt->t = KEYWORD;
  86. //mt->syntax = regex_compile(new_word);
  87. mt->syntax = regex_compile(word);
  88. append_token(mt);
  89. return mt;
  90. }
  91. int new_keyword_tokens(const char * const * words,
  92. hl_group_t * const g) {
  93. int i = 0;
  94. while (*words) {
  95. if(new_keyword_token(*words, g)) {
  96. ++i;
  97. }
  98. ++words;
  99. }
  100. return i;
  101. }
  102. token_t * new_region_token(const char * start,
  103. const char * end,
  104. hl_group_t * g) {
  105. char buffer[100];
  106. buffer[0] = '\0';
  107. strcat(buffer, start);
  108. strcat(buffer, "[\\d\\D]*");
  109. strcat(buffer, end);
  110. token_t * mt = (token_t*)malloc(sizeof(token_t));
  111. mt->hl = g;
  112. mt->t = KEYSYMBOL;
  113. mt->syntax = regex_compile(buffer);
  114. append_token(mt);
  115. return mt;
  116. }
  117. token_t * new_token(const char * const word,
  118. const token_type_t t,
  119. hl_group_t * const g) {
  120. switch (t) {
  121. case KEYSYMBOL: {
  122. return new_symbol_token(word, g);
  123. }
  124. case KEYWORD: {
  125. return new_keyword_token(word, g);
  126. }
  127. case MATCH: {
  128. token_t * mt = (token_t*)malloc(sizeof(token_t));
  129. mt->hl = g;
  130. mt->t = MATCH;
  131. mt->syntax = regex_compile(word);
  132. append_token(mt);
  133. } break;
  134. case REGION: {
  135. } break;
  136. }
  137. return NULL;
  138. }
  139. // --------------------
  140. // ### Highlighting ###
  141. // --------------------
  142. int token_fits(const token_t * const token,
  143. const char * const to,
  144. const int string_offset,
  145. const bool is_start_of_line,
  146. int * match_offset) {
  147. return 0;
  148. UNUSED(match_offset);
  149. match_t * matches = regex_match(token->syntax, to, is_start_of_line);
  150. if (matches->position == -1) {
  151. return 0;
  152. }
  153. const int r = matches->width;
  154. match_offset = matches->position;
  155. free(matches);
  156. return r;
  157. }
  158. void render_string(const char * const string,
  159. const char * const mode) {
  160. display_t * display;
  161. HASH_FIND_STR(display_table,
  162. mode,
  163. display);
  164. typedef struct {
  165. const token_t * t;
  166. const match_t * m;
  167. int i;
  168. } result_t;
  169. result_t * const r = (result_t *)malloc(sizeof(result_t) * 1024); // XXX: dont
  170. int rrs = 0;
  171. for (int i = 0; i < token_table.element_count; i++) {
  172. token_t * t = *(token_t**)vector_get(&token_table,
  173. i);
  174. match_t * match = regex_match(t->syntax, string, true);
  175. for(match_t * m = match; m->position != -1; m++){
  176. printf("%s: %d %d\n", t->syntax->str, m->position, m->width);
  177. }
  178. if (match->position == -1) {
  179. free(match);
  180. continue;
  181. }
  182. r[rrs++] = (result_t){
  183. .t = t,
  184. .m = match,
  185. .i = 0,
  186. };
  187. }
  188. for (const char * s = string; *s != '\00';) {
  189. const result_t sentinel = (result_t){NULL, &(match_t){ -1, -1}, -1};
  190. const result_t * max;
  191. max = &sentinel;
  192. for (int h = 0; h < rrs; h++) {
  193. result_t * const current_result = r + h;
  194. for (int j = 0; current_result->m[j].position != -1; j++) {
  195. if (current_result->m[j].position == (s - string)) {
  196. if (current_result->m[j].width > max->m->width) {
  197. current_result->i = j;
  198. max = current_result;
  199. }
  200. break;
  201. }
  202. }
  203. }
  204. if (max != &sentinel) {
  205. const match_t * mymatch = &(max->m[max->i]);
  206. const int padding = mymatch->position - (s - string);
  207. if (padding) {
  208. display->callback(s,
  209. padding,
  210. NULL);
  211. }
  212. display->callback(s + padding,
  213. mymatch->width,
  214. max->t->hl->attributes);
  215. s += padding + mymatch->width;
  216. } else {
  217. display->callback(s, 1, NULL);
  218. ++s;
  219. }
  220. }
  221. }
  222. int hl_init(void) {
  223. return 0;
  224. }
  225. int hl_deinit(void) {
  226. //for (size_t i = 0; i < token_table.element_count; i++) {
  227. // free_token(*(token_t**)vector_get(&token_table, i));
  228. //}
  229. return 0;
  230. }